1
use super::*;
2
use crate::form_render::fixture::assert_autocomplete;
3
use axum::{body::to_bytes, http::StatusCode};
4

            
5
#[derive(Template)]
6
#[template(source = "secret {{ self.broken()? }}", ext = "html")]
7
struct Broken;
8

            
9
impl Broken {
10
2
    fn broken(&self) -> Result<&'static str, std::io::Error> {
11
2
        Err(std::io::Error::other("askama secret detail"))
12
2
    }
13
}
14

            
15
#[derive(Template)]
16
#[template(source = "hello", ext = "html")]
17
struct Fine;
18

            
19
#[test]
20
2
fn the_register_page_marks_its_credential_fields() {
21
2
    let html = RegisterTemplate.render().unwrap();
22
2
    assert_autocomplete(
23
2
        &html,
24
2
        &[
25
2
            ("name=\"email\"", "username"),
26
2
            ("name=\"password\"", "new-password"),
27
2
            ("name=\"passwordConfirm\"", "new-password"),
28
2
        ],
29
2
    );
30
2
}
31

            
32
#[tokio::test]
33
2
async fn a_failed_render_is_a_generic_internal_error() {
34
2
    let response = HtmlTemplate(Broken).into_response();
35
2
    assert_eq!(response.status(), StatusCode::INTERNAL_SERVER_ERROR);
36
2
    let error = response.extensions().get::<WebError>().cloned();
37
2
    assert_eq!(
38
2
        error,
39
2
        Some(WebError::new(
40
2
            StatusCode::INTERNAL_SERVER_ERROR,
41
2
            "Internal error"
42
2
        ))
43
    );
44
2
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
45
2
    assert!(!String::from_utf8_lossy(&body).contains("askama secret detail"));
46
2
}
47

            
48
#[tokio::test]
49
2
async fn a_good_render_is_served_as_html() {
50
2
    let response = HtmlTemplate(Fine).into_response();
51
2
    assert_eq!(response.status(), StatusCode::OK);
52
2
    let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
53
2
    assert_eq!(&body[..], b"hello");
54
2
}