diff --git a/controller/northbound/server/auth.go b/controller/northbound/server/auth.go index 7eec9d316d0c850e3e6a2b7502b8670dcc48a0a0..9abf3a936b6ac0568d125b5ca71f920c67bfba01 100644 --- a/controller/northbound/server/auth.go +++ b/controller/northbound/server/auth.go @@ -24,10 +24,11 @@ type Auth struct { userService rbacInterfaces.UserService } -// NewAuthServer receives a JWTManager and returns a new Auth interface. -func NewAuthServer(jwtManager *rbac.JWTManager) *Auth { +// NewAuthServer receives a JWTManager and a userService and returns a new Auth interface. +func NewAuthServer(jwtManager *rbac.JWTManager, userService rbacInterfaces.UserService) *Auth { return &Auth{ - jwtManager: jwtManager, + jwtManager: jwtManager, + userService: userService, } } diff --git a/controller/northbound/server/auth_test.go b/controller/northbound/server/auth_test.go index 9a875ec84e1a9485e6da80759f9672729073deb1..fa3e0d9ef891be7ff9d4f961b7501fb0d3c931c6 100644 --- a/controller/northbound/server/auth_test.go +++ b/controller/northbound/server/auth_test.go @@ -4,12 +4,31 @@ import ( "context" "log" "testing" + "time" apb "code.fbi.h-da.de/danet/gosdn/api/go/gosdn/rbac" "code.fbi.h-da.de/danet/gosdn/controller/rbac" "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) { type args struct { ctx context.Context @@ -47,9 +66,7 @@ func TestAuth_Login(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - r := Auth{ - jwtManager: jwt, - } + 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) @@ -67,7 +84,8 @@ func TestAuth_Login(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 { log.Fatal(err) } @@ -99,9 +117,6 @@ func TestAuth_Logout(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := Auth{ - jwtManager: jwt, - } got, 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) @@ -158,7 +173,7 @@ func TestAuth_isValidUser(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - s := Auth{} + 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) } @@ -167,12 +182,13 @@ func TestAuth_isValidUser(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 { log.Fatal(err) } - invalidToken, err := createTestUserToken("testAdmin", false) + invalidToken, err := createTestUserToken("testAdmin", false, s.userService, s.jwtManager) if err != nil { log.Fatal(err) } @@ -221,9 +237,6 @@ func TestAuth_handleLogout(t *testing.T) { } for _, tt := range tests { 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 { t.Errorf("Auth.handleLogout() error = %v, wantErr %v", err, tt.wantErr) }