diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-15 22:40:55 +0100 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2021-12-15 22:40:55 +0100 |
commit | 6aa72b3c8717f32e62c772aeed327d3cd9a6fa65 (patch) | |
tree | e6ac67e68c410aed1f0baa2857aeaf60d73448bd /backend/tests | |
parent | 13cdbc7ff760ae91ee3f62b2a2f62c7559ccaa3c (diff) | |
download | planetwars.dev-6aa72b3c8717f32e62c772aeed327d3cd9a6fa65.tar.xz planetwars.dev-6aa72b3c8717f32e62c772aeed327d3cd9a6fa65.zip |
gracefully handle invalid login credentials
Diffstat (limited to 'backend/tests')
-rw-r--r-- | backend/tests/login.rs (renamed from backend/tests/common.rs) | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/backend/tests/common.rs b/backend/tests/login.rs index 8ab68a1..9c70af2 100644 --- a/backend/tests/common.rs +++ b/backend/tests/login.rs @@ -37,6 +37,22 @@ macro_rules! run_test { }}; } +pub struct BearerAuth { + token: String, +} + +impl BearerAuth { + pub fn new(token: String) -> Self { + Self { token } + } +} + +impl<'a> Into<Header<'a>> for BearerAuth { + fn into(self) -> Header<'a> { + Header::new("Authorization", format!("Bearer {}", self.token)) + } +} + #[test] fn test_registration() { run_test!(|client, _conn| { @@ -62,7 +78,7 @@ fn test_registration() { let response = client .get("/users/me") - .header(Header::new("Authorization", token)) + .header(BearerAuth::new(token)) .dispatch() .await; @@ -73,3 +89,18 @@ fn test_registration() { assert_eq!(json["username"], "piepkonijn"); }); } + +#[test] +fn test_reject_invalid_credentials() { + run_test!(|client, _conn| { + let response = client + .post("/login") + .header(ContentType::JSON) + .body(r#"{"username": "piepkonijn", "password": "letmeinplease"}"#) + .dispatch() + .await; + + assert_eq!(response.status(), Status::Forbidden); + // assert_eq!(response.content_type(), Some(ContentType::JSON)); + }); +} |