tangled
alpha
login
or
join now
kacaii.dev
/
sigo
0
fork
atom
๐ฉโ๐ Firefighters API written in Gleam!
lustre
gleam
0
fork
atom
overview
issues
pulls
pipelines
:truck: move signup test to the user module
kacaii.dev
6 days ago
9f0fcf1c
c44fa0b4
verified
This commit was signed with the committer's
known signature
.
kacaii.dev
SSH Key Fingerprint:
SHA256:n9v7QGNWHCUv1x/483hCtPUvTsVabU5PzC5CSJMUNtI=
+114
-44
4 changed files
expand all
collapse all
unified
split
justfile
server
test
auth_test.gleam
server_test.gleam
user_test.gleam
+1
-1
justfile
···
55
55
56
56
# Init the database container
57
57
[group("podman")]
58
58
-
@init-database: _create-pod
58
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
8
-
import shared/contract/signup as signup_contract
9
9
-
import shared/role
10
10
-
import shared/user
11
11
-
import wisp
8
8
+
import shared/session
12
9
import wisp/simulate
13
10
14
11
pub fn login_test() {
15
15
-
use ctx <- server_test.with_test_context()
12
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
22
-
let resp =
19
19
+
let req =
23
20
simulate.request(http.Post, url)
24
21
|> simulate.json_body(body)
25
25
-
|> router.handle_request(ctx)
22
22
+
23
23
+
let resp = router.handle_request(req, ctx)
26
24
27
25
assert resp.status == 200
28
26
assert response.get_cookies(resp) != []
29
29
-
}
30
27
31
31
-
pub fn signup_test() {
32
32
-
use ctx <- server_test.with_test_context()
33
33
-
let url = "/api/signup"
28
28
+
let body = simulate.read_body(resp)
29
29
+
let body_decoder = login_contract.response_decoder()
30
30
+
let assert Ok(session) = json.parse(body, body_decoder)
31
31
+
let assert session.Authenticated(returned) = session
34
32
35
35
-
let body =
36
36
-
signup_contract.RequestBody(
37
37
-
name: wisp.random_string(12),
38
38
-
role: role.None,
39
39
-
email: wisp.random_string(12),
40
40
-
phone: wisp.random_string(12),
41
41
-
password: wisp.random_string(12),
42
42
-
is_active: False,
43
43
-
)
44
44
-
|> signup_contract.request_to_json()
45
45
-
46
46
-
let req =
47
47
-
simulate.browser_request(http.Post, url)
48
48
-
|> simulate.json_body(body)
49
49
-
50
50
-
// not authenticated
51
51
-
{
52
52
-
let resp = router.handle_request(req, ctx)
53
53
-
assert resp.status == 401
54
54
-
}
55
55
-
56
56
-
// authenticated
57
57
-
{
58
58
-
let resp =
59
59
-
server_test.with_authorization(next: req, ctx:)
60
60
-
|> router.handle_request(ctx)
61
61
-
62
62
-
let body = simulate.read_body(resp)
63
63
-
assert resp.status == 201
64
64
-
let assert Ok(_) = json.parse(body, user.decoder())
65
65
-
}
33
33
+
assert returned.full_name == seed.full_name
34
34
+
assert returned.role == seed.role
35
35
+
assert returned.email == seed.email
36
36
+
assert returned.phone == seed.phone
66
37
}
+1
-1
server/test/server_test.gleam
···
33
33
})
34
34
}
35
35
36
36
-
pub fn with_test_context(next: fn(Context) -> a) -> Nil {
36
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
1
+
import gleam/http
2
2
+
import gleam/json
3
3
+
import server/router
4
4
+
import server/seed
5
5
+
import server_test
6
6
+
import shared/contract/signup as signup_contract
7
7
+
import shared/role
8
8
+
import shared/user
9
9
+
import wisp
10
10
+
import wisp/simulate
11
11
+
12
12
+
pub fn signup_test() -> Nil {
13
13
+
use ctx <- server_test.with_context()
14
14
+
let url = "/api/signup"
15
15
+
16
16
+
let body =
17
17
+
signup_contract.RequestBody(
18
18
+
name: wisp.random_string(12),
19
19
+
role: role.None,
20
20
+
email: wisp.random_string(12),
21
21
+
phone: wisp.random_string(12),
22
22
+
password: wisp.random_string(12),
23
23
+
is_active: False,
24
24
+
)
25
25
+
|> signup_contract.request_to_json()
26
26
+
27
27
+
let req =
28
28
+
simulate.browser_request(http.Post, url)
29
29
+
|> simulate.json_body(body)
30
30
+
31
31
+
// not authenticated
32
32
+
{
33
33
+
let resp = router.handle_request(req, ctx)
34
34
+
assert resp.status == 401
35
35
+
}
36
36
+
37
37
+
// authenticated
38
38
+
{
39
39
+
let resp =
40
40
+
server_test.with_authorization(next: req, ctx:)
41
41
+
|> router.handle_request(ctx)
42
42
+
43
43
+
let body = simulate.read_body(resp)
44
44
+
assert resp.status == 201
45
45
+
let assert Ok(_) = json.parse(body, user.decoder())
46
46
+
}
47
47
+
}
48
48
+
49
49
+
pub fn signup_with_email_conflict_test() -> Nil {
50
50
+
use ctx <- server_test.with_context()
51
51
+
let url = "/api/signup"
52
52
+
53
53
+
let body =
54
54
+
signup_contract.RequestBody(
55
55
+
name: wisp.random_string(12),
56
56
+
role: role.None,
57
57
+
email: seed.email,
58
58
+
phone: wisp.random_string(12),
59
59
+
password: wisp.random_string(12),
60
60
+
is_active: False,
61
61
+
)
62
62
+
|> signup_contract.request_to_json()
63
63
+
64
64
+
let req =
65
65
+
simulate.browser_request(http.Post, url)
66
66
+
|> simulate.json_body(body)
67
67
+
68
68
+
let resp =
69
69
+
server_test.with_authorization(next: req, ctx:)
70
70
+
|> router.handle_request(ctx)
71
71
+
72
72
+
assert resp.status == 409
73
73
+
}
74
74
+
75
75
+
pub fn signup_with_phone_conflict_test() -> Nil {
76
76
+
use ctx <- server_test.with_context()
77
77
+
let url = "/api/signup"
78
78
+
79
79
+
let body =
80
80
+
signup_contract.RequestBody(
81
81
+
name: wisp.random_string(12),
82
82
+
role: role.None,
83
83
+
email: wisp.random_string(12),
84
84
+
phone: seed.phone,
85
85
+
password: wisp.random_string(12),
86
86
+
is_active: False,
87
87
+
)
88
88
+
|> signup_contract.request_to_json()
89
89
+
90
90
+
let req =
91
91
+
simulate.browser_request(http.Post, url)
92
92
+
|> simulate.json_body(body)
93
93
+
94
94
+
let resp =
95
95
+
server_test.with_authorization(next: req, ctx:)
96
96
+
|> router.handle_request(ctx)
97
97
+
98
98
+
assert resp.status == 409
99
99
+
}