chore(auth): add tests

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
Kasper Juul Hermansen 2023-08-19 16:43:50 +02:00
parent 7dcd3b4efe
commit a2db6ca64a
Signed by: kjuulh
GPG Key ID: 9AA7BC13CE474394
2 changed files with 69 additions and 15 deletions

View File

@ -32,3 +32,6 @@ tower = "0.4.13"
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
oauth2 = "4.4.0"
openidconnect = "3.0.0"
[dev-dependencies]
pretty_assertions.workspace = true

View File

@ -3,36 +3,49 @@ use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, T
use std::ops::Deref;
use std::sync::Arc;
#[derive(Clone, clap::Args)]
#[derive(Clone, clap::Args, Debug)]
pub struct OAuthClientClap {
#[clap(flatten)]
zitadel: Option<ZitadelClap>,
zitadel: ZitadelClap,
#[clap(flatten)]
noop: Option<NoopConfig>,
noop: NoopConfig,
}
#[derive(Clone, clap::Args)]
#[derive(Clone, clap::Args, Debug)]
pub struct NoopConfig {
#[arg(env = "OAUTH_NOOP", long = "oauth-noop", group = "auth")]
#[arg(env = "OAUTH_NOOP", long = "oauth-noop", group = "auth", global = true)]
pub oauth_noop: Option<bool>,
}
#[derive(clap::Args, Clone)]
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
pub struct ZitadelClap {
#[arg(env = "ZITADEL_AUTH_URL", long = "zitadel-auth-url", group = "auth")]
#[arg(
env = "ZITADEL_AUTH_URL",
long = "zitadel-auth-url",
group = "auth",
global = true
)]
pub auth_url: Option<String>,
#[arg(env = "ZITADEL_CLIENT_ID", long = "zitadel-client-id")]
#[arg(env = "ZITADEL_CLIENT_ID", long = "zitadel-client-id", global = true)]
pub client_id: Option<String>,
#[arg(env = "ZITADEL_CLIENT_SECRET", long = "zitadel-client-secret")]
#[arg(
env = "ZITADEL_CLIENT_SECRET",
long = "zitadel-client-secret",
global = true
)]
pub client_secret: Option<String>,
#[arg(env = "ZITADEL_REDIRECT_URL", long = "zitadel-redirect-url")]
#[arg(
env = "ZITADEL_REDIRECT_URL",
long = "zitadel-redirect-url",
global = true
)]
pub redirect_url: Option<String>,
#[arg(env = "ZITADEL_TOKEN_URL", long = "zitadel-token-url")]
#[arg(env = "ZITADEL_TOKEN_URL", long = "zitadel-token-url", global = true)]
pub token_url: Option<String>,
}
@ -163,14 +176,22 @@ impl OAuthClient for ZitadelOAuthClient {
#[cfg(test)]
mod tests {
use crate::oauth::{OAuth, OAuthClientClap, OAuthConfig, ZitadelConfig};
use crate::oauth::{OAuth, OAuthClientClap, OAuthConfig, ZitadelClap, ZitadelConfig};
use clap::Parser;
#[derive(Parser)]
#[command()]
#[command(author, version, about, long_about = None)]
pub struct Cli {
#[clap(flatten)]
options: OAuthClientClap,
#[command(subcommand)]
command: Commands,
}
#[derive(clap::Subcommand, Clone)]
pub enum Commands {
One,
}
#[tokio::test]
@ -193,7 +214,37 @@ mod tests {
}
#[tokio::test]
async fn test_parse_clap() {
let cmd = Cli::parse_from(&["--oauth-noop"]);
async fn test_parse_clap_noop() {
let cli: Cli = Cli::parse_from(&["base", "one", "--oauth-noop=true"]);
assert_eq!(cli.options.noop.oauth_noop, Some(true));
println!("{:?}", cli.options);
}
#[tokio::test]
async fn test_parse_clap_zitadel() {
let cli: Cli = Cli::parse_from(&[
"base",
"one",
"--zitadel-client-id=something",
"--zitadel-client-secret=something",
"--zitadel-auth-url=https://something",
"--zitadel-redirect-url=https://something",
"--zitadel-token-url=https://something",
]);
pretty_assertions::assert_eq!(
cli.options.zitadel,
ZitadelClap {
auth_url: Some("https://something".into()),
client_id: Some("something".into()),
client_secret: Some("something".into()),
redirect_url: Some("https://something".into()),
token_url: Some("https://something".into())
}
);
println!("{:?}", cli.options);
}
}