This commit is contained in:
2023-12-03 19:13:02 +01:00
parent 382e43b55f
commit 503c674d5e
5 changed files with 389 additions and 30 deletions

View File

@@ -48,3 +48,104 @@ pub fn index_without_lang() -> Redirect {
// Default language is czech (TODO: config)
Redirect::to(format!("{}/{}", "", "cs"))
}
#[cfg(test)]
mod test {
use std::collections::HashMap;
use crate::template_fairing;
use super::*;
use chrono::NaiveDate;
use nanobyte_opentelemetry::rocket::TracingFairing;
use ovlach_data::cv::data::{CV, SocalLinks, Person};
use rocket::local::asynchronous::Client;
use rocket::{routes, Build};
use serde_json::json;
use wiremock::{MockServer, Mock, ResponseTemplate};
use wiremock::matchers::{method, path};
fn create_cv() -> CV {
let mut about = HashMap::<String, String>::new();
about.insert("cs".to_string(), "czech".to_string());
CV {
person: Person {
name: "TEST@NAME".to_string(),
email: "TEST@EMAIL".to_string(),
surname: "TEST@SURNAME".to_string(),
birthday: NaiveDate::from_ymd_opt(1980, 1, 1).unwrap(),
phone: 800017002,
adress: None,
social: SocalLinks {
facebook: None,
linkedin: None,
github: None,
instagram: None,
mastodon: None,
},
about,
},
languages: vec![],
education: vec![],
jobs: vec![],
skills: vec![],
}
}
fn build_rocket(port: u16) -> rocket::Rocket<Build> {
let data_source = CVBackendConfig{
cv_backend_path: format!("http://localhost:{}", port),
};
let presentation_config = PresentationConfig {
static_route: format!("http://localhost:{}", port),
};
let default_person = DefaultPerson {
default_person_name: "foo".to_string(),
};
let frontend_svc = FrontendSVCConfig {
contact_service: "http://localhost:8080".to_string(),
pdf_service: "http://localhost:8080".to_string(),
};
rocket::build()
.mount("/", routes![index])
.attach(TracingFairing::ignite())
.attach(template_fairing())
.manage(presentation_config)
.manage(default_person)
.manage(data_source)
.manage(frontend_svc)
}
#[test_log::test(tokio::test)]
async fn main() {
// Start a background HTTP server on a random local port
let mock_server = MockServer::start().await;
// Arrange the behaviour of the MockServer adding a Mock:
// when it receives a GET request on '/hello' it will respond with a 200.
Mock::given(method("GET"))
.and(path("/api/v1/cv/foo"))
.respond_with(ResponseTemplate::new(200).set_body_json(json!(create_cv())))
// Mounting the mock on the mock server - it's now effective!
.mount(&mock_server)
.await;
let rocket = build_rocket(mock_server.address().port());
let client = Client::tracked(rocket).await.unwrap();
let response = client
.get("/cs").dispatch().await;
assert_eq!(response.status(), Status::Ok);
assert!(response.body().is_some());
assert_eq!(response.content_type(), Some("text/html".parse().unwrap()));
let body = response.into_string().await.unwrap();
assert!(body.contains("TEST@NAME"));
assert!(body.contains("TEST@EMAIL"));
assert!(body.contains("TEST@SURNAME"))
}
}