-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_test.go
More file actions
105 lines (99 loc) · 2.66 KB
/
auth_test.go
File metadata and controls
105 lines (99 loc) · 2.66 KB
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
package runway_auth_test
import (
"context"
auth "github.com/Runway-Club/auth_lib"
"github.com/Runway-Club/auth_lib/domain"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"testing"
)
func TestAuth(t *testing.T) {
auth.Initialize("configs/dev.yaml", func() gorm.Dialector {
return sqlite.Open("file::memory:?cache=shared")
}, func() gorm.Dialector {
return sqlite.Open("file::memory:?cache=shared")
}, true)
t.Run("sign up", func(t *testing.T) {
err := auth.SignUp(context.Background(), &domain.Auth{
Id: "test",
Username: "user01",
Password: "Strong123456",
})
if err != nil {
t.Error(err)
}
})
t.Run("sign in", func(t *testing.T) {
token, err := auth.SignIn(context.Background(), "user01", "Strong123456")
if err != nil {
t.Error(err)
}
if token == nil {
t.Error("token is nil")
}
if token.Id != "test" {
t.Errorf("expected id 1, got %s", token.Id)
}
if token.UserId != "test" {
t.Errorf("expected user id 1, got %s", token.UserId)
}
if token.RoleId != "default" {
t.Errorf("expected role id default, got %s", token.RoleId)
}
if token.Jwt == "" {
t.Error("jwt is empty")
}
})
t.Run("Create aci", func(t *testing.T) {
err := auth.GetACIUseCase().Create(context.Background(), &domain.ACI{
Id: "100",
Resource: "v1/course.GET",
RoleId: "default",
})
if err != nil {
t.Error(err)
}
err = auth.GetACIUseCase().Create(context.Background(), &domain.ACI{
Id: "101",
Resource: "v1/course.POST",
RoleId: "admin",
})
if err != nil {
t.Error(err)
}
err = auth.GetACIUseCase().Create(context.Background(), &domain.ACI{
Id: "102",
Resource: "v1/course.PUT",
RoleId: "admin",
UserId: "1",
})
})
t.Run("verify token and check perm", func(t *testing.T) {
token, err := auth.SignIn(context.Background(), "user01", "Strong123456")
if err != nil {
t.Error(err)
}
result := auth.VerifyTokenAndPerm(context.Background(), token.Jwt, "v1/course.GET", "")
if result != nil {
t.Error("expected true, got false")
}
result = auth.VerifyTokenAndPerm(context.Background(), token.Jwt, "v1/course.POST", "demo")
if result != nil {
t.Error("expected true, got false")
}
result = auth.VerifyTokenAndPerm(context.Background(), token.Jwt, "v1/course.PUT", "")
if result != nil {
t.Error("expected true, got false")
}
})
t.Run("bypass with admin user", func(t *testing.T) {
token, err := auth.SignIn(context.Background(), "admin", "Adminpassword@123")
if err != nil {
t.Error(err)
}
result := auth.VerifyTokenAndPerm(context.Background(), token.Jwt, "v1/payment.POST", "")
if result != nil {
t.Error("expected true, got false")
}
})
}