rjango 0.1.1

A full-stack Rust backend framework inspired by Django
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::collections::HashMap;

use axum::{
    Router,
    http::{StatusCode, header::LOCATION},
};

use crate::http::request::HttpRequest;

pub use super::utils::{assert_form_error, assert_template_used};
use super::{
    client::{TestClient, TestResponse},
    request_factory::RequestFactory,
    runner::{TestRunner, TestSuiteResult},
};

macro_rules! impl_case_helpers {
    ($name:ident) => {
        impl $name {
            #[must_use]
            pub fn request_factory(&self) -> RequestFactory {
                RequestFactory::new()
            }

            #[must_use]
            pub fn get(&self, path: &str) -> HttpRequest {
                self.request_factory().get(path)
            }

            #[must_use]
            pub fn client(&self, router: Router) -> TestClient {
                TestClient::new(router)
            }

            #[must_use]
            pub fn run_tests(&self, tests: Vec<(&str, Box<dyn Fn() -> bool>)>) -> TestSuiteResult {
                TestRunner::new().run_tests(tests)
            }

            #[must_use]
            pub fn run_tests_with(
                &self,
                runner: &TestRunner,
                tests: Vec<(&str, Box<dyn Fn() -> bool>)>,
            ) -> TestSuiteResult {
                runner.run_tests(tests)
            }
        }
    };
}

/// Django SimpleTestCase equivalent.
pub struct SimpleTestCase {
    pub settings_overrides: HashMap<String, String>,
}

impl SimpleTestCase {
    #[must_use]
    pub fn new() -> Self {
        Self {
            settings_overrides: HashMap::new(),
        }
    }

    pub fn override_settings(&mut self, key: &str, value: &str) {
        self.settings_overrides.insert(key.into(), value.into());
    }
}

impl_case_helpers!(SimpleTestCase);

impl Default for SimpleTestCase {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Default)]
pub struct TestCase {
    pub base: SimpleTestCase,
}

impl TestCase {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn client_following_redirects(&self, router: Router) -> TestClient {
        TestClient::with_follow_redirects(router, true)
    }
}

impl_case_helpers!(TestCase);

#[derive(Default)]
pub struct TransactionTestCase {
    pub base: SimpleTestCase,
}

impl TransactionTestCase {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn uses_database_transactions(&self) -> bool {
        false
    }
}

impl_case_helpers!(TransactionTestCase);

const TEST_SERVER_HOST: &str = "http://127.0.0.1";

pub struct LiveServerTestCase {
    pub base: SimpleTestCase,
    pub port: u16,
}

impl LiveServerTestCase {
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    #[must_use]
    pub fn live_server_url(&self) -> String {
        format!("{TEST_SERVER_HOST}:{}", self.port)
    }

    #[must_use]
    pub fn absolute_url(&self, path: &str) -> String {
        format!(
            "{}{}",
            self.live_server_url(),
            normalize_live_server_path(path)
        )
    }
}

impl_case_helpers!(LiveServerTestCase);

impl Default for LiveServerTestCase {
    fn default() -> Self {
        Self {
            base: SimpleTestCase::new(),
            port: 8081,
        }
    }
}

#[must_use]
fn normalize_live_server_path(path: &str) -> String {
    if path.starts_with('/') {
        path.to_string()
    } else {
        format!("/{path}")
    }
}

pub fn assert_redirects(response: &TestResponse, expected_url: &str) {
    assert!(
        response.status_code.is_redirection(),
        "expected redirect status for {}, got {}",
        response.url,
        response.status_code
    );

    let actual_url = response
        .headers
        .get(LOCATION)
        .and_then(|value| value.to_str().ok())
        .unwrap_or_default();

    assert_eq!(
        actual_url, expected_url,
        "expected redirect location {expected_url:?}, got {actual_url:?}"
    );
}

pub fn assert_contains(response: &TestResponse, text: &str) {
    assert!(
        response.content.contains(text),
        "expected response body for {} to contain {text:?}, got {:?}",
        response.url,
        response.content
    );
}

pub fn assert_not_contains(response: &TestResponse, text: &str) {
    assert!(
        !response.content.contains(text),
        "expected response body for {} to not contain {text:?}, got {:?}",
        response.url,
        response.content
    );
}

