From cae725767336bbb6390bad93ad29d207b99d9dd9 Mon Sep 17 00:00:00 2001 From: Christoph Kluge Date: Mon, 6 Mar 2023 16:32:58 +0100 Subject: [PATCH] Add tests for role checks, update test.db --- internal/auth/auth.go | 1 - internal/auth/auth_test.go | 128 +++++++++++++++++++++++++++++++++++++ test/test.db | Bin 131072 -> 131072 bytes 3 files changed, 128 insertions(+), 1 deletion(-) diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 4db4737..aab4c3a 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -76,7 +76,6 @@ func getRoleEnum(roleStr string) Role { func isValidRole(role string) bool { if getRoleEnum(role) == RoleError { - log.Errorf("Unknown Role %s", role) return false } return true diff --git a/internal/auth/auth_test.go b/internal/auth/auth_test.go index 8832b06..a83ac30 100644 --- a/internal/auth/auth_test.go +++ b/internal/auth/auth_test.go @@ -1 +1,129 @@ +// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. +// All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. package auth + +import ( + "testing" +) + +func TestHasValidRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + exists, _ := u.HasValidRole("user") + + if !exists { + t.Fatalf(`User{Roles: ["user"]} -> HasValidRole("user"): EXISTS = %v, expected 'true'.`, exists) + } +} + +func TestHasNotValidRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + exists, _ := u.HasValidRole("manager") + + if exists { + t.Fatalf(`User{Roles: ["user"]} -> HasValidRole("manager"): EXISTS = %v, expected 'false'.`, exists) + } +} + +func TestHasInvalidRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + _, valid := u.HasValidRole("invalid") + + if valid { + t.Fatalf(`User{Roles: ["user"]} -> HasValidRole("invalid"): VALID = %v, expected 'false'.`, valid) + } +} + +func TestHasNotInvalidRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + _, valid := u.HasValidRole("user") + + if !valid { + t.Fatalf(`User{Roles: ["user"]} -> HasValidRole("user"): VALID = %v, expected 'true'.`, valid) + } +} + +func TestHasRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + exists := u.HasRole(RoleUser) + + if !exists { + t.Fatalf(`User{Roles: ["user"]} -> HasRole(RoleUser): EXISTS = %v, expected 'true'.`, exists) + } +} + +func TestHasNotRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user"}} + + exists := u.HasRole(RoleManager) + + if exists { + t.Fatalf(`User{Roles: ["user"]} -> HasRole(RoleManager): EXISTS = %v, expected 'false'.`, exists) + } +} + +func TestHasAnyRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager"}} + + result := u.HasAnyRole([]Role{RoleManager, RoleSupport}) + + if !result { + t.Fatalf(`User{Roles: ["user", "manager"]} -> HasAnyRole([]Role{RoleManager, RoleSupport}): RESULT = %v, expected 'true'.`, result) + } +} + +func TestHasNotAnyRole(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager"}} + + result := u.HasAnyRole([]Role{RoleSupport}) + + if result { + t.Fatalf(`User{Roles: ["user", "manager"]} -> HasAllRoles([]Role{RoleSupport}): RESULT = %v, expected 'false'.`, result) + } +} + +func TestHasAllRoles(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager", "support"}} + + result := u.HasAllRoles([]Role{RoleUser, RoleManager, RoleSupport}) + + if !result { + t.Fatalf(`User{Roles: ["user", "manager", "support"]} -> HasAllRoles([]Role{RoleUser, RoleManager, RoleSupport}): RESULT = %v, expected 'true'.`, result) + } +} + +func TestHasNotAllRoles(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager"}} + + result := u.HasAllRoles([]Role{RoleUser, RoleManager, RoleSupport}) + + if result { + t.Fatalf(`User{Roles: ["user", "manager"]} -> HasAllRoles([]Role{RoleUser, RoleManager, RoleSupport}): RESULT = %v, expected 'false'.`, result) + } +} + +func TestHasNotRoles(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager"}} + + result := u.HasNotRoles([]Role{RoleSupport, RoleAdmin}) + + if !result { + t.Fatalf(`User{Roles: ["user", "manager"]} -> HasNotRoles([]Role{RoleSupport, RoleAdmin}): RESULT = %v, expected 'true'.`, result) + } +} + +func TestHasAllNotRoles(t *testing.T) { + u := User{Username: "testuser", Roles: []string{"user", "manager"}} + + result := u.HasNotRoles([]Role{RoleUser, RoleManager}) + + if result { + t.Fatalf(`User{Roles: ["user", "manager"]} -> HasNotRoles([]Role{RoleUser, RoleManager}): RESULT = %v, expected 'false'.`, result) + } +} diff --git a/test/test.db b/test/test.db index 359662ff31b3144a326f0696467402d18a16e2b9..bf9e2ce1e19164d38e839a233778f7b3156208ec 100644 GIT binary patch delta 201 zcmZo@;Am*zm>@0a!@$6x0K_oBH&Mr!--kiZdm=C283ryESq2u_jd|;txtdHF*~LXg z8C!fe8?taRsuU}fB^D)TBo=8HnVM=U`1yw@_=WoTD7d(~IfnX#C@4k8Ds2v7-NePW zmVtjQzY7d*78Jt#>rddo#Yszl@e2OGxL;UxqvQW kBU)ZhM7pOO6n0H$U$-~a#s delta 136 zcmZo@;Am*zm>@0a#=yX!0K_oBGf~Hw-;F`fdm=C283ryEbp{sojd|;tCkAkFHEA=l zi;IdfwpeafXW?X=e2_(Ia|r7uE|zr+{OdLgIxOL54`AdFZe-<{yhYwgI9e$&B{wrq gDV7UpI3xd02L7KwMeF$|PH<@cslWZFJ|p`D0F|2|_y7O^