Skip to content

Commit 46d16df

Browse files
thquadrichard-cox
authored andcommitted
Adjust unit tests (#4876)
Signed-off-by: Thomas Quandt <[email protected]>
1 parent 04aa4d5 commit 46d16df

File tree

5 files changed

+123
-71
lines changed

5 files changed

+123
-71
lines changed

src/jetstream/auth_test.go

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -640,15 +640,19 @@ func TestLoginToCNSIWithUserEndpointsEnabled(t *testing.T) {
640640
})
641641
_, ctxConnectToUser2 := setupEchoContext(res, req)
642642

643+
adminAndUserEndpointRows := sqlmock.NewRows(rowFieldsForCNSI).AddRow(adminEndpointArgs...).AddRow(userEndpoint1Args...)
644+
643645
Convey("As admin", func() {
644646

645-
Convey("Connect to admin endpoint", func() {
647+
Convey("Connect to system endpoint", func() {
646648
if errSession := pp.setSessionValues(ctxConnectToAdmin, mockAdmin.SessionValues); errSession != nil {
647649
t.Error(errors.New("unable to mock/stub user in session object"))
648650
}
649651

650652
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(adminEndpointRows)
651653

654+
mock.ExpectQuery(selectAnyFromCNSIs).WillReturnRows(adminEndpointRows)
655+
652656
mock.ExpectQuery(selectAnyFromTokens).
653657
WithArgs(adminEndpointArgs[0], mockAdmin.ConnectedUser.GUID).
654658
WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("0"))
@@ -674,22 +678,16 @@ func TestLoginToCNSIWithUserEndpointsEnabled(t *testing.T) {
674678

675679
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(userEndpoint1Rows)
676680

677-
mockStratosAuth.
678-
EXPECT().
679-
GetUser(gomock.Eq(mockAdmin.ConnectedUser.GUID)).
680-
Return(mockAdmin.ConnectedUser, nil)
681-
682681
err := pp.loginToCNSI(ctxConnectToUser1)
683682
dberr := mock.ExpectationsWereMet()
684683

685684
Convey("should fail", func() {
686-
So(err, ShouldResemble, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - admins are not allowed to connect to user created endpoints"))
685+
So(err, ShouldResemble, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - users are not allowed to connect to personal endpoints created by other users"))
687686
})
688687

689688
Convey("there should be no db error", func() {
690689
So(dberr, ShouldBeNil)
691690
})
692-
693691
})
694692
})
695693
Convey("As user", func() {
@@ -698,13 +696,49 @@ func TestLoginToCNSIWithUserEndpointsEnabled(t *testing.T) {
698696
t.Error(errors.New("unable to mock/stub user in session object"))
699697
}
700698

701-
mockStratosAuth.
702-
EXPECT().
703-
GetUser(gomock.Eq(mockEndpointAdmin1.ConnectedUser.GUID)).
704-
Return(mockEndpointAdmin1.ConnectedUser, nil)
699+
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(userEndpoint1Rows)
700+
701+
mock.ExpectQuery(selectAnyFromCNSIs).WillReturnRows(userEndpoint1Rows)
702+
703+
mock.ExpectQuery(selectAnyFromTokens).
704+
WithArgs(userEndpoint1Args[0], mockEndpointAdmin1.ConnectedUser.GUID).
705+
WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("0"))
706+
707+
mock.ExpectExec(insertIntoTokens).
708+
WillReturnResult(sqlmock.NewResult(1, 1))
709+
710+
err := pp.loginToCNSI(ctxConnectToUser1)
711+
dberr := mock.ExpectationsWereMet()
712+
713+
Convey("there should be no error", func() {
714+
So(err, ShouldBeNil)
715+
})
716+
717+
Convey("there should be no db error", func() {
718+
So(dberr, ShouldBeNil)
719+
})
720+
})
721+
Convey("Connect to own endpoint while already connected to same url with system endpoint", func() {
722+
if errSession := pp.setSessionValues(ctxConnectToUser1, mockEndpointAdmin1.SessionValues); errSession != nil {
723+
t.Error(errors.New("unable to mock/stub user in session object"))
724+
}
705725

706726
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(userEndpoint1Rows)
707727

728+
// args is the api url
729+
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(userEndpoint1Args[3]).WillReturnRows(adminAndUserEndpointRows)
730+
731+
// connected system endpoint found
732+
mock.ExpectQuery(selectAnyFromTokens).
733+
WithArgs(adminEndpointArgs[0], mockEndpointAdmin1.ConnectedUser.GUID, mockAdminGUID).
734+
WillReturnRows(sqlmock.NewRows([]string{"token_guid", "auth_token", "refresh_token", "token_expiry", "disconnected", "auth_type", "meta_data", "user_guid", "linked_token"}).
735+
AddRow("", mockUAAToken, mockUAAToken, time.Now().Add(-time.Hour).Unix(), false, "", "", "", nil))
736+
737+
// remove other connection, since it has the same api url
738+
mock.ExpectExec(deleteTokens).
739+
WithArgs(adminEndpointArgs[0], mockEndpointAdmin1.ConnectedUser.GUID).
740+
WillReturnResult(sqlmock.NewResult(1, 1))
741+
708742
mock.ExpectQuery(selectAnyFromTokens).
709743
WithArgs(userEndpoint1Args[0], mockEndpointAdmin1.ConnectedUser.GUID).
710744
WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("0"))
@@ -723,13 +757,15 @@ func TestLoginToCNSIWithUserEndpointsEnabled(t *testing.T) {
723757
So(dberr, ShouldBeNil)
724758
})
725759
})
726-
Convey("Connect to admin endpoint", func() {
760+
Convey("Connect to system endpoint", func() {
727761
if errSession := pp.setSessionValues(ctxConnectToAdmin, mockEndpointAdmin1.SessionValues); errSession != nil {
728762
t.Error(errors.New("unable to mock/stub user in session object"))
729763
}
730764

731765
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(adminEndpointRows)
732766

767+
mock.ExpectQuery(selectAnyFromCNSIs).WillReturnRows(adminEndpointRows)
768+
733769
mock.ExpectQuery(selectAnyFromTokens).
734770
WithArgs(adminEndpointArgs[0], mockEndpointAdmin1.ConnectedUser.GUID).
735771
WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("0"))
@@ -755,16 +791,11 @@ func TestLoginToCNSIWithUserEndpointsEnabled(t *testing.T) {
755791

756792
mock.ExpectQuery(selectFromCNSIs).WillReturnRows(userEndpoint2Rows)
757793

758-
mockStratosAuth.
759-
EXPECT().
760-
GetUser(gomock.Eq(mockEndpointAdmin1.ConnectedUser.GUID)).
761-
Return(mockEndpointAdmin1.ConnectedUser, nil)
762-
763794
err := pp.loginToCNSI(ctxConnectToUser2)
764795
dberr := mock.ExpectationsWereMet()
765796

766797
Convey("should fail", func() {
767-
So(err, ShouldResemble, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - non-admins are not allowed to connect to endpoints created by other non-admins"))
798+
So(err, ShouldResemble, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - users are not allowed to connect to personal endpoints created by other users"))
768799
})
769800

770801
Convey("there should be no db error", func() {

src/jetstream/authcnsi.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ func (p *portalProxy) DoLoginToCNSI(c echo.Context, cnsiGUID string, systemShare
153153
// admins are note allowed to connect to user created endpoints
154154
if p.GetConfig().UserEndpointsEnabled != config.UserEndpointsConfigEnum.Disabled {
155155

156+
if len(cnsiRecord.Creator) != 0 && cnsiRecord.Creator != userID {
157+
return nil, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - users are not allowed to connect to personal endpoints created by other users")
158+
}
159+
156160
// search for system or personal endpoints and check if they are connected
157161
// automatically disconnect other endpoint if already connected to same url
158162
cnsiList, err := p.listCNSIByAPIEndpoint(cnsiRecord.APIEndpoint.String())
@@ -165,17 +169,13 @@ func (p *portalProxy) DoLoginToCNSI(c echo.Context, cnsiGUID string, systemShare
165169
}
166170

167171
for _, cnsi := range cnsiList {
168-
if cnsi.Creator == userID || len(cnsi.Creator) == 0 {
172+
if (cnsi.Creator == userID || len(cnsi.Creator) == 0) && cnsi.GUID != cnsiGUID {
169173
_, ok := p.GetCNSITokenRecord(cnsi.GUID, userID)
170174
if ok {
171175
p.ClearCNSIToken(*cnsi, userID)
172176
}
173177
}
174178
}
175-
176-
if len(cnsiRecord.Creator) != 0 && cnsiRecord.Creator != userID {
177-
return nil, echo.NewHTTPError(http.StatusUnauthorized, "Can not connect - users are not allowed to connect to personal endpoints created by other users")
178-
}
179179
}
180180

181181
// Register as a system endpoint?

src/jetstream/cnsi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func (p *portalProxy) buildCNSIList(c echo.Context) ([]*interfaces.CNSIRecord, e
283283
}
284284

285285
if p.GetConfig().UserEndpointsEnabled != config.UserEndpointsConfigEnum.AdminOnly {
286-
// remove existing system endpoint if user endpoint already exists and sessionuser not admin
286+
// if endpoint with same url exists as system and user endpoint, hide the system endpoint
287287
unfilteredList, err := p.ListAdminEndpoints(userID.(string))
288288
if err != nil {
289289
return unfilteredList, err

src/jetstream/cnsi_test.go

Lines changed: 59 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
307307
pp.GetConfig().UserEndpointsEnabled = config.UserEndpointsConfigEnum.Enabled
308308

309309
Convey("as admin", func() {
310-
Convey("with overwrite disabled", func() {
310+
Convey("with createUserEndpoint disabled", func() {
311311
// setup
312-
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", false)
312+
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", false, true)
313313

314314
if errSession := pp.setSessionValues(adminEndpoint.EchoContext, mockAdmin.SessionValues); errSession != nil {
315315
t.Error(errors.New("unable to mock/stub user in session object"))
@@ -341,9 +341,9 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
341341
So(dberr, ShouldBeNil)
342342
})
343343
})
344-
Convey("overwrite existing user endpoints", func() {
344+
Convey("create system endpoint over existing user endpoints", func() {
345345
// setup
346-
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1 User", false)
346+
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1 User", false, false)
347347

348348
// mock executions
349349
mockStratosAuth.
@@ -355,33 +355,24 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
355355
rows := sqlmock.NewRows(rowFieldsForCNSI).AddRow(userEndpoint.QueryArgs...)
356356
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
357357

358+
// save cnsi
359+
mock.ExpectExec(insertIntoCNSIs).
360+
WithArgs(adminEndpoint.InsertArgs...).
361+
WillReturnResult(sqlmock.NewResult(1, 1))
362+
358363
// test
359364
err := pp.RegisterEndpoint(adminEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
360365
dberr := mock.ExpectationsWereMet()
361366

362367
Convey("there should be no error", func() {
363-
So(err, ShouldResemble, interfaces.NewHTTPShadowError(
364-
http.StatusBadRequest,
365-
"Can not register same endpoint multiple times",
366-
"Can not register same endpoint multiple times",
367-
))
368+
So(err, ShouldBeNil)
368369
})
369370

370371
Convey("there should be no db error", func() {
371372
So(dberr, ShouldBeNil)
372373
})
373374
})
374-
})
375-
Convey("with overwrite enabled", func() {
376-
377-
// setup
378-
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", true)
379-
380-
if errSession := pp.setSessionValues(adminEndpoint.EchoContext, mockAdmin.SessionValues); errSession != nil {
381-
t.Error(errors.New("unable to mock/stub user in session object"))
382-
}
383-
384-
Convey("overwrite existing admin endpoints", func() {
375+
Convey("create system endpoint over existing system endpoints", func() {
385376
// mock executions
386377
mockStratosAuth.
387378
EXPECT().
@@ -399,36 +390,38 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
399390
Convey("should fail ", func() {
400391
So(err, ShouldResemble, interfaces.NewHTTPShadowError(
401392
http.StatusBadRequest,
402-
"Can not register same admin endpoint multiple times",
403-
"Can not register same admin endpoint multiple times",
393+
"Can not register same system endpoint multiple times",
394+
"Can not register same system endpoint multiple times",
404395
))
405396
})
406397

407398
Convey("no insert should be executed", func() {
408399
So(dberr, ShouldBeNil)
409400
})
410401
})
411-
Convey("overwrite existing user endpoints", func() {
402+
})
403+
Convey("with createUserEndpoint enabled", func() {
412404

413-
// setup
414-
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1 User", false)
405+
// setup
406+
adminEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", true, false)
407+
systemEndpoint := setupMockEndpointRegisterRequest(t, mockAdmin.ConnectedUser, mockV2Info[0], "CF Cluster 1", true, true)
408+
409+
if errSession := pp.setSessionValues(adminEndpoint.EchoContext, mockAdmin.SessionValues); errSession != nil {
410+
t.Error(errors.New("unable to mock/stub user in session object"))
411+
}
415412

413+
Convey("register personal endpoint over system endpoint", func() {
416414
// mock executions
417415
mockStratosAuth.
418416
EXPECT().
419417
GetUser(gomock.Eq(mockAdmin.ConnectedUser.GUID)).
420418
Return(mockAdmin.ConnectedUser, nil)
421419

422-
// return a user endpoint with same apiurl
423-
rows := sqlmock.NewRows(rowFieldsForCNSI).AddRow(userEndpoint.QueryArgs...)
420+
// return a admin endpoint with same apiurl
421+
rows := sqlmock.NewRows(rowFieldsForCNSI).AddRow(systemEndpoint.QueryArgs...)
424422
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
425423

426-
// user endpoints should be deleted
427-
mock.ExpectExec(deleteFromCNSIs).
428-
WithArgs(userEndpoint.QueryArgs[0]).
429-
WillReturnResult(sqlmock.NewResult(1, 1))
430-
431-
// a new admin endpoint with same url will be registered
424+
// save cnsi
432425
mock.ExpectExec(insertIntoCNSIs).
433426
WithArgs(adminEndpoint.InsertArgs...).
434427
WillReturnResult(sqlmock.NewResult(1, 1))
@@ -441,6 +434,33 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
441434
So(err, ShouldBeNil)
442435
})
443436

437+
Convey("there should be no db error", func() {
438+
So(dberr, ShouldBeNil)
439+
})
440+
})
441+
Convey("register personal endpoint twice", func() {
442+
// mock executions
443+
mockStratosAuth.
444+
EXPECT().
445+
GetUser(gomock.Eq(mockAdmin.ConnectedUser.GUID)).
446+
Return(mockAdmin.ConnectedUser, nil)
447+
448+
// return a user endpoint with same apiurl
449+
rows := sqlmock.NewRows(rowFieldsForCNSI).AddRow(adminEndpoint.QueryArgs...)
450+
mock.ExpectQuery(selectAnyFromCNSIs).WithArgs(mockV2Info[0].URL).WillReturnRows(rows)
451+
452+
// test
453+
err := pp.RegisterEndpoint(adminEndpoint.EchoContext, getCFPlugin(pp, "cf").Info)
454+
dberr := mock.ExpectationsWereMet()
455+
456+
Convey("there should be no error", func() {
457+
So(err, ShouldResemble, interfaces.NewHTTPShadowError(
458+
http.StatusBadRequest,
459+
"Can not register same endpoint multiple times",
460+
"Can not register same endpoint multiple times",
461+
))
462+
})
463+
444464
Convey("there should be no db error", func() {
445465
So(dberr, ShouldBeNil)
446466
})
@@ -449,9 +469,9 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
449469
})
450470

451471
Convey("as user", func() {
452-
Convey("with overwrite disabled", func() {
472+
Convey("with createUserEndpoint disabled", func() {
453473
// setup
454-
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", false)
474+
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", false, false)
455475

456476
if errSession := pp.setSessionValues(userEndpoint.EchoContext, mockUser1.SessionValues); errSession != nil {
457477
t.Error(errors.New("unable to mock/stub user in session object"))
@@ -483,7 +503,7 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
483503
})
484504
})
485505
Convey("register existing endpoint from different user", func() {
486-
userEndpoint2 := setupMockEndpointRegisterRequest(t, mockUser2.ConnectedUser, mockV2Info[0], "CF Cluster 2", false)
506+
userEndpoint2 := setupMockEndpointRegisterRequest(t, mockUser2.ConnectedUser, mockV2Info[0], "CF Cluster 2", false, false)
487507

488508
// mock executions
489509
mockStratosAuth.
@@ -535,13 +555,13 @@ func TestRegisterWithUserEndpointsEnabled(t *testing.T) {
535555
})
536556
})
537557
})
538-
Convey("with overwrite enabled", func() {
539-
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", false)
558+
Convey("with createUserEndpoint enabled", func() {
559+
userEndpoint := setupMockEndpointRegisterRequest(t, mockUser1.ConnectedUser, mockV2Info[0], "CF Cluster 1", true, false)
540560

541561
if errSession := pp.setSessionValues(userEndpoint.EchoContext, mockUser1.SessionValues); errSession != nil {
542562
t.Error(errors.New("unable to mock/stub user in session object"))
543563
}
544-
Convey("overwrite existing endpoints from same user, with overwrite enabled", func() {
564+
Convey("register existing endpoint from same user", func() {
545565
// mock executions
546566
mockStratosAuth.
547567
EXPECT().

0 commit comments

Comments
 (0)