@@ -97,9 +97,11 @@ func (h dummyCohortDefinitionDataModel) GetCohortDefinitionIdsForTeamProject(tea
97
97
}
98
98
99
99
func (h dummyCohortDefinitionDataModel ) GetTeamProjectsThatMatchAllCohortDefinitionIds (uniqueCohortDefinitionIdsList []int ) ([]string , error ) {
100
- // dummy switch just to support two test scenarios:
100
+ // dummy switch just to support three test scenarios:
101
101
if uniqueCohortDefinitionIdsList [0 ] == 0 {
102
102
return nil , nil
103
+ } else if len (uniqueCohortDefinitionIdsList ) == 1 {
104
+ return []string {"teamProject1" }, nil
103
105
} else {
104
106
return []string {"teamProject1" , "teamProject2" }, nil
105
107
}
@@ -122,6 +124,48 @@ func (h dummyCohortDefinitionDataModel) GetAllCohortDefinitions() ([]*models.Coh
122
124
return nil , nil
123
125
}
124
126
127
+ func TestTeamProjectValidationForCohort (t * testing.T ) {
128
+ setUp (t )
129
+ config .Init ("mocktest" )
130
+ arboristAuthzResponseCode := 200
131
+ dummyHttpClient := & dummyHttpClient {statusCode : arboristAuthzResponseCode }
132
+ teamProjectAuthz := middlewares .NewTeamProjectAuthz (* new (dummyCohortDefinitionDataModel ),
133
+ dummyHttpClient )
134
+ requestContext := new (gin.Context )
135
+ requestContext .Request = new (http.Request )
136
+ requestContext .Request .Header = map [string ][]string {
137
+ "Authorization" : {"dummy_token_value" },
138
+ }
139
+ result := teamProjectAuthz .TeamProjectValidationForCohort (requestContext , 1 )
140
+ if result == false {
141
+ t .Errorf ("Expected TeamProjectValidationForCohort result to be 'true'" )
142
+ }
143
+ if dummyHttpClient .nrCalls != 1 {
144
+ t .Errorf ("Expected dummyHttpClient to have been only once" )
145
+ }
146
+ }
147
+
148
+ func TestTeamProjectValidationForCohortPart2 (t * testing.T ) {
149
+ setUp (t )
150
+ config .Init ("mocktest" )
151
+ arboristAuthzResponseCode := 401
152
+ dummyHttpClient := & dummyHttpClient {statusCode : arboristAuthzResponseCode }
153
+ teamProjectAuthz := middlewares .NewTeamProjectAuthz (* new (dummyCohortDefinitionDataModel ),
154
+ dummyHttpClient )
155
+ requestContext := new (gin.Context )
156
+ requestContext .Request = new (http.Request )
157
+ requestContext .Request .Header = map [string ][]string {
158
+ "Authorization" : {"dummy_token_value" },
159
+ }
160
+ result := teamProjectAuthz .TeamProjectValidationForCohort (requestContext , 1 )
161
+ if result == true {
162
+ t .Errorf ("Expected TeamProjectValidationForCohort result to be 'false'" )
163
+ }
164
+ if dummyHttpClient .nrCalls != 1 {
165
+ t .Errorf ("Expected dummyHttpClient to have been only once" )
166
+ }
167
+ }
168
+
125
169
func TestTeamProjectValidation (t * testing.T ) {
126
170
setUp (t )
127
171
config .Init ("mocktest" )
@@ -134,7 +178,7 @@ func TestTeamProjectValidation(t *testing.T) {
134
178
requestContext .Request .Header = map [string ][]string {
135
179
"Authorization" : {"dummy_token_value" },
136
180
}
137
- result := teamProjectAuthz .TeamProjectValidation (requestContext , []int {1 }, nil )
181
+ result := teamProjectAuthz .TeamProjectValidation (requestContext , []int {1 , 2 }, nil )
138
182
if result == false {
139
183
t .Errorf ("Expected TeamProjectValidation result to be 'true'" )
140
184
}
@@ -155,7 +199,7 @@ func TestTeamProjectValidationArborist401(t *testing.T) {
155
199
requestContext .Request .Header = map [string ][]string {
156
200
"Authorization" : {"dummy_token_value" },
157
201
}
158
- result := teamProjectAuthz .TeamProjectValidation (requestContext , []int {1 }, nil )
202
+ result := teamProjectAuthz .TeamProjectValidation (requestContext , []int {1 , 2 }, nil )
159
203
if result == true {
160
204
t .Errorf ("Expected TeamProjectValidation result to be 'false'" )
161
205
}
@@ -184,3 +228,30 @@ func TestTeamProjectValidationNoTeamProjectMatchingAllCohortDefinitions(t *testi
184
228
t .Errorf ("Expected dummyHttpClient to NOT have been called" )
185
229
}
186
230
}
231
+
232
+ func TestHasAccessToTeamProjectAbortOnArboristPrepError (t * testing.T ) {
233
+ setUp (t )
234
+ config .Init ("mocktest" )
235
+ arboristAuthzResponseCode := 200
236
+ dummyHttpClient := & dummyHttpClient {statusCode : arboristAuthzResponseCode }
237
+ requestContext := new (gin.Context )
238
+ requestContext .Request = new (http.Request )
239
+ requestContext .Writer = new (tests.CustomResponseWriter )
240
+ // add empty header to force an error during PrepareNewArboristRequestForResourceAndService:
241
+ requestContext .Request .Header = map [string ][]string {
242
+ "Authorization" : {"" },
243
+ }
244
+ teamProjectAuthz := middlewares .NewTeamProjectAuthz (* new (dummyCohortDefinitionDataModel ),
245
+ dummyHttpClient )
246
+
247
+ defer func () {
248
+ if err := recover (); err != nil {
249
+ log .Println ("panic occurred:" , err )
250
+ if err != "Error while preparing Arborist request" {
251
+ t .Errorf ("Expected error: 'Error while preparing Arborist request'" )
252
+ }
253
+ }
254
+ }()
255
+ teamProjectAuthz .HasAccessToTeamProject (requestContext , "dummyTeam" )
256
+ t .Errorf ("Expected error" )
257
+ }
0 commit comments