2020
2121from unittest import mock
2222
23+ from restalchemy .api import constants
2324from restalchemy .common import contexts
2425
2526from gcl_iam import controllers
@@ -41,8 +42,7 @@ def __init__(self, *args, **kwargs):
4142 super ().__init__ (* args , ** kwargs )
4243
4344
44- @pytest .fixture
45- def user_context ():
45+ def ctx_storage_context (** kwargs ):
4646 ctx_storage = mock .Mock ()
4747 contexts .ContextWithStorage ._store_context_session (ctx_storage )
4848 ctx_storage .iam_context .introspection_info .return_value = {
@@ -56,31 +56,45 @@ def user_context():
5656 "genesis_core.vm.admin" ,
5757 ],
5858 }
59-
60- yield ctx_storage .iam_context .introspection_info
61-
62- contexts .ContextWithStorage ._clear_context ()
59+ ctx_storage .iam_context .introspection_info .return_value .update (kwargs )
60+ return ctx_storage .iam_context .introspection_info
6361
6462
6563@pytest .fixture
6664def unscoped_context ():
67- ctx_storage = mock .Mock ()
68- contexts .ContextWithStorage ._store_context_session (ctx_storage )
69- ctx_storage .iam_context .introspection_info .return_value = {
70- "user_info" : {},
71- "project_id" : None ,
72- "otp_verified" : True ,
73- "permission_hash" : "xxxx" ,
74- "permissions" : [
65+ yield ctx_storage_context (
66+ project_id = None ,
67+ introspection_infopermissions = [
7568 "service.resource.action" ,
7669 "genesis_core.vm.create" ,
7770 "genesis_core.vm.admin" ,
7871 "*.*.*" ,
7972 ],
80- }
73+ )
74+ contexts .ContextWithStorage ._clear_context ()
75+
76+
77+ @pytest .fixture
78+ def user_context ():
79+ yield ctx_storage_context (project_id = FAKE_PROJECT_ID )
80+ contexts .ContextWithStorage ._clear_context ()
8181
82- yield ctx_storage .iam_context .introspection_info
8382
83+ @pytest .fixture
84+ def otp_enabled_context ():
85+ yield ctx_storage_context (otp_enabled = True )
86+ contexts .ContextWithStorage ._clear_context ()
87+
88+
89+ @pytest .fixture
90+ def otp_not_verified_context ():
91+ yield ctx_storage_context (otp_enabled = True , otp_verified = False )
92+ contexts .ContextWithStorage ._clear_context ()
93+
94+
95+ @pytest .fixture
96+ def otp_not_enabled_context ():
97+ yield ctx_storage_context (otp_verified = False )
8498 contexts .ContextWithStorage ._clear_context ()
8599
86100
@@ -164,3 +178,24 @@ def test_override_project_id_from_ctx(self, user_context):
164178 pc ._enforce_and_override_project_id_in_kwargs ("create" , kwargs )
165179
166180 assert kwargs == {"project_id" : FAKE_PROJECT_ID }
181+
182+
183+ class TestPolicyBasedCheckOtpController :
184+
185+ def test_check_otp_verified_true (self , otp_enabled_context ):
186+ pc = controllers .PolicyBasedCheckOtpController (request = mock .Mock ())
187+ pc ._check_otp (constants .GET )
188+
189+ def test_check_otp_verified_false (self , otp_not_verified_context ):
190+ pc = controllers .PolicyBasedCheckOtpController (request = mock .Mock ())
191+ with pytest .raises (exceptions .OTPInvalidCodeError ):
192+ pc ._check_otp (constants .GET )
193+
194+ def test_check_otp_mandatory_on (self , otp_not_enabled_context ):
195+ pc = controllers .PolicyBasedCheckOtpController (request = mock .Mock ())
196+ with pytest .raises (exceptions .OTPInvalidCodeError ):
197+ pc ._check_otp (constants .CREATE )
198+
199+ def test_check_otp_mandatory_off (self , otp_not_enabled_context ):
200+ pc = controllers .PolicyBasedCheckOtpController (request = mock .Mock ())
201+ pc ._check_otp (constants .FILTER )
0 commit comments