Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: make acl test more readable (#3361)
Tianzhou (天舟)9ac49a8d4649
diff --git a/server/acl_project_test.go b/server/acl_project_test.goindex 3954208989..43b8ec6e5e 100644--- a/server/acl_project_test.go+++ b/server/acl_project_test.go@@ -5,6 +5,7 @@ import ("testing""github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/common")func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {@@ -59,7 +60,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "GET",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -67,7 +68,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "GET",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "",},{@@ -75,7 +76,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "GET",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "",},{@@ -83,7 +84,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -91,7 +92,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project general setting",},{@@ -99,7 +100,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -107,7 +108,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "GET",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -115,7 +116,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "GET",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "",},{@@ -123,7 +124,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "GET",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "",},{@@ -131,7 +132,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "POST",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -139,7 +140,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "POST",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project general setting",},{@@ -147,7 +148,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "POST",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -155,7 +156,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -163,7 +164,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project general setting",},{@@ -171,7 +172,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -179,7 +180,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "DELETE",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -187,7 +188,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "DELETE",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project general setting",},{@@ -195,7 +196,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/repository",method: "DELETE",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -203,7 +204,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member",method: "POST",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -211,7 +212,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member",method: "POST",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project member",},{@@ -219,7 +220,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member",method: "POST",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -227,7 +228,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -235,7 +236,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project member",},{@@ -243,7 +244,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},{@@ -251,7 +252,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "DELETE",- principalID: 200,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectOwner),errMsg: "",},{@@ -259,7 +260,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "DELETE",- principalID: 201,+ principalID: testFindPrincipalIDFromProject(100, common.ProjectDeveloper),errMsg: "not have permission to manage the project member",},{@@ -267,7 +268,7 @@ func TestEnforceWorkspaceDeveloperProjectRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/project/100/member/567",method: "DELETE",- principalID: 202,+ principalID: testFindPrincipalIDFromProject(100, ""),errMsg: "is not a member of the project",},}diff --git a/server/acl_sheet_test.go b/server/acl_sheet_test.goindex ec220af10d..ddcc304d22 100644--- a/server/acl_sheet_test.go+++ b/server/acl_sheet_test.go@@ -22,7 +22,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/my",method: "GET",- principalID: 200,+ principalID: 1234,errMsg: "",},{@@ -30,7 +30,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/shared",method: "GET",- principalID: 200,+ principalID: 1234,errMsg: "",},{@@ -38,7 +38,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/starred",method: "GET",- principalID: 200,+ principalID: 1234,errMsg: "",},{@@ -46,7 +46,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/9999",method: "GET",- principalID: 202,+ principalID: 1234,errMsg: "Sheet ID not found: 9999",},{@@ -54,7 +54,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/9999/organize",method: "PATCH",- principalID: 202,+ principalID: 1234,errMsg: "Sheet ID not found: 9999",},}@@ -65,7 +65,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "GET",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1000, "CREATOR"),errMsg: "",},{@@ -73,7 +73,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "GET",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1000, "OWNER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -81,7 +81,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "GET",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1000, "DEVELOPER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -89,7 +89,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "GET",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1000, ""),errMsg: "not allowed to access private sheet created by other user",},{@@ -97,7 +97,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1000, "CREATOR"),errMsg: "",},{@@ -105,7 +105,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1000, "OWNER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -113,7 +113,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1000, "DEVELOPER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -121,7 +121,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1000, ""),errMsg: "not allowed to access private sheet created by other user",},{@@ -129,7 +129,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1000, "CREATOR"),errMsg: "",},{@@ -137,7 +137,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000/organize",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1000, "OWNER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -145,7 +145,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000/organize",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1000, "DEVELOPER"),errMsg: "not allowed to access private sheet created by other user",},{@@ -153,7 +153,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1000/organize",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1000, ""),errMsg: "not allowed to access private sheet created by other user",},}@@ -164,7 +164,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "GET",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1001, "CREATOR"),errMsg: "",},{@@ -172,7 +172,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "GET",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1001, "OWNER"),errMsg: "",},{@@ -180,7 +180,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "GET",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1001, "DEVELOPER"),errMsg: "",},{@@ -188,7 +188,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "GET",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1001, ""),errMsg: "is not a member of the project containing the sheet",},{@@ -196,7 +196,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1001, "CREATOR"),errMsg: "",},{@@ -204,7 +204,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1001, "OWNER"),errMsg: "",},{@@ -212,7 +212,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1001, "DEVELOPER"),errMsg: "not have permission to change the project sheet",},{@@ -220,7 +220,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1001, ""),errMsg: "is not a member of the project containing the sheet",},{@@ -228,7 +228,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001/organize",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1001, "CREATOR"),errMsg: "",},{@@ -236,7 +236,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001/organize",method: "PATCH",- principalID: 200,+ principalID: testFindPrincipalIDFromSheet(1001, "OWNER"),errMsg: "",},{@@ -244,7 +244,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001/organize",method: "PATCH",- principalID: 201,+ principalID: testFindPrincipalIDFromSheet(1001, "DEVELOPER"),errMsg: "",},{@@ -252,7 +252,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1001/organize",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1001, ""),errMsg: "is not a member of the project containing the sheet",},}@@ -263,7 +263,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002",method: "GET",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1002, "CREATOR"),errMsg: "",},{@@ -271,7 +271,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002",method: "GET",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1002, ""),errMsg: "",},{@@ -279,7 +279,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1002, "CREATOR"),errMsg: "",},{@@ -287,7 +287,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1002, ""),errMsg: "not allowed to change public sheet created by other user",},{@@ -295,7 +295,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002/organize",method: "PATCH",- principalID: 202,+ principalID: testFindPrincipalIDFromSheet(1002, "CREATOR"),errMsg: "",},{@@ -303,7 +303,7 @@ func TestEnforceWorkspaceDeveloperSheetRouteACL(t *testing.T) {plan: api.ENTERPRISE,path: "/sheet/1002/organize",method: "PATCH",- principalID: 204,+ principalID: testFindPrincipalIDFromSheet(1002, ""),errMsg: "",},}diff --git a/server/acl_test_helper.go b/server/acl_test_helper.goindex 16a60b29ef..fe2e1bd4ba 100644--- a/server/acl_test_helper.go+++ b/server/acl_test_helper.go@@ -5,23 +5,79 @@ import ("github.com/bytebase/bytebase/common")+// map from project ID to the map of <role, principal ID>.+var testProjectMemberMap = map[int]map[common.ProjectRole]int{+ 100: {+ common.ProjectOwner: 200,+ common.ProjectDeveloper: 201,+ },+ 101: {+ common.ProjectOwner: 202,+ },+}++// map from sheet ID to the project ID.+var testSheetProjectMap = map[int]int{+ 1000: 100,+ 1001: 100,+ 1002: 100,+}++// map from sheet ID to the map of <string, principal ID>+// string can be one of the project roles or "CREATOR" which represents the sheet creator.+var testSheetMemberMap = map[int]map[string]int{+ 1000: {+ "OWNER": 200,+ "DEVELOPER": 201,+ "CREATOR": 202,+ },+ 1001: {+ "OWNER": 200,+ "DEVELOPER": 201,+ "CREATOR": 202,+ },+ 1002: {+ "OWNER": 200,+ "DEVELOPER": 201,+ "CREATOR": 202,+ },+}++func testFindPrincipalIDFromProject(projectID int, role common.ProjectRole) int {+ m, ok := testProjectMemberMap[projectID]+ if !ok {+ return 999+ }++ id, ok := m[role]+ if !ok {+ return 999+ }+ return id+}++func testFindPrincipalIDFromSheet(sheetID int, v string) int {+ m, ok := testSheetMemberMap[sheetID]+ if !ok {+ return 999+ }++ id, ok := m[v]+ if !ok {+ return 999+ }+ return id+}+var roleFinder = func(projectID int, principalID int) (common.ProjectRole, error) {- switch projectID {- case 100:- switch principalID {- case 200:- return common.ProjectOwner, nil- case 201:- return common.ProjectDeveloper, nil- }- case 101:- switch principalID {- case 200:- return "", nil- case 201:- return "", nil- case 202:- return common.ProjectOwner, nil+ m, ok := testProjectMemberMap[projectID]+ if !ok {+ return "", nil+ }++ for role, id := range m {+ if id == principalID {+ return role, nil}}return "", nil@@ -32,22 +88,22 @@ var sheetFinder = func(sheetID int) (*api.Sheet, error) {case 1000:return &api.Sheet{ID: 1000,- CreatorID: 202,- ProjectID: 100,+ CreatorID: testFindPrincipalIDFromSheet(1000, "CREATOR"),+ ProjectID: testSheetProjectMap[1000],Visibility: api.PrivateSheet,}, nilcase 1001:return &api.Sheet{ID: 1001,- CreatorID: 202,- ProjectID: 100,+ CreatorID: testFindPrincipalIDFromSheet(1001, "CREATOR"),+ ProjectID: testSheetProjectMap[1001],Visibility: api.ProjectSheet,}, nilcase 1002:return &api.Sheet{ID: 1002,- CreatorID: 202,- ProjectID: 100,+ CreatorID: testFindPrincipalIDFromSheet(1002, "CREATOR"),+ ProjectID: testSheetProjectMap[1002],Visibility: api.PublicSheet,}, nil}