louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 1 | /// The session store: an actor holding the users and sessions |
| 2 | /// (gen_server style). It is linked to the web server that starts it, so |
| 3 | /// the supervisor restarts both if either crashes. |
| 4 | |
| 5 | import app.{type RegisterError, type User, NameTaken, User} |
| 6 | import argon2 |
| 7 | import std/actor.{Continue, Stop} |
| 8 | import std/dict.{type Dict} |
| 9 | import std/option |
| 10 | import std/process.{type Subject, type proc} |
| 11 | import std/random.{type random} |
| 12 | import std/result |
| 13 | import std/string |
| 14 | import std/test.{assert, assert_equal} |
| 15 | |
| 16 | pub type Msg |
| 17 | { FindUser(name: String, reply: Subject<Option<User>>) |
| 18 | , AddUser(user: User, reply: Subject<Result<(), RegisterError>>) |
| 19 | , NewSession(name: String, reply: Subject<String>) |
| 20 | , SessionUser(token: String, reply: Subject<Option<String>>) |
| 21 | } |
| 22 | |
| 23 | type State { State(users: Dict<String, User>, sessions: Dict<String, String>) } |
| 24 | |
| 25 | /// What the store's messages and tests may perform (ADR 0044). |
| 26 | effect alias store_fx = proc, random |
| 27 | |
| 28 | /// Start a store, linked to the calling actor, with one user: `ada`, |
| 29 | /// password `lovelace`. |
| 30 | pub fn start_linked :: -> Subject<Msg> / <proc, ffi, random> |
| 31 | { |
| 32 | let ada = match argon2.hash_password "lovelace" (app.hashing ()) |
| 33 | { Ok(hash) -> User("ada", hash) |
| 34 | , Error(e) -> panic "cannot hash the seed password: {e}" |
| 35 | } |
| 36 | let users = dict.insert (dict.empty ()) "ada" ada |
| 37 | // A lambda, so `on_message` declares only the effects it uses. |
| 38 | let store = |
| 39 | actor.start |
| 40 | State(users, dict.empty ()) |
| 41 | fn state msg { on_message state msg } |
| 42 | process.link (process.owner store) |
| 43 | store |
| 44 | } |
| 45 | |
| 46 | fn on_message :: state: State msg: Msg -> actor.Next<State> / <store_fx> |
| 47 | { |
| 48 | match msg |
| 49 | { FindUser(name, reply) -> |
| 50 | { |
| 51 | process.send reply (dict.get state.users name) |
| 52 | Continue(state) |
| 53 | } |
| 54 | , AddUser |
| 55 | ( user |
| 56 | , reply |
| 57 | ) -> if dict.has state.users user.name |
| 58 | { |
| 59 | process.send reply Error(NameTaken) |
| 60 | Continue(state) |
| 61 | } |
| 62 | else |
| 63 | { |
| 64 | process.send reply Ok(()) |
| 65 | Continue(State(..state, users: dict.insert state.users user.name user)) |
| 66 | } |
| 67 | , NewSession(name, reply) -> |
| 68 | { |
| 69 | let token = random.token 16 |
| 70 | process.send reply token |
| 71 | Continue(State(..state, sessions: dict.insert state.sessions token name)) |
| 72 | } |
| 73 | , SessionUser(token, reply) -> |
| 74 | { |
| 75 | process.send reply (dict.get state.sessions token) |
| 76 | Continue(state) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // The tests below call `on_message` directly, with a reply subject of the |
| 82 | // test's own, and read the reply back. |
| 83 | |
| 84 | /// The reply `send` gets, and the state after it. |
| 85 | fn ask :: state: State send: fn(Subject<r>) -> Msg -> (r, State) / <store_fx> |
| 86 | { |
| 87 | let reply = process.subject () |
| 88 | match on_message state (send reply) |
| 89 | { Continue(next) -> match process.receive reply 0 |
| 90 | { Some(answer) -> (answer, next), None -> panic "no reply" } |
| 91 | , Stop -> panic "the store stopped" |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /// A store with one user, `ada`, whose hash is "h1". |
| 96 | fn with_ada :: -> State |
| 97 | { |
| 98 | State(dict.insert (dict.empty ()) "ada" User("ada", "h1"), dict.empty ()) |
| 99 | } |
| 100 | |
| 101 | @test |
| 102 | fn a_taken_name_keeps_its_first_user :: -> () / <store_fx> |
| 103 | { |
| 104 | let (added, state) = ask (with_ada ()) AddUser(User("ada", "h2"), _) |
| 105 | assert (added |> result.is_error) "refused" |
| 106 | let (found, _) = ask state FindUser("ada", _) |
| 107 | assert_equal (found |> option.map fn u { u.password_hash }) Some("h1") |
| 108 | } |
| 109 | |
| 110 | @test |
| 111 | fn a_new_user_can_be_found :: -> () / <store_fx> |
| 112 | { |
| 113 | let (added, state) = ask (with_ada ()) AddUser(User("bob", "h2"), _) |
| 114 | assert (added |> result.is_ok) "added" |
| 115 | let (found, _) = ask state FindUser("bob", _) |
| 116 | assert_equal (found |> option.map fn u { u.password_hash }) Some("h2") |
| 117 | } |
| 118 | |
| 119 | @test |
| 120 | fn a_new_session_names_its_user :: -> () / <store_fx> |
| 121 | { |
| 122 | let (token, state) = ask (with_ada ()) NewSession("ada", _) |
| 123 | assert_equal (string.length token) 32 |
| 124 | let (user, _) = ask state SessionUser(token, _) |
| 125 | assert_equal user Some("ada") |
| 126 | let (nobody, _) = ask state SessionUser("not{token}", _) |
| 127 | assert_equal nobody None |
| 128 | } |