chore(auth): add tests
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
parent
7dcd3b4efe
commit
a2db6ca64a
@ -32,3 +32,6 @@ tower = "0.4.13"
|
|||||||
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
|
tower-http = { version = "0.4.0", features = ["cors", "trace"] }
|
||||||
oauth2 = "4.4.0"
|
oauth2 = "4.4.0"
|
||||||
openidconnect = "3.0.0"
|
openidconnect = "3.0.0"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
pretty_assertions.workspace = true
|
||||||
|
@ -3,36 +3,49 @@ use oauth2::{basic::BasicClient, AuthUrl, ClientId, ClientSecret, RedirectUrl, T
|
|||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[derive(Clone, clap::Args)]
|
#[derive(Clone, clap::Args, Debug)]
|
||||||
pub struct OAuthClientClap {
|
pub struct OAuthClientClap {
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
zitadel: Option<ZitadelClap>,
|
zitadel: ZitadelClap,
|
||||||
|
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
noop: Option<NoopConfig>,
|
noop: NoopConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, clap::Args)]
|
#[derive(Clone, clap::Args, Debug)]
|
||||||
pub struct NoopConfig {
|
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>,
|
pub oauth_noop: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::Args, Clone)]
|
#[derive(clap::Args, Clone, Debug, PartialEq, Eq)]
|
||||||
pub struct ZitadelClap {
|
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>,
|
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>,
|
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>,
|
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>,
|
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>,
|
pub token_url: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,14 +176,22 @@ impl OAuthClient for ZitadelOAuthClient {
|
|||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::oauth::{OAuth, OAuthClientClap, OAuthConfig, ZitadelConfig};
|
use crate::oauth::{OAuth, OAuthClientClap, OAuthConfig, ZitadelClap, ZitadelConfig};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[command()]
|
#[command(author, version, about, long_about = None)]
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
#[clap(flatten)]
|
#[clap(flatten)]
|
||||||
options: OAuthClientClap,
|
options: OAuthClientClap,
|
||||||
|
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Commands,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(clap::Subcommand, Clone)]
|
||||||
|
pub enum Commands {
|
||||||
|
One,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@ -193,7 +214,37 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn test_parse_clap() {
|
async fn test_parse_clap_noop() {
|
||||||
let cmd = Cli::parse_from(&["--oauth-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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user