Skip to content
Snippets Groups Projects
Commit c61ab601 authored by Fabian Seidl's avatar Fabian Seidl
Browse files

finished user api tests, minor refactoring

parent b09c1434
No related branches found
No related tags found
1 merge request!308Improve test coverage of rbac stuff
This commit is part of merge request !308. Comments created here will be created in the context of that merge request.
......@@ -6,6 +6,7 @@ import (
"testing"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
"github.com/google/uuid"
)
func TestCreateUsers(t *testing.T) {
......@@ -86,7 +87,7 @@ func TestGetUser(t *testing.T) {
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
name: "foo",
name: "foos",
},
want: nil,
wantErr: true,
......@@ -100,6 +101,7 @@ func TestGetUser(t *testing.T) {
t.Errorf("GetUser() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && got.Status == tt.want.Status {
if got.User.Name != tt.want.User.Name || got.User.Id != tt.want.User.Id {
t.Errorf("GetUser() = %v, want %v", got, tt.want)
......@@ -114,6 +116,11 @@ func TestGetUser(t *testing.T) {
}
func TestGetAllUsers(t *testing.T) {
err := clearAndCreateAuthTestSetup()
if err != nil {
t.Fatalf("%v", err)
}
type args struct {
ctx context.Context
addr string
......@@ -122,10 +129,27 @@ func TestGetAllUsers(t *testing.T) {
name string
args args
want *apb.GetUsersResponse
wantLen int
wantErr bool
}{
// TODO: Add test cases.
{
name: "default get users",
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
},
want: &apb.GetUsersResponse{
Status: apb.Status_STATUS_OK,
User: []*apb.User{
{Name: "testAdmin"},
{Name: "testUser"},
{Name: "testRandom"}},
},
wantLen: 3,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetAllUsers(tt.args.ctx, tt.args.addr)
......@@ -133,8 +157,24 @@ func TestGetAllUsers(t *testing.T) {
t.Errorf("GetAllUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetAllUsers() = %v, want %v", got, tt.want)
if got != nil && got.Status == apb.Status_STATUS_OK {
if len(got.User) != tt.wantLen {
t.Errorf("User.GetUsers() = %v, want %v", got, tt.want)
}
for _, gotU := range got.User {
containsExpected := false
for _, wantU := range tt.want.User {
if gotU.Name == wantU.Name {
containsExpected = true
break
}
}
if !containsExpected {
t.Errorf("User.GetUsers() = %v, want %v", got, tt.want)
}
}
}
})
}
......@@ -152,8 +192,40 @@ func TestUpdateUsers(t *testing.T) {
want *apb.UpdateUsersResponse
wantErr bool
}{
// TODO: Add test cases.
{
name: "default update user",
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
users: []*apb.User{
{
Id: adminID,
Name: "sth Else",
},
},
},
want: &apb.UpdateUsersResponse{
Status: apb.Status_STATUS_OK,
},
wantErr: false,
},
{
name: "error update user",
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
users: []*apb.User{
{
Id: uuid.NewString(),
Name: "not a User",
},
},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := UpdateUsers(tt.args.ctx, tt.args.addr, tt.args.users)
......@@ -161,7 +233,7 @@ func TestUpdateUsers(t *testing.T) {
t.Errorf("UpdateUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
if got != nil && got.Status != tt.want.Status {
t.Errorf("UpdateUsers() = %v, want %v", got, tt.want)
}
})
......@@ -180,7 +252,28 @@ func TestDeleteUsers(t *testing.T) {
want *apb.DeleteUsersResponse
wantErr bool
}{
// TODO: Add test cases.
{
name: "default delete users",
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
userNames: []string{"testUser", "testAdmin"},
},
want: &apb.DeleteUsersResponse{
Status: apb.Status_STATUS_OK,
},
wantErr: false,
},
{
name: "error delete users",
args: args{
ctx: context.TODO(),
addr: testAPIEndpoint,
userNames: []string{"no User"},
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
......@@ -189,8 +282,9 @@ func TestDeleteUsers(t *testing.T) {
t.Errorf("DeleteUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("DeleteUsers() = %v, want %v", got, tt.want)
if got != nil && got.Status != tt.want.Status {
t.Errorf("User.DeleteUsers() = %v, want %v", got, tt.want)
}
})
}
......
......@@ -167,6 +167,7 @@ func TestUser_GetUsers(t *testing.T) {
break
}
}
if !containsExpected {
t.Errorf("User.GetUsers() = %v, want %v", got, tt.want)
}
......@@ -222,6 +223,7 @@ func TestUser_UpdateUsers(t *testing.T) {
t.Errorf("User.UpdateUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && got.Status != tt.want.Status {
t.Errorf("User.UpdateUsers() = %v, want %v", got, tt.want)
}
......@@ -265,6 +267,7 @@ func TestUser_DeleteUsers(t *testing.T) {
t.Errorf("User.DeleteUsers() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != nil && got.Status != tt.want.Status {
t.Errorf("User.DeleteUsers() = %v, want %v", got, tt.want)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment