This repository was archived by the owner on Sep 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
rbac: assign permissions to roles #47401
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
979146b
assign permissions to user and site administrator roles on creation
BolajiOlajide d94307e
add tests for compare permissions func
BolajiOlajide 1953349
create oob migration for assigning permissions to roles
BolajiOlajide a964e2e
add comment to sql query
BolajiOlajide 2f231e6
add test for migrator
BolajiOlajide a37280a
rename batch changes namespace
BolajiOlajide 1cfe3f8
remove role_permission assignment oob migration
BolajiOlajide dc3924f
Update cmd/frontend/internal/bg/update_permissions.go
BolajiOlajide 2a6f253
add test for deleted and added permissions
BolajiOlajide File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| package rbac | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| "github.com/google/go-cmp/cmp/cmpopts" | ||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/sourcegraph/sourcegraph/internal/database" | ||
| "github.com/sourcegraph/sourcegraph/internal/types" | ||
| ) | ||
|
|
||
| func TestComparePermissions(t *testing.T) { | ||
| dbPerms := []*types.Permission{ | ||
| {ID: 1, Namespace: "TEST-NAMESPACE", Action: "READ"}, | ||
| {ID: 2, Namespace: "TEST-NAMESPACE", Action: "WRITE"}, | ||
| {ID: 3, Namespace: "TEST-NAMESPACE-2", Action: "READ"}, | ||
| {ID: 4, Namespace: "TEST-NAMESPACE-2", Action: "WRITE"}, | ||
| {ID: 5, Namespace: "TEST-NAMESPACE-3", Action: "READ"}, | ||
| } | ||
|
|
||
| t.Run("no changes to permissions", func(t *testing.T) { | ||
| schemaPerms := Schema{ | ||
| Namespaces: []Namespace{ | ||
| {Name: "TEST-NAMESPACE", Actions: []string{"READ", "WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-2", Actions: []string{"READ", "WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-3", Actions: []string{"READ"}}, | ||
| }, | ||
| } | ||
|
|
||
| added, deleted := ComparePermissions(dbPerms, schemaPerms) | ||
|
|
||
| assert.Len(t, added, 0) | ||
| assert.Len(t, deleted, 0) | ||
| }) | ||
|
|
||
| t.Run("permissions deleted", func(t *testing.T) { | ||
| schemaPerms := Schema{ | ||
| Namespaces: []Namespace{ | ||
| {Name: "TEST-NAMESPACE", Actions: []string{"READ", "WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-2", Actions: []string{"READ"}}, | ||
| }, | ||
| } | ||
|
|
||
| want := []database.DeletePermissionOpts{ | ||
| {ID: int32(4)}, | ||
| {ID: int32(5)}, | ||
| } | ||
|
|
||
| added, deleted := ComparePermissions(dbPerms, schemaPerms) | ||
|
|
||
| assert.Len(t, added, 0) | ||
| assert.Len(t, deleted, 2) | ||
| if diff := cmp.Diff(want, deleted, cmpopts.SortSlices(sortDeletePermissionOptSlice)); diff != "" { | ||
| t.Error(diff) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("permissions added", func(t *testing.T) { | ||
BolajiOlajide marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| schemaPerms := Schema{ | ||
| Namespaces: []Namespace{ | ||
| {Name: "TEST-NAMESPACE", Actions: []string{"READ", "WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-2", Actions: []string{"READ", "WRITE", "EXECUTE"}}, | ||
| {Name: "TEST-NAMESPACE-3", Actions: []string{"READ", "WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-4", Actions: []string{"READ", "WRITE"}}, | ||
| }, | ||
| } | ||
|
|
||
| want := []database.CreatePermissionOpts{ | ||
| {Namespace: "TEST-NAMESPACE-2", Action: "EXECUTE"}, | ||
| {Namespace: "TEST-NAMESPACE-3", Action: "WRITE"}, | ||
| {Namespace: "TEST-NAMESPACE-4", Action: "READ"}, | ||
| {Namespace: "TEST-NAMESPACE-4", Action: "WRITE"}, | ||
| } | ||
|
|
||
| added, deleted := ComparePermissions(dbPerms, schemaPerms) | ||
|
|
||
| assert.Len(t, added, 4) | ||
| assert.Len(t, deleted, 0) | ||
| if diff := cmp.Diff(want, added); diff != "" { | ||
| t.Error(diff) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("permissions deleted and added", func(t *testing.T) { | ||
| schemaPerms := Schema{ | ||
| Namespaces: []Namespace{ | ||
| {Name: "TEST-NAMESPACE", Actions: []string{"READ"}}, | ||
| {Name: "TEST-NAMESPACE-2", Actions: []string{"READ", "WRITE", "EXECUTE"}}, | ||
| {Name: "TEST-NAMESPACE-3", Actions: []string{"WRITE"}}, | ||
| {Name: "TEST-NAMESPACE-4", Actions: []string{"READ", "WRITE"}}, | ||
| }, | ||
| } | ||
|
|
||
| wantAdded := []database.CreatePermissionOpts{ | ||
| {Namespace: "TEST-NAMESPACE-2", Action: "EXECUTE"}, | ||
| {Namespace: "TEST-NAMESPACE-3", Action: "WRITE"}, | ||
| {Namespace: "TEST-NAMESPACE-4", Action: "READ"}, | ||
| {Namespace: "TEST-NAMESPACE-4", Action: "WRITE"}, | ||
| } | ||
|
|
||
| wantDeleted := []database.DeletePermissionOpts{ | ||
| // Represents TEST-NAMESPACE-3#READ | ||
| {ID: 5}, | ||
| // Represents TEST-NAMESPACE#WRITE | ||
| {ID: 2}, | ||
| } | ||
|
|
||
| // do stuff | ||
| added, deleted := ComparePermissions(dbPerms, schemaPerms) | ||
|
|
||
| assert.Len(t, added, 4) | ||
| if diff := cmp.Diff(wantAdded, added); diff != "" { | ||
| t.Error(diff) | ||
| } | ||
|
|
||
| assert.Len(t, deleted, 2) | ||
| less := func(a, b database.DeletePermissionOpts) bool { return a.ID < b.ID } | ||
| if diff := cmp.Diff(wantDeleted, deleted, cmpopts.SortSlices(less)); diff != "" { | ||
| t.Error(diff) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| func sortDeletePermissionOptSlice(a, b database.DeletePermissionOpts) bool { return a.ID < b.ID } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| namespaces: | ||
| - name: BATCHCHANGES | ||
| - name: BATCH_CHANGES | ||
| actions: | ||
| - READ | ||
| - WRITE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.