@@ -3,18 +3,32 @@ package policy
33import (
44 "testing"
55
6- "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
7- "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
8- testprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/test/provider"
96 "go.uber.org/zap"
107 zapobserver "go.uber.org/zap/zaptest/observer"
8+ "gotest.tools/v3/assert"
119 rtesting "knative.dev/pkg/reconciler/testing"
10+
11+ "github.com/openshift-pipelines/pipelines-as-code/pkg/apis/pipelinesascode/v1alpha1"
12+ "github.com/openshift-pipelines/pipelines-as-code/pkg/events"
13+ "github.com/openshift-pipelines/pipelines-as-code/pkg/params/info"
14+ testclient "github.com/openshift-pipelines/pipelines-as-code/pkg/test/clients"
15+ testprovider "github.com/openshift-pipelines/pipelines-as-code/pkg/test/provider"
1216)
1317
18+ func newRepoWithPolicy (policy * v1alpha1.Policy ) * v1alpha1.Repository {
19+ return & v1alpha1.Repository {
20+ Spec : v1alpha1.RepositorySpec {
21+ Settings : & v1alpha1.Settings {
22+ Policy : policy ,
23+ },
24+ },
25+ }
26+ }
27+
1428func TestPolicy_IsAllowed (t * testing.T ) {
1529 type fields struct {
16- Settings * v1alpha1.Settings
17- Event * info.Event
30+ repository * v1alpha1.Repository
31+ event * info.Event
1832 }
1933 type args struct {
2034 tType info.TriggerType
@@ -24,172 +38,141 @@ func TestPolicy_IsAllowed(t *testing.T) {
2438 fields fields
2539 args args
2640 replyAllowed bool
27- want Result
41+ want bool
2842 wantErr bool
43+ wantReason string
2944 }{
3045 {
3146 name : "Test Policy.IsAllowed with no settings" ,
3247 fields : fields {
33- Settings : nil ,
34- Event : nil ,
48+ repository : nil ,
49+ event : nil ,
3550 },
3651 args : args {
3752 tType : info .TriggerTypePush ,
3853 },
39- want : ResultNotSet ,
54+ want : false ,
4055 },
4156 {
4257 name : "Test Policy.IsAllowed with unknown event type" ,
4358 fields : fields {
44- Settings : & v1alpha1.Settings {
45- Policy : & v1alpha1.Policy {
46- PullRequest : []string {"pull_request" },
47- },
48- },
49- Event : nil ,
59+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
60+ event : nil ,
5061 },
5162 args : args {
5263 tType : "unknown" ,
5364 },
54- want : ResultNotSet ,
65+ want : false ,
5566 },
5667 {
5768 name : "allowing member not in team for pull request" ,
5869 fields : fields {
59- Settings : & v1alpha1.Settings {
60- Policy : & v1alpha1.Policy {
61- PullRequest : []string {"pull_request" },
62- },
63- },
64- Event : info .NewEvent (),
70+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
71+ event : info .NewEvent (),
6572 },
6673 args : args {
6774 tType : info .TriggerTypePullRequest ,
6875 },
6976 replyAllowed : true ,
70- want : ResultAllowed ,
77+ want : true ,
7178 },
7279 {
73- name : "empty settings policy ignore" ,
80+ name : "empty settings policy ignore" ,
81+ wantReason : "policy check: pull_request, policy disallowing" ,
7482 fields : fields {
75- Settings : & v1alpha1.Settings {
76- Policy : & v1alpha1.Policy {
77- PullRequest : []string {},
78- },
79- },
80- Event : info .NewEvent (),
83+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"" }}),
84+ event : info .NewEvent (),
8185 },
8286 args : args {
8387 tType : info .TriggerTypePullRequest ,
8488 },
85- replyAllowed : true ,
86- want : ResultNotSet ,
89+ want : false ,
8790 },
8891 {
89- name : "disallowing member not in team for pull request" ,
92+ name : "disallowing member not in team for pull request" ,
93+ wantReason : "policy check: pull_request, policy disallowing" ,
9094 fields : fields {
91- Settings : & v1alpha1.Settings {
92- Policy : & v1alpha1.Policy {
93- PullRequest : []string {"pull_request" },
94- },
95- },
96- Event : info .NewEvent (),
95+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"pull_request" }}),
96+ event : info .NewEvent (),
9797 },
9898 args : args {
9999 tType : info .TriggerTypePullRequest ,
100100 },
101- replyAllowed : false ,
102- want : ResultDisallowed ,
103- wantErr : true ,
101+ want : false ,
102+ wantErr : true ,
104103 },
105104 {
106- name : "allowing member not in team for ok-to-test" ,
105+ name : "allowing member in team for ok-to-test" ,
107106 fields : fields {
108- Settings : & v1alpha1.Settings {
109- Policy : & v1alpha1.Policy {
110- OkToTest : []string {"ok-to-test" },
111- },
112- },
113- Event : info .NewEvent (),
107+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
108+ event : info .NewEvent (),
114109 },
115110 args : args {
116111 tType : info .TriggerTypeOkToTest ,
117112 },
118113 replyAllowed : true ,
119- want : ResultAllowed ,
114+ want : false ,
120115 },
121116 {
122117 name : "disallowing member not in team for ok-to-test" ,
123118 fields : fields {
124- Settings : & v1alpha1.Settings {
125- Policy : & v1alpha1.Policy {
126- OkToTest : []string {"ok-to-test" },
127- },
128- },
129- Event : info .NewEvent (),
119+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
120+ event : info .NewEvent (),
130121 },
131122 args : args {
132123 tType : info .TriggerTypeOkToTest ,
133124 },
134- replyAllowed : false ,
135- want : ResultDisallowed ,
136- wantErr : true ,
125+ want : false ,
126+ wantErr : true ,
137127 },
138128 {
139129 name : "allowing member not in team for retest" ,
140130 fields : fields {
141- Settings : & v1alpha1.Settings {
142- Policy : & v1alpha1.Policy {
143- OkToTest : []string {"ok-to-test" },
144- },
145- },
146- Event : info .NewEvent (),
131+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
132+ event : info .NewEvent (),
147133 },
148134 args : args {
149135 tType : info .TriggerTypeRetest ,
150136 },
151- replyAllowed : true ,
152- want : ResultAllowed ,
137+ want : false ,
153138 },
154139 {
155140 name : "disallowing member not in team for retest" ,
156141 fields : fields {
157- Settings : & v1alpha1.Settings {
158- Policy : & v1alpha1.Policy {
159- OkToTest : []string {"ok-to-test" },
160- },
161- },
162- Event : info .NewEvent (),
142+ repository : newRepoWithPolicy (& v1alpha1.Policy {PullRequest : []string {"ok-to-test" }}),
143+ event : info .NewEvent (),
163144 },
164145 args : args {
165146 tType : info .TriggerTypeRetest ,
166147 },
167- replyAllowed : false ,
168- want : ResultDisallowed ,
169- wantErr : true ,
148+ want : false ,
149+ wantErr : true ,
170150 },
171151 }
172152 for _ , tt := range tests {
173153 t .Run (tt .name , func (t * testing.T ) {
174154 observer , _ := zapobserver .New (zap .InfoLevel )
175155 logger := zap .New (observer ).Sugar ()
176156
157+ ctx , _ := rtesting .SetupFakeContext (t )
158+ stdata , _ := testclient .SeedTestData (t , ctx , testclient.Data {})
159+
177160 vcx := & testprovider.TestProviderImp {PolicyDisallowing : ! tt .replyAllowed }
178- p := & Policy {
179- Settings : tt .fields .Settings ,
180- Event : tt .fields .Event ,
181- VCX : vcx ,
182- Logger : logger ,
161+ if tt .fields .event == nil {
162+ tt .fields .event = info .NewEvent ()
183163 }
184- ctx , _ := rtesting .SetupFakeContext (t )
185- got , err := p .IsAllowed (ctx , tt .args .tType )
186- if (err != nil ) != tt .wantErr {
187- t .Errorf ("Policy.IsAllowed() error = %v, wantErr %v" , err , tt .wantErr )
188- return
164+ p := & Policy {
165+ Repository : tt .fields .repository ,
166+ Event : tt .fields .event ,
167+ VCX : vcx ,
168+ Logger : logger ,
169+ EventEmitter : events .NewEventEmitter (stdata .Kube , logger ),
189170 }
171+ got , reason := p .IsAllowed (ctx , tt .args .tType )
190172 if got != tt .want {
191173 t .Errorf ("Policy.IsAllowed() = %v, want %v" , got , tt .want )
192174 }
175+ assert .Equal (t , tt .wantReason , reason )
193176 })
194177 }
195178}
0 commit comments