Skip to content

Commit 1289100

Browse files
committed
Merge branch 'release/0.10.0'
2 parents ab272cd + 229d510 commit 1289100

File tree

15 files changed

+572
-339
lines changed

15 files changed

+572
-339
lines changed

api/policies_test.go

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ var (
2424
)
2525

2626
func 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

157176
func TestFailedAddPoliciesWithBadJson(t *testing.T) {
@@ -196,11 +215,10 @@ func TestFailedAddPoliciesWithBadJson(t *testing.T) {
196215
So(err, ShouldEqual, io.EOF)
197216
})
198217
})
199-
200218
}
201219

202220
func 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

239256
func 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

308322
func 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

415428
func 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

452464
func 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
}

features/delete_policies.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Feature: Behaviour of application when performing requests against /v1/policies
2929
],
3030
"conditions": [
3131
{
32-
"operator": "=",
32+
"operator": "StringEquals",
3333
"attributes": [
3434
"collection-id"
3535
],

features/get_permissions_bundle.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ Feature: GET /v1/permissions-bundle endpoint
5454
],
5555
"conditions": [
5656
{
57-
"operator": "=",
57+
"operator": "StringEquals",
5858
"attributes": [
5959
"collection-id"
6060
],
@@ -93,7 +93,7 @@ Feature: GET /v1/permissions-bundle endpoint
9393
"attributes": [
9494
"collection-id"
9595
],
96-
"operator": "=",
96+
"operator": "StringEquals",
9797
"values": [
9898
"collection-765"
9999
]

features/get_policies.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Feature: Behaviour of application when performing requests against /v1/policies
2929
],
3030
"conditions": [
3131
{
32-
"operator": "=",
32+
"operator": "StringEquals",
3333
"attributes": [
3434
"collection-id"
3535
],
@@ -78,7 +78,7 @@ Feature: Behaviour of application when performing requests against /v1/policies
7878
],
7979
"conditions": [
8080
{
81-
"operator": "=",
81+
"operator": "StringEquals",
8282
"attributes": [
8383
"collection-id"
8484
],

features/post_policies.feature

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
1515
"attributes": [
1616
"a1"
1717
],
18-
"operator": "and",
18+
"operator": "StringEquals",
1919
"values": [
2020
"v1"
2121
]
@@ -51,7 +51,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
5151
"attributes": [
5252
"a1"
5353
],
54-
"operator": "and",
54+
"operator": "StringEquals",
5555
"values": [
5656
"v1"
5757
]
@@ -77,7 +77,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
7777
"attributes": [
7878
"a1"
7979
],
80-
"operator": "and",
80+
"operator": "StringEquals",
8181
"values": [
8282
"v1"
8383
]
@@ -106,7 +106,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
106106
"attributes": [
107107
"a1"
108108
],
109-
"operator": "and",
109+
"operator": "StringEquals",
110110
"values": [
111111
"v1"
112112
]
@@ -133,7 +133,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
133133
"attributes": [
134134
"a1"
135135
],
136-
"operator": "and",
136+
"operator": "StringEquals",
137137
"values": [
138138
"v1"
139139
]
@@ -159,7 +159,7 @@ Feature: Behaviour of application when doing the POST /v1/policies endpoint, usi
159159
"attributes": [
160160
"a1"
161161
],
162-
"operator": "and",
162+
"operator": "StringEquals",
163163
"values": [
164164
"v1"
165165
]

features/steps/permissions_component.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,16 @@ func NewPermissionsComponent(mongoFeature *componenttest.MongoFeature) (*Permiss
147147
f.Config.AuthorisationConfig.JWTVerificationPublicKeys = rsaJWKS
148148
f.Config.AuthorisationConfig.PermissionsAPIURL = fakePermissionsAPI.URL()
149149

150-
return f, nil
150+
return f, nil
151151
}
152152

153153
func createCredsInDB(getMongoURI string, databaseName string) (string, string, error) {
154154
username := "admin"
155155
password, _ := uuid.NewV4()
156156
mongoConnectionConfig := &dpMongoDriver.MongoConnectionConfig{
157-
IsSSL: false,
157+
TLSConnectionConfig: dpMongoDriver.TLSConnectionConfig{
158+
IsSSL: false,
159+
},
158160
ConnectTimeoutInSeconds: 15,
159161
QueryTimeoutInSeconds: 15,
160162

@@ -167,23 +169,21 @@ func createCredsInDB(getMongoURI string, databaseName string) (string, string, e
167169
if err != nil {
168170
return username, password.String(), errors.New(fmt.Sprintf("expected db connection to be opened: %+v", err))
169171
}
170-
mongoDatabaseSelection := mongoConnection.
171-
GetMongoCollection().
172-
Database()
173-
createCollectionResponse := mongoDatabaseSelection.RunCommand(context.TODO(), bson.D{
174-
{"create", "test"},
172+
173+
createCollectionResponse := mongoConnection.RunCommand(context.TODO(), bson.D{
174+
{Key: "create", Value: "test"},
175175
})
176-
if createCollectionResponse.Err() != nil {
176+
if createCollectionResponse != nil {
177177
return username, password.String(), errors.New(fmt.Sprintf("expected database creation to go through: %+v", err))
178178
}
179-
userCreationResponse := mongoDatabaseSelection.RunCommand(context.TODO(), bson.D{
180-
{"createUser", username},
181-
{"pwd", password.String()},
182-
{"roles", []bson.M{
179+
userCreationResponse := mongoConnection.RunCommand(context.TODO(), bson.D{
180+
{Key: "createUser", Value: username},
181+
{Key: "pwd", Value: password.String()},
182+
{Key: "roles", Value: []bson.M{
183183
{"role": "root", "db": "admin"},
184184
}},
185185
})
186-
if userCreationResponse.Err() != nil {
186+
if userCreationResponse != nil {
187187
return username, password.String(), errors.New(fmt.Sprintf("expected user creation to go through: %+v", err))
188188
}
189189
return username, password.String(), nil
@@ -216,9 +216,9 @@ func (f *PermissionsComponent) Close() error {
216216

217217
func (f *PermissionsComponent) InitialiseService() (http.Handler, error) {
218218
initMock := &serviceMock.InitialiserMock{
219-
DoGetMongoDBFunc: f.DoGetMongoDB,
220-
DoGetHealthCheckFunc: f.DoGetHealthcheckOk,
221-
DoGetHTTPServerFunc: f.DoGetHTTPServer,
219+
DoGetMongoDBFunc: f.DoGetMongoDB,
220+
DoGetHealthCheckFunc: f.DoGetHealthcheckOk,
221+
DoGetHTTPServerFunc: f.DoGetHTTPServer,
222222
DoGetAuthorisationMiddlewareFunc: f.DoGetAuthorisationMiddleware,
223223
}
224224

0 commit comments

Comments
 (0)