new laptop setup

This commit is contained in:
2022-12-24 16:34:17 +01:00
parent 5b95621d04
commit 31b47e892d
55 changed files with 789 additions and 5 deletions
+75
View File
@@ -0,0 +1,75 @@
use super::jwt;
use actix_web::dev::ServiceRequest;
pub fn check_password(password: String) -> Result<String, &'static str> {
match jwt::JwtToken::decode(password) {
Ok(_token) => Ok(String::from("passed")),
Err(message) => Err(message),
}
}
pub fn extract_header_token(request: &ServiceRequest) -> Result<String, &'static str> {
match request.headers().get("user-token") {
Some(token) => match token.to_str() {
Ok(processed_password) => Ok(String::from(processed_password)),
Err(_processed_password) => Err("there was an error processing token"),
},
None => Err("there is no token"),
}
}
#[cfg(test)]
mod processes_test {
use actix_web::test::TestRequest;
use crate::auth::jwt::JwtToken;
use super::check_password;
#[test]
fn check_correct_password() {
let password_string: String = JwtToken::encode(32);
let result = check_password(password_string);
match result {
Ok(check) => assert_eq!("passed", check),
_ => panic!("Check correct password failed."),
}
}
#[test]
fn incorrect_check_password() {
let password: String = String::from("test");
match check_password(password) {
Err(message) => assert_eq!("could not decode token", message),
_ => panic!("check password should not be able to be decoded"),
}
}
#[test]
fn successful_extract_header_token() {
let request = TestRequest::default()
.insert_header(("user-token", "token"))
.to_srv_request();
match super::extract_header_token(&request) {
Ok(processed_password) => assert_eq!("token", processed_password),
_ => panic!("failed extract_header_token"),
}
}
#[test]
fn failed_extract_header_token() {
let request = TestRequest::default()
.insert_header(("wrong", "bla"))
.to_srv_request();
match super::extract_header_token(&request) {
Err(processed_password) => assert_eq!("there is no token", processed_password),
_ => panic!("Extract header token should fail when not provided."),
}
}
}