Got rid of raw json strings in unit tests

This commit is contained in:
Antoine Gersant 2018-11-12 22:31:43 -08:00
parent 64e86f5079
commit b781071b4e
2 changed files with 90 additions and 82 deletions

View file

@ -217,10 +217,10 @@ fn trigger_index(
Ok(()) Ok(())
} }
#[derive(Deserialize)] #[derive(Serialize, Deserialize)]
struct AuthCredentials { pub struct AuthCredentials {
username: String, pub username: String,
password: String, pub password: String,
} }
#[derive(Serialize)] #[derive(Serialize)]

View file

@ -69,27 +69,32 @@ fn get_test_environment(db_name: &str) -> TestEnvironment {
} }
fn complete_initial_setup(client: &Client) { fn complete_initial_setup(client: &Client) {
let body = format!( let configuration = config::Config {
r#" album_art_pattern: None,
{{ "users": [{{ "name": "{}", "password": "{}", "admin": true }}] prefix_url: None,
, "mount_dirs": [{{ "name": "{}", "source": "{}" }}] reindex_every_n_seconds: None,
}}"#, ydns: None,
TEST_USERNAME, TEST_PASSWORD, TEST_MOUNT_NAME, TEST_MOUNT_SOURCE users: Some(vec![config::ConfigUser {
); name: TEST_USERNAME.into(),
password: TEST_PASSWORD.into(),
admin: true,
}]),
mount_dirs: Some(vec![vfs::MountPoint {
name: TEST_MOUNT_NAME.into(),
source: TEST_MOUNT_SOURCE.into(),
}]),
};
let body = serde_json::to_string(&configuration).unwrap();
let response = client.put("/api/settings").body(&body).dispatch(); let response = client.put("/api/settings").body(&body).dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
} }
fn do_auth(client: &Client) { fn do_auth(client: &Client) {
let body = format!( let credentials = api::AuthCredentials {
r#" username: TEST_USERNAME.into(),
{{ "username": "{}" password: TEST_PASSWORD.into(),
, "password": "{}" };
}}"#, let body = serde_json::to_string(&credentials).unwrap();
TEST_USERNAME, TEST_PASSWORD
);
let response = client.post("/api/auth").body(body).dispatch(); let response = client.post("/api/auth").body(body).dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
} }
@ -183,65 +188,62 @@ fn settings() {
); );
} }
client let mut configuration = config::Config {
.put("/api/settings") album_art_pattern: Some("my_pattern".to_owned()),
.body( reindex_every_n_seconds: Some(3600),
r#" mount_dirs: Some(vec![
{ "users": [ { "name": "test_user", "password": "test_password", "admin": true } vfs::MountPoint {
, { "name": "other_user", "password": "other_password", "admin": false } name: TEST_MOUNT_NAME.into(),
] source: TEST_MOUNT_SOURCE.into(),
, "mount_dirs": [ { "name": "collection", "source": "test/collection" } },
, { "name": "more_music", "source": "test/collection" } vfs::MountPoint {
] name: "more_music".into(),
, "album_art_pattern": "my_pattern" source: "test/collection".into(),
, "reindex_every_n_seconds": 3600 },
, "prefix_url": "my_prefix" ]),
, "ydns": { "host": "my_host", "username": "my_username", "password": "my_password" } prefix_url: Some("my_prefix".to_owned()),
}"#, users: Some(vec![
) config::ConfigUser {
.dispatch(); name: "test_user".into(),
password: "some_password".into(),
admin: true,
},
config::ConfigUser {
name: "other_user".into(),
password: "some_other_password".into(),
admin: false,
},
]),
ydns: Some(ddns::DDNSConfig {
host: "my_host".into(),
username: "my_username".into(),
password: "my_password".into(),
}),
};
let body = serde_json::to_string(&configuration).unwrap();
configuration.users = Some(vec![
config::ConfigUser {
name: "test_user".into(),
password: "".into(),
admin: true,
},
config::ConfigUser {
name: "other_user".into(),
password: "".into(),
admin: false,
},
]);
client.put("/api/settings").body(body).dispatch();
{ {
let mut response = client.get("/api/settings").dispatch(); let mut response = client.get("/api/settings").dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
let response_body = response.body_string().unwrap(); let response_body = response.body_string().unwrap();
let response_json: config::Config = serde_json::from_str(&response_body).unwrap(); let response_json: config::Config = serde_json::from_str(&response_body).unwrap();
assert_eq!(response_json, configuration);
assert_eq!(
response_json,
config::Config {
album_art_pattern: Some("my_pattern".to_owned()),
reindex_every_n_seconds: Some(3600),
mount_dirs: Some(vec![
vfs::MountPoint {
name: TEST_MOUNT_NAME.into(),
source: TEST_MOUNT_SOURCE.into()
},
vfs::MountPoint {
name: "more_music".into(),
source: "test/collection".into()
}
]),
prefix_url: Some("my_prefix".to_owned()),
users: Some(vec![
config::ConfigUser {
name: "test_user".into(),
password: "".into(),
admin: true
},
config::ConfigUser {
name: "other_user".into(),
password: "".into(),
admin: false
}
]),
ydns: Some(ddns::DDNSConfig {
host: "my_host".into(),
username: "my_username".into(),
password: "my_password".into()
}),
}
);
} }
} }
@ -289,29 +291,35 @@ fn auth() {
complete_initial_setup(client); complete_initial_setup(client);
{ {
let credentials = api::AuthCredentials {
username: "garbage".into(),
password: "garbage".into(),
};
let response = client let response = client
.post("/api/auth") .post("/api/auth")
.body(r#"{"username": "garbage", "password": "garbage"}"#) .body(serde_json::to_string(&credentials).unwrap())
.dispatch(); .dispatch();
assert_eq!(response.status(), Status::Unauthorized); assert_eq!(response.status(), Status::Unauthorized);
} }
{ {
let credentials = api::AuthCredentials {
username: TEST_USERNAME.into(),
password: "garbage".into(),
};
let response = client let response = client
.post("/api/auth") .post("/api/auth")
.body(format!( .body(serde_json::to_string(&credentials).unwrap())
r#"{{"username": "{}", "password": "garbage"}}"#,
TEST_USERNAME
))
.dispatch(); .dispatch();
assert_eq!(response.status(), Status::Unauthorized); assert_eq!(response.status(), Status::Unauthorized);
} }
{ {
let credentials = api::AuthCredentials {
username: TEST_USERNAME.into(),
password: TEST_PASSWORD.into(),
};
let response = client let response = client
.post("/api/auth") .post("/api/auth")
.body(format!( .body(serde_json::to_string(&credentials).unwrap())
r#"{{"username": "{}", "password": "{}"}}"#,
TEST_USERNAME, TEST_PASSWORD
))
.dispatch(); .dispatch();
assert_eq!(response.status(), Status::Ok); assert_eq!(response.status(), Status::Ok);
assert_eq!(response.cookies()[0].name(), "session"); assert_eq!(response.cookies()[0].name(), "session");