Skip to content

Commit

Permalink
added Unit test for RoleCheck and CheckUser
Browse files Browse the repository at this point in the history
  • Loading branch information
gouravmpk committed Mar 3, 2024
1 parent 09829eb commit 0dc107b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
13 changes: 8 additions & 5 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ func (db database) ConvertMetricsBountiesToMap(metricsCsv []MetricsBountyCsv) []
return metricsMap
}

func RolesCheck(userRoles []UserRoles, check string) bool {
rolesMap := GetRolesMap()
func RolesCheck(rolesMap map[string]string, userRoles []UserRoles, check string) bool {

userRolesMap := GetUserRolesMap(userRoles)

// check if roles exist in config
Expand Down Expand Up @@ -257,7 +257,8 @@ func UserHasAccess(pubKeyFromAuth string, uuid string, role string) bool {
var hasRole bool = false
if pubKeyFromAuth != org.OwnerPubKey {
userRoles := DB.GetUserRoles(uuid, pubKeyFromAuth)
hasRole = RolesCheck(userRoles, role)
rolesMap := GetRolesMap()
hasRole = RolesCheck(rolesMap, userRoles, role)
return hasRole
}
return true
Expand All @@ -268,7 +269,8 @@ func (db database) UserHasAccess(pubKeyFromAuth string, uuid string, role string
var hasRole bool = false
if pubKeyFromAuth != org.OwnerPubKey {
userRoles := DB.GetUserRoles(uuid, pubKeyFromAuth)
hasRole = RolesCheck(userRoles, role)
rolesMap := GetRolesMap()
hasRole = RolesCheck(rolesMap, userRoles, role)
return hasRole
}
return true
Expand All @@ -279,10 +281,11 @@ func (db database) UserHasManageBountyRoles(pubKeyFromAuth string, uuid string)
org := DB.GetOrganizationByUuid(uuid)
if pubKeyFromAuth != org.OwnerPubKey {
userRoles := DB.GetUserRoles(uuid, pubKeyFromAuth)
rolesMap := GetRolesMap()

for _, role := range ManageBountiesGroup {
// check for the manage bounty roles
hasRole := RolesCheck(userRoles, role)
hasRole := RolesCheck(rolesMap, userRoles, role)
if hasRole {
manageRolesCount--
}
Expand Down
74 changes: 74 additions & 0 deletions db/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package db

import "testing"

var mockConfigBountyRoles = []struct {
Name string
}{
{Name: "admin"},
{Name: "editor"},
{Name: "viewer"},
}

func TestRolesCheck(t *testing.T) {
mockRolesMap := make(map[string]string)
for _, role := range mockConfigBountyRoles {
mockRolesMap[role.Name] = role.Name
}

userWithRoles := []UserRoles{
{Role: "admin"},
{Role: "editor"},
}

userWithoutRoles := []UserRoles{
{Role: "viewer"},
}

tests := []struct {
name string
rolesMap map[string]string
userRoles []UserRoles
check string
want bool
}{
{"RoleExistsAndUserHasRole", mockRolesMap, userWithRoles, "admin", true},
{"RoleExistsButUserDoesNotHaveRole", mockRolesMap, userWithoutRoles, "admin", false},
{"RoleDoesNotExist", mockRolesMap, userWithRoles, "nonexistent", false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RolesCheck(tt.rolesMap, tt.userRoles, tt.check); got != tt.want {
t.Errorf("RolesCheck() = %v, want %v", got, tt.want)
}
})
}
}

func TestCheckUser(t *testing.T) {
userRoles := []UserRoles{
{OwnerPubKey: "pubkey1"},
{OwnerPubKey: "pubkey2"},
{OwnerPubKey: "pubkey3"},
}

tests := []struct {
name string
userRoles []UserRoles
pubkey string
want bool
}{
{"UserExists", userRoles, "pubkey1", true},

{"UserDoesNotExist", userRoles, "pubkeyNonExistent", false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := CheckUser(tt.userRoles, tt.pubkey); got != tt.want {
t.Errorf("CheckUser() for pubkey %v = %v, want %v", tt.pubkey, got, tt.want)
}
})
}
}

0 comments on commit 0dc107b

Please sign in to comment.