11from pathlib import Path
2- from typing import Any
32from typing import cast
43from unittest import mock
54
109
1110from statbank .auth import StatbankAuth
1211from statbank .auth import StatbankConfig
12+ from statbank .auth import TokenAuth
1313from statbank .auth import UseDb
1414from statbank .globals import DaplaEnvironment
1515from statbank .globals import DaplaRegion
1616from statbank .writable_netrc import Netrc
1717
1818
19- # Mock for os.environ.get
20- @pytest .fixture
21- def mock_environ_test ():
22- with mock .patch .dict (
23- "os.environ" ,
24- {
25- "DAPLA_ENVIRONMENT" : "TEST" ,
26- "DAPLA_SERVICE" : "JUPYTERLAB" ,
27- "DAPLA_REGION" : "ON_PREM" ,
28- "STATBANK_ENCRYPT_URL" : "https://fakeurl.com/encrypt" ,
29- "STATBANK_BASE_URL" : "https://fakeurl.com/" ,
30- },
31- ):
32- yield
33-
34-
35- @pytest .fixture
36- def mock_environ_prod_dapla ():
37- with mock .patch .dict (
38- "os.environ" ,
39- {
40- "DAPLA_ENVIRONMENT" : "PROD" ,
41- "DAPLA_SERVICE" : "JUPYTERLAB" ,
42- "DAPLA_REGION" : "ON_PREM" ,
43- "STATBANK_ENCRYPT_URL" : "https://fakeurl.com/encrypt" ,
44- "STATBANK_TEST_ENCRYPT_URL" : "https://test.fakeurl.com/encrypt" ,
45- "STATBANK_BASE_URL" : "https://fakeurl.com/" ,
46- "STATBANK_TEST_BASE_URL" : "https://test.fakeurl.com/" ,
47- },
48- ):
49- yield
50-
51-
52- # Mock for getpass.getpass
5319@pytest .fixture
5420def patch_getpass (monkeypatch : pytest .MonkeyPatch ):
5521 def getpass_return (prompt : str ) -> str :
@@ -63,56 +29,25 @@ def getpass_return(prompt: str) -> str:
6329
6430
6531@pytest .fixture
66- def fake_encrypt_response (monkeypatch : pytest .MonkeyPatch , fake_auth : str ) -> None :
67- def fake_request (* _ : Any , ** __ : Any ):
68- mock_response = mock .Mock (spec_set = Response )
69- mock_response .json .return_value = {"message" : fake_auth }
70- return mock_response
71-
72- monkeypatch .setattr ("requests.post" , fake_request )
73-
74-
75- @pytest .mark .usefixtures ("mock_environ_test" )
76- def test_build_urls (
77- auth_fixture : requests .auth .AuthBase ,
78- ) -> None :
79- # Instantiate the class
80- statbank_auth = StatbankAuth (use_db = UseDb .PROD , auth = auth_fixture )
32+ def fake_encrypt_response (monkeypatch : pytest .MonkeyPatch , fake_auth : str ) -> mock .Mock :
33+ mock_response = mock .Mock (spec_set = Response )
34+ mock_response .json .return_value = {"message" : fake_auth }
8135
82- # Call the _build_urls method
83- urls = statbank_auth . _build_urls () # noqa: SLF001
36+ mock_post = mock . Mock ( return_value = mock_response )
37+ monkeypatch . setattr ( "requests.post" , mock_post )
8438
85- # Verify the expected URLs
86- expected_urls = {
87- "loader" : furl ("https://fakeurl.com/statbank/sos/v1/DataLoader" ),
88- "uttak" : furl ("https://fakeurl.com/statbank/sos/v1/uttaksbeskrivelse" ),
89- "gui" : furl ("https://fakeurl.com/lastelogg/gui" ),
90- "api" : furl ("https://fakeurl.com/lastelogg/api" ),
91- }
92- assert urls == expected_urls
39+ return mock_post
9340
9441
95- @pytest .mark .usefixtures ("mock_environ_prod_dapla" )
96- def test_build_urls_testdb_from_prod (
97- auth_fixture : requests .auth .AuthBase ,
98- ) -> None :
99- # Instantiate the class
100- statbank_auth = StatbankAuth (use_db = UseDb .TEST , auth = auth_fixture )
101-
102- # Call the _build_urls method
103- urls = statbank_auth ._build_urls () # noqa: SLF001
104-
105- # Verify the expected URLs
106- expected_urls = {
107- "loader" : furl ("https://test.fakeurl.com/statbank/sos/v1/DataLoader" ),
108- "uttak" : furl ("https://test.fakeurl.com/statbank/sos/v1/uttaksbeskrivelse" ),
109- "gui" : furl ("https://test.fakeurl.com/lastelogg/gui" ),
110- "api" : furl ("https://test.fakeurl.com/lastelogg/api" ),
111- }
112- assert urls == expected_urls
42+ @pytest .fixture
43+ def patch_dapla_auth (monkeypatch : pytest .MonkeyPatch ) -> None :
44+ monkeypatch .setattr (
45+ "dapla_auth_client.AuthClient.fetch_personal_token" ,
46+ lambda : "token" ,
47+ )
11348
11449
115- @pytest .mark .usefixtures ("mock_environ_prod_dapla " )
50+ @pytest .mark .usefixtures ("mock_environ_on_prem_prod " )
11651def test_check_databases_from_prod (
11752 auth_fixture : requests .auth .AuthBase ,
11853) -> None :
@@ -122,8 +57,12 @@ def test_check_databases_from_prod(
12257 assert statbank_auth .check_database () == "TEST"
12358
12459
125- @pytest .mark .usefixtures ("fake_encrypt_response" , "patch_getpass" )
126- def test_auth_without_authfile (empty_netrc_file : Path , fake_auth : str ):
60+ @pytest .mark .usefixtures ("patch_getpass" , "patch_dapla_auth" )
61+ def test_auth_without_authfile (
62+ empty_netrc_file : Path ,
63+ fake_auth : str ,
64+ fake_encrypt_response : mock .Mock ,
65+ ):
12766 config = StatbankConfig (
12867 environment = DaplaEnvironment .PROD ,
12968 region = DaplaRegion .ON_PREM ,
@@ -134,14 +73,53 @@ def test_auth_without_authfile(empty_netrc_file: Path, fake_auth: str):
13473 )
13574
13675 statbankauth = StatbankAuth (config = config )
76+
77+ fake_encrypt_response .assert_called_once_with (
78+ config .encrypt_url .url ,
79+ json = {"message" : "qwerty" },
80+ auth = None ,
81+ timeout = mock .ANY ,
82+ )
83+
84+ auth = statbankauth ._auth # noqa: SLF001
85+
86+ assert isinstance (auth , requests .auth .HTTPBasicAuth )
87+ assert auth .username == "kari"
88+ assert auth .password == fake_auth
89+
90+
91+ @pytest .mark .usefixtures ("patch_getpass" , "patch_dapla_auth" )
92+ def test_auth_without_authfile_dapla_lab (
93+ empty_netrc_file : Path ,
94+ fake_auth : str ,
95+ fake_encrypt_response : mock .Mock ,
96+ ):
97+ config = StatbankConfig (
98+ environment = DaplaEnvironment .PROD ,
99+ region = DaplaRegion .DAPLA_LAB ,
100+ endpoint_base = furl ("https://fakeurl.com" ),
101+ encrypt_url = furl ("https://fakeurl.com/encrypt" ),
102+ useragent = "statbank-test" ,
103+ netrc_path = empty_netrc_file ,
104+ )
105+
106+ statbankauth = StatbankAuth (config = config )
107+
108+ fake_encrypt_response .assert_called_once_with (
109+ config .encrypt_url .url ,
110+ json = {"message" : "qwerty" },
111+ auth = TokenAuth ("token" ),
112+ timeout = mock .ANY ,
113+ )
114+
137115 auth = statbankauth ._auth # noqa: SLF001
138116
139117 assert isinstance (auth , requests .auth .HTTPBasicAuth )
140118 assert auth .username == "kari"
141119 assert auth .password == fake_auth
142120
143121
144- @pytest .mark .usefixtures ("fake_encrypt_response" , "patch_getpass" )
122+ @pytest .mark .usefixtures ("fake_encrypt_response" , "patch_getpass" , "patch_dapla_auth" )
145123def test_auth_persisted (empty_netrc_file : Path , fake_auth : str ):
146124 config = StatbankConfig (
147125 environment = DaplaEnvironment .PROD ,
@@ -161,7 +139,11 @@ def test_auth_persisted(empty_netrc_file: Path, fake_auth: str):
161139
162140
163141@pytest .mark .usefixtures ("patch_getpass" )
164- def test_read_auth_from_authfile (existing_netrc_file : Path , fake_auth : str ):
142+ def test_read_auth_from_authfile (
143+ existing_netrc_file : Path ,
144+ fake_auth : str ,
145+ fake_encrypt_response : mock .Mock ,
146+ ):
165147 config = StatbankConfig (
166148 environment = DaplaEnvironment .PROD ,
167149 region = DaplaRegion .ON_PREM ,
@@ -172,6 +154,9 @@ def test_read_auth_from_authfile(existing_netrc_file: Path, fake_auth: str):
172154 )
173155
174156 statbankauth = StatbankAuth (use_db = UseDb .PROD , config = config )
157+
158+ fake_encrypt_response .assert_not_called ()
159+
175160 auth = statbankauth ._auth # noqa: SLF001
176161
177162 assert isinstance (auth , requests .auth .HTTPBasicAuth )
0 commit comments