Update the testsuite...

now tests:
  * creating .env
  * migrating
  * creating admin
  * login
  * listing links
  * creating links
  * filtering links
This commit is contained in:
Dietrich 2021-07-13 14:20:11 +02:00 committed by Franz Dietrich
parent cb6ee80e43
commit b9a02b1740
5 changed files with 270 additions and 116 deletions

9
Cargo.lock generated
View File

@ -772,9 +772,9 @@ dependencies = [
[[package]]
name = "cc"
version = "1.0.68"
version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787"
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2"
dependencies = [
"jobserver",
]
@ -3145,6 +3145,7 @@ dependencies = [
"percent-encoding",
"pin-project-lite 0.2.7",
"serde",
"serde_json",
"serde_urlencoded",
"time 0.2.27",
"tokio",
@ -3744,9 +3745,9 @@ dependencies = [
[[package]]
name = "subtle"
version = "2.4.0"
version = "2.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2"
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
[[package]]
name = "syn"

View File

@ -68,5 +68,5 @@ predicates = "2.0.0"
[dev-dependencies.reqwest]
features = ["cookies"]
features = ["cookies", "json"]
version = "0.10.10"

View File

@ -281,7 +281,9 @@ pub async fn process_login_json(
Ok(HttpResponse::Ok().json2(&u))
} else {
info!("Invalid password for user: {}", &u.username);
Ok(redirect_builder("/admin/login/"))
Ok(HttpResponse::Unauthorized().json2(&Status::Error(Message {
message: "Failed to Login".to_string(),
})))
}
} else {
// should fail earlier if secret is missing.

View File

@ -1,5 +1 @@
app_bg.wasm
app_bg.wasm.d.ts
app.d.ts
app.js
package.json
*

View File

@ -1,12 +1,10 @@
use assert_cmd::prelude::*; // Add methods on commands
use predicates::prelude::*;
use reqwest::header::HeaderMap;
use std::{
collections::HashMap,
io::Read,
process::{Child, Command},
};
use tempdir::TempDir; // Used for writing assertions
use shared::datatypes::Secret;
#[test]
fn test_help_of_command_for_breaking_changes() {
@ -167,7 +165,6 @@ async fn test_migrate_database() {
struct RunningServer {
server: Child,
port: i32,
dir: TempDir,
}
impl Drop for RunningServer {
@ -181,13 +178,14 @@ async fn run_server() -> RunningServer {
use rand::thread_rng;
use rand::Rng;
let mut rng = thread_rng();
let port = rng.gen_range(12000..20000);
#[derive(serde::Serialize, Debug)]
pub struct Count {
pub number: i32,
}
let mut rng = thread_rng();
let port = rng.gen_range(12000..20000);
let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
// generate .env file
let _output = Command::cargo_bin("pslink")
@ -266,16 +264,12 @@ async fn run_server() -> RunningServer {
}
}
RunningServer {
server,
port,
dir: tmp_dir,
}
RunningServer { server, port }
}
#[actix_rt::test]
async fn test_web_paths() {
let mut server = run_server().await;
let server = run_server().await;
// We need to bring in `reqwest`
// to perform HTTP requests against our application.
@ -295,110 +289,271 @@ async fn test_web_paths() {
.await
.expect("Failed to execute request.");
// // The basic redirection is working!
// assert!(response.status().is_redirection());
// let location = response.headers().get("location").unwrap();
// assert!(location.to_str().unwrap().contains("github"));
// The basic redirection is working!
assert!(response.status().is_redirection());
let location = response.headers().get("location").unwrap();
assert!(location.to_str().unwrap().contains("github"));
// let login_url = base_url.clone() + "/admin/login/";
// // Act
// let response = client
// .get(login_url.clone())
// .send()
// .await
// .expect("Failed to execute request.");
let app_url = base_url.clone() + "app/";
// Act
let response = client
.get(&app_url.clone())
.send()
.await
.expect("Failed to execute request.");
// // The Loginpage is reachable and contains a password field!
// assert!(response.status().is_success());
// let content = response.text().await.unwrap();
// assert!(
// content.contains(r#"<input type="password"#),
// "No password field was found!"
// );
println!("{:?}", response);
// // Act
// let formdata = &[("username", "test"), ("password", "testpw")];
// let response = client
// .post(login_url)
// .form(formdata)
// .send()
// .await
// .expect("Failed to execute request.");
// The app page is reachable and contains the wasm file!
assert!(response.status().is_success());
let content = response.text().await.unwrap();
assert!(
content.contains(r#"init('/static/wasm/app_bg.wasm');"#),
"The app page has unexpected content!"
);
// // It is possible to login
// assert!(response.status().is_redirection());
// let location = response.headers().get("location").unwrap();
// assert_eq!("/admin/index/", location.to_str().unwrap());
// assert!(
// response.headers().get("set-cookie").is_some(),
// "A auth cookie is not set even though authentication succeeds"
// );
// Act
let mut formdata = HashMap::new();
formdata.insert("username", "test");
formdata.insert("password", "testpw");
let response = client
.post(&(base_url.clone() + "admin/json/login_user/"))
.json(&formdata)
.send()
.await
.expect("Failed to execute request.");
// // After login this should return a redirect
// let response = client
// .get("http://localhost:8080/admin/login/")
// .send()
// .await
// .expect("Failed to execute request.");
println!("Login response:\n {:?}", response);
// // The Loginpage redirects to link index when logged in
// assert!(
// response.status().is_redirection(),
// "/admin/login/ is not redirecting correctly when logged in!"
// );
// let location = response.headers().get("location").unwrap();
// assert_eq!("/admin/index/", location.to_str().unwrap());
// It is possible to login
assert!(response.status().is_success());
// // After login this should return a redirect
// let response = client
// .get("http://localhost:8080/admin/index/")
// .send()
// .await
// .expect("Failed to execute request.");
// Extract the cookie as it is not automatically saved for some reason.
let cookie = {
response
.headers()
.get("set-cookie")
.expect("A auth cookie is not set even though authentication succeeds")
.to_str()
.unwrap()
.split(';')
.next()
.unwrap()
.to_string()
};
println!("{:?}", cookie);
assert!(cookie.starts_with("auth-cookie="));
// // The Loginpage redirects to link index when logged in
// assert!(
// response.status().is_success(),
// "Could not access /admin/index/"
// );
// let content = response.text().await.unwrap();
// assert!(
// content.contains(r#"<a href="/admin/logout/">"#),
// "No Logout Button was found on /admin/index/!"
// );
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(content.contains(r#""id":1"#), "id missing in content");
// // Act title=haupt&target=http%3A%2F%2Fdas.geht%2Fjetzt%2F&code=tpuah
// let formdata = &[
// ("title", "haupt"),
// ("target", "https://das.geht/jetzt/"),
// ("code", "tpuah"),
// ];
// let response = client
// .post("http://localhost:8080/admin/submit/")
// .form(formdata)
// .send()
// .await
// .expect("Failed to execute request.");
let mut custom_headers = HeaderMap::new();
custom_headers.insert("content-type", "application/json".parse().unwrap());
custom_headers.insert("Cookie", cookie.parse().unwrap());
// // It is possible to login
// assert!(response.status().is_redirection());
// let location = response.headers().get("location").unwrap();
// assert_eq!("/admin/view/link/tpuah", location.to_str().unwrap());
// After login this should return an empty list
let query = client
.post(&(base_url.clone() + "admin/json/list_links/"))
.headers(custom_headers.clone())
.body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// // Act
// let response = client
// .get("http://localhost:8080/tpuah")
// .send()
// .await
// .expect("Failed to execute request.");
// Make sure the list was retrieved and the status codes are correct
assert!(response.status().is_success());
// // The basic redirection is working!
// assert!(response.status().is_redirection());
// let location = response.headers().get("location").unwrap();
// assert!(location
// .to_str()
// .unwrap()
// .contains("https://das.geht/jetzt/"));
// Make sure that the content is an empty list as until now no links were created.
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(content.contains(r#"[]"#), "id missing in content");
// Create a link
let query = client
.post(&(base_url.clone() + "admin/json/create_link/"))
.headers(custom_headers.clone())
.body(r#"{"edit":"Create","id":null,"title":"ein testlink","target":"https://github.com/enaut/pslink","code":"test","author":0,"created_at":null}"#)
.build()
.unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the status codes are correct
assert!(response.status().is_success());
// Make sure that the content is a success message
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(
content.contains(r#""Success":"#),
"Make sure the link creation response contains Success"
);
// After inserting a link make sure the link is saved
let query = client
.post(&(base_url.clone() + "admin/json/list_links/"))
.headers(custom_headers.clone())
.body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the list was retrieved and the status codes are correct
assert!(response.status().is_success());
// Make sure that the content now contains the newly created link
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(
content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#),
"the new target and the new code are not in the result"
);
// Create a duplicate which should fail
let query = client
.post(&(base_url.clone() + "admin/json/create_link/"))
.headers(custom_headers.clone())
.body(r#"{"edit":"Create","id":null,"title":"ein testlink","target":"https://github.com/enaut/pslink","code":"test","author":0,"created_at":null}"#)
.build()
.unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the status codes are correct
assert!(response.status().is_server_error());
// Make sure that the content is a error message
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(
content.contains(r#"error"#),
"Make sure the link creation response contains error"
);
// Create a second link
let query = client
.post(&(base_url.clone() + "admin/json/create_link/"))
.headers(custom_headers.clone())
.body(r#"{"edit":"Create","id":null,"title":"ein second testlink","target":"https://crates.io/crates/pslink","code":"x","author":0,"created_at":null}"#)
.build()
.unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the status codes are correct
assert!(response.status().is_success());
// Make sure that the content is a success message
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(
content.contains(r#""Success":"#),
"Make sure the link creation response contains Success"
);
// After inserting a link make sure the link is saved
let query = client
.post(&(base_url.clone() + "admin/json/list_links/"))
.headers(custom_headers.clone())
.body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":""},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the list was retrieved and the status codes are correct
assert!(response.status().is_success());
// Make sure that the content now contains the newly created link
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
assert!(
content.contains(r#""target":"https://crates.io/crates/pslink","code":"x""#),
"the new target and the new code are not in the result"
);
assert!(
content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#),
"the new target and the new code are not in the result"
);
// After inserting two links make sure the filters work (searching for a description containing se)
let query = client
.post(&(base_url.clone() + "admin/json/list_links/"))
.headers(custom_headers.clone())
.body(r#"{"filter":{"Code":{"sieve":""},"Description":{"sieve":"se"},"Target":{"sieve":""},"Author":{"sieve":""},"Statistics":{"sieve":""}},"order":null,"amount":20}"#).build().unwrap();
println!("{:?}", query);
let response = client
.execute(query)
.await
.expect("Failed to execute request.");
println!("List urls response:\n {:?}", response);
// Make sure the list was retrieved and the status codes are correct
assert!(response.status().is_success());
// Make sure that the content now contains the newly created link
let content = response.text().await.unwrap();
println!("Content: {:?}", content);
// Code x should be in the result but not code test
assert!(
content.contains(r#""target":"https://crates.io/crates/pslink","code":"x""#),
"the new target and the new code are not in the result"
);
assert!(
!content.contains(r#""target":"https://github.com/enaut/pslink","code":"test""#),
"the new target and the new code are not in the result"
);
// Make sure we are redirected correctly.
let response = client
.get(&(base_url.clone() + "test"))
.send()
.await
.expect("Failed to execute request.");
// The basic redirection is working!
assert!(response.status().is_redirection());
let location = response.headers().get("location").unwrap();
assert!(location
.to_str()
.unwrap()
.contains("https://github.com/enaut/pslink"));
// And for the second link - also check that casing is correctly ignored
let response = client
.get(&(base_url.clone() + "X"))
.send()
.await
.expect("Failed to execute request.");
// The basic redirection is working!
assert!(response.status().is_redirection());
let location = response.headers().get("location").unwrap();
assert!(location
.to_str()
.unwrap()
.contains("https://crates.io/crates/pslink"));
drop(server);
}