wip: currently rewriting the project as a full stack application
tangled.org/kacaii.dev/sigo
gleam
1import app
2import app/http_router
3import app/web/context.{type Context, Context}
4import envoy
5import gleam/erlang/process
6import gleam/http
7import gleam/json
8import gleeunit
9import global_value
10import pog
11import wisp
12import wisp/simulate
13
14pub const n_tests = 1
15
16pub fn main() -> Nil {
17 gleeunit.main()
18}
19
20/// Global context data used in unit tests
21pub fn global_data(next: fn(Context) -> a) -> a {
22 use <- global_value.create_with_unique_name("global_context")
23
24 let db_process_name = process.new_name("db_conn")
25 let registry_name = process.new_name("registry")
26 let env = context.Dev
27
28 let assert Ok(config) = app.read_connection_url(db_process_name, env)
29 let assert Ok(secret_key_base) = envoy.get("COOKIE_TOKEN")
30
31 let db = pog.named_connection(db_process_name)
32 let assert Ok(_) = pog.start(config)
33
34 let static_directory = app.static_directory()
35 next(Context(static_directory:, db:, registry_name:, secret_key_base:, env:))
36}
37
38/// Create a request with admin privileges
39pub fn with_authorization(next req: wisp.Request) -> wisp.Request {
40 use ctx <- global_data()
41
42 let login_req =
43 simulate.browser_request(http.Post, "/user/login")
44 |> simulate.json_body(
45 json.object([
46 #("matricula", json.string("000")),
47 #("senha", json.string("aluno")),
48 ]),
49 )
50
51 let login_resp = http_router.handle_request(login_req, ctx)
52
53 // Continue the session after being logged in
54 simulate.session(req, login_req, login_resp)
55}