Skip to main content

faker_rust/default/
omniauth.rs

1//! OmniAuth generator
2
3use crate::base::sample;
4use crate::locale::fetch_locale;
5use crate::config::FakerConfig;
6
7/// Generate a random OmniAuth provider
8pub fn provider() -> String {
9    fetch_locale("omniauth.providers", "en")
10        .map(|v| sample(&v))
11        .unwrap_or_else(|| sample(FALLBACK_PROVIDERS).to_string())
12}
13
14/// Generate a random OmniAuth UID
15pub fn uid() -> String {
16    let config = FakerConfig::current();
17    let numbers: String = (0..17)
18        .map(|_| config.rand_char(&['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']))
19        .collect();
20    numbers
21}
22
23/// Generate a random OmniAuth name
24pub fn name() -> String {
25    let names = [
26        "google_oauth2", "facebook", "twitter", "github", "linkedin",
27        "developer", "saml", "openid_connect", "auth0", "okta",
28    ];
29    sample(&names).to_string()
30}
31
32/// Generate a random directory name
33pub fn directory() -> String {
34    let dirs = [
35        "src", "lib", "bin", "test", "docs", "config", "assets",
36        "public", "private", "vendor", "node_modules", "target",
37    ];
38    sample(&dirs).to_string()
39}
40
41// Fallback data
42const FALLBACK_PROVIDERS: &[&str] = &[
43    "google_oauth2", "facebook", "twitter", "github", "linkedin", "developer",
44    "saml", "openid_connect", "auth0", "okta", "microsoft", "slack", "discord",
45];
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn test_provider() {
53        assert!(!provider().is_empty());
54    }
55
56    #[test]
57    fn test_uid() {
58        let id = uid();
59        assert_eq!(id.len(), 17);
60        assert!(id.chars().all(|c| c.is_ascii_digit()));
61    }
62
63    #[test]
64    fn test_name() {
65        assert!(!name().is_empty());
66    }
67}