1
use actix_http::body::MessageBody;
2
use actix_web::{dev::ServiceResponse, http::header::ContentType, test, web, App};
3
use fs_extra::dir::{copy, CopyOptions};
4
use ngi0review::{app, data::Data, ReviewRequestForm};
5
use tempfile::{tempdir, TempDir};
6

            
7
3
fn create_data() -> (TempDir, web::Data<Data>) {
8
3
    let tempdir = tempdir().unwrap();
9
3
    copy("tests/data", tempdir.path(), &CopyOptions::new()).unwrap();
10
3
    let data = web::Data::new(Data::new(tempdir.path().join("data")).unwrap());
11
3
    (tempdir, data)
12
3
}
13

            
14
1
#[actix_web::test]
15
async fn test_form_get() {
16
    let (tempdir, data) = create_data();
17
    let cors = actix_cors::Cors::default();
18
    let app = test::init_service(app!(data, cors)).await;
19
    let req = test::TestRequest::get()
20
        .uri("/static/form.html")
21
        .insert_header(ContentType::plaintext())
22
        .to_request();
23
    let resp = test::call_service(&app, req).await;
24
    assert!(resp.status().is_success());
25
    drop(tempdir);
26
}
27

            
28
2
async fn test_form(form: ReviewRequestForm) -> ServiceResponse<impl MessageBody> {
29
2
    let (tempdir, data) = create_data();
30
2
    let cors = actix_cors::Cors::default();
31
2
    let app = test::init_service(app!(data, cors)).await;
32
2
    let req = test::TestRequest::post()
33
2
        .uri("/NGI0/review/form.cgi")
34
2
        .set_form(form)
35
2
        .to_request();
36
2
    let result = test::call_service(&app, req).await;
37
2
    drop(tempdir);
38
2
    result
39
2
}
40

            
41
1
#[actix_web::test]
42
async fn test_incomplete_form_cgi_post() {
43
    let resp = test_form(ReviewRequestForm {
44
        name: "HELLO".to_string(),
45
        ..Default::default()
46
    })
47
    .await;
48
    assert!(resp.status().is_client_error());
49
}
50

            
51
1
#[actix_web::test]
52
async fn test_minimal_form_cgi_post() {
53
    let resp = test_form(ReviewRequestForm {
54
        projectname: "HELLO".to_string(),
55
        name: "HELLO".to_string(),
56
        email: "user@example.com".to_string(),
57
        programme: "NGI SomeProgramme".to_string(),
58
        www: "HELLO.com".to_string(),
59
        ..Default::default()
60
    })
61
    .await;
62
    assert!(resp.status().is_success());
63
}