|
| 1 | +use crate::cache::Key; |
| 2 | +use crate::handlers::html::qr::{code_from, dark_modules}; |
| 3 | +use crate::handlers::html::{make_error, ErrorResponse}; |
| 4 | +use crate::{Error, Page}; |
| 5 | +use askama::Template; |
| 6 | +use axum::extract::{Path, State}; |
| 7 | + |
| 8 | +/// GET handler for the burn page. |
| 9 | +pub async fn burn(Path(id): Path<String>, State(page): State<Page>) -> Result<Burn, ErrorResponse> { |
| 10 | + async { |
| 11 | + let code = tokio::task::spawn_blocking({ |
| 12 | + let page = page.clone(); |
| 13 | + let id = id.clone(); |
| 14 | + move || code_from(&page.base_url, &id) |
| 15 | + }) |
| 16 | + .await |
| 17 | + .map_err(Error::from)??; |
| 18 | + |
| 19 | + let key: Key = id.parse()?; |
| 20 | + |
| 21 | + Ok(Burn { |
| 22 | + page: page.clone(), |
| 23 | + key, |
| 24 | + code, |
| 25 | + }) |
| 26 | + } |
| 27 | + .await |
| 28 | + .map_err(|err| make_error(err, page)) |
| 29 | +} |
| 30 | + |
| 31 | +/// Burn page shown if "burn-after-reading" was selected during insertion. |
| 32 | +#[derive(Template)] |
| 33 | +#[template(path = "burn.html", escape = "none")] |
| 34 | +pub struct Burn { |
| 35 | + page: Page, |
| 36 | + key: Key, |
| 37 | + code: qrcodegen::QrCode, |
| 38 | +} |
| 39 | + |
| 40 | +impl Burn { |
| 41 | + fn dark_modules(&self) -> Vec<(i32, i32)> { |
| 42 | + dark_modules(&self.code) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use crate::test_helpers::Client; |
| 49 | + use reqwest::{header, StatusCode}; |
| 50 | + use serde::Serialize; |
| 51 | + |
| 52 | + #[tokio::test] |
| 53 | + async fn burn() -> Result<(), Box<dyn std::error::Error>> { |
| 54 | + let client = Client::new().await; |
| 55 | + |
| 56 | + let data = crate::handlers::insert::form::Entry { |
| 57 | + text: "FooBarBaz".to_string(), |
| 58 | + extension: None, |
| 59 | + expires: "burn".to_string(), |
| 60 | + password: "".to_string(), |
| 61 | + title: "".to_string(), |
| 62 | + }; |
| 63 | + |
| 64 | + let res = client.post("/").form(&data).send().await?; |
| 65 | + assert_eq!(res.status(), StatusCode::SEE_OTHER); |
| 66 | + |
| 67 | + let location = res.headers().get("location").unwrap().to_str()?; |
| 68 | + |
| 69 | + // Location is the `/burn/foo` page not the paste itself, so remove the prefix. |
| 70 | + let location = location.replace("burn/", ""); |
| 71 | + |
| 72 | + let res = client |
| 73 | + .get(&location) |
| 74 | + .header(header::ACCEPT, "text/html; charset=utf-8") |
| 75 | + .send() |
| 76 | + .await?; |
| 77 | + |
| 78 | + assert_eq!(res.status(), StatusCode::OK); |
| 79 | + |
| 80 | + let res = client |
| 81 | + .get(&location) |
| 82 | + .header(header::ACCEPT, "text/html; charset=utf-8") |
| 83 | + .send() |
| 84 | + .await?; |
| 85 | + |
| 86 | + assert_eq!(res.status(), StatusCode::NOT_FOUND); |
| 87 | + |
| 88 | + Ok(()) |
| 89 | + } |
| 90 | + |
| 91 | + #[tokio::test] |
| 92 | + async fn burn_encrypted() -> Result<(), Box<dyn std::error::Error>> { |
| 93 | + let client = Client::new().await; |
| 94 | + let password = "asd"; |
| 95 | + |
| 96 | + let data = crate::handlers::insert::form::Entry { |
| 97 | + text: "FooBarBaz".to_string(), |
| 98 | + extension: None, |
| 99 | + expires: "burn".to_string(), |
| 100 | + password: password.to_string(), |
| 101 | + title: "".to_string(), |
| 102 | + }; |
| 103 | + |
| 104 | + let res = client.post("/").form(&data).send().await?; |
| 105 | + assert_eq!(res.status(), StatusCode::SEE_OTHER); |
| 106 | + |
| 107 | + let location = res.headers().get("location").unwrap().to_str()?; |
| 108 | + |
| 109 | + // Location is the `/burn/foo` page not the paste itself, so remove the prefix. |
| 110 | + let location = location.replace("burn/", ""); |
| 111 | + |
| 112 | + let res = client |
| 113 | + .get(&location) |
| 114 | + .header(header::ACCEPT, "text/html; charset=utf-8") |
| 115 | + .send() |
| 116 | + .await?; |
| 117 | + |
| 118 | + assert_eq!(res.status(), StatusCode::OK); |
| 119 | + |
| 120 | + #[derive(Debug, Serialize)] |
| 121 | + struct Form { |
| 122 | + password: String, |
| 123 | + } |
| 124 | + |
| 125 | + let data = Form { |
| 126 | + password: password.to_string(), |
| 127 | + }; |
| 128 | + |
| 129 | + let res = client |
| 130 | + .post(&location) |
| 131 | + .form(&data) |
| 132 | + .header(header::ACCEPT, "text/html; charset=utf-8") |
| 133 | + .send() |
| 134 | + .await?; |
| 135 | + |
| 136 | + assert_eq!(res.status(), StatusCode::OK); |
| 137 | + |
| 138 | + let res = client |
| 139 | + .get(&location) |
| 140 | + .header(header::ACCEPT, "text/html; charset=utf-8") |
| 141 | + .send() |
| 142 | + .await?; |
| 143 | + |
| 144 | + assert_eq!(res.status(), StatusCode::NOT_FOUND); |
| 145 | + |
| 146 | + Ok(()) |
| 147 | + } |
| 148 | +} |
0 commit comments