diff --git a/controller/api/auth_test.go b/controller/api/auth_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8cd4dedd2225d38116488ca0f70f22aee6f82387 --- /dev/null +++ b/controller/api/auth_test.go @@ -0,0 +1,88 @@ +package api + +import ( + "context" + "reflect" + "testing" + + apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac" + "code.fbi.h-da.de/danet/gosdn/controller/rbac" + "github.com/google/uuid" +) + +func TestLogin(t *testing.T) { + type args struct { + ctx context.Context + addr string + username string + pwd string + } + tests := []struct { + name string + args args + want *apb.LoginResponse + wantErr bool + }{ + { + name: "default", + args: args{ + ctx: context.TODO(), + addr: testAPIEndpoint, + username: "admin", + pwd: "admin", + }, + want: &apb.LoginResponse{ + Status: apb.Status_STATUS_OK, + }, + wantErr: false, + }, + } + + err := userService.Add(&rbac.User{UserID: uuid.New(), UserName: "admin", Roles: map[string]string{pndID: "admin"}, Password: "admin"}) + if err != nil { + t.Errorf("Login() error = %v", err) + return + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Login(tt.args.ctx, tt.args.addr, tt.args.username, tt.args.pwd) + if (err != nil) != tt.wantErr { + t.Errorf("Login() error = %v, wantErr %v", err, tt.wantErr) + return + } + if got.Status != apb.Status_STATUS_OK || got.Token == "" { + t.Errorf("Login() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestLogout(t *testing.T) { + type args struct { + ctx context.Context + addr string + username string + } + tests := []struct { + name string + args args + want *apb.LogoutResponse + wantErr bool + }{ + // TODO: Add test cases. + // Not implemented yet! + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := Logout(tt.args.ctx, tt.args.addr, tt.args.username) + if (err != nil) != tt.wantErr { + t.Errorf("Logout() error = %v, wantErr %v", err, tt.wantErr) + return + } + if !reflect.DeepEqual(got, tt.want) { + t.Errorf("Logout() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/controller/api/initialise_test.go b/controller/api/initialise_test.go index bdd70085037ef49cedd18f19aa2ba437871e6405..a0b66962c91b6f03123bebb414da318a543c1101 100644 --- a/controller/api/initialise_test.go +++ b/controller/api/initialise_test.go @@ -10,6 +10,7 @@ import ( cpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/core" ppb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/pnd" + apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac" tpb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/transport" "code.fbi.h-da.de/danet/gosdn/controller/config" "code.fbi.h-da.de/danet/gosdn/controller/interfaces/device" @@ -132,9 +133,16 @@ func bootstrapUnitTest() { if err := pndStore.Add(&mockPnd); err != nil { log.Fatal(err) } + + jwtManager := rbacImpl.NewJWTManager("", (10000 * time.Hour)) + northbound := nbi.NewNBI(pndStore, userService, roleService) + northbound.Auth = nbi.NewAuthServer(jwtManager) + cpb.RegisterCoreServiceServer(s, northbound.Core) ppb.RegisterPndServiceServer(s, northbound.Pnd) + apb.RegisterAuthServiceServer(s, northbound.Auth) + go func() { if err := s.Serve(lis); err != nil { log.Fatalf("Server exited with error: %v", err)