2424)
2525
2626func TestSuccessfulAddPolicies (t * testing.T ) {
27-
2827 t .Parallel ()
2928
3029 Convey ("Given a permissions store" , t , func () {
@@ -42,7 +41,7 @@ func TestSuccessfulAddPolicies(t *testing.T) {
4241 permissionsApi := setupAPIWithStore (mockedPermissionsStore )
4342
4443 Convey ("When a POST request is made to the policies endpoint with all the policies properties" , func () {
45- reader := strings .NewReader (`{"entities": ["e1", "e2"], "role": "r1", "conditions": [{"attributes": ["a1"], "operator": "and ", "values": ["v1"]}]}` )
44+ reader := strings .NewReader (`{"entities": ["e1", "e2"], "role": "r1", "conditions": [{"attributes": ["a1"], "operator": "StringEquals ", "values": ["v1"]}]}` )
4645 request , _ := http .NewRequest ("POST" , "http://localhost:25400/v1/policies" , reader )
4746 responseWriter := httptest .NewRecorder ()
4847 permissionsApi .Router .ServeHTTP (responseWriter , request )
@@ -70,7 +69,7 @@ func TestSuccessfulAddPolicies(t *testing.T) {
7069 So (policy .Role , ShouldResemble , "r1" )
7170 So (policy .Entities , ShouldResemble , []string {"e1" , "e2" })
7271 So (policy .Conditions , ShouldResemble , []models.Condition {
73- {Attributes : []string {"a1" }, Values : []string {"v1" }, Operator : "and" }},
72+ {Attributes : []string {"a1" }, Values : []string {"v1" }, Operator : models . OperatorStringEquals }},
7473 )
7574 })
7675 })
@@ -110,7 +109,7 @@ func TestSuccessfulAddPolicies(t *testing.T) {
110109
111110}
112111
113- func TestFailedAddPoliciesWithEmptyFields (t * testing.T ) {
112+ func TestFailedAddPoliciesWithInvalidPolicy (t * testing.T ) {
114113 t .Parallel ()
115114
116115 Convey ("When a POST request is made to the policies endpoint with empty entities" , t , func () {
@@ -136,7 +135,7 @@ func TestFailedAddPoliciesWithEmptyFields(t *testing.T) {
136135 Convey ("When a POST request is made to the policies without a role" , t , func () {
137136 permissionsApi := setupAPI ()
138137
139- reader := strings .NewReader (`{"entities": ["e1", "e2"], "conditions": [{"attributes": ["a1"], "operator": "and ", "values": ["v1"]}]}` )
138+ reader := strings .NewReader (`{"entities": ["e1", "e2"], "conditions": [{"attributes": ["a1"], "operator": "StringEquals ", "values": ["v1"]}]}` )
140139 request , _ := http .NewRequest ("POST" , "http://localhost:25400/v1/policies" , reader )
141140 responseWriter := httptest .NewRecorder ()
142141 permissionsApi .Router .ServeHTTP (responseWriter , request )
@@ -152,6 +151,26 @@ func TestFailedAddPoliciesWithEmptyFields(t *testing.T) {
152151 So (err , ShouldEqual , io .EOF )
153152 })
154153 })
154+
155+ Convey ("When a POST request is made to the policies with an invalid condition operator" , t , func () {
156+ permissionsApi := setupAPI ()
157+
158+ reader := strings .NewReader (`{"entities": ["e1", "e2"], "role": "r1", "conditions": [{"attributes": ["a1"], "operator": "And", "values": ["v1"]}]}` )
159+ request , _ := http .NewRequest ("POST" , "http://localhost:25400/v1/policies" , reader )
160+ responseWriter := httptest .NewRecorder ()
161+ permissionsApi .Router .ServeHTTP (responseWriter , request )
162+
163+ Convey ("Then the response is 400 bad request, with the expected response body" , func () {
164+ So (responseWriter .Code , ShouldEqual , http .StatusBadRequest )
165+ response := responseWriter .Body .String ()
166+ So (response , ShouldContainSubstring , "invalid field values: condition operator And" )
167+ })
168+ Convey ("Then the request body has been drained" , func () {
169+ bytesRead , err := request .Body .Read (make ([]byte , 1 ))
170+ So (bytesRead , ShouldEqual , 0 )
171+ So (err , ShouldEqual , io .EOF )
172+ })
173+ })
155174}
156175
157176func TestFailedAddPoliciesWithBadJson (t * testing.T ) {
@@ -196,11 +215,10 @@ func TestFailedAddPoliciesWithBadJson(t *testing.T) {
196215 So (err , ShouldEqual , io .EOF )
197216 })
198217 })
199-
200218}
201219
202220func TestFailedAddPoliciesWhenPermissionStoreFails (t * testing.T ) {
203- Convey ("when a permission store fails to insert a policy to data store" , t , func () {
221+ Convey ("When a permission store fails to insert a policy to data store" , t , func () {
204222
205223 mockedPermissionsStore := & mock.PermissionsStoreMock {
206224 AddPolicyFunc : func (ctx context.Context , policy * models.Policy ) (* models.Policy , error ) {
@@ -233,11 +251,9 @@ func TestFailedAddPoliciesWhenPermissionStoreFails(t *testing.T) {
233251 })
234252
235253 })
236-
237254}
238255
239256func TestGetPolicyHandler (t * testing.T ) {
240-
241257 Convey ("Given a GetPolicy Handler" , t , func () {
242258
243259 mockedPermissionsStore := & mock.PermissionsStoreMock {
@@ -248,7 +264,7 @@ func TestGetPolicyHandler(t *testing.T) {
248264 ID : testPolicyID ,
249265 Entities : []string {"e1" , "e2" },
250266 Role : "r1" ,
251- Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : "And" , Values : []string {"v1" }}}}, nil
267+ Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : models . OperatorStringEquals , Values : []string {"v1" }}}}, nil
252268 case "NOTFOUND" :
253269 return nil , apierrors .ErrPolicyNotFound
254270 default :
@@ -270,19 +286,18 @@ func TestGetPolicyHandler(t *testing.T) {
270286 ID : testPolicyID ,
271287 Entities : []string {"e1" , "e2" },
272288 Role : "r1" ,
273- Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : "And" , Values : []string {"v1" }}}}
289+ Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : models . OperatorStringEquals , Values : []string {"v1" }}}}
274290
275291 policy := models.Policy {}
276- payload , err := ioutil .ReadAll (responseRecorder .Body )
277- err = json .Unmarshal (payload , & policy )
278-
292+ payload , _ := ioutil .ReadAll (responseRecorder .Body )
293+ err := json .Unmarshal (payload , & policy )
279294 So (err , ShouldBeNil )
280295 So (responseRecorder .Code , ShouldEqual , http .StatusOK )
281296 So (policy , ShouldResemble , expectedPolicy )
282297 })
283298 })
284299
285- Convey ("When a non existing policy id is requested a Not Found response with 404 status code is returned" , func () {
300+ Convey ("When a non existing policy id is requested a Not Found response with 404 status code is returned" , func () {
286301 request := httptest .NewRequest (http .MethodGet , "http://localhost:25400/v1/policies/NOTFOUND" , nil )
287302 responseWriter := httptest .NewRecorder ()
288303 permissionsApi .Router .ServeHTTP (responseWriter , request )
@@ -302,11 +317,9 @@ func TestGetPolicyHandler(t *testing.T) {
302317 So (response , ShouldContainSubstring , "Something went wrong" )
303318 })
304319 })
305-
306320}
307321
308322func TestSuccessfulUpdatePolicy (t * testing.T ) {
309-
310323 t .Parallel ()
311324
312325 Convey ("Given a permissions store" , t , func () {
@@ -325,7 +338,7 @@ func TestSuccessfulUpdatePolicy(t *testing.T) {
325338 permissionsApi := setupAPIWithStore (mockedPermissionsStore )
326339
327340 Convey ("When a PUT request is made to the update policies endpoint to update an existing policy" , func () {
328- reader := strings .NewReader (`{"entities": ["e1", "e2"], "role": "r1", "conditions": [{"attributes": ["a1"], "operator": "and ", "values": ["v1"]}]}` )
341+ reader := strings .NewReader (`{"entities": ["e1", "e2"], "role": "r1", "conditions": [{"attributes": ["a1"], "operator": "StringEquals ", "values": ["v1"]}]}` )
329342 request , _ := http .NewRequest ("PUT" , "http://localhost:25400/v1/policies/existing_policy" , reader )
330343 responseWriter := httptest .NewRecorder ()
331344 permissionsApi .Router .ServeHTTP (responseWriter , request )
@@ -345,7 +358,7 @@ func TestSuccessfulUpdatePolicy(t *testing.T) {
345358 })
346359 })
347360
348- Convey ("When a PUT request is made to the update policies endpoint with an non-existing policy id" , func () {
361+ Convey ("When a PUT request is made to the update policies endpoint with a non-existing policy id" , func () {
349362 reader := strings .NewReader (`{"entities": ["e1"], "role": "r1"}` )
350363 request , _ := http .NewRequest ("PUT" , "http://localhost:25400/v1/policies/new_policy" , reader )
351364 responseWriter := httptest .NewRecorder ()
@@ -413,7 +426,7 @@ func TestFailedUpdatePoliciesWithBadJson(t *testing.T) {
413426}
414427
415428func TestFailedUpdatePoliciesWhenPermissionStoreFails (t * testing.T ) {
416- Convey ("when a permission store fails to insert a policy to data store" , t , func () {
429+ Convey ("When a permission store fails to insert a policy to data store" , t , func () {
417430
418431 mockedPermissionsStore := & mock.PermissionsStoreMock {
419432 UpdatePolicyFunc : func (ctx context.Context , policy * models.Policy ) (* models.UpdateResult , error ) {
@@ -446,11 +459,9 @@ func TestFailedUpdatePoliciesWhenPermissionStoreFails(t *testing.T) {
446459 })
447460
448461 })
449-
450462}
451463
452464func TestDeletePolicyHandler (t * testing.T ) {
453-
454465 Convey ("Given a DeletePolicy Handler" , t , func () {
455466
456467 mockedPermissionsStore := & mock.PermissionsStoreMock {
@@ -471,7 +482,7 @@ func TestDeletePolicyHandler(t *testing.T) {
471482 ID : testPolicyID ,
472483 Entities : []string {"e1" , "e2" },
473484 Role : "r1" ,
474- Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : "And" , Values : []string {"v1" }}}}, nil
485+ Conditions : []models.Condition {{Attributes : []string {"al" }, Operator : models . OperatorStringEquals , Values : []string {"v1" }}}}, nil
475486 case "NOTFOUND" :
476487 return nil , apierrors .ErrPolicyNotFound
477488 default :
@@ -517,5 +528,4 @@ func TestDeletePolicyHandler(t *testing.T) {
517528 So (response , ShouldContainSubstring , "Something went wrong" )
518529 })
519530 })
520-
521531}
0 commit comments