-
Notifications
You must be signed in to change notification settings - Fork 0
/
login_test.go
123 lines (119 loc) · 3.02 KB
/
login_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package movielite
import (
"net/http"
"testing"
"github.com/jinzhu/gorm"
"github.com/ms900ft/movielite/models"
log "github.com/sirupsen/logrus"
"github.com/steinfletcher/apitest"
jsonpath "github.com/steinfletcher/apitest-jsonpath"
"golang.org/x/crypto/bcrypt"
)
func TestLoginSuccess(t *testing.T) {
apitest.New(). // configuration
Handler(S.Router).
Post("/login").
JSON(`{"username":"test", "password": "test123"}`).
Expect(t).
Assert(jsonpath.Matches(`$.token`, `^ey.+$`)).
Assert(jsonpath.Contains(`$.user_name`, "test")).
Status(http.StatusOK).
End()
}
func TestLoginFailed(t *testing.T) {
apitest.New(). // configuration
Handler(S.Router).
Post("/login").
JSON(`{"username":"test", "password": "test"}`).
Expect(t).
Assert(jsonpath.Contains(`$.error`, InvalidCredentials.Error())).
Status(http.StatusBadRequest).
End()
}
func TestLoginInvalid(t *testing.T) {
apitest.New(). // configuration
Handler(S.Router).
Post("/login").
FormData("username", "test").
FormData("password", "test").
Expect(t).
Status(http.StatusBadRequest).
End()
}
func TestService_FindOne(t *testing.T) {
//c := Config{Secret: "test123"}
dbc := models.DBConfig{DBName: ":memory:"}
db := models.ConnectDataBase(dbc)
pass, _ := bcrypt.GenerateFromPassword([]byte("test123"), bcrypt.DefaultCost)
user := models.User{UserName: "login", Password: string(pass)}
if err := db.Create(&user).Error; err != nil {
log.Fatal(err)
}
type fields struct {
Config *Config
DB *gorm.DB
}
type args struct {
username string
password string
}
tests := []struct {
name string
fields fields
args args
want token
wantErr error
}{
{
name: "success",
fields: fields{
Config: &Config{Secret: "test123"}, DB: db},
args: args{username: "login", password: "test123"},
want: token{UserName: "login"},
wantErr: nil,
},
{
name: "wrong password",
fields: fields{
Config: &Config{Secret: "test123"}, DB: db},
args: args{username: "login", password: "test456"},
//want: token{UserName: "login"},
wantErr: InvalidCredentials,
},
{
name: "user not found",
fields: fields{
Config: &Config{Secret: "test123"}, DB: db},
args: args{username: "login123", password: "test456"},
//want: token{UserName: "login"},
wantErr: InvalidCredentials,
},
{
name: "no secret",
fields: fields{
Config: &Config{}, DB: db},
args: args{username: "login", password: "test123"},
//want: token{UserName: "login"},
wantErr: NoSecretFound,
},
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := &Service{
Config: tt.fields.Config,
DB: tt.fields.DB,
}
got, err := s.FindOne(tt.args.username, tt.args.password)
if err != nil {
if err != tt.wantErr {
t.Errorf("Service.FindOne() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if got.UserName != tt.want.UserName {
t.Errorf("Service.FindOne() = %v, want %v", got, tt.want)
}
})
}
}