11package gitlab
22
33import (
4+ "encoding/json"
45 "fmt"
56 "net/http"
67 "testing"
78
89 "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
10+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/settings"
911 thelp "github.com/openshift-pipelines/pipelines-as-code/pkg/provider/gitlab/test"
12+ "github.com/openshift-pipelines/pipelines-as-code/pkg/test/logger"
13+ gitlab "gitlab.com/gitlab-org/api/client-go"
1014 rtesting "knative.dev/pkg/reconciler/testing"
1115)
1216
@@ -19,19 +23,38 @@ func TestIsAllowed(t *testing.T) {
1923 type args struct {
2024 event * info.Event
2125 }
26+
27+ testEvent := thelp.TEvent {
28+ UserID : 1111 ,
29+ TargetProjectID : 2525 ,
30+ NoteID : 1111 ,
31+ Username : "admin" ,
32+ DefaultBranch : "main" ,
33+ URL : "https://gitlab.com/admin/testrepo" ,
34+ SHA : "sha" ,
35+ SHAurl : "https://url" ,
36+ SHAtitle : "commit it" ,
37+ Headbranch : "branch" ,
38+ Basebranch : "main" ,
39+ HeadURL : "https://url" ,
40+ BaseURL : "https://url" ,
41+ }
42+
2243 tests := []struct {
23- name string
24- fields fields
25- args args
26- allowed bool
27- wantErr bool
28- wantClient bool
29- allowMemberID int
30- ownerFile string
31- commentContent string
32- commentAuthor string
33- commentAuthorID int
34- threadFirstNote string
44+ name string
45+ fields fields
46+ args args
47+ allowed bool
48+ wantErr bool
49+ wantClient bool
50+ allowMemberID int
51+ ownerFile string
52+ commentContent string
53+ commentAuthor string
54+ commentAuthorID int
55+ threadFirstNote string
56+ rememberOKToTest bool
57+ wantEventStruct bool
3558 }{
3659 {
3760 name : "check client has been set" ,
@@ -64,7 +87,25 @@ func TestIsAllowed(t *testing.T) {
6487 ownerFile : "---\n approvers:\n - allowmeplease\n " ,
6588 },
6689 {
67- name : "allowed from ok-to-test" ,
90+ name : "allowed from ok-to-test with RememberOKToTest enabled" ,
91+ allowed : true ,
92+ wantClient : true ,
93+ fields : fields {
94+ userID : 6666 ,
95+ targetProjectID : 2525 ,
96+ },
97+ args : args {
98+ event : & info.Event {Sender : "noowner" , PullRequestNumber : 1 },
99+ },
100+ allowMemberID : 1111 ,
101+ commentContent : "/ok-to-test" ,
102+ commentAuthor : "admin" ,
103+ commentAuthorID : 1111 ,
104+ wantEventStruct : true ,
105+ rememberOKToTest : true ,
106+ },
107+ {
108+ name : "allowed from ok-to-test with RememberOKToTest disabled" ,
68109 allowed : true ,
69110 wantClient : true ,
70111 fields : fields {
@@ -74,10 +115,12 @@ func TestIsAllowed(t *testing.T) {
74115 args : args {
75116 event : & info.Event {Sender : "noowner" , PullRequestNumber : 1 },
76117 },
77- allowMemberID : 1111 ,
78- commentContent : "/ok-to-test" ,
79- commentAuthor : "admin" ,
80- commentAuthorID : 1111 ,
118+ allowMemberID : 1111 ,
119+ commentContent : "/ok-to-test" ,
120+ commentAuthor : "admin" ,
121+ commentAuthorID : 1111 ,
122+ wantEventStruct : true ,
123+ rememberOKToTest : false , // make it disabled explicitly to indicate what we're testing 🙃
81124 },
82125 {
83126 name : "allowed when /ok-to-test is in a reply note" ,
@@ -90,11 +133,13 @@ func TestIsAllowed(t *testing.T) {
90133 args : args {
91134 event : & info.Event {Sender : "noowner" , PullRequestNumber : 2 },
92135 },
93- allowMemberID : 1111 ,
94- threadFirstNote : "random comment" ,
95- commentContent : "/ok-to-test" ,
96- commentAuthor : "admin" ,
97- commentAuthorID : 1111 ,
136+ allowMemberID : 1111 ,
137+ threadFirstNote : "random comment" ,
138+ commentContent : "/ok-to-test" ,
139+ commentAuthor : "admin" ,
140+ commentAuthorID : 1111 ,
141+ wantEventStruct : true ,
142+ rememberOKToTest : true ,
98143 },
99144 {
100145 name : "disallowed from non authorized note" ,
@@ -113,16 +158,20 @@ func TestIsAllowed(t *testing.T) {
113158 for _ , tt := range tests {
114159 t .Run (tt .name , func (t * testing.T ) {
115160 ctx , _ := rtesting .SetupFakeContext (t )
161+ logger , _ := logger .GetLogger ()
116162
117163 v := & Provider {
118164 targetProjectID : tt .fields .targetProjectID ,
119165 sourceProjectID : tt .fields .sourceProjectID ,
120166 userID : tt .fields .userID ,
167+ Logger : logger ,
168+ pacInfo : & info.PacOpts {Settings : settings.Settings {RememberOKToTest : tt .rememberOKToTest }},
121169 }
122170 if tt .wantClient {
123171 client , mux , tearDown := thelp .Setup (t )
124172 v .gitlabClient = client
125173 if tt .allowMemberID != 0 {
174+ v .userID = tt .allowMemberID
126175 thelp .MuxAllowUserID (mux , tt .fields .targetProjectID , tt .allowMemberID )
127176 } else {
128177 thelp .MuxDisallowUserID (mux , tt .fields .targetProjectID , tt .allowMemberID )
@@ -143,9 +192,22 @@ func TestIsAllowed(t *testing.T) {
143192 } else {
144193 thelp .MuxDiscussionsNoteEmpty (mux , tt .fields .targetProjectID , tt .args .event .PullRequestNumber )
145194 }
195+ if tt .commentContent != "" {
196+ thelp .MuxMergeRequestNote (mux , tt .fields .targetProjectID , tt .args .event .PullRequestNumber , testEvent .NoteID , testEvent .UserID , tt .commentContent , testEvent .Username )
197+ }
146198
147199 defer tearDown ()
148200 }
201+
202+ if tt .wantEventStruct {
203+ glEvent := & gitlab.MergeCommentEvent {}
204+ err := json .Unmarshal ([]byte (testEvent .MergeCommentEventAsJSON (tt .commentContent )), glEvent )
205+ if err != nil {
206+ t .Fatalf ("failed to unmarshal event: %v" , err )
207+ }
208+ tt .args .event .Event = glEvent
209+ }
210+
149211 got , err := v .IsAllowed (ctx , tt .args .event )
150212 if (err != nil ) != tt .wantErr {
151213 t .Errorf ("IsAllowed() error = %v, wantErr %v" , err , tt .wantErr )
0 commit comments