Skip to content

Commit

Permalink
Removed the fallback to environment vars
Browse files Browse the repository at this point in the history
I asked for feedback about the inclusion of a fallback to ENV. I
concluded that it's better to be explicit.
  • Loading branch information
Eric Biggs committed Sep 13, 2024
1 parent 57c107c commit f8019e6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 74 deletions.
28 changes: 4 additions & 24 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,34 +30,14 @@ pub struct AuthClient {

impl AuthClient {
/// Create a new AuthClient
/// Will use `SUPABASE_URL` `SUPABASE_API_KEY` and `SUPABASE_JWT_SECRET` environment variables if no params are provided
pub fn new<S: Into<String>>(
project_url: Option<S>,
api_key: Option<S>,
jwt_secret: Option<S>,
) -> Self {
pub fn new(project_url: &str, api_key: &str, jwt_secret: &str) -> Self {
let client = Client::new();

let project_url = project_url
.map(Into::into)
.or_else(|| env::var("SUPABASE_URL").ok())
.unwrap_or_else(String::new);

let api_key = api_key
.map(Into::into)
.or_else(|| env::var("SUPABASE_API_KEY").ok())
.unwrap_or_else(String::new);

let jwt_secret = jwt_secret
.map(Into::into)
.or_else(|| env::var("SUPABASE_JWT_SECRET").ok())
.unwrap_or_else(String::new);

AuthClient {
client,
project_url,
api_key,
jwt_secret,
project_url: project_url.to_string(),
api_key: api_key.to_string(),
jwt_secret: jwt_secret.to_string(),
}
}

Expand Down
60 changes: 10 additions & 50 deletions tests/client_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ async fn create_client_test_valid() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

assert!(auth_client.project_url == test_project_url)
}
Expand All @@ -24,11 +20,7 @@ async fn sign_in_with_password_test_valid() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let demo_email = "[email protected]";
let demo_password = "qwerqwer";
Expand All @@ -50,11 +42,7 @@ async fn sign_in_with_password_test_invalid() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let demo_email = "[email protected]";
let demo_password = "invalid";
Expand All @@ -76,11 +64,7 @@ async fn sign_up_with_email_test_valid() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let uuid = uuid::Uuid::now_v7();

Expand Down Expand Up @@ -108,11 +92,7 @@ async fn sign_up_with_phone_test_valid() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let demo_phone = "13334445555";
let demo_password = "ciJUAojfZZYKfCxkiUWH";
Expand All @@ -138,11 +118,7 @@ async fn send_login_email_with_magic_link() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let demo_email = "[email protected]";

Expand All @@ -167,11 +143,7 @@ async fn send_sms_with_otp() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

let demo_phone = "1333444555";

Expand All @@ -194,11 +166,7 @@ async fn sign_in_with_oauth_test() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

// Must login to get a user bearer token
let demo_email = "[email protected]";
Expand Down Expand Up @@ -234,11 +202,7 @@ async fn get_user_test() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

// Must login to get a user bearer token
let demo_email = "[email protected]";
Expand Down Expand Up @@ -267,11 +231,7 @@ async fn update_user_test() {
let test_api_key = env::var("SUPABASE_API_KEY").unwrap();
let test_jwt_secret = env::var("SUPABASE_JWT_SECRET").unwrap();

let auth_client = AuthClient::new(
Some(&test_project_url),
Some(&test_api_key),
Some(&test_jwt_secret),
);
let auth_client = AuthClient::new(&test_project_url, &test_api_key, &test_jwt_secret);

// Must login to get a user bearer token
let demo_email = "[email protected]";
Expand Down

0 comments on commit f8019e6

Please sign in to comment.