From 0dc107b2fa49ad3178ba4b019622168a61b2b204 Mon Sep 17 00:00:00 2001 From: gouravmpk Date: Sun, 3 Mar 2024 06:50:47 +0530 Subject: [PATCH] added Unit test for RoleCheck and CheckUser --- db/config.go | 13 +++++---- db/config_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 5 deletions(-) create mode 100644 db/config_test.go diff --git a/db/config.go b/db/config.go index 19b948254..f78086df2 100644 --- a/db/config.go +++ b/db/config.go @@ -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 @@ -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 @@ -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 @@ -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-- } diff --git a/db/config_test.go b/db/config_test.go new file mode 100644 index 000000000..b357d6873 --- /dev/null +++ b/db/config_test.go @@ -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) + } + }) + } +}