Skip to content

Commit

Permalink
fix(auth): user query columns (#1669)
Browse files Browse the repository at this point in the history
* fix(auth): user query columns

* clippy
  • Loading branch information
jonaro00 authored Mar 7, 2024
1 parent d39724a commit b3f3c60
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion auth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use args::StartArgs;
use shuttle_common::{claims::AccountTier, ApiKey};
use sqlx::{migrate::Migrator, query, PgPool};
use tracing::info;
use user::User;
pub use user::User;

use crate::api::serve;
pub use api::ApiBuilder;
Expand Down
44 changes: 22 additions & 22 deletions auth/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,19 @@ pub struct UserManager {
#[async_trait]
impl UserManagement for UserManager {
async fn create_user(&self, name: AccountName, tier: AccountTier) -> Result<User, Error> {
let key = ApiKey::generate();
let user = User::new(name, ApiKey::generate(), tier, vec![]);

query("INSERT INTO users (account_name, key, account_tier) VALUES ($1, $2, $3)")
.bind(&name)
.bind(&key)
.bind(tier.to_string())
.execute(&self.pool)
.await?;
query(
"INSERT INTO users (account_name, key, account_tier, user_id) VALUES ($1, $2, $3, $4)",
)
.bind(&user.name)
.bind(user.key.expose())
.bind(user.account_tier.to_string())
.bind(&user.id)
.execute(&self.pool)
.await?;

Ok(User::new(name, key, tier, vec![]))
Ok(user)
}

// Update tier leaving the subscription_id untouched.
Expand All @@ -91,13 +94,11 @@ impl UserManagement for UserManager {
}

async fn get_user(&self, name: AccountName) -> Result<User, Error> {
let mut user: User = sqlx::query_as(
"SELECT account_name, key, account_tier FROM users WHERE account_name = $1",
)
.bind(&name)
.fetch_optional(&self.pool)
.await?
.ok_or(Error::UserNotFound)?;
let mut user: User = sqlx::query_as("SELECT * FROM users WHERE account_name = $1")
.bind(&name)
.fetch_optional(&self.pool)
.await?
.ok_or(Error::UserNotFound)?;

let subscriptions: Vec<Subscription> = sqlx::query_as(
"SELECT subscription_id, type, quantity, created_at, updated_at FROM subscriptions WHERE account_name = $1",
Expand Down Expand Up @@ -125,12 +126,11 @@ impl UserManagement for UserManager {
}

async fn get_user_by_key(&self, key: ApiKey) -> Result<User, Error> {
let mut user: User =
sqlx::query_as("SELECT account_name, key, account_tier FROM users WHERE key = $1")
.bind(&key)
.fetch_optional(&self.pool)
.await?
.ok_or(Error::UserNotFound)?;
let mut user: User = sqlx::query_as("SELECT * FROM users WHERE key = $1")
.bind(&key)
.fetch_optional(&self.pool)
.await?
.ok_or(Error::UserNotFound)?;

let subscriptions: Vec<Subscription> = sqlx::query_as(
"SELECT subscription_id, type, quantity, created_at, updated_at FROM subscriptions WHERE account_name = $1",
Expand Down Expand Up @@ -277,7 +277,7 @@ impl User {
format!("user_{}", ulid::Ulid::new())
}

pub fn new(
fn new(
name: AccountName,
key: ApiKey,
account_tier: AccountTier,
Expand Down
3 changes: 2 additions & 1 deletion auth/tests/api/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ pub(crate) async fn app() -> TestApp {
let mock_server = MockServer::start().await;

// Insert an admin user for the tests.
query("INSERT INTO users (account_name, key, account_tier) VALUES ($1, $2, $3)")
query("INSERT INTO users VALUES ($1, $2, $3, $4)")
.bind("admin")
.bind(ADMIN_KEY)
.bind(AccountTier::Admin.to_string())
.bind(shuttle_auth::User::new_user_id())
.execute(&pg_pool)
.await
.unwrap();
Expand Down

0 comments on commit b3f3c60

Please sign in to comment.