Skip to content

Commit

Permalink
chore(users): migrate tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ABCxFF committed Jan 22, 2025
1 parent 14ecb51 commit 6fb3d05
Show file tree
Hide file tree
Showing 12 changed files with 451 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions packages/services/user/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@ token-create.workspace = true
upload-file-list.workspace = true
upload-get.workspace = true
upload-complete.workspace = true
user-identity-get.workspace = true

[dependencies.sqlx]
workspace = true
default-features = false
default-features = false

[dev-dependencies]
faker-user.workspace = true
rand = "0.8"
reqwest = "0.11"
upload-get.workspace = true
upload-prepare.workspace = true
62 changes: 62 additions & 0 deletions packages/services/user/tests/avatar_upload_complete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use chirp_workflow::prelude::*;
use rivet_operation::prelude::proto::backend;

const TEST_BODY: &[u8] = b"test file";

#[workflow_test]
async fn empty(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {}).await.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

// Create the upload
let upload_prepare_res = op!([ctx] upload_prepare {
bucket: "bucket-user-avatar".into(),
files: vec![
backend::upload::PrepareFile {
path: "image.png".to_owned(),
mime: Some("image/png".into()),
content_length: TEST_BODY.len() as u64,
..Default::default()
},
],
})
.await
.unwrap();
let upload_id = upload_prepare_res.upload_id.unwrap();
let presigned_request = upload_prepare_res.presigned_requests.first().unwrap();

tracing::info!("writing test files");
let res = reqwest::Client::new()
.put(&presigned_request.url)
.body(TEST_BODY.to_vec())
.header("content-type", "image/png")
.send()
.await
.expect("failed to upload");
if res.status().is_success() {
tracing::info!("uploaded successfully");
} else {
panic!(
"failed to upload ({}): {:?}",
res.status(),
res.text().await
);
}

ctx.op(::user::ops::avatar_upload_complete::Input {
user_id: user_id,
upload_id: upload_id.as_uuid()
})
.await
.unwrap();

let uploads_res = op!([ctx] upload_get {
upload_ids: vec![upload_id]
})
.await
.unwrap();

let upload = uploads_res.uploads.first().unwrap();

assert!(upload.complete_ts.is_some(), "Upload did not complete");
}
72 changes: 72 additions & 0 deletions packages/services/user/tests/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use chirp_workflow::prelude::*;
use proto::backend::{pkg::*};
use rivet_operation::prelude::proto;
use rand::Rng;

#[workflow_test]
async fn empty(ctx: TestCtx) {
let res = ctx.op(::user::ops::get::Input {
user_ids: Vec::new(),
})
.await
.unwrap();
assert!(res.users.is_empty());
}

#[workflow_test]
async fn fetch(ctx: TestCtx) {
struct TestUser {
user_id: Option<Uuid>,
display_name: String,
account_number: i64,
bio: String,
}

// Generate test users
let mut users = std::iter::repeat_with(|| TestUser {
user_id: None,
display_name: util::faker::display_name(),
account_number: rand::thread_rng().gen_range(1..10000),
bio: util::faker::ident(),
})
.take(8)
.collect::<Vec<_>>();

// Insert test users
for user in &mut users {
let user_res = op!([ctx] faker_user { }).await.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

msg!([ctx] user::msg::profile_set(user_id) -> user::msg::update {
user_id: Some(user_id.into()),
display_name: Some(user.display_name.clone()),
account_number: Some(user.account_number as u32),
bio: Some(user.bio.clone()),
})
.await
.unwrap();

user.user_id = Some(user_id);
}

// Fetch the users
let res = ctx.op(::user::ops::get::Input {
user_ids: users.iter().map(|u| u.user_id.unwrap()).collect(),
})
.await
.unwrap();

// Validate the users
assert_eq!(users.len(), res.users.len());
for user in &users {
let user_res = res
.users
.iter()
.find(|u| u.user_id.unwrap().as_uuid() == user.user_id.unwrap())
.expect("user not returned");

assert_eq!(user.display_name, user_res.display_name);
assert_eq!(user.account_number, user_res.account_number as i64);
assert_eq!(user.bio, user_res.bio);
}
}
35 changes: 35 additions & 0 deletions packages/services/user/tests/identity_create.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use chirp_workflow::prelude::*;
use rivet_operation::prelude::proto::backend;

#[workflow_test]
async fn email(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {
..Default::default()
})
.await
.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

let email = util::faker::email();
ctx.op(::user::ops::identity::create::Input {
user_id: user_id,
identity: backend::user_identity::Identity {
kind: Some(backend::user_identity::identity::Kind::Email(
backend::user_identity::identity::Email {
email: email.clone(),
}
)),
},
})
.await
.unwrap();

let (sql_exists,) = sqlx::query_as::<_, (bool,)>(
"SELECT EXISTS (SELECT 1 FROM db_user_identity.emails WHERE email = $1)",
)
.bind(&email)
.fetch_one(&ctx.crdb().await.unwrap())
.await
.unwrap();
assert!(sql_exists, "identity not created");
}
41 changes: 41 additions & 0 deletions packages/services/user/tests/identity_delete.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use chirp_workflow::prelude::*;
use rivet_operation::prelude::proto::backend;

#[workflow_test]
async fn empty(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {
..Default::default()
})
.await
.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

let email = util::faker::email();
ctx.op(::user::ops::identity::create::Input {
user_id: user_id,
identity: backend::user_identity::Identity {
kind: Some(backend::user_identity::identity::Kind::Email(
backend::user_identity::identity::Email {
email: email.clone()
}
)),
},
})
.await
.unwrap();

ctx.op(::user::ops::identity::delete::Input {
user_ids: vec![user_id],
})
.await
.unwrap();

let (sql_exists,) = sqlx::query_as::<_, (bool,)>(
"SELECT EXISTS (SELECT 1 FROM db_user_identity.emails WHERE email = $1)",
)
.bind(&email)
.fetch_one(&ctx.crdb().await.unwrap())
.await
.unwrap();
assert!(!sql_exists, "identity not deleted");
}
42 changes: 42 additions & 0 deletions packages/services/user/tests/identity_get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use chirp_workflow::prelude::*;
use rivet_operation::prelude::proto::backend;

#[workflow_test]
async fn empty(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {
..Default::default()
})
.await
.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

let email = util::faker::email();
ctx.op(::user::ops::identity::create::Input {
user_id: user_id,
identity: backend::user_identity::Identity {
kind: Some(backend::user_identity::identity::Kind::Email(
backend::user_identity::identity::Email {
email: email.clone()
}
)),
},
})
.await
.unwrap();

let res = ctx.op(::user::ops::identity::get::Input {
user_ids: vec![user_id, Uuid::new_v4()],
})
.await
.unwrap();
assert_eq!(1, res.users.len());
assert_eq!(
1,
res.users
.iter()
.find(|u| u.user_id == user_id)
.unwrap()
.identities
.len()
);
}
67 changes: 67 additions & 0 deletions packages/services/user/tests/pending_delete_toggle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use chirp_workflow::prelude::*;
use rivet_operation::prelude::proto::backend;

#[workflow_test]
async fn empty(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {}).await.unwrap();
let user_id = user_res.user_id.as_ref().unwrap().as_uuid();

// Register user
let email = util::faker::email();
let _res = ctx.op(::user::ops::identity::create::Input {
user_id: user_id,
identity: backend::user_identity::Identity {
kind: Some(backend::user_identity::identity::Kind::Email(
backend::user_identity::identity::Email {
email: email.clone()
}
)),
},
})
.await
.unwrap();

ctx.op(::user::ops::pending_delete_toggle::Input {
user_id: user_id,
active: true,
})
.await
.unwrap();

let (delete_request_ts,): (Option<i64>,) = sqlx::query_as(indoc!(
"
SELECT delete_request_ts
FROM db_user.users
WHERE
user_id = $1
",
))
.bind(user_id)
.fetch_one(&ctx.crdb().await.unwrap())
.await
.unwrap();

assert!(delete_request_ts.is_some());

ctx.op(::user::ops::pending_delete_toggle::Input {
user_id: user_id,
active: false,
})
.await
.unwrap();

let (delete_request_ts,): (Option<i64>,) = sqlx::query_as(indoc!(
"
SELECT delete_request_ts
FROM db_user.users
WHERE
user_id = $1
",
))
.bind(user_id)
.fetch_one(&ctx.crdb().await.unwrap())
.await
.unwrap();

assert!(delete_request_ts.is_none());
}
18 changes: 18 additions & 0 deletions packages/services/user/tests/profile_validate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use chirp_workflow::prelude::*;

#[workflow_test]
async fn empty(ctx: TestCtx) {
let user_res = op!([ctx] faker_user {}).await.unwrap();
let user_id = user_res.user_id.unwrap().as_uuid();

let res = ctx.op(::user::ops::profile_validate::Input {
user_id: user_id,
display_name: Some(" bad display name".to_owned()),
account_number: Some(10000),
bio: Some("bad\n\n\n\n\n\nbio".to_owned())
})
.await
.unwrap();

assert_eq!(res.errors.len(), 3, "validation failed");
}
Loading

0 comments on commit 6fb3d05

Please sign in to comment.