1
use super::*;
2
use axum::body::to_bytes;
3
use finance::error::TransactionError;
4
use server::command::CmdError;
5
use server::db::DBError;
6

            
7
34
fn mapped(err: CmdError) -> WebError {
8
34
    WebError::from(err)
9
34
}
10

            
11
#[test]
12
2
fn args_map_to_bad_request_with_the_bare_message() {
13
2
    let err = mapped(CmdError::Args("blank tag".to_string()));
14
2
    assert_eq!(err, WebError::bad_request("blank tag"));
15
2
}
16

            
17
#[test]
18
2
fn finance_maps_to_unprocessable_with_the_innermost_message() {
19
2
    let err = mapped(CmdError::Finance(TransactionError::ZeroSum.into()));
20
2
    assert_eq!(err.status, StatusCode::UNPROCESSABLE_ENTITY);
21
2
    assert_eq!(err.message, "Transaction sum is not zero");
22
2
}
23

            
24
#[test]
25
2
fn finance_storage_failures_are_internal() {
26
2
    for inner in [
27
2
        FinanceError::DB(sqlx::Error::Protocol("x".into())),
28
2
        FinanceError::Internal("secret".to_string()),
29
2
    ] {
30
4
        let err = mapped(CmdError::Finance(inner));
31
4
        assert_eq!(
32
4
            err,
33
4
            WebError::new(StatusCode::INTERNAL_SERVER_ERROR, "Internal error")
34
        );
35
    }
36
2
}
37

            
38
#[test]
39
2
fn script_failures_are_internal_without_the_detail() {
40
2
    let err = mapped(CmdError::Script("JoinError { secret }".to_string()));
41
2
    assert_eq!(
42
2
        err,
43
2
        WebError::new(StatusCode::INTERNAL_SERVER_ERROR, "Internal error")
44
    );
45
2
}
46

            
47
#[test]
48
2
fn not_found_maps_to_404_with_its_message() {
49
2
    let err = mapped(CmdError::NotFound("Transaction not found".to_string()));
50
2
    assert_eq!(err, WebError::not_found("Transaction not found"));
51
2
}
52

            
53
#[test]
54
2
fn missing_rows_map_to_a_generic_404() {
55
2
    let causes = [
56
2
        WebError::from(CmdError::DB(sqlx::Error::RowNotFound)),
57
2
        WebError::from(CmdError::Server(ServerError::DB(DBError::Sqlx(
58
2
            sqlx::Error::RowNotFound,
59
2
        )))),
60
2
        WebError::from(ServerError::DB(DBError::Sqlx(sqlx::Error::RowNotFound))),
61
2
    ];
62
2
    for err in causes {
63
6
        assert_eq!(err, WebError::not_found("Not found"));
64
    }
65
2
}
66

            
67
#[test]
68
2
fn disabled_maps_to_forbidden() {
69
2
    let err = mapped(CmdError::Disabled);
70
2
    assert_eq!(err, WebError::forbidden("This account is disabled"));
71
2
}
72

            
73
#[test]
74
2
fn config_db_and_server_map_to_generic_internal() {
75
2
    let causes = [
76
2
        CmdError::Config(server::config::ConfigError::NoConfig("k".into())),
77
2
        CmdError::DB(sqlx::Error::Protocol("x".into())),
78
2
        CmdError::Server(ServerError::Lock),
79
2
    ];
80
2
    for cause in causes {
81
6
        let err = mapped(cause);
82
6
        assert_eq!(err.status, StatusCode::INTERNAL_SERVER_ERROR);
83
6
        assert_eq!(err.message, "Internal error");
84
    }
85
2
}
86

            
87
#[test]
88
2
fn an_unreachable_database_maps_to_service_unavailable() {
89
2
    let causes = [
90
2
        CmdError::DB(sqlx::Error::PoolTimedOut),
91
2
        CmdError::Server(DBError::PoolBudgetExhausted.into()),
92
2
        CmdError::Server(DBError::TenantQuarantined.into()),
93
2
        CmdError::Server(DBError::UnknownTenant.into()),
94
2
    ];
95
2
    for cause in causes {
96
8
        let err = mapped(cause);
97
8
        assert_eq!(err, WebError::unavailable(UNAVAILABLE_MESSAGE));
98
    }
99
2
    let server = WebError::from(ServerError::from(DBError::PoolBudgetExhausted));
100
2
    assert_eq!(server.status, StatusCode::SERVICE_UNAVAILABLE);
101
2
}
102

            
103
#[test]
104
2
fn server_errors_map_like_commands() {
105
2
    let finance = WebError::from(ServerError::Finance(TransactionError::ZeroSum.into()));
106
2
    assert_eq!(finance.status, StatusCode::UNPROCESSABLE_ENTITY);
107
2
    let creation = WebError::from(ServerError::Creation);
108
2
    assert_eq!(creation, WebError::unprocessable("Can't create an entity"));
109
2
    let internal = WebError::from(ServerError::Lock);
110
2
    assert_eq!(
111
2
        internal,
112
2
        WebError::new(StatusCode::INTERNAL_SERVER_ERROR, "Internal error")
113
    );
114
2
}
115

            
116
#[test]
117
2
fn not_found_carries_404() {
118
2
    assert_eq!(WebError::not_found("x").status, StatusCode::NOT_FOUND);
119
2
}
120

            
121
#[tokio::test]
122
2
async fn into_response_is_json_and_keeps_the_error_in_extensions() {
123
2
    let response = WebError::unprocessable("bad <b>").into_response();
124
2
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
125
2
    assert_eq!(
126
2
        response.extensions().get::<WebError>(),
127
2
        Some(&WebError::unprocessable("bad <b>"))
128
    );
129
2
    let bytes = to_bytes(response.into_body(), usize::MAX).await.unwrap();
130
2
    let body: serde_json::Value = serde_json::from_slice(&bytes).unwrap();
131
2
    assert_eq!(
132
2
        body,
133
2
        serde_json::json!({"status": "error", "message": "bad <b>"})
134
2
    );
135
2
}
136

            
137
#[test]
138
2
fn finance_wrapped_in_server_error_is_still_shown() {
139
2
    let err = mapped(CmdError::Server(ServerError::Finance(
140
2
        TransactionError::ZeroSum.into(),
141
2
    )));
142
2
    assert_eq!(err.status, StatusCode::UNPROCESSABLE_ENTITY);
143
2
    assert_eq!(err.message, "Transaction sum is not zero");
144
2
}
145

            
146
#[test]
147
2
fn template_author_errors_are_unprocessable_and_host_failures_internal() {
148
2
    let author = WebError::from(rpc::TemplateError::Runtime("my-code: boom".to_string()));
149
2
    assert_eq!(author.status, StatusCode::UNPROCESSABLE_ENTITY);
150
2
    assert!(author.message.contains("my-code: boom"));
151
2
    for internal in [
152
2
        rpc::TemplateError::Host("db down".to_string()),
153
2
        rpc::TemplateError::Engine("wasm".to_string()),
154
2
    ] {
155
4
        assert_eq!(
156
4
            WebError::from(internal),
157
4
            WebError::new(StatusCode::INTERNAL_SERVER_ERROR, "Internal error")
158
        );
159
    }
160
2
}
161

            
162
#[tokio::test]
163
2
async fn a_write_conflict_is_a_retryable_409_with_its_message() -> anyhow::Result<()> {
164
4
    let conflict = || ServerError::DB(DBError::WriteConflict);
165
2
    let expected = WebError::new(
166
2
        StatusCode::CONFLICT,
167
2
        "The data changed while saving; try again",
168
2
    );
169
2
    assert_eq!(WebError::from(conflict()), expected);
170
2
    assert_eq!(mapped(CmdError::Server(conflict())), expected);
171

            
172
2
    let response = expected.into_response();
173
2
    assert_eq!(response.status(), StatusCode::CONFLICT);
174
2
    let bytes = to_bytes(response.into_body(), usize::MAX).await?;
175
2
    let body: serde_json::Value = serde_json::from_slice(&bytes)?;
176
2
    assert_eq!(body["message"], "The data changed while saving; try again");
177
4
    Ok(())
178
2
}
179

            
180
#[test]
181
2
fn a_missing_price_is_unprocessable_like_the_convert_form() {
182
2
    let err = mapped(CmdError::NoPrice {
183
2
        from: uuid::Uuid::nil(),
184
2
        to: uuid::Uuid::nil(),
185
2
    });
186
2
    assert_eq!(err.status, StatusCode::UNPROCESSABLE_ENTITY);
187
2
}