-
Notifications
You must be signed in to change notification settings - Fork 116
fix(gitlab): check permission according to RememberOkToTest setting #2318
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| package gitlab | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings" | ||
| thelp "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab/test" | ||
| "github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger" | ||
| gitlab "gitlab.com/gitlab-org/api/client-go" | ||
| rtesting "knative.dev/pkg/reconciler/testing" | ||
| ) | ||
|
|
||
|
|
@@ -19,19 +23,38 @@ func TestIsAllowed(t *testing.T) { | |
| type args struct { | ||
| event *info.Event | ||
| } | ||
|
|
||
| testEvent := thelp.TEvent{ | ||
| UserID: 1111, | ||
| TargetProjectID: 2525, | ||
| NoteID: 1111, | ||
| Username: "admin", | ||
| DefaultBranch: "main", | ||
| URL: "https://gitlab.com/admin/testrepo", | ||
| SHA: "sha", | ||
| SHAurl: "https://url", | ||
| SHAtitle: "commit it", | ||
| Headbranch: "branch", | ||
| Basebranch: "main", | ||
| HeadURL: "https://url", | ||
| BaseURL: "https://url", | ||
| } | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| fields fields | ||
| args args | ||
| allowed bool | ||
| wantErr bool | ||
| wantClient bool | ||
| allowMemberID int | ||
| ownerFile string | ||
| commentContent string | ||
| commentAuthor string | ||
| commentAuthorID int | ||
| threadFirstNote string | ||
| name string | ||
| fields fields | ||
| args args | ||
| allowed bool | ||
| wantErr bool | ||
| wantClient bool | ||
| allowMemberID int | ||
| ownerFile string | ||
| commentContent string | ||
| commentAuthor string | ||
| commentAuthorID int | ||
| threadFirstNote string | ||
| rememberOKToTest bool | ||
| wantEventStruct bool | ||
| }{ | ||
| { | ||
| name: "check client has been set", | ||
|
|
@@ -64,7 +87,25 @@ func TestIsAllowed(t *testing.T) { | |
| ownerFile: "---\n approvers:\n - allowmeplease\n", | ||
| }, | ||
| { | ||
| name: "allowed from ok-to-test", | ||
| name: "allowed from ok-to-test with RememberOKToTest enabled", | ||
| allowed: true, | ||
| wantClient: true, | ||
| fields: fields{ | ||
| userID: 6666, | ||
| targetProjectID: 2525, | ||
| }, | ||
| args: args{ | ||
| event: &info.Event{Sender: "noowner", PullRequestNumber: 1}, | ||
| }, | ||
| allowMemberID: 1111, | ||
| commentContent: "/ok-to-test", | ||
| commentAuthor: "admin", | ||
| commentAuthorID: 1111, | ||
| wantEventStruct: true, | ||
| rememberOKToTest: true, | ||
| }, | ||
| { | ||
| name: "allowed from ok-to-test with RememberOKToTest disabled", | ||
| allowed: true, | ||
| wantClient: true, | ||
| fields: fields{ | ||
|
|
@@ -74,10 +115,12 @@ func TestIsAllowed(t *testing.T) { | |
| args: args{ | ||
| event: &info.Event{Sender: "noowner", PullRequestNumber: 1}, | ||
| }, | ||
| allowMemberID: 1111, | ||
| commentContent: "/ok-to-test", | ||
| commentAuthor: "admin", | ||
| commentAuthorID: 1111, | ||
| allowMemberID: 1111, | ||
| commentContent: "/ok-to-test", | ||
| commentAuthor: "admin", | ||
| commentAuthorID: 1111, | ||
| wantEventStruct: true, | ||
| rememberOKToTest: false, // make it disabled explicitly to indicate what we're testing π | ||
| }, | ||
| { | ||
| name: "allowed when /ok-to-test is in a reply note", | ||
|
|
@@ -90,11 +133,13 @@ func TestIsAllowed(t *testing.T) { | |
| args: args{ | ||
| event: &info.Event{Sender: "noowner", PullRequestNumber: 2}, | ||
| }, | ||
| allowMemberID: 1111, | ||
| threadFirstNote: "random comment", | ||
| commentContent: "/ok-to-test", | ||
| commentAuthor: "admin", | ||
| commentAuthorID: 1111, | ||
| allowMemberID: 1111, | ||
| threadFirstNote: "random comment", | ||
| commentContent: "/ok-to-test", | ||
| commentAuthor: "admin", | ||
| commentAuthorID: 1111, | ||
| wantEventStruct: true, | ||
| rememberOKToTest: true, | ||
| }, | ||
| { | ||
| name: "disallowed from non authorized note", | ||
|
|
@@ -113,16 +158,20 @@ func TestIsAllowed(t *testing.T) { | |
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| ctx, _ := rtesting.SetupFakeContext(t) | ||
| logger, _ := logger.GetLogger() | ||
|
|
||
| v := &Provider{ | ||
| targetProjectID: tt.fields.targetProjectID, | ||
| sourceProjectID: tt.fields.sourceProjectID, | ||
| userID: tt.fields.userID, | ||
| Logger: logger, | ||
| pacInfo: &info.PacOpts{Settings: settings.Settings{RememberOKToTest: tt.rememberOKToTest}}, | ||
| } | ||
| if tt.wantClient { | ||
| client, mux, tearDown := thelp.Setup(t) | ||
| v.gitlabClient = client | ||
| if tt.allowMemberID != 0 { | ||
| v.userID = tt.allowMemberID | ||
zakisk marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This line For tests checking the By setting Please remove this line to ensure the tests are correctly validating the ACL logic for
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in test |
||
| thelp.MuxAllowUserID(mux, tt.fields.targetProjectID, tt.allowMemberID) | ||
| } else { | ||
| thelp.MuxDisallowUserID(mux, tt.fields.targetProjectID, tt.allowMemberID) | ||
|
|
@@ -143,9 +192,22 @@ func TestIsAllowed(t *testing.T) { | |
| } else { | ||
| thelp.MuxDiscussionsNoteEmpty(mux, tt.fields.targetProjectID, tt.args.event.PullRequestNumber) | ||
| } | ||
| if tt.commentContent != "" { | ||
| thelp.MuxMergeRequestNote(mux, tt.fields.targetProjectID, tt.args.event.PullRequestNumber, testEvent.NoteID, testEvent.UserID, tt.commentContent, testEvent.Username) | ||
| } | ||
|
|
||
| defer tearDown() | ||
| } | ||
|
|
||
| if tt.wantEventStruct { | ||
| glEvent := &gitlab.MergeCommentEvent{} | ||
| err := json.Unmarshal([]byte(testEvent.MergeCommentEventAsJSON(tt.commentContent)), glEvent) | ||
| if err != nil { | ||
| t.Fatalf("failed to unmarshal event: %v", err) | ||
| } | ||
| tt.args.event.Event = glEvent | ||
| } | ||
|
|
||
| got, err := v.IsAllowed(ctx, tt.args.event) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("IsAllowed() error = %v, wantErr %v", err, tt.wantErr) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.