Newer
Older
package server
import (
"context"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
Fabian Seidl
committed
eventservice "code.fbi.h-da.de/danet/gosdn/controller/eventService"
"github.com/bufbuild/protovalidate-go"
func getTestAuthServer(t *testing.T) *AuthServer {
jwtManager := rbac.NewJWTManager("test", time.Minute)
Fabian Seidl
committed
eventService := eventservice.NewMockEventService()
userStore := rbac.NewMemoryUserStore()
Fabian Seidl
committed
userService := rbac.NewUserService(userStore, eventService)
roleStore := rbac.NewMemoryRoleStore()
Fabian Seidl
committed
roleService := rbac.NewRoleService(roleStore, eventService)
protoValidator, err := protovalidate.New()
if err != nil {
panic(err)
}
s := NewAuthServer(jwtManager, userService, protoValidator)
err = clearAndCreateAuthTestSetup(s.userService, roleService)
if err != nil {
t.Fatalf("%v", err)
}
return s
}
func TestAuth_Login(t *testing.T) {
type args struct {
ctx context.Context
request *apb.LoginRequest
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
name: "default login",
want: "testAdmin",
args: args{
request: &apb.LoginRequest{
Username: "testAdmin",
Pwd: "admin",
},
},
wantErr: false,
},
{
name: "login fail wrong pwd",
want: "",
args: args{
request: &apb.LoginRequest{
Username: "testAdmin",
Pwd: "nope",
},
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r := getTestAuthServer(t)
resp, err := r.Login(tt.args.ctx, tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("Auth.Login() error = %v, wantErr %v", err, tt.wantErr)
if resp != nil {
got := resp.Token
if got == "" {
t.Errorf("Auth.Login() = %v, want non empty token", got)
}
func TestAuth_Logout(t *testing.T) {
s := getTestAuthServer(t)
validToken, err := createTestUserToken("testAdmin", true, s.userService, s.jwtManager)
type args struct {
ctx context.Context
request *apb.LogoutRequest
}
tests := []struct {
name string
args args
want *apb.LogoutResponse
wantErr bool
}{
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorize", validToken)),
request: &apb.LogoutRequest{
Username: "testAdmin",
want: &apb.LogoutResponse{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := s.Logout(tt.args.ctx, tt.args.request)
if (err != nil) != tt.wantErr {
t.Errorf("Auth.Logout() error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}
}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
func TestAuth_isValidUser(t *testing.T) {
type args struct {
user rbac.User
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default valid user",
args: args{
user: rbac.User{
UserName: "testAdmin",
Password: "admin",
},
},
wantErr: false,
},
{
name: "error wrong user name",
args: args{
user: rbac.User{
UserName: "foo",
Password: "admin",
},
},
wantErr: true,
},
{
name: "error wrong password",
args: args{
user: rbac.User{
UserName: "testAdmin",
Password: "foo",
},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
s := getTestAuthServer(t)
if err := s.isValidUser(tt.args.user); (err != nil) != tt.wantErr {
t.Errorf("Auth.isValidUser() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAuth_handleLogout(t *testing.T) {
s := getTestAuthServer(t)
validToken, err := createTestUserToken("testAdmin", true, s.userService, s.jwtManager)
invalidToken, err := createTestUserToken("testAdmin", false, s.userService, s.jwtManager)
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
if err != nil {
log.Fatal(err)
}
type args struct {
ctx context.Context
userName string
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "default handle logout",
args: args{
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorize", validToken)),
userName: "testAdmin",
},
wantErr: false,
},
{
name: "fail no metadata",
args: args{
ctx: context.TODO(),
userName: "testAdmin",
},
wantErr: true,
},
{
name: "fail invalid token for user",
args: args{
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorize", invalidToken)),
userName: "testAdmin",
},
wantErr: true,
},
{
name: "fail invalid user for token",
args: args{
ctx: metadata.NewIncomingContext(context.Background(), metadata.Pairs("authorize", validToken)),
userName: "testUser",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := s.handleLogout(tt.args.ctx, tt.args.userName); (err != nil) != tt.wantErr {
t.Errorf("Auth.handleLogout() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}