@@ -54,10 +54,11 @@ var cohortDataController = controllers.NewCohortDataController(*new(dummyCohortD
54
54
var cohortDataControllerWithFailingTeamProjectAuthz = controllers .NewCohortDataController (* new (dummyCohortDataModel ), * new (dummyFailingTeamProjectAuthz ))
55
55
56
56
// instance of the controller that talks to the regular model implementation (that needs a real DB):
57
- var cohortDefinitionControllerNeedsDb = controllers .NewCohortDefinitionController (* new (models.CohortDefinition ))
57
+ var cohortDefinitionControllerNeedsDb = controllers .NewCohortDefinitionController (* new (models.CohortDefinition ), * new ( dummyTeamProjectAuthz ) )
58
58
59
59
// instance of the controller that talks to a mock implementation of the model:
60
- var cohortDefinitionController = controllers .NewCohortDefinitionController (* new (dummyCohortDefinitionDataModel ))
60
+ var cohortDefinitionController = controllers .NewCohortDefinitionController (* new (dummyCohortDefinitionDataModel ), * new (dummyTeamProjectAuthz ))
61
+ var cohortDefinitionControllerWithFailingTeamProjectAuthz = controllers .NewCohortDefinitionController (* new (dummyCohortDefinitionDataModel ), * new (dummyFailingTeamProjectAuthz ))
61
62
62
63
type dummyCohortDataModel struct {}
63
64
@@ -151,6 +152,10 @@ func (h dummyTeamProjectAuthz) TeamProjectValidationForCohortIdsList(ctx *gin.Co
151
152
return true
152
153
}
153
154
155
+ func (h dummyTeamProjectAuthz ) HasAccessToTeamProject (ctx * gin.Context , teamProject string ) bool {
156
+ return true
157
+ }
158
+
154
159
type dummyFailingTeamProjectAuthz struct {}
155
160
156
161
func (h dummyFailingTeamProjectAuthz ) TeamProjectValidationForCohort (ctx * gin.Context , cohortDefinitionId int ) bool {
@@ -165,6 +170,10 @@ func (h dummyFailingTeamProjectAuthz) TeamProjectValidationForCohortIdsList(ctx
165
170
return false
166
171
}
167
172
173
+ func (h dummyFailingTeamProjectAuthz ) HasAccessToTeamProject (ctx * gin.Context , teamProject string ) bool {
174
+ return false
175
+ }
176
+
168
177
var conceptController = controllers .NewConceptController (* new (dummyConceptDataModel ), * new (dummyCohortDefinitionDataModel ), * new (dummyTeamProjectAuthz ))
169
178
var conceptControllerWithFailingTeamProjectAuthz = controllers .NewConceptController (* new (dummyConceptDataModel ), * new (dummyCohortDefinitionDataModel ), * new (dummyFailingTeamProjectAuthz ))
170
179
@@ -463,18 +472,37 @@ func TestRetriveStatsBySourceIdAndTeamProjectCheckMandatoryTeamProject(t *testin
463
472
}
464
473
}
465
474
475
+ func TestRetriveStatsBySourceIdAndTeamProjectAuthorizationError (t * testing.T ) {
476
+ setUp (t )
477
+ requestContext := new (gin.Context )
478
+ requestContext .Params = append (requestContext .Params , gin.Param {Key : "sourceid" , Value : strconv .Itoa (tests .GetTestSourceId ())})
479
+ requestContext .Request = & http.Request {URL : & url.URL {}}
480
+ teamProject := "/test/dummyname/dummy-team-project"
481
+ requestContext .Request .URL .RawQuery = "team-project=" + teamProject
482
+ requestContext .Writer = new (tests.CustomResponseWriter )
483
+ cohortDefinitionControllerWithFailingTeamProjectAuthz .RetriveStatsBySourceIdAndTeamProject (requestContext )
484
+ result := requestContext .Writer .(* tests.CustomResponseWriter )
485
+ if ! requestContext .IsAborted () {
486
+ t .Errorf ("Expected aborted request" )
487
+ }
488
+ if result .Status () != http .StatusForbidden {
489
+ t .Errorf ("Expected StatusForbidden, got %d" , result .Status ())
490
+ }
491
+ if ! strings .Contains (result .CustomResponseWriterOut , "access denied" ) {
492
+ t .Errorf ("Expected 'access denied' in response" )
493
+ }
494
+ }
495
+
466
496
func TestRetriveStatsBySourceIdAndTeamProject (t * testing.T ) {
467
497
setUp (t )
468
498
requestContext := new (gin.Context )
469
499
requestContext .Params = append (requestContext .Params , gin.Param {Key : "sourceid" , Value : strconv .Itoa (tests .GetTestSourceId ())})
470
- //requestContext.Params = append(requestContext.Params, gin.Param{Key: "teamproject", Value: "dummy-team-project"})
471
500
requestContext .Request = & http.Request {URL : & url.URL {}}
472
501
teamProject := "/test/dummyname/dummy-team-project"
473
502
requestContext .Request .URL .RawQuery = "team-project=" + teamProject
474
503
requestContext .Writer = new (tests.CustomResponseWriter )
475
504
cohortDefinitionController .RetriveStatsBySourceIdAndTeamProject (requestContext )
476
505
result := requestContext .Writer .(* tests.CustomResponseWriter )
477
- log .Printf ("result: %s" , result )
478
506
// expect result with all of the dummy data:
479
507
if ! strings .Contains (result .CustomResponseWriterOut , "name1_" + teamProject ) ||
480
508
! strings .Contains (result .CustomResponseWriterOut , "name2_" + teamProject ) ||
@@ -502,7 +530,6 @@ func TestRetriveById(t *testing.T) {
502
530
requestContext .Writer = new (tests.CustomResponseWriter )
503
531
cohortDefinitionController .RetriveById (requestContext )
504
532
result := requestContext .Writer .(* tests.CustomResponseWriter )
505
- log .Printf ("result: %s" , result )
506
533
// expect result with dummy data:
507
534
if ! strings .Contains (result .CustomResponseWriterOut , "test 1" ) {
508
535
t .Errorf ("Expected data in result" )
@@ -522,6 +549,26 @@ func TestRetriveByIdModelError(t *testing.T) {
522
549
}
523
550
}
524
551
552
+ func TestRetriveByIdAuthorizationError (t * testing.T ) {
553
+ setUp (t )
554
+ requestContext := new (gin.Context )
555
+ requestContext .Params = append (requestContext .Params , gin.Param {Key : "id" , Value : "1" })
556
+ requestContext .Writer = new (tests.CustomResponseWriter )
557
+ cohortDefinitionControllerWithFailingTeamProjectAuthz .RetriveById (requestContext )
558
+ result := requestContext .Writer .(* tests.CustomResponseWriter )
559
+ if ! requestContext .IsAborted () {
560
+ t .Errorf ("Expected aborted request" )
561
+ }
562
+ if result .Status () != http .StatusForbidden {
563
+ t .Errorf ("Expected StatusForbidden, got %d" , result .Status ())
564
+ }
565
+ // expect result with dummy data:
566
+ if ! strings .Contains (result .CustomResponseWriterOut , "access denied" ) {
567
+ t .Errorf ("Expected 'access denied' in response" )
568
+ }
569
+
570
+ }
571
+
525
572
func TestRetrieveBreakdownStatsBySourceIdAndCohortId (t * testing.T ) {
526
573
setUp (t )
527
574
requestContext := new (gin.Context )
@@ -532,7 +579,6 @@ func TestRetrieveBreakdownStatsBySourceIdAndCohortId(t *testing.T) {
532
579
requestContext .Writer = new (tests.CustomResponseWriter )
533
580
conceptController .RetrieveBreakdownStatsBySourceIdAndCohortId (requestContext )
534
581
result := requestContext .Writer .(* tests.CustomResponseWriter )
535
- log .Printf ("result: %s" , result )
536
582
// expect result with dummy data:
537
583
if ! strings .Contains (result .CustomResponseWriterOut , "persons_in_cohort_with_value" ) {
538
584
t .Errorf ("Expected data in result" )
@@ -563,7 +609,6 @@ func TestRetrieveBreakdownStatsBySourceIdAndCohortIdAndVariables(t *testing.T) {
563
609
requestContext .Writer = new (tests.CustomResponseWriter )
564
610
conceptController .RetrieveBreakdownStatsBySourceIdAndCohortIdAndVariables (requestContext )
565
611
result := requestContext .Writer .(* tests.CustomResponseWriter )
566
- log .Printf ("result: %s" , result )
567
612
// expect result with dummy data:
568
613
if ! strings .Contains (result .CustomResponseWriterOut , "persons_in_cohort_with_value" ) {
569
614
t .Errorf ("Expected data in result" )
@@ -608,7 +653,6 @@ func TestRetrieveInfoBySourceIdAndConceptIds(t *testing.T) {
608
653
requestContext .Writer = new (tests.CustomResponseWriter )
609
654
conceptController .RetrieveInfoBySourceIdAndConceptIds (requestContext )
610
655
result := requestContext .Writer .(* tests.CustomResponseWriter )
611
- log .Printf ("result: %s" , result )
612
656
// expect result with dummy data:
613
657
if ! strings .Contains (result .CustomResponseWriterOut , "Concept A" ) ||
614
658
! strings .Contains (result .CustomResponseWriterOut , "Concept B" ) {
@@ -625,7 +669,6 @@ func TestRetrieveInfoBySourceIdAndConceptTypes(t *testing.T) {
625
669
requestContext .Writer = new (tests.CustomResponseWriter )
626
670
conceptController .RetrieveInfoBySourceIdAndConceptTypes (requestContext )
627
671
result := requestContext .Writer .(* tests.CustomResponseWriter )
628
- log .Printf ("result: %s" , result )
629
672
// expect result with dummy data:
630
673
if ! strings .Contains (result .CustomResponseWriterOut , "Concept A" ) ||
631
674
! strings .Contains (result .CustomResponseWriterOut , "Concept B" ) {
@@ -644,7 +687,6 @@ func TestRetrieveInfoBySourceIdAndConceptTypesModelError(t *testing.T) {
644
687
dummyModelReturnError = true
645
688
conceptController .RetrieveInfoBySourceIdAndConceptTypes (requestContext )
646
689
result := requestContext .Writer .(* tests.CustomResponseWriter )
647
- log .Printf ("result: %s" , result )
648
690
if ! requestContext .IsAborted () {
649
691
t .Errorf ("Expected aborted request" )
650
692
}
@@ -662,7 +704,6 @@ func TestRetrieveInfoBySourceIdAndConceptTypesArgsError(t *testing.T) {
662
704
dummyModelReturnError = true
663
705
conceptController .RetrieveInfoBySourceIdAndConceptTypes (requestContext )
664
706
result := requestContext .Writer .(* tests.CustomResponseWriter )
665
- log .Printf ("result: %s" , result )
666
707
if ! requestContext .IsAborted () {
667
708
t .Errorf ("Expected aborted request" )
668
709
}
@@ -680,7 +721,6 @@ func TestRetrieveInfoBySourceIdAndConceptTypesMissingBody(t *testing.T) {
680
721
dummyModelReturnError = true
681
722
conceptController .RetrieveInfoBySourceIdAndConceptTypes (requestContext )
682
723
result := requestContext .Writer .(* tests.CustomResponseWriter )
683
- log .Printf ("result: %s" , result )
684
724
if ! requestContext .IsAborted () {
685
725
t .Errorf ("Expected aborted request" )
686
726
}
@@ -982,7 +1022,6 @@ func TestRetrieveAttritionTable(t *testing.T) {
982
1022
requestContext .Writer = new (tests.CustomResponseWriter )
983
1023
conceptController .RetrieveAttritionTable (requestContext )
984
1024
result := requestContext .Writer .(* tests.CustomResponseWriter )
985
- log .Printf ("result: %s" , result .CustomResponseWriterOut )
986
1025
// check result vs expect result:
987
1026
csvLines := strings .Split (strings .TrimRight (result .CustomResponseWriterOut , "\n " ), "\n " )
988
1027
expectedLines := []string {
0 commit comments