From b9eb593acd58455ca0d6552182b97cf25ca00e31 Mon Sep 17 00:00:00 2001 From: corubba Date: Thu, 19 May 2022 00:53:05 +0200 Subject: [PATCH] Fix tests Increased the version of pytest to make it work with py 3.10 [0]. The GET calls no longer return list but the object itself, fixed the tests and assertions to account for that. The tests did not account for the later added `allow_user_remove_domain` setting. And there were issues with missing and non-stopped patchers/mocks. Now all tests are at least passing. [0] https://github.com/pytest-dev/pytest/pull/8540 --- requirements.txt | 2 +- tests/fixtures.py | 4 +++ .../integration/api/apikey/test_admin_user.py | 4 +-- tests/integration/api/management/__init__.py | 14 +++++--- .../api/management/test_admin_user.py | 17 ++++++---- tests/integration/api/management/test_user.py | 16 +++++---- tests/unit/zone/test_admin_apikey.py | 30 ++++++++++++++++ tests/unit/zone/test_admin_user.py | 26 ++++++++++++++ tests/unit/zone/test_user.py | 26 ++++++++++++++ tests/unit/zone/test_user_apikey.py | 34 +++++++++++++++++++ 10 files changed, 153 insertions(+), 20 deletions(-) diff --git a/requirements.txt b/requirements.txt index 6415116..6ecea27 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,7 +22,7 @@ Flask-SeaSurf==1.1.1 bravado-core==5.17.0 jsonschema[format]>=2.5.1,<4.0.0 # until https://github.com/Yelp/bravado-core/pull/385 lima==0.5 -pytest==6.1.1 +pytest==6.2.5 pytimeparse==1.1.8 PyYAML==5.4 Flask-SSLify==0.1.5 diff --git a/tests/fixtures.py b/tests/fixtures.py index 39b6a70..3662916 100644 --- a/tests/fixtures.py +++ b/tests/fixtures.py @@ -36,6 +36,8 @@ def load_data(setting_name, *args, **kwargs): return 10 if setting_name == 'allow_user_create_domain': return True + if setting_name == 'allow_user_remove_domain': + return True @pytest.fixture @@ -131,6 +133,7 @@ def initial_apikey_data(): api_url_setting = Setting('pdns_api_url', pdns_api_url) api_key_setting = Setting('pdns_api_key', os.environ['PDNS_API_KEY']) allow_create_domain_setting = Setting('allow_user_create_domain', True) + allow_remove_domain_setting = Setting('allow_user_remove_domain', True) try: flask_migrate.upgrade() @@ -138,6 +141,7 @@ def initial_apikey_data(): db.session.add(api_url_setting) db.session.add(api_key_setting) db.session.add(allow_create_domain_setting) + db.session.add(allow_remove_domain_setting) test_user_apikey = app.config.get('TEST_USER_APIKEY') test_admin_apikey = app.config.get('TEST_ADMIN_APIKEY') diff --git a/tests/integration/api/apikey/test_admin_user.py b/tests/integration/api/apikey/test_admin_user.py index d8d8f29..c367fe1 100644 --- a/tests/integration/api/apikey/test_admin_user.py +++ b/tests/integration/api/apikey/test_admin_user.py @@ -39,7 +39,7 @@ class TestIntegrationApiApiKeyAdminUser(object): assert res.status_code == 201 apikey_url_format = "/api/v1/pdnsadmin/apikeys/{0}" - apikey_url = apikey_url_format.format(data[0]['id']) + apikey_url = apikey_url_format.format(data['id']) res = client.delete(apikey_url, headers=basic_auth_admin_headers) @@ -123,7 +123,7 @@ class TestIntegrationApiApiKeyAdminUser(object): assert res.status_code == 201 apikey_url_format = "/api/v1/pdnsadmin/apikeys/{0}" - apikey_url = apikey_url_format.format(data[0]['id']) + apikey_url = apikey_url_format.format(data['id']) res = client.delete(apikey_url, headers=basic_auth_admin_headers) assert res.status_code == 204 diff --git a/tests/integration/api/management/__init__.py b/tests/integration/api/management/__init__.py index b546f46..af748be 100644 --- a/tests/integration/api/management/__init__.py +++ b/tests/integration/api/management/__init__.py @@ -14,8 +14,10 @@ class IntegrationApiManagement(object): assert res.status_code == status_code if res.status_code == 200: data = res.get_json(force=True) - assert len(data) == 1 - return data[0] + assert isinstance(data, dict) + assert len(data) == 7 + assert data.get('id', None) + return data return None def check_account(self, cmpdata, data=None): @@ -37,8 +39,10 @@ class IntegrationApiManagement(object): assert res.status_code == status_code if status_code == 200: data = res.get_json(force=True) - assert len(data) == 1 - return data[0] + assert isinstance(data, dict) + assert len(data) == 7 + assert data.get('id', None) + return data return None def check_user(self, cmpdata, data=None): @@ -50,5 +54,5 @@ class IntegrationApiManagement(object): elif key == 'role': assert data[key]['name'] == cmpdata['role_name'] else: - assert key in ("id",) + assert key in ("id","accounts",) return data diff --git a/tests/integration/api/management/test_admin_user.py b/tests/integration/api/management/test_admin_user.py index 3df0384..eae61f8 100644 --- a/tests/integration/api/management/test_admin_user.py +++ b/tests/integration/api/management/test_admin_user.py @@ -89,8 +89,9 @@ class TestIntegrationApiManagementAdminUser(IntegrationApiManagement): ) data = res.get_json(force=True) assert res.status_code == 200 - assert len(data) == 1 - data = data[0] + assert isinstance(data, dict) + assert len(data) == 7 + assert data.get('id', None) account_id = data["id"] for key, value in account_data.items(): assert data[key] == value @@ -142,10 +143,12 @@ class TestIntegrationApiManagementAdminUser(IntegrationApiManagement): ) data = res.get_json(force=True) assert res.status_code == 201 - assert len(data) == 1 + assert isinstance(data, dict) + assert len(data) == 6 + assert data.get('id', None) # Check user - user1 = self.check_user(user1_data, data[0]) + user1 = self.check_user(user1_data, data) user1_id = user1["id"] updated = user1_data.copy() @@ -240,10 +243,12 @@ class TestIntegrationApiManagementAdminUser(IntegrationApiManagement): ) data = res.get_json(force=True) assert res.status_code == 201 - assert len(data) == 1 + assert isinstance(data, dict) + assert len(data) == 6 + assert data.get('id', None) # Check user - user1 = self.check_user(user1_data, data[0]) + user1 = self.check_user(user1_data, data) user1_id = user1["id"] # Assert test account has no users diff --git a/tests/integration/api/management/test_user.py b/tests/integration/api/management/test_user.py index b56652e..31bf69f 100644 --- a/tests/integration/api/management/test_user.py +++ b/tests/integration/api/management/test_user.py @@ -33,8 +33,8 @@ class TestIntegrationApiManagementUser(IntegrationApiManagement): headers=basic_auth_user_headers) data = res.get_json(force=True) assert res.status_code == 200 - assert len(data) == 1, data - self.user = data + assert data + self.user = [data] def test_accounts( self, client, initial_data, # noqa: F811 @@ -118,10 +118,12 @@ class TestIntegrationApiManagementUser(IntegrationApiManagement): ) data = res.get_json(force=True) assert res.status_code == 201 - assert len(data) == 1 + assert isinstance(data, dict) + assert len(data) == 6 + assert data.get('id', None) # Check user - user1 = self.check_user(user1_data, data[0]) + user1 = self.check_user(user1_data, data) user1_id = user1["id"] # Update to defaults (should fail) @@ -181,10 +183,12 @@ class TestIntegrationApiManagementUser(IntegrationApiManagement): ) data = res.get_json(force=True) assert res.status_code == 201 - assert len(data) == 1 + assert isinstance(data, dict) + assert len(data) == 6 + assert data.get('id', None) # Check user - user1 = self.check_user(user1_data, data[0]) + user1 = self.check_user(user1_data, data) user1_id = user1["id"] # Assert test account has no users diff --git a/tests/unit/zone/test_admin_apikey.py b/tests/unit/zone/test_admin_apikey.py index 6f3a9c3..fed2561 100644 --- a/tests/unit/zone/test_admin_apikey.py +++ b/tests/unit/zone/test_admin_apikey.py @@ -27,6 +27,9 @@ class TestUnitApiZoneAdminApiKey(object): self.github_setting_patcher = patch( 'powerdnsadmin.services.github.Setting', spec=powerdnsadmin.models.setting.Setting) + self.azure_setting_patcher = patch( + 'powerdnsadmin.services.azure.Setting', + spec=powerdnsadmin.models.setting.Setting) self.oidc_setting_patcher = patch( 'powerdnsadmin.services.oidc.Setting', spec=powerdnsadmin.models.setting.Setting) @@ -54,6 +57,9 @@ class TestUnitApiZoneAdminApiKey(object): self.mock_setting_patcher = patch( 'powerdnsadmin.routes.api.Setting', spec=powerdnsadmin.models.setting.Setting) + self.mock_decorators_setting_patcher = patch( + 'powerdnsadmin.decorators.Setting', + spec=powerdnsadmin.models.setting.Setting) data = admin_apikey_data() api_key = ApiKey(desc=data['description'], @@ -63,6 +69,7 @@ class TestUnitApiZoneAdminApiKey(object): self.mock_google_setting = self.google_setting_patcher.start() self.mock_github_setting = self.github_setting_patcher.start() + self.mock_azure_setting = self.azure_setting_patcher.start() self.mock_oidc_setting = self.oidc_setting_patcher.start() self.mock_helpers_setting = self.helpers_setting_patcher.start() self.mock_models_setting = self.models_setting_patcher.start() @@ -75,17 +82,40 @@ class TestUnitApiZoneAdminApiKey(object): self.mock_apikey = self.mock_apikey_patcher.start() self.mock_hist = self.mock_hist_patcher.start() self.mock_setting = self.mock_setting_patcher.start() + self.mock_decorators_setting = self.mock_decorators_setting_patcher.start() self.mock_google_setting.return_value.get.side_effect = load_data self.mock_github_setting.return_value.get.side_effect = load_data + self.mock_azure_setting.return_value.get.side_effect = load_data self.mock_oidc_setting.return_value.get.side_effect = load_data self.mock_helpers_setting.return_value.get.side_effect = load_data self.mock_models_setting.return_value.get.side_effect = load_data self.mock_domain_model_setting.return_value.get.side_effect = load_data self.mock_record_model_setting.return_value.get.side_effect = load_data self.mock_server_model_setting.return_value.get.side_effect = load_data + self.mock_decorators_setting.return_value.get.side_effect = load_data self.mock_apikey.return_value.is_validate.return_value = api_key + yield + + for patcher in [ + self.google_setting_patcher, + self.github_setting_patcher, + self.azure_setting_patcher, + self.oidc_setting_patcher, + self.helpers_setting_patcher, + self.models_setting_patcher, + self.domain_model_setting_patcher, + self.record_model_setting_patcher, + self.server_model_setting_patcher, + self.mock_apikey_patcher, + self.mock_hist_patcher, + self.mock_setting_patcher, + self.mock_decorators_setting_patcher, + ]: + patcher.stop() + + def test_empty_get(self, client, common_data_mock, admin_apikey): with patch('powerdnsadmin.routes.api.Domain') as mock_domain, \ patch('powerdnsadmin.lib.utils.requests.get') as mock_get: diff --git a/tests/unit/zone/test_admin_user.py b/tests/unit/zone/test_admin_user.py index 085ba74..7212045 100644 --- a/tests/unit/zone/test_admin_user.py +++ b/tests/unit/zone/test_admin_user.py @@ -22,6 +22,9 @@ class TestUnitApiZoneAdminUser(object): self.github_setting_patcher = patch( 'powerdnsadmin.services.github.Setting', spec=powerdnsadmin.models.setting.Setting) + self.azure_setting_patcher = patch( + 'powerdnsadmin.services.azure.Setting', + spec=powerdnsadmin.models.setting.Setting) self.oidc_setting_patcher = patch( 'powerdnsadmin.services.oidc.Setting', spec=powerdnsadmin.models.setting.Setting) @@ -57,6 +60,7 @@ class TestUnitApiZoneAdminUser(object): self.mock_google_setting = self.google_setting_patcher.start() self.mock_github_setting = self.github_setting_patcher.start() + self.mock_azure_setting = self.azure_setting_patcher.start() self.mock_oidc_setting = self.oidc_setting_patcher.start() self.mock_base_route_user = self.base_route_user_patcher.start() self.mock_helpers_setting = self.helpers_setting_patcher.start() @@ -74,6 +78,7 @@ class TestUnitApiZoneAdminUser(object): self.mock_google_setting.return_value.get.side_effect = load_data self.mock_github_setting.return_value.get.side_effect = load_data + self.mock_azure_setting.return_value.get.side_effect = load_data self.mock_oidc_setting.return_value.get.side_effect = load_data self.mock_helpers_setting.return_value.get.side_effect = load_data self.mock_models_setting.return_value.get.side_effect = load_data @@ -91,6 +96,27 @@ class TestUnitApiZoneAdminUser(object): self.mock_base_route_user.query.filter.return_value.first.return_value = self.mockk self.mock_base_route_user.return_value.is_validate.return_value = True + yield + + for patcher in [ + self.google_setting_patcher, + self.github_setting_patcher, + self.azure_setting_patcher, + self.oidc_setting_patcher, + self.base_route_user_patcher, + self.helpers_setting_patcher, + self.models_setting_patcher, + self.domain_model_setting_patcher, + self.record_model_setting_patcher, + self.server_model_setting_patcher, + self.decorators_setting_patcher, + self.api_setting_patcher, + self.mock_user_patcher, + self.mock_hist_patcher, + ]: + patcher.stop() + + def test_empty_get(self, client, common_data_mock, basic_auth_admin_headers): with patch('powerdnsadmin.routes.api.Domain') as mock_domain, \ diff --git a/tests/unit/zone/test_user.py b/tests/unit/zone/test_user.py index 7a8d8d9..b15e944 100644 --- a/tests/unit/zone/test_user.py +++ b/tests/unit/zone/test_user.py @@ -22,6 +22,9 @@ class TestUnitApiZoneUser(object): self.github_setting_patcher = patch( 'powerdnsadmin.services.github.Setting', spec=powerdnsadmin.models.setting.Setting) + self.azure_setting_patcher = patch( + 'powerdnsadmin.services.azure.Setting', + spec=powerdnsadmin.models.setting.Setting) self.oidc_setting_patcher = patch( 'powerdnsadmin.services.oidc.Setting', spec=powerdnsadmin.models.setting.Setting) @@ -57,6 +60,7 @@ class TestUnitApiZoneUser(object): self.mock_google_setting = self.google_setting_patcher.start() self.mock_github_setting = self.github_setting_patcher.start() + self.mock_azure_setting = self.azure_setting_patcher.start() self.mock_oidc_setting = self.oidc_setting_patcher.start() self.mock_base_route_user = self.base_route_user_patcher.start() self.mock_helpers_setting = self.helpers_setting_patcher.start() @@ -74,6 +78,7 @@ class TestUnitApiZoneUser(object): self.mock_google_setting.return_value.get.side_effect = load_data self.mock_github_setting.return_value.get.side_effect = load_data + self.mock_azure_setting.return_value.get.side_effect = load_data self.mock_oidc_setting.return_value.get.side_effect = load_data self.mock_helpers_setting.return_value.get.side_effect = load_data self.mock_models_setting.return_value.get.side_effect = load_data @@ -91,6 +96,27 @@ class TestUnitApiZoneUser(object): self.mock_base_route_user.query.filter.return_value.first.return_value = self.mockk self.mock_base_route_user.return_value.is_validate.return_value = True + yield + + for patcher in [ + self.google_setting_patcher, + self.github_setting_patcher, + self.azure_setting_patcher, + self.oidc_setting_patcher, + self.base_route_user_patcher, + self.helpers_setting_patcher, + self.models_setting_patcher, + self.domain_model_setting_patcher, + self.record_model_setting_patcher, + self.server_model_setting_patcher, + self.decorators_setting_patcher, + self.api_setting_patcher, + self.mock_user_patcher, + self.mock_hist_patcher, + ]: + patcher.stop() + + def test_create_zone(self, client, common_data_mock, zone_data, basic_auth_user_headers, created_zone_data): with patch('powerdnsadmin.lib.helper.requests.request') as mock_post, \ diff --git a/tests/unit/zone/test_user_apikey.py b/tests/unit/zone/test_user_apikey.py index 84cd53c..e8bd915 100644 --- a/tests/unit/zone/test_user_apikey.py +++ b/tests/unit/zone/test_user_apikey.py @@ -25,6 +25,9 @@ class TestUnitApiZoneUserApiKey(object): self.github_setting_patcher = patch( 'powerdnsadmin.services.github.Setting', spec=powerdnsadmin.models.setting.Setting) + self.azure_setting_patcher = patch( + 'powerdnsadmin.services.azure.Setting', + spec=powerdnsadmin.models.setting.Setting) self.oidc_setting_patcher = patch( 'powerdnsadmin.services.oidc.Setting', spec=powerdnsadmin.models.setting.Setting) @@ -49,9 +52,16 @@ class TestUnitApiZoneUserApiKey(object): self.mock_hist_patcher = patch( 'powerdnsadmin.routes.api.History', spec=powerdnsadmin.models.history.History) + self.mock_setting_patcher = patch( + 'powerdnsadmin.routes.api.Setting', + spec=powerdnsadmin.models.setting.Setting) + self.mock_decorators_setting_patcher = patch( + 'powerdnsadmin.decorators.Setting', + spec=powerdnsadmin.models.setting.Setting) self.mock_google_setting = self.google_setting_patcher.start() self.mock_github_setting = self.github_setting_patcher.start() + self.mock_azure_setting = self.azure_setting_patcher.start() self.mock_oidc_setting = self.oidc_setting_patcher.start() self.mock_helpers_setting = self.helpers_setting_patcher.start() self.mock_models_setting = self.models_setting_patcher.start() @@ -63,15 +73,19 @@ class TestUnitApiZoneUserApiKey(object): ) self.mock_apikey = self.mock_apikey_patcher.start() self.mock_hist = self.mock_hist_patcher.start() + self.mock_setting = self.mock_setting_patcher.start() + self.mock_decorators_setting = self.mock_decorators_setting_patcher.start() self.mock_google_setting.return_value.get.side_effect = load_data self.mock_github_setting.return_value.get.side_effect = load_data + self.mock_azure_setting.return_value.get.side_effect = load_data self.mock_oidc_setting.return_value.get.side_effect = load_data self.mock_helpers_setting.return_value.get.side_effect = load_data self.mock_models_setting.return_value.get.side_effect = load_data self.mock_domain_model_setting.return_value.get.side_effect = load_data self.mock_record_model_setting.return_value.get.side_effect = load_data self.mock_server_model_setting.return_value.get.side_effect = load_data + self.mock_decorators_setting.return_value.get.side_effect = load_data data = user_apikey_data() domain = Domain(name=data['domains'][0]) @@ -83,6 +97,26 @@ class TestUnitApiZoneUserApiKey(object): self.mock_apikey.return_value.is_validate.return_value = api_key + yield + + for patcher in [ + self.google_setting_patcher, + self.github_setting_patcher, + self.azure_setting_patcher, + self.oidc_setting_patcher, + self.helpers_setting_patcher, + self.models_setting_patcher, + self.domain_model_setting_patcher, + self.record_model_setting_patcher, + self.server_model_setting_patcher, + self.mock_apikey_patcher, + self.mock_hist_patcher, + self.mock_setting_patcher, + self.mock_decorators_setting_patcher, + ]: + patcher.stop() + + def test_create_zone(self, client, common_data_mock, zone_data, user_apikey, created_zone_data): with patch('powerdnsadmin.lib.helper.requests.request') as mock_post, \