Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Unit Tests] - UserHasAccess #2042

Open
tomsmith8 opened this issue Dec 2, 2024 · 0 comments
Open

[Unit Tests] - UserHasAccess #2042

tomsmith8 opened this issue Dec 2, 2024 · 0 comments
Assignees

Comments

@tomsmith8
Copy link

Unit Test Coverage for " UserHasAccess"


Stakwork Run


Unit Test Code


package config

import (
  "testing"

  "github.com/stretchr/testify/assert"
  "github.com/stretchr/testify/mock"
)

// MockDatabase is a mock implementation of the database interface
type MockDatabase struct {
  mock.Mock
}

func (m *MockDatabase) GetWorkspaceByUuid(uuid string) Workspace {
  args := m.Called(uuid)
  return args.Get(0).(Workspace)
}

func (m *MockDatabase) GetUserRoles(uuid string, pubkey string) []WorkspaceUserRoles {
  args := m.Called(uuid, pubkey)
  return args.Get(0).([]WorkspaceUserRoles)
}

func TestUserHasAccess(t *testing.T) {
  mockDB := new(MockDatabase)
  DB = mockDB

  // Define test cases
  testCases := []struct {
  	name           string
  	pubKeyFromAuth string
  	uuid           string
  	role           string
  	setupMocks     func()
  	expected       bool
  }{
  	{
  		name:           "Owner Access",
  		pubKeyFromAuth: "ownerKey",
  		uuid:           "workspace1",
  		role:           "anyRole",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey"})
  		},
  		expected: true,
  	},
  	{
  		name:           "User with Role Access",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: true,
  	},
  	{
  		name:           "User without Role Access",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "user"}})
  		},
  		expected: false,
  	},
  	{
  		name:           "Empty Role Check",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: false,
  	},
  	{
  		name:           "Non-Existent Workspace",
  		pubKeyFromAuth: "userKey",
  		uuid:           "nonExistentWorkspace",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "nonExistentWorkspace").Return(Workspace{})
  		},
  		expected: false,
  	},
  	{
  		name:           "Invalid Public Key",
  		pubKeyFromAuth: "",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "").Return([]WorkspaceUserRoles{})
  		},
  		expected: false,
  	},
  	{
  		name:           "Null Inputs",
  		pubKeyFromAuth: "",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "").Return([]WorkspaceUserRoles{})
  		},
  		expected: false,
  	},
  	{
  		name:           "Invalid Role Format",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "adm!n",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: false,
  	},
  	{
  		name:           "Large Number of Roles",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			roles := make([]WorkspaceUserRoles, 1000)
  			for i := 0; i < 1000; i++ {
  				roles[i] = WorkspaceUserRoles{Role: "role" + string(i)}
  			}
  			roles[999] = WorkspaceUserRoles{Role: "admin"}
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return(roles)
  		},
  		expected: true,
  	},
  	{
  		name:           "Case Sensitivity in Roles",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "Admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: false,
  	},
  	{
  		name:           "Multiple Users with Same Role",
  		pubKeyFromAuth: "userKey2",
  		uuid:           "workspace1",
  		role:           "admin",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey2").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: true,
  	},
  	{
  		name:           "Role Hierarchy",
  		pubKeyFromAuth: "userKey",
  		uuid:           "workspace1",
  		role:           "user",
  		setupMocks: func() {
  			mockDB.On("GetWorkspaceByUuid", "workspace1").Return(Workspace{OwnerPubKey: "ownerKey2"})
  			mockDB.On("GetUserRoles", "workspace1", "userKey").Return([]WorkspaceUserRoles{{Role: "admin"}})
  		},
  		expected: true,
  	},
  }

  // Execute test cases
  for _, tc := range testCases {
  	t.Run(tc.name, func(t *testing.T) {
  		tc.setupMocks()
  		result := UserHasAccess(tc.pubKeyFromAuth, tc.uuid, tc.role)
  		assert.Equal(t, tc.expected, result)
  		mockDB.AssertExpectations(t)
  	})
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants