152 lines
4.3 KiB
Rust
152 lines
4.3 KiB
Rust
|
use crate::helpers::TestApp;
|
||
|
use sqlx::PgPool;
|
||
|
use whitelist_api::domain::user::{User, UserID};
|
||
|
|
||
|
#[sqlx::test]
|
||
|
async fn gets_404_for_missing_user(pool: PgPool) {
|
||
|
// Arrange
|
||
|
let app = TestApp::spawn(&pool).await;
|
||
|
|
||
|
// Act
|
||
|
let user_id: i64 = 76561197960287930;
|
||
|
let response = app
|
||
|
.client
|
||
|
.get(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::NOT_FOUND);
|
||
|
}
|
||
|
|
||
|
#[sqlx::test]
|
||
|
async fn can_get_existing_user_from_db(pool: PgPool) {
|
||
|
// Arrange
|
||
|
let app = TestApp::spawn(&pool).await;
|
||
|
let user_id: i64 = 76561197960287930;
|
||
|
sqlx::query!("INSERT INTO users (id) VALUES ($1);", &user_id)
|
||
|
.execute(&pool)
|
||
|
.await
|
||
|
.expect("Failed to insert user into database!");
|
||
|
|
||
|
// Act
|
||
|
let response = app
|
||
|
.client
|
||
|
.get(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
||
|
assert_eq!(
|
||
|
response
|
||
|
.headers()
|
||
|
.get("Content-Type")
|
||
|
.expect("No content type was returned to the request."),
|
||
|
"application/json"
|
||
|
);
|
||
|
assert!(response.content_length() > Some(0));
|
||
|
let user: User = serde_json::from_str(
|
||
|
&response
|
||
|
.text()
|
||
|
.await
|
||
|
.expect("Failed to get returned text from the request."),
|
||
|
)
|
||
|
.expect("Failed to deserialize response as a User.");
|
||
|
assert_eq!(user.id(), &UserID::new(user_id));
|
||
|
// Check that the user is not a superuser by default
|
||
|
assert_eq!(user.superuser, false);
|
||
|
// Check that the user has a null discord_id by default
|
||
|
assert_eq!(user.discord_id, None.into());
|
||
|
}
|
||
|
|
||
|
|
||
|
#[sqlx::test]
|
||
|
async fn can_create_a_new_user(pool: PgPool) {
|
||
|
// Arrange
|
||
|
let app = TestApp::spawn(&pool).await;
|
||
|
let user_id: i64 = 76561197960287930;
|
||
|
|
||
|
// Act
|
||
|
let response = app
|
||
|
.client
|
||
|
.post(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::NO_CONTENT);
|
||
|
assert_eq!(response.content_length(), Some(0));
|
||
|
|
||
|
// Check that the user was actually created
|
||
|
let response = app
|
||
|
.client
|
||
|
.get(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::OK);
|
||
|
assert_eq!(
|
||
|
response
|
||
|
.headers()
|
||
|
.get("Content-Type")
|
||
|
.expect("No content type was returned to the request."),
|
||
|
"application/json"
|
||
|
);
|
||
|
assert!(response.content_length() > Some(0));
|
||
|
let user: User = serde_json::from_str(
|
||
|
&response
|
||
|
.text()
|
||
|
.await
|
||
|
.expect("Failed to get returned text from the request."),
|
||
|
)
|
||
|
.expect("Failed to deserialize response as a User.");
|
||
|
assert_eq!(user.id(), &UserID::new(user_id));
|
||
|
// Check that the user is not a superuser by default
|
||
|
assert_eq!(user.superuser, false);
|
||
|
// Check that the user has a null discord_id by default
|
||
|
assert_eq!(user.discord_id, None.into());
|
||
|
}
|
||
|
|
||
|
#[sqlx::test]
|
||
|
async fn attempting_to_create_user_twice_results_in_conflict(pool: PgPool) {
|
||
|
// Arrange
|
||
|
let app = TestApp::spawn(&pool).await;
|
||
|
let user_id: i64 = 76561197960287930;
|
||
|
|
||
|
// Act
|
||
|
let response = app
|
||
|
.client
|
||
|
.post(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::NO_CONTENT);
|
||
|
assert_eq!(response.content_length(), Some(0));
|
||
|
|
||
|
// Act Pt.2
|
||
|
let response = app
|
||
|
.client
|
||
|
.post(format!("{}/user/{}", &app.address, &user_id))
|
||
|
.send()
|
||
|
.await
|
||
|
.expect("Failed to execute request.");
|
||
|
|
||
|
// Check
|
||
|
assert_eq!(response.status(), reqwest::StatusCode::CONFLICT);
|
||
|
assert_eq!(
|
||
|
response
|
||
|
.headers()
|
||
|
.get("Content-Type")
|
||
|
.expect("No content type was returned to the request."),
|
||
|
"application/json"
|
||
|
);
|
||
|
}
|