1
//! Creating an account with a name its parent already holds succeeds and
2
//! shows the duplicate-name notice; the rename side is in the manage tests.
3

            
4
use axum::{Router, body::Body, http::Request, http::StatusCode, routing::post};
5
use server::command::account_name::duplicate_name_notice;
6
use web::model::User;
7
use web::pages::account::create::account_create_submit;
8
use web::pages::forms::message::escaped;
9

            
10
use crate::common::create_mock_user;
11
use crate::common::new_page::{Reply, send_to};
12
use crate::common::tenant::provision_tenant;
13

            
14
4
async fn create(user: &User, name: &str) -> Reply {
15
4
    let router = Router::new().route("/account/create/submit", post(account_create_submit));
16
4
    let body = serde_urlencoded::to_string([("name", name)]).expect("urlencoded");
17
4
    let request = Request::builder()
18
4
        .method("POST")
19
4
        .uri("/account/create/submit")
20
4
        .header("content-type", "application/x-www-form-urlencoded")
21
4
        .body(Body::from(body))
22
4
        .expect("request");
23
4
    send_to(router, user, request).await
24
4
}
25

            
26
#[supp_macro::shared_runtime_test]
27
1
async fn a_twin_create_succeeds_with_the_notice() {
28
1
    let user = create_mock_user();
29
1
    provision_tenant(user.id).await;
30
1
    let first = create(&user, "Cafe").await;
31
1
    let twin = create(&user, "Cafe").await;
32

            
33
1
    let notice = escaped(&duplicate_name_notice("Cafe")).expect("renders");
34
1
    assert_eq!(first.status, StatusCode::OK, "{}", first.body);
35
1
    assert!(!first.body.contains(&notice), "{}", first.body);
36
1
    assert_eq!(twin.status, StatusCode::OK, "{}", twin.body);
37
1
    assert!(twin.body.starts_with("New account id: "), "{}", twin.body);
38
1
    assert!(twin.body.ends_with(&format!(". {notice}")), "{}", twin.body);
39
}
40

            
41
#[supp_macro::shared_runtime_test]
42
1
async fn a_markup_name_is_escaped_in_the_twin_reply() {
43
1
    let user = create_mock_user();
44
1
    provision_tenant(user.id).await;
45
1
    let name = "<img src=x onerror=1>";
46
1
    create(&user, name).await;
47
1
    let twin = create(&user, name).await;
48

            
49
1
    assert_eq!(twin.status, StatusCode::OK, "{}", twin.body);
50
1
    assert!(!twin.body.contains("<img"), "{}", twin.body);
51
1
    assert!(twin.body.contains("&#60;img"), "{}", twin.body);
52
}