| @@ -3,16 +3,19 @@ |
| 3 | 3 | /// the session store actor); the tests run them with mocks. |
| 4 | 4 | |
| 5 | 5 | import std/bytes |
| 6 | | −import std/http.{type Request, type Response, Response} |
| 6 | +import std/http.{type Request, type Response, Request, Response} |
| 7 | +import std/list |
| 7 | 8 | import std/option |
| 8 | 9 | import std/result |
| 9 | 10 | import std/string |
| 11 | +import std/test.{assert_equal} |
| 10 | 12 | import argon2 |
| 11 | 13 | import views |
| 12 | 14 | |
| 13 | 15 | pub type User { User(name: String, password_hash: String) } |
| 14 | 16 | |
| 15 | 17 | /// Why a registration failed. |
| 18 | +@derive(Show) |
| 16 | 19 | pub type RegisterError { |
| 17 | 20 | /// The name has fewer than 2 characters, or the password fewer than 8. |
| 18 | 21 | TooShort |
| @@ -128,6 +131,41 @@ fn new_user(name: String, password: String) -> Result<(), RegisterError> / <user |
| 128 | 131 | } |
| 129 | 132 | } |
| 130 | 133 | |
| 134 | +/// Users and sessions for the tests here: no users, and one session, |
| 135 | +/// `abc`, for `ada`. |
| 136 | +fn with_one_session(action: fn() -> a / <users | e>) -> a / <e> { |
| 137 | + handle { action() } with { |
| 138 | + find_user(_) -> None, |
| 139 | + add_user(_) -> Ok(()), |
| 140 | + new_session(_) -> "abc", |
| 141 | + session_user(token) -> if token == "abc" { Some("ada") } else { None }, |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +/// What `new_user` answers, as text: `()` has no `Eq` nor `Show`, so |
| 146 | +/// its `Result` cannot be compared directly. |
| 147 | +fn try_new_user(name: String, password: String) -> String / <ffi> { |
| 148 | + use <- with_one_session |
| 149 | + match new_user(name, password) { |
| 150 | + Ok(()) -> "added", |
| 151 | + Error(e) -> show(e), |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +@test |
| 156 | +fn names_need_two_characters_and_passwords_eight() -> () / <ffi> { |
| 157 | + assert_equal(try_new_user("a", "long enough"), "TooShort") |
| 158 | + assert_equal(try_new_user("ab", "1234567"), "TooShort") |
| 159 | + assert_equal(try_new_user("ab", "12345678"), "added") |
| 160 | +} |
| 161 | + |
| 162 | +@test |
| 163 | +fn lengths_count_characters_not_bytes() -> () / <ffi> { |
| 164 | + // "é" is 2 bytes in UTF-8, but 1 character. |
| 165 | + assert_equal(try_new_user("é", "long enough"), "TooShort") |
| 166 | + assert_equal(try_new_user("ab", "ééééééé"), "TooShort") |
| 167 | +} |
| 168 | + |
| 131 | 169 | fn start_session(name: String) -> Response / <users> { |
| 132 | 170 | redirect("/", "session=" <> new_session(name) <> "; Path=/; HttpOnly; SameSite=Lax") |
| 133 | 171 | } |
| @@ -151,14 +189,68 @@ fn session_of(cookies: List<String>) -> Option<String> / <users> { |
| 151 | 189 | } |
| 152 | 190 | } |
| 153 | 191 | |
| 192 | +fn with_cookie(cookie: String) -> Request { |
| 193 | + Request("GET", "/", [("Cookie", cookie)], bytes.empty()) |
| 194 | +} |
| 195 | + |
| 196 | +@test |
| 197 | +fn the_session_cookie_is_found_among_others() { |
| 198 | + use <- with_one_session |
| 199 | + assert_equal(current_user(with_cookie("theme=dark;session=abc; lang=fr")), Some("ada")) |
| 200 | +} |
| 201 | + |
| 202 | +@test |
| 203 | +fn without_a_session_cookie_there_is_no_user() { |
| 204 | + use <- with_one_session |
| 205 | + assert_equal(current_user(Request("GET", "/", [], bytes.empty())), None) |
| 206 | + assert_equal(current_user(with_cookie("theme=dark")), None) |
| 207 | + assert_equal(current_user(with_cookie("xsession=abc")), None) |
| 208 | +} |
| 209 | + |
| 210 | +@test |
| 211 | +fn an_unknown_session_token_has_no_user() { |
| 212 | + use <- with_one_session |
| 213 | + assert_equal(current_user(with_cookie("session=abd")), None) |
| 214 | + assert_equal(current_user(with_cookie("session=")), None) |
| 215 | +} |
| 216 | + |
| 154 | 217 | fn field(fields, name) { |
| 155 | 218 | http.field(fields, name) |> option.unwrap_or("") |
| 156 | 219 | } |
| 157 | 220 | |
| 221 | +@test |
| 222 | +fn a_missing_field_is_empty() { |
| 223 | + let fields = [("name", "ada"), ("password", "")] |
| 224 | + assert_equal(field(fields, "name"), "ada") |
| 225 | + assert_equal(field(fields, "password"), "") |
| 226 | + assert_equal(field(fields, "email"), "") |
| 227 | +} |
| 228 | + |
| 158 | 229 | fn redirect(to: String, cookie: String) -> Response { |
| 159 | 230 | Response(303, [("Location", to), ("Set-Cookie", cookie)], bytes.empty()) |
| 160 | 231 | } |
| 161 | 232 | |
| 233 | +/// The headers of `resp`, as they are sent (tuples have no `Eq`). |
| 234 | +fn header_lines(resp: Response) -> List<String> { |
| 235 | + list.map(resp.headers, fn(h) { h.0 <> ": " <> h.1 }) |
| 236 | +} |
| 237 | + |
| 238 | +@test |
| 239 | +fn a_redirect_sets_its_location_and_cookie() { |
| 240 | + let resp = redirect("/home", "session=abc") |
| 241 | + assert_equal(resp.status, 303) |
| 242 | + assert_equal(header_lines(resp), ["Location: /home", "Set-Cookie: session=abc"]) |
| 243 | + assert_equal(bytes.length(resp.body), 0) |
| 244 | +} |
| 245 | + |
| 162 | 246 | fn with_status(status: Int, resp: Response) -> Response { |
| 163 | 247 | Response(status, resp.headers, resp.body) |
| 164 | 248 | } |
| 249 | + |
| 250 | +@test |
| 251 | +fn with_status_keeps_the_headers_and_body() { |
| 252 | + let resp = with_status(401, http.text(200, "no")) |
| 253 | + assert_equal(resp.status, 401) |
| 254 | + assert_equal(header_lines(resp), ["Content-Type: text/plain; charset=utf-8"]) |
| 255 | + assert_equal(bytes.to_string_lossy(resp.body), "no") |
| 256 | +} |