From 689b67ca745f6a67337ed293f40ebd7dbd6b8152 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Fri, 21 Feb 2020 09:13:35 +0800 Subject: [PATCH] tests: add more test cases for auth API --- src/tests.rs | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/src/tests.rs b/src/tests.rs index 0cdff69..61633fb 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -109,4 +109,91 @@ fn should_log_in_fail() { }"#) .dispatch(); assert_eq!(resp.status(), Status::InternalServerError); +} + +#[test] +fn should_change_pw_successfully() { + CLIENT.post("/auth") + .header(ContentType::JSON) + .body(r#"{ + "email": "test4@example.com", + "password": "testpw", + "pw_cost": "100", + "pw_nonce": "whatever", + "version": "001" + }"#) + .dispatch() + .body_string() + .unwrap(); + let resp = CLIENT + .post("/auth/change_pw") + .header(ContentType::JSON) + .body(r#"{ + "email": "test4@example.com", + "password": "testpw1", + "current_password": "testpw" + }"#) + .dispatch(); + assert_eq!(resp.status(), Status::NoContent); +} + +#[test] +fn should_change_pw_fail() { + CLIENT.post("/auth") + .header(ContentType::JSON) + .body(r#"{ + "email": "test5@example.com", + "password": "testpw", + "pw_cost": "100", + "pw_nonce": "whatever", + "version": "001" + }"#) + .dispatch() + .body_string() + .unwrap(); + let resp = CLIENT + .post("/auth/change_pw") + .header(ContentType::JSON) + .body(r#"{ + "email": "test5@example.com", + "password": "testpw1", + "current_password": "testpw2" + }"#) + .dispatch(); + assert_eq!(resp.status(), Status::InternalServerError); +} + +#[test] +fn should_change_pw_successfully_and_log_in_successfully() { + CLIENT.post("/auth") + .header(ContentType::JSON) + .body(r#"{ + "email": "test6@example.com", + "password": "testpw", + "pw_cost": "100", + "pw_nonce": "whatever", + "version": "001" + }"#) + .dispatch() + .body_string() + .unwrap(); + let resp = CLIENT + .post("/auth/change_pw") + .header(ContentType::JSON) + .body(r#"{ + "email": "test6@example.com", + "password": "testpw1", + "current_password": "testpw" + }"#) + .dispatch(); + assert_eq!(resp.status(), Status::NoContent); + let resp = CLIENT + .post("/auth/sign_in") + .header(ContentType::JSON) + .body(r#"{ + "email": "test6@example.com", + "password": "testpw1" + }"#) + .dispatch(); + assert_eq!(resp.status(), Status::Ok); } \ No newline at end of file