11package policy
22
33import (
4+ "fmt"
5+ "strings"
46 "testing"
57
68 "go.uber.org/zap"
@@ -26,6 +28,10 @@ func newRepoWithPolicy(policy *v1alpha1.Policy) *v1alpha1.Repository {
2628}
2729
2830func TestPolicy_IsAllowed (t * testing.T ) {
31+ senderName := "sender"
32+ eventWithSender := info .NewEvent ()
33+ eventWithSender .Sender = senderName
34+
2935 type fields struct {
3036 repository * v1alpha1.Repository
3137 event * info.Event
@@ -34,130 +40,180 @@ func TestPolicy_IsAllowed(t *testing.T) {
3440 tType info.TriggerType
3541 }
3642 tests := []struct {
37- name string
38- fields fields
39- args args
40- replyAllowed bool
41- want bool
42- wantErr bool
43- wantReason string
43+ name string
44+ fields fields
45+ args args
46+ vcsReplyAllowed bool
47+ want Result
48+ wantErr bool
49+ wantReason string
50+ allowedInOwnersFile bool
51+ expectedLogsSnippets []string
4452 }{
4553 {
46- name : "Test Policy.IsAllowed with no settings " ,
54+ name : "notset/not set " ,
4755 fields : fields {
4856 repository : nil ,
4957 event : nil ,
5058 },
5159 args : args {
5260 tType : info .TriggerTypePush ,
5361 },
54- want : false ,
62+ want : ResultNotSet ,
5563 },
5664 {
57- name : "Test Policy.IsAllowed with unknown event type" ,
65+ name : "notset/ unknown event type" ,
5866 fields : fields {
5967 repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
6068 event : nil ,
6169 },
6270 args : args {
6371 tType : "unknown" ,
6472 },
65- want : false ,
73+ want : ResultNotSet ,
6674 },
6775 {
68- name : "allowing member not in team for pull request " ,
76+ name : "notset/push " ,
6977 fields : fields {
7078 repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
71- event : info . NewEvent () ,
79+ event : nil ,
7280 },
7381 args : args {
74- tType : info .TriggerTypePullRequest ,
82+ tType : info .TriggerTypePush ,
7583 },
76- replyAllowed : true ,
77- want : true ,
84+ want : ResultNotSet ,
7885 },
7986 {
80- name : "empty settings policy ignore" ,
81- wantReason : "policy check: pull_request, policy disallowing" ,
87+ name : "allowed/allowing member for pull request" ,
8288 fields : fields {
83- repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"" }}),
89+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request " }}),
8490 event : info .NewEvent (),
8591 },
8692 args : args {
8793 tType : info .TriggerTypePullRequest ,
8894 },
89- want : false ,
95+ vcsReplyAllowed : true ,
96+ want : ResultAllowed ,
9097 },
9198 {
92- name : "disallowing member not in team for pull request" ,
93- wantReason : "policy check: pull_request, policy disallowing" ,
99+ name : "allowed/from owners file" ,
94100 fields : fields {
95101 repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
96- event : info . NewEvent () ,
102+ event : eventWithSender ,
97103 },
98104 args : args {
99105 tType : info .TriggerTypePullRequest ,
100106 },
101- want : false ,
102- wantErr : true ,
107+ vcsReplyAllowed : false ,
108+ allowedInOwnersFile : true ,
109+ want : ResultAllowed ,
110+ expectedLogsSnippets : []string {
111+ fmt .Sprintf ("policy check: policy is set, sender %s not in the allowed policy but allowed via OWNERS file" , senderName ),
112+ },
103113 },
104114 {
105- name : "allowing member in team for ok-to-test" ,
115+ name : "allowed/ member in team for ok-to-test" ,
106116 fields : fields {
107- repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
117+ repository : newRepoWithPolicy (& v1alpha1.Policy {OkToTest : []string {"ok-to-test" }}),
108118 event : info .NewEvent (),
109119 },
110120 args : args {
111121 tType : info .TriggerTypeOkToTest ,
112122 },
113- replyAllowed : true ,
114- want : false ,
123+ vcsReplyAllowed : true ,
124+ want : ResultAllowed ,
125+ },
126+ {
127+ name : "allowed/retest same as ok-to-test" ,
128+ fields : fields {
129+ repository : newRepoWithPolicy (& v1alpha1.Policy {OkToTest : []string {"ok-to-test" }}),
130+ event : info .NewEvent (),
131+ },
132+ args : args {
133+ tType : info .TriggerTypeRetest ,
134+ },
135+ vcsReplyAllowed : true ,
136+ want : ResultAllowed ,
137+ },
138+ {
139+ name : "disallowed/policy set with empty list" ,
140+ expectedLogsSnippets : []string {"policy set and empty with no groups" },
141+ fields : fields {
142+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"" }}),
143+ event : eventWithSender ,
144+ },
145+ args : args {
146+ tType : info .TriggerTypePullRequest ,
147+ },
148+ want : ResultDisallowed ,
149+ },
150+ {
151+ name : "disallowed/member not in team for pull request" ,
152+ fields : fields {
153+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
154+ event : info .NewEvent (),
155+ },
156+ args : args {
157+ tType : info .TriggerTypePullRequest ,
158+ },
159+ want : ResultDisallowed ,
160+ wantErr : true ,
161+ expectedLogsSnippets : []string {"policy check: pull_request, policy disallowing" },
115162 },
116163 {
117- name : "disallowing member not in team for ok-to-test" ,
164+ name : "disallowed/ member not in team for ok-to-test" ,
118165 fields : fields {
119- repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test " }}),
166+ repository : newRepoWithPolicy (& v1alpha1.Policy {OkToTest : []string {"nono " }}),
120167 event : info .NewEvent (),
121168 },
122169 args : args {
123170 tType : info .TriggerTypeOkToTest ,
124171 },
125- want : false ,
126- wantErr : true ,
172+ want : ResultDisallowed ,
173+ wantErr : true ,
174+ expectedLogsSnippets : []string {"policy check: ok-to-test, policy disallowing" },
127175 },
128176 {
129- name : "allowing member not in team for retest " ,
177+ name : "disallowed/ member not in team" ,
130178 fields : fields {
131- repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
179+ repository : newRepoWithPolicy (& v1alpha1.Policy {OkToTest : []string {"ok-to-test" }}),
132180 event : info .NewEvent (),
133181 },
134182 args : args {
135183 tType : info .TriggerTypeRetest ,
136184 },
137- want : false ,
185+ want : ResultDisallowed ,
186+ wantErr : true ,
187+ vcsReplyAllowed : false ,
188+ expectedLogsSnippets : []string {"policy check: retest, policy disallowing" },
138189 },
139190 {
140- name : "disallowing member not in team for retest " ,
191+ name : "disallowed/from owners file " ,
141192 fields : fields {
142- repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test " }}),
193+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request " }}),
143194 event : info .NewEvent (),
144195 },
145196 args : args {
146- tType : info .TriggerTypeRetest ,
197+ tType : info .TriggerTypePullRequest ,
147198 },
148- want : false ,
149- wantErr : true ,
199+ vcsReplyAllowed : false ,
200+ allowedInOwnersFile : false ,
201+ want : ResultDisallowed ,
202+ expectedLogsSnippets : []string {"policy check: pull_request, policy disallowing" },
150203 },
151204 }
152205 for _ , tt := range tests {
153206 t .Run (tt .name , func (t * testing.T ) {
154- observer , _ := zapobserver .New (zap .InfoLevel )
207+ observer , log := zapobserver .New (zap .InfoLevel )
155208 logger := zap .New (observer ).Sugar ()
156209
157210 ctx , _ := rtesting .SetupFakeContext (t )
158211 stdata , _ := testclient .SeedTestData (t , ctx , testclient.Data {})
159212
160- vcx := & testprovider.TestProviderImp {PolicyDisallowing : ! tt .replyAllowed }
213+ vcx := & testprovider.TestProviderImp {
214+ PolicyDisallowing : ! tt .vcsReplyAllowed ,
215+ AllowedInOwnersFile : tt .allowedInOwnersFile ,
216+ }
161217 if tt .fields .event == nil {
162218 tt .fields .event = info .NewEvent ()
163219 }
@@ -169,10 +225,20 @@ func TestPolicy_IsAllowed(t *testing.T) {
169225 EventEmitter : events .NewEventEmitter (stdata .Kube , logger ),
170226 }
171227 got , reason := p .IsAllowed (ctx , tt .args .tType )
172- if got != tt .want {
173- t .Errorf ("Policy.IsAllowed() = %v, want %v" , got , tt .want )
228+ switch got {
229+ case ResultAllowed :
230+ assert .Equal (t , tt .want , ResultAllowed )
231+ case ResultDisallowed :
232+ assert .Equal (t , tt .want , ResultDisallowed )
233+ case ResultNotSet :
234+ assert .Equal (t , tt .want , ResultNotSet )
174235 }
175236 assert .Equal (t , tt .wantReason , reason )
237+
238+ for k , snippet := range tt .expectedLogsSnippets {
239+ logmsg := log .AllUntimed ()[k ].Message
240+ assert .Assert (t , strings .Contains (logmsg , snippet ), "\n on index: %d\n we want: %s\n we got: %s" , k , snippet , logmsg )
241+ }
176242 })
177243 }
178244}
0 commit comments