-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth_test.go
108 lines (99 loc) · 1.88 KB
/
auth_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package sqlite_test
import (
"context"
"sqlite"
"testing"
)
func TestAuthServiceSignup(t *testing.T) {
ctx := context.Background()
db, err := sqlite.CreateAndMigrateDb(ctx, ":memory:")
if err != nil {
t.Fatal(err)
return
}
svc := sqlite.NewAuthService(db)
// sign up with new creds
output, err := svc.Signup(ctx, sqlite.AuthInput{
UserName: "test",
Password: "test",
})
if err != nil {
t.Fatal(err)
return
}
if !output.OK {
t.Fatalf("expected successful signup")
return
}
identity, err := svc.GetTeamUserFromSession(ctx, output.Token)
if err != nil {
t.Fatal(err)
return
}
if identity.ID == 0 {
t.Fatalf("expected non-zero user, got 0")
return
}
// sign up with the existing creds
output, err = svc.Signup(ctx, sqlite.AuthInput{
UserName: "test",
Password: "test",
})
if err != nil {
t.Fatal(err)
return
}
if output.OK {
t.Fatalf("expected unsuccessful signup")
return
}
}
func TestAuthServiceLogin(t *testing.T) {
ctx := context.Background()
db, err := sqlite.CreateAndMigrateDb(ctx, ":memory:")
if err != nil {
t.Fatal(err)
return
}
svc := sqlite.NewAuthService(db)
svc.Signup(ctx, sqlite.AuthInput{
UserName: "test",
Password: "test",
})
// login with the correct creds
output, err := svc.Login(ctx, sqlite.AuthInput{
UserName: "test",
Password: "test",
})
if err != nil {
t.Fatal(err)
return
}
if !output.OK {
t.Fatal("expected successful login")
}
// login with the incorrect password
output, err = svc.Login(ctx, sqlite.AuthInput{
UserName: "test",
Password: "test wrong",
})
if err != nil {
t.Fatal(err)
return
}
if output.OK {
t.Fatal("expected failed login")
}
// login with an invalid username
output, err = svc.Login(ctx, sqlite.AuthInput{
UserName: "test wrong",
Password: "test",
})
if err != nil {
t.Fatal(err)
return
}
if output.OK {
t.Fatal("expected failed login")
}
}