pub fn assert_status_code(response: &TestResponse, expected: StatusCode) {
    assert_eq!(
        response.status_code, expected,
        "expected status {} for {}, got {}",
        expected, response.url, response.status_code
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use axum::{
        Router,
        http::{HeaderMap, HeaderValue, Method},
        response::Redirect,
        routing::get,
    };
    use std::panic::catch_unwind;

    fn response_with(status: StatusCode, content: &str, url: &str) -> TestResponse {
        TestResponse {
            status_code: status,
            content: content.to_string(),
            headers: HeaderMap::new(),
            url: url.to_string(),
        }
    }

    fn test_router() -> Router {
        Router::new()
            .route("/hello/", get(|| async { "hello from router" }))
            .route("/source/", get(|| async { Redirect::to("/target/") }))
            .route("/target/", get(|| async { "redirect target" }))
    }

    #[test]
    fn simple_test_case_starts_with_no_overrides() {
        let case = SimpleTestCase::new();

        assert!(case.settings_overrides.is_empty());
    }

    #[test]
    fn simple_test_case_records_setting_overrides() {
        let mut case = SimpleTestCase::new();
        case.override_settings("DEBUG", "true");

        assert_eq!(
            case.settings_overrides.get("DEBUG"),
            Some(&"true".to_string())
        );
    }

    #[test]
    fn default_test_case_wraps_simple_test_case() {
        let case = TestCase::default();

        assert!(case.base.settings_overrides.is_empty());
    }

    #[test]
    fn live_server_test_case_uses_default_port() {
        let case = LiveServerTestCase::default();

        assert_eq!(case.port, 8081);
        assert!(case.base.settings_overrides.is_empty());
    }

    #[test]
    fn test_assert_redirects_success() {
        let mut response = response_with(StatusCode::FOUND, "", "/redirect/");
        response
            .headers
            .insert(LOCATION, HeaderValue::from_static("/target/"));

        assert_redirects(&response, "/target/");
    }

    #[test]
    fn test_assert_redirects_wrong_url_panics() {
        let mut response = response_with(StatusCode::FOUND, "", "/redirect/");
        response
            .headers
            .insert(LOCATION, HeaderValue::from_static("/target/"));

        let result = catch_unwind(|| assert_redirects(&response, "/other/"));
        assert!(result.is_err());
    }

    #[test]
    fn test_assert_contains_found() {
        let response = response_with(StatusCode::OK, "hello rust", "/hello/");
        assert_contains(&response, "rust");
    }

    #[test]
    fn test_assert_contains_missing_panics() {
        let response = response_with(StatusCode::OK, "hello rust", "/hello/");
        let result = catch_unwind(|| assert_contains(&response, "django"));
        assert!(result.is_err());
    }

    #[test]
    fn test_assert_not_contains_success() {
        let response = response_with(StatusCode::OK, "hello rust", "/hello/");
        assert_not_contains(&response, "django");
    }

    #[test]
    fn assert_not_contains_panics_when_text_is_present() {
        let response = response_with(StatusCode::OK, "hello rust", "/hello/");
        let result = catch_unwind(|| assert_not_contains(&response, "rust"));
        assert!(result.is_err());
    }

    #[test]
    fn test_assert_status_code_match() {
        let response = response_with(StatusCode::CREATED, "", "/items/");
        assert_status_code(&response, StatusCode::CREATED);
    }

    #[test]
    fn test_assert_status_code_mismatch_panics() {
        let response = response_with(StatusCode::OK, "", "/items/");
        let result = catch_unwind(|| assert_status_code(&response, StatusCode::NOT_FOUND));
        assert!(result.is_err());
    }

    #[test]
    fn assert_template_used_accepts_plain_template_marker() {
        let response = response_with(StatusCode::OK, "rendered by app/detail.html", "/items/1/");
        assert_template_used(&response, "app/detail.html");
    }

    #[test]
    fn assert_template_used_panics_when_marker_is_missing() {
        let response = response_with(StatusCode::OK, "plain body", "/items/1/");
        let result = catch_unwind(|| assert_template_used(&response, "app/detail.html"));
        assert!(result.is_err());
    }

    #[test]
    fn assert_form_error_requires_field_and_message() {
        let response = response_with(StatusCode::OK, "email: This field is required.", "/signup/");
        assert_form_error(&response, "email", "This field is required.");
    }

    #[test]
    fn assert_form_error_panics_when_message_is_missing() {
        let response = response_with(StatusCode::OK, "email", "/signup/");
        let result = catch_unwind(|| assert_form_error(&response, "email", "required"));
        assert!(result.is_err());
    }

    #[test]
    fn simple_test_case_builds_get_requests() {
        let request = SimpleTestCase::new().get("/hello/");

        assert_eq!(request.method, Method::GET);
        assert_eq!(request.path, "/hello/");
    }

    #[test]
    fn simple_test_case_runs_tests_with_failfast_runner() {
        let case = SimpleTestCase::new();
        let runner = TestRunner::new().with_failfast();
        let result = case.run_tests_with(
            &runner,
            vec![
                ("first", Box::new(|| false)),
                (
                    "second",
                    Box::new(|| panic!("failfast should stop before this test")),
                ),
            ],
        );

        assert_eq!(result.total(), 1);
        assert_eq!(result.failed(), 1);
    }

    #[test]
    fn test_case_can_follow_redirects() {
        let mut client = TestCase::new().client_following_redirects(test_router());
        let response = client.get("/source/");

        assert_eq!(response.status_code, StatusCode::OK);
        assert_eq!(response.url, "/target/");
        assert!(response.content.contains("redirect target"));
    }

    #[test]
    fn transaction_test_case_reports_transactions_are_unavailable() {
        assert!(!TransactionTestCase::new().uses_database_transactions());
    }

    #[test]
    fn live_server_test_case_builds_absolute_urls_for_test_client() {
        let case = LiveServerTestCase::new();
        let mut client = case.client(test_router());
        let response = client.get(&case.absolute_url("hello/"));

        assert_eq!(
            case.live_server_url(),
            format!("{TEST_SERVER_HOST}:{}", case.port)
        );
        assert_eq!(response.status_code, StatusCode::OK);
        assert!(response.content.contains("hello from router"));
    }
}