๐Ÿ‘ฉโ€๐Ÿš’ Firefighters API written in Gleam!
lustre gleam

:truck: move signup test to the user module

kacaii.dev 9f0fcf1c c44fa0b4

verified
+114 -44
+1 -1
justfile
··· 55 55 56 56 # Init the database container 57 57 [group("podman")] 58 - @init-database: _create-pod 58 + @init-database: 59 59 podman run -d --pod {{ pod_name }} --name postgres-database {{ pg_env }} \ 60 60 {{ pg_health_cmd }} {{ pg_health_conf }} {{ pg_health_start_period }} \ 61 61 {{ pg_volume }} {{ pg_image }}
+13 -42
server/test/auth_test.gleam
··· 5 5 import server/seed 6 6 import server_test 7 7 import shared/contract/login as login_contract 8 - import shared/contract/signup as signup_contract 9 - import shared/role 10 - import shared/user 11 - import wisp 8 + import shared/session 12 9 import wisp/simulate 13 10 14 11 pub fn login_test() { 15 - use ctx <- server_test.with_test_context() 12 + use ctx <- server_test.with_context() 16 13 let url = "/api/login" 17 14 18 15 let body = 19 16 login_contract.RequestBody(email: seed.email, password: seed.password) 20 17 |> login_contract.request_to_json 21 18 22 - let resp = 19 + let req = 23 20 simulate.request(http.Post, url) 24 21 |> simulate.json_body(body) 25 - |> router.handle_request(ctx) 22 + 23 + let resp = router.handle_request(req, ctx) 26 24 27 25 assert resp.status == 200 28 26 assert response.get_cookies(resp) != [] 29 - } 30 27 31 - pub fn signup_test() { 32 - use ctx <- server_test.with_test_context() 33 - let url = "/api/signup" 28 + let body = simulate.read_body(resp) 29 + let body_decoder = login_contract.response_decoder() 30 + let assert Ok(session) = json.parse(body, body_decoder) 31 + let assert session.Authenticated(returned) = session 34 32 35 - let body = 36 - signup_contract.RequestBody( 37 - name: wisp.random_string(12), 38 - role: role.None, 39 - email: wisp.random_string(12), 40 - phone: wisp.random_string(12), 41 - password: wisp.random_string(12), 42 - is_active: False, 43 - ) 44 - |> signup_contract.request_to_json() 45 - 46 - let req = 47 - simulate.browser_request(http.Post, url) 48 - |> simulate.json_body(body) 49 - 50 - // not authenticated 51 - { 52 - let resp = router.handle_request(req, ctx) 53 - assert resp.status == 401 54 - } 55 - 56 - // authenticated 57 - { 58 - let resp = 59 - server_test.with_authorization(next: req, ctx:) 60 - |> router.handle_request(ctx) 61 - 62 - let body = simulate.read_body(resp) 63 - assert resp.status == 201 64 - let assert Ok(_) = json.parse(body, user.decoder()) 65 - } 33 + assert returned.full_name == seed.full_name 34 + assert returned.role == seed.role 35 + assert returned.email == seed.email 36 + assert returned.phone == seed.phone 66 37 }
+1 -1
server/test/server_test.gleam
··· 33 33 }) 34 34 } 35 35 36 - pub fn with_test_context(next: fn(Context) -> a) -> Nil { 36 + pub fn with_context(next: fn(Context) -> a) -> Nil { 37 37 let ctx = global_context() 38 38 let transaction = fn(db) { 39 39 next(Context(..ctx, db:))
+99
server/test/user_test.gleam
··· 1 + import gleam/http 2 + import gleam/json 3 + import server/router 4 + import server/seed 5 + import server_test 6 + import shared/contract/signup as signup_contract 7 + import shared/role 8 + import shared/user 9 + import wisp 10 + import wisp/simulate 11 + 12 + pub fn signup_test() -> Nil { 13 + use ctx <- server_test.with_context() 14 + let url = "/api/signup" 15 + 16 + let body = 17 + signup_contract.RequestBody( 18 + name: wisp.random_string(12), 19 + role: role.None, 20 + email: wisp.random_string(12), 21 + phone: wisp.random_string(12), 22 + password: wisp.random_string(12), 23 + is_active: False, 24 + ) 25 + |> signup_contract.request_to_json() 26 + 27 + let req = 28 + simulate.browser_request(http.Post, url) 29 + |> simulate.json_body(body) 30 + 31 + // not authenticated 32 + { 33 + let resp = router.handle_request(req, ctx) 34 + assert resp.status == 401 35 + } 36 + 37 + // authenticated 38 + { 39 + let resp = 40 + server_test.with_authorization(next: req, ctx:) 41 + |> router.handle_request(ctx) 42 + 43 + let body = simulate.read_body(resp) 44 + assert resp.status == 201 45 + let assert Ok(_) = json.parse(body, user.decoder()) 46 + } 47 + } 48 + 49 + pub fn signup_with_email_conflict_test() -> Nil { 50 + use ctx <- server_test.with_context() 51 + let url = "/api/signup" 52 + 53 + let body = 54 + signup_contract.RequestBody( 55 + name: wisp.random_string(12), 56 + role: role.None, 57 + email: seed.email, 58 + phone: wisp.random_string(12), 59 + password: wisp.random_string(12), 60 + is_active: False, 61 + ) 62 + |> signup_contract.request_to_json() 63 + 64 + let req = 65 + simulate.browser_request(http.Post, url) 66 + |> simulate.json_body(body) 67 + 68 + let resp = 69 + server_test.with_authorization(next: req, ctx:) 70 + |> router.handle_request(ctx) 71 + 72 + assert resp.status == 409 73 + } 74 + 75 + pub fn signup_with_phone_conflict_test() -> Nil { 76 + use ctx <- server_test.with_context() 77 + let url = "/api/signup" 78 + 79 + let body = 80 + signup_contract.RequestBody( 81 + name: wisp.random_string(12), 82 + role: role.None, 83 + email: wisp.random_string(12), 84 + phone: seed.phone, 85 + password: wisp.random_string(12), 86 + is_active: False, 87 + ) 88 + |> signup_contract.request_to_json() 89 + 90 + let req = 91 + simulate.browser_request(http.Post, url) 92 + |> simulate.json_body(body) 93 + 94 + let resp = 95 + server_test.with_authorization(next: req, ctx:) 96 + |> router.handle_request(ctx) 97 + 98 + assert resp.status == 409 99 + }