22
22
"variables" : variables ,
23
23
"hare_population" : "hare" ,
24
24
"out_prefix" : "vadc_genesis" ,
25
- "outcome" : 1 ,
25
+ "outcome" : {
26
+ "variable_type" : "custom_dichotomous" ,
27
+ "cohort_ids" : [2 ],
28
+ "provided_name" : "test Pheno" ,
29
+ },
26
30
"maf_threshold" : 0.01 ,
27
31
"imputation_score_cutoff" : 0.3 ,
28
32
"template_version" : "gwas-template-latest" ,
29
33
"source_id" : 4 ,
30
34
"case_cohort_definition_id" : 70 ,
31
35
"control_cohort_definition_id" : - 1 ,
36
+ "source_population_cohort" : 4 ,
32
37
"workflow_name" : "wf_name" ,
33
38
TEAM_PROJECT_FIELD_NAME : "dummy-team-project" ,
39
+ "user_tags" : None , # For testing purpose
40
+ }
41
+
42
+ cohort_definition_data = {
43
+ "cohort_definitions_and_stats" : [
44
+ {"cohort_definition_id" : 1 , "cohort_name" : "Cohort 1" , "size" : 1 },
45
+ {"cohort_definition_id" : 2 , "cohort_name" : "Cohort 2" , "size" : 2 },
46
+ {"cohort_definition_id" : 3 , "cohort_name" : "Cohort 3" , "size" : 3 },
47
+ {"cohort_definition_id" : 4 , "cohort_name" : "Cohort 4" , "size" : 4 },
48
+ ]
34
49
}
35
50
36
51
@@ -55,17 +70,50 @@ def client(app: FastAPI) -> Generator[TestClient, Any, None]:
55
70
yield client
56
71
57
72
73
+ def mocked_requests_get (* args , ** kwargs ):
74
+ class MockResponse :
75
+ def __init__ (self , json_data , status_code ):
76
+ self .json_data = json_data
77
+ self .status_code = status_code
78
+
79
+ def json (self ):
80
+ return self .json_data
81
+
82
+ def raise_for_status (self ):
83
+ if self .status_code == 500 :
84
+ raise Exception ("fence is down" )
85
+ if self .status_code != 200 :
86
+ raise Exception ()
87
+
88
+ if (
89
+ kwargs ["url" ]
90
+ == "http://cohort-middleware-service/cohortdefinition-stats/by-source-id/4/by-team-project?team-project=dummy-team-project"
91
+ ):
92
+ return MockResponse (cohort_definition_data , 200 )
93
+
94
+ if kwargs ["url" ] == "http://fence-service/user" :
95
+ if data ["user_tags" ] != 500 :
96
+ return MockResponse (data ["user_tags" ], 200 )
97
+ else :
98
+ return MockResponse ({}, 500 )
99
+
100
+ return None
101
+
102
+
58
103
def test_submit_workflow (client ):
59
104
with patch ("argowrapper.routes.routes.auth.authenticate" ) as mock_auth , patch (
60
105
"argowrapper.routes.routes.argo_engine.workflow_submission"
61
106
) as mock_engine , patch (
62
107
"argowrapper.routes.routes.log_auth_check_type"
63
108
) as mock_log , patch (
64
109
"argowrapper.routes.routes.check_user_billing_id"
65
- ) as mock_check_billing_id :
110
+ ) as mock_check_billing_id , patch (
111
+ "requests.get"
112
+ ) as mock_requests :
66
113
mock_auth .return_value = True
67
114
mock_engine .return_value = "workflow_123"
68
115
mock_check_billing_id .return_value = None
116
+ mock_requests .side_effect = mocked_requests_get
69
117
70
118
response = client .post (
71
119
"/submit" ,
@@ -466,59 +514,44 @@ def test_submit_workflow_with_user_billing_id(client):
466
514
"argowrapper.routes.routes.argo_engine.workflow_submission"
467
515
) as mock_engine , patch (
468
516
"argowrapper.routes.routes.log_auth_check_type"
469
- ) as mock_log :
517
+ ) as mock_log , patch (
518
+ "requests.get"
519
+ ) as mock_requests :
470
520
mock_auth .return_value = True
471
521
mock_engine .return_value = "workflow_123"
472
- with patch ("requests.get" ) as mock_request :
473
- mock_resp = mock .Mock ()
474
- mock_resp .status_code = 200
475
- mock_resp .raise_for_status = mock .Mock ()
476
- mock_resp .json = mock .Mock (return_value = {"tags" : {}})
477
- mock_request .return_value = mock_resp
522
+ mock_requests .side_effect = mocked_requests_get
478
523
479
- response = client .post (
480
- "/submit" ,
481
- data = json .dumps (data ),
482
- headers = {
483
- "Content-Type" : "application/json" ,
484
- "Authorization" : EXAMPLE_AUTH_HEADER ,
485
- },
486
- )
487
- assert response .status_code == 200
488
- assert mock_engine .call_args .args [2 ] == None
524
+ data ["user_tags" ] = {"tags" : {}}
525
+ response = client .post (
526
+ "/submit" ,
527
+ data = json .dumps (data ),
528
+ headers = {
529
+ "Content-Type" : "application/json" ,
530
+ "Authorization" : EXAMPLE_AUTH_HEADER ,
531
+ },
532
+ )
533
+ assert response .status_code == 200
534
+ assert mock_engine .call_args .args [2 ] == None
489
535
490
- mock_resp . json = mock . Mock ( return_value = {"tags" : {"othertag1" : "tag1" }})
536
+ data [ "user_tags" ] = {"tags" : {"othertag1" : "tag1" }}
491
537
492
- response = client .post (
493
- "/submit" ,
494
- data = json .dumps (data ),
495
- headers = {
496
- "Content-Type" : "application/json" ,
497
- "Authorization" : EXAMPLE_AUTH_HEADER ,
498
- },
499
- )
500
- assert response .status_code == 200
501
- assert mock_engine .call_args .args [2 ] == None
538
+ response = client .post (
539
+ "/submit" ,
540
+ data = json .dumps (data ),
541
+ headers = {
542
+ "Content-Type" : "application/json" ,
543
+ "Authorization" : EXAMPLE_AUTH_HEADER ,
544
+ },
545
+ )
546
+ assert response .status_code == 200
547
+ assert mock_engine .call_args .args [2 ] == None
502
548
503
- mock_resp .json = mock .Mock (
504
- return_value = {"tags" : {"othertag1" : "tag1" , "billing_id" : "1234" }}
505
- )
506
- with patch (
507
- "argowrapper.routes.routes.check_user_reached_monthly_workflow_cap"
508
- ) as mock_check_monthly_cap :
509
- mock_check_monthly_cap .return_value = False
510
- response = client .post (
511
- "/submit" ,
512
- data = json .dumps (data ),
513
- headers = {
514
- "Content-Type" : "application/json" ,
515
- "Authorization" : EXAMPLE_AUTH_HEADER ,
516
- },
517
- )
518
- assert mock_engine .call_args .args [2 ] == "1234"
519
-
520
- mock_resp .status_code == 500
521
- mock_resp .raise_for_status .side_effect = Exception ("fence is down" )
549
+ data ["user_tags" ] = {"tags" : {"othertag1" : "tag1" , "billing_id" : "1234" }}
550
+
551
+ with patch (
552
+ "argowrapper.routes.routes.check_user_reached_monthly_workflow_cap"
553
+ ) as mock_check_monthly_cap :
554
+ mock_check_monthly_cap .return_value = False
522
555
response = client .post (
523
556
"/submit" ,
524
557
data = json .dumps (data ),
@@ -527,8 +560,19 @@ def test_submit_workflow_with_user_billing_id(client):
527
560
"Authorization" : EXAMPLE_AUTH_HEADER ,
528
561
},
529
562
)
530
- assert response .status_code == 500
531
- assert "fence is down" in str (response .content )
563
+ assert mock_engine .call_args .args [2 ] == "1234"
564
+
565
+ data ["user_tags" ] == 500
566
+
567
+ response = client .post (
568
+ "/submit" ,
569
+ data = json .dumps (data ),
570
+ headers = {
571
+ "Content-Type" : "application/json" ,
572
+ "Authorization" : EXAMPLE_AUTH_HEADER ,
573
+ },
574
+ )
575
+ assert response .status_code == 500
532
576
533
577
534
578
def test_check_user_reached_monthly_workflow_cap ():
@@ -566,11 +610,14 @@ def test_submit_workflow_with_billing_id_and_over_monthly_cap(client):
566
610
"argowrapper.routes.routes.check_user_billing_id"
567
611
) as mock_check_billing_id , patch (
568
612
"argowrapper.routes.routes.check_user_reached_monthly_workflow_cap"
569
- ) as mock_check_monthly_cap :
613
+ ) as mock_check_monthly_cap , patch (
614
+ "requests.get"
615
+ ) as mock_requests :
570
616
mock_auth .return_value = True
571
617
mock_engine .return_value = "workflow_123"
572
618
mock_check_billing_id .return_value = "1234"
573
619
mock_check_monthly_cap .return_value = True
620
+ mock_requests .side_effect = mocked_requests_get
574
621
575
622
response = client .post (
576
623
"/submit" ,
@@ -581,3 +628,31 @@ def test_submit_workflow_with_billing_id_and_over_monthly_cap(client):
581
628
},
582
629
)
583
630
assert response .status_code == 403
631
+
632
+
633
+ def test_submit_workflow_with_non_team_project_cohort (client ):
634
+ with patch ("argowrapper.routes.routes.auth.authenticate" ) as mock_auth , patch (
635
+ "argowrapper.routes.routes.argo_engine.workflow_submission"
636
+ ) as mock_engine , patch (
637
+ "argowrapper.routes.routes.log_auth_check_type"
638
+ ) as mock_log , patch (
639
+ "argowrapper.routes.routes.check_user_billing_id"
640
+ ) as mock_check_billing_id , patch (
641
+ "requests.get"
642
+ ) as mock_requests :
643
+ mock_auth .return_value = True
644
+ mock_engine .return_value = "workflow_123"
645
+ mock_check_billing_id .return_value = None
646
+ mock_requests .side_effect = mocked_requests_get
647
+
648
+ data ["outcome" ]["cohort_ids" ] = [400 ]
649
+
650
+ response = client .post (
651
+ "/submit" ,
652
+ data = json .dumps (data ),
653
+ headers = {
654
+ "Content-Type" : "application/json" ,
655
+ "Authorization" : EXAMPLE_AUTH_HEADER ,
656
+ },
657
+ )
658
+ assert response .status_code == 400
0 commit comments