Update the testsuite...
now tests: * creating .env * migrating * creating admin * login * listing links * creating links * filtering links
This commit is contained in:
parent
cb6ee80e43
commit
b9a02b1740
9
Cargo.lock
generated
9
Cargo.lock
generated
@ -772,9 +772,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cc"
|
name = "cc"
|
||||||
version = "1.0.68"
|
version = "1.0.69"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "4a72c244c1ff497a746a7e1fb3d14bd08420ecda70c8f25c7112f2781652d787"
|
checksum = "e70cc2f62c6ce1868963827bd677764c62d07c3d9a3e1fb1177ee1a9ab199eb2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"jobserver",
|
"jobserver",
|
||||||
]
|
]
|
||||||
@ -3145,6 +3145,7 @@ dependencies = [
|
|||||||
"percent-encoding",
|
"percent-encoding",
|
||||||
"pin-project-lite 0.2.7",
|
"pin-project-lite 0.2.7",
|
||||||
"serde",
|
"serde",
|
||||||
|
"serde_json",
|
||||||
"serde_urlencoded",
|
"serde_urlencoded",
|
||||||
"time 0.2.27",
|
"time 0.2.27",
|
||||||
"tokio",
|
"tokio",
|
||||||
@ -3744,9 +3745,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "subtle"
|
name = "subtle"
|
||||||
version = "2.4.0"
|
version = "2.4.1"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1e81da0851ada1f3e9d4312c704aa4f8806f0f9d69faaf8df2f3464b4a9437c2"
|
checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
|
@ -68,5 +68,5 @@ predicates = "2.0.0"
|
|||||||
|
|
||||||
|
|
||||||
[dev-dependencies.reqwest]
|
[dev-dependencies.reqwest]
|
||||||
features = ["cookies"]
|
features = ["cookies", "json"]
|
||||||
version = "0.10.10"
|
version = "0.10.10"
|
||||||
|
@ -281,7 +281,9 @@ pub async fn process_login_json(
|
|||||||
Ok(HttpResponse::Ok().json2(&u))
|
Ok(HttpResponse::Ok().json2(&u))
|
||||||
} else {
|
} else {
|
||||||
info!("Invalid password for user: {}", &u.username);
|
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 {
|
} else {
|
||||||
// should fail earlier if secret is missing.
|
// should fail earlier if secret is missing.
|
||||||
|
6
pslink/static/wasm/.gitignore
vendored
6
pslink/static/wasm/.gitignore
vendored
@ -1,5 +1 @@
|
|||||||
app_bg.wasm
|
*
|
||||||
app_bg.wasm.d.ts
|
|
||||||
app.d.ts
|
|
||||||
app.js
|
|
||||||
package.json
|
|
@ -1,12 +1,10 @@
|
|||||||
use assert_cmd::prelude::*; // Add methods on commands
|
use assert_cmd::prelude::*; // Add methods on commands
|
||||||
use predicates::prelude::*;
|
use reqwest::header::HeaderMap;
|
||||||
use std::{
|
use std::{
|
||||||
|
collections::HashMap,
|
||||||
io::Read,
|
io::Read,
|
||||||
process::{Child, Command},
|
process::{Child, Command},
|
||||||
};
|
};
|
||||||
use tempdir::TempDir; // Used for writing assertions
|
|
||||||
|
|
||||||
use shared::datatypes::Secret;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_help_of_command_for_breaking_changes() {
|
fn test_help_of_command_for_breaking_changes() {
|
||||||
@ -167,7 +165,6 @@ async fn test_migrate_database() {
|
|||||||
struct RunningServer {
|
struct RunningServer {
|
||||||
server: Child,
|
server: Child,
|
||||||
port: i32,
|
port: i32,
|
||||||
dir: TempDir,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for RunningServer {
|
impl Drop for RunningServer {
|
||||||
@ -181,13 +178,14 @@ async fn run_server() -> RunningServer {
|
|||||||
|
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
let mut rng = thread_rng();
|
|
||||||
let port = rng.gen_range(12000..20000);
|
|
||||||
|
|
||||||
#[derive(serde::Serialize, Debug)]
|
#[derive(serde::Serialize, Debug)]
|
||||||
pub struct Count {
|
pub struct Count {
|
||||||
pub number: i32,
|
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");
|
let tmp_dir = tempdir::TempDir::new("pslink_test_env").expect("create temp dir");
|
||||||
// generate .env file
|
// generate .env file
|
||||||
let _output = Command::cargo_bin("pslink")
|
let _output = Command::cargo_bin("pslink")
|
||||||
@ -266,16 +264,12 @@ async fn run_server() -> RunningServer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
RunningServer {
|
RunningServer { server, port }
|
||||||
server,
|
|
||||||
port,
|
|
||||||
dir: tmp_dir,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[actix_rt::test]
|
#[actix_rt::test]
|
||||||
async fn test_web_paths() {
|
async fn test_web_paths() {
|
||||||
let mut server = run_server().await;
|
let server = run_server().await;
|
||||||
|
|
||||||
// We need to bring in `reqwest`
|
// We need to bring in `reqwest`
|
||||||
// to perform HTTP requests against our application.
|
// to perform HTTP requests against our application.
|
||||||
@ -295,110 +289,271 @@ async fn test_web_paths() {
|
|||||||
.await
|
.await
|
||||||
.expect("Failed to execute request.");
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
// // The basic redirection is working!
|
// The basic redirection is working!
|
||||||
// assert!(response.status().is_redirection());
|
assert!(response.status().is_redirection());
|
||||||
// let location = response.headers().get("location").unwrap();
|
let location = response.headers().get("location").unwrap();
|
||||||
// assert!(location.to_str().unwrap().contains("github"));
|
assert!(location.to_str().unwrap().contains("github"));
|
||||||
|
|
||||||
// let login_url = base_url.clone() + "/admin/login/";
|
let app_url = base_url.clone() + "app/";
|
||||||
// // Act
|
// Act
|
||||||
// let response = client
|
let response = client
|
||||||
// .get(login_url.clone())
|
.get(&app_url.clone())
|
||||||
// .send()
|
.send()
|
||||||
// .await
|
.await
|
||||||
// .expect("Failed to execute request.");
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
// // The Loginpage is reachable and contains a password field!
|
println!("{:?}", response);
|
||||||
// assert!(response.status().is_success());
|
|
||||||
// let content = response.text().await.unwrap();
|
|
||||||
// assert!(
|
|
||||||
// content.contains(r#"<input type="password"#),
|
|
||||||
// "No password field was found!"
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // Act
|
// The app page is reachable and contains the wasm file!
|
||||||
// let formdata = &[("username", "test"), ("password", "testpw")];
|
assert!(response.status().is_success());
|
||||||
// let response = client
|
let content = response.text().await.unwrap();
|
||||||
// .post(login_url)
|
assert!(
|
||||||
// .form(formdata)
|
content.contains(r#"init('/static/wasm/app_bg.wasm');"#),
|
||||||
// .send()
|
"The app page has unexpected content!"
|
||||||
// .await
|
);
|
||||||
// .expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// // It is possible to login
|
// Act
|
||||||
// assert!(response.status().is_redirection());
|
let mut formdata = HashMap::new();
|
||||||
// let location = response.headers().get("location").unwrap();
|
formdata.insert("username", "test");
|
||||||
// assert_eq!("/admin/index/", location.to_str().unwrap());
|
formdata.insert("password", "testpw");
|
||||||
// assert!(
|
let response = client
|
||||||
// response.headers().get("set-cookie").is_some(),
|
.post(&(base_url.clone() + "admin/json/login_user/"))
|
||||||
// "A auth cookie is not set even though authentication succeeds"
|
.json(&formdata)
|
||||||
// );
|
.send()
|
||||||
|
.await
|
||||||
|
.expect("Failed to execute request.");
|
||||||
|
|
||||||
// // After login this should return a redirect
|
println!("Login response:\n {:?}", response);
|
||||||
// let response = client
|
|
||||||
// .get("http://localhost:8080/admin/login/")
|
|
||||||
// .send()
|
|
||||||
// .await
|
|
||||||
// .expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// // The Loginpage redirects to link index when logged in
|
// It is possible to login
|
||||||
// assert!(
|
assert!(response.status().is_success());
|
||||||
// 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());
|
|
||||||
|
|
||||||
// // After login this should return a redirect
|
// Extract the cookie as it is not automatically saved for some reason.
|
||||||
// let response = client
|
let cookie = {
|
||||||
// .get("http://localhost:8080/admin/index/")
|
response
|
||||||
// .send()
|
.headers()
|
||||||
// .await
|
.get("set-cookie")
|
||||||
// .expect("Failed to execute request.");
|
.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
|
let content = response.text().await.unwrap();
|
||||||
// assert!(
|
println!("Content: {:?}", content);
|
||||||
// response.status().is_success(),
|
assert!(content.contains(r#""id":1"#), "id missing in content");
|
||||||
// "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/!"
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // Act title=haupt&target=http%3A%2F%2Fdas.geht%2Fjetzt%2F&code=tpuah
|
let mut custom_headers = HeaderMap::new();
|
||||||
// let formdata = &[
|
custom_headers.insert("content-type", "application/json".parse().unwrap());
|
||||||
// ("title", "haupt"),
|
custom_headers.insert("Cookie", cookie.parse().unwrap());
|
||||||
// ("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.");
|
|
||||||
|
|
||||||
// // It is possible to login
|
// After login this should return an empty list
|
||||||
// assert!(response.status().is_redirection());
|
let query = client
|
||||||
// let location = response.headers().get("location").unwrap();
|
.post(&(base_url.clone() + "admin/json/list_links/"))
|
||||||
// assert_eq!("/admin/view/link/tpuah", location.to_str().unwrap());
|
.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
|
// Make sure the list was retrieved and the status codes are correct
|
||||||
// let response = client
|
assert!(response.status().is_success());
|
||||||
// .get("http://localhost:8080/tpuah")
|
|
||||||
// .send()
|
|
||||||
// .await
|
|
||||||
// .expect("Failed to execute request.");
|
|
||||||
|
|
||||||
// // The basic redirection is working!
|
// Make sure that the content is an empty list as until now no links were created.
|
||||||
// assert!(response.status().is_redirection());
|
let content = response.text().await.unwrap();
|
||||||
// let location = response.headers().get("location").unwrap();
|
println!("Content: {:?}", content);
|
||||||
// assert!(location
|
assert!(content.contains(r#"[]"#), "id missing in content");
|
||||||
// .to_str()
|
|
||||||
// .unwrap()
|
// Create a link
|
||||||
// .contains("https://das.geht/jetzt/"));
|
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);
|
drop(server);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user