Skip to content
Snippets Groups Projects
Commit 201c1e69 authored by André Sterba's avatar André Sterba
Browse files

Adjust auth and tests

parent 202be968
Branches
No related tags found
6 merge requests!376Add additional example application hostname-checker,!349Northbound refactoring to implement NIB concept for devices,!343Add basic application framework and example application to show interaction between events an NBI,!339Create basic venv-manager for use with arista,!327Remove NBI package global services and refactor NBI servers,!324Provide prototype implementation for topology handling
...@@ -24,10 +24,11 @@ type Auth struct { ...@@ -24,10 +24,11 @@ type Auth struct {
userService rbacInterfaces.UserService userService rbacInterfaces.UserService
} }
// NewAuthServer receives a JWTManager and returns a new Auth interface. // NewAuthServer receives a JWTManager and a userService and returns a new Auth interface.
func NewAuthServer(jwtManager *rbac.JWTManager) *Auth { func NewAuthServer(jwtManager *rbac.JWTManager, userService rbacInterfaces.UserService) *Auth {
return &Auth{ return &Auth{
jwtManager: jwtManager, jwtManager: jwtManager,
userService: userService,
} }
} }
......
...@@ -4,12 +4,31 @@ import ( ...@@ -4,12 +4,31 @@ import (
"context" "context"
"log" "log"
"testing" "testing"
"time"
apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac" apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac"
"code.fbi.h-da.de/danet/gosdn/controller/rbac" "code.fbi.h-da.de/danet/gosdn/controller/rbac"
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
func getTestAuthServer(t *testing.T) *Auth {
jwtManager := rbac.NewJWTManager("test", time.Minute)
userStore := rbac.NewMemoryUserStore()
userService := rbac.NewUserService(userStore)
roleStore := rbac.NewMemoryRoleStore()
roleService := rbac.NewRoleService(roleStore)
s := NewAuthServer(jwtManager, userService)
err := clearAndCreateAuthTestSetup(s.userService, roleService)
if err != nil {
t.Fatalf("%v", err)
}
return s
}
func TestAuth_Login(t *testing.T) { func TestAuth_Login(t *testing.T) {
type args struct { type args struct {
ctx context.Context ctx context.Context
...@@ -47,9 +66,7 @@ func TestAuth_Login(t *testing.T) { ...@@ -47,9 +66,7 @@ func TestAuth_Login(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
r := Auth{ r := getTestAuthServer(t)
jwtManager: jwt,
}
resp, err := r.Login(tt.args.ctx, tt.args.request) resp, err := r.Login(tt.args.ctx, tt.args.request)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("Auth.Login() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Auth.Login() error = %v, wantErr %v", err, tt.wantErr)
...@@ -67,7 +84,8 @@ func TestAuth_Login(t *testing.T) { ...@@ -67,7 +84,8 @@ func TestAuth_Login(t *testing.T) {
} }
func TestAuth_Logout(t *testing.T) { func TestAuth_Logout(t *testing.T) {
validToken, err := createTestUserToken("testAdmin", true) s := getTestAuthServer(t)
validToken, err := createTestUserToken("testAdmin", true, s.userService, s.jwtManager)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -99,9 +117,6 @@ func TestAuth_Logout(t *testing.T) { ...@@ -99,9 +117,6 @@ func TestAuth_Logout(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := Auth{
jwtManager: jwt,
}
got, err := s.Logout(tt.args.ctx, tt.args.request) got, err := s.Logout(tt.args.ctx, tt.args.request)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("Auth.Logout() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Auth.Logout() error = %v, wantErr %v", err, tt.wantErr)
...@@ -158,7 +173,7 @@ func TestAuth_isValidUser(t *testing.T) { ...@@ -158,7 +173,7 @@ func TestAuth_isValidUser(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := Auth{} s := getTestAuthServer(t)
if err := s.isValidUser(tt.args.user); (err != nil) != tt.wantErr { if err := s.isValidUser(tt.args.user); (err != nil) != tt.wantErr {
t.Errorf("Auth.isValidUser() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("Auth.isValidUser() error = %v, wantErr %v", err, tt.wantErr)
} }
...@@ -167,12 +182,13 @@ func TestAuth_isValidUser(t *testing.T) { ...@@ -167,12 +182,13 @@ func TestAuth_isValidUser(t *testing.T) {
} }
func TestAuth_handleLogout(t *testing.T) { func TestAuth_handleLogout(t *testing.T) {
validToken, err := createTestUserToken("testAdmin", true) s := getTestAuthServer(t)
validToken, err := createTestUserToken("testAdmin", true, s.userService, s.jwtManager)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
invalidToken, err := createTestUserToken("testAdmin", false) invalidToken, err := createTestUserToken("testAdmin", false, s.userService, s.jwtManager)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
...@@ -221,9 +237,6 @@ func TestAuth_handleLogout(t *testing.T) { ...@@ -221,9 +237,6 @@ func TestAuth_handleLogout(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
s := Auth{
jwtManager: jwt,
}
if err := s.handleLogout(tt.args.ctx, tt.args.userName); (err != nil) != tt.wantErr { 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) t.Errorf("Auth.handleLogout() error = %v, wantErr %v", err, tt.wantErr)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment