2019-12-06 03:32:57 +00:00
|
|
|
import json
|
2019-03-01 22:49:31 +00:00
|
|
|
import pytest
|
|
|
|
from unittest.mock import patch, MagicMock
|
|
|
|
from collections import namedtuple
|
2019-12-06 03:32:57 +00:00
|
|
|
|
|
|
|
import powerdnsadmin
|
|
|
|
from powerdnsadmin.models.user import User
|
|
|
|
from powerdnsadmin.models.domain import Domain
|
|
|
|
from powerdnsadmin.lib.validators import validate_zone
|
|
|
|
from powerdnsadmin.lib.schema import DomainSchema
|
2022-06-24 12:19:52 +00:00
|
|
|
from tests.conftest import load_data
|
2019-03-01 22:49:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestUnitApiZoneUser(object):
|
|
|
|
@pytest.fixture
|
2022-12-22 21:47:02 +00:00
|
|
|
def common_data_mock(self, app, initial_data):
|
|
|
|
|
2019-12-06 03:32:57 +00:00
|
|
|
self.google_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.services.google.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
|
|
|
self.github_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.services.github.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
2022-05-18 22:53:05 +00:00
|
|
|
self.azure_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.services.azure.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
2019-12-06 03:32:57 +00:00
|
|
|
self.oidc_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.services.oidc.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
2019-03-01 22:49:31 +00:00
|
|
|
self.helpers_setting_patcher = patch(
|
2019-12-06 03:32:57 +00:00
|
|
|
'powerdnsadmin.lib.helper.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
2019-03-01 22:49:31 +00:00
|
|
|
self.models_setting_patcher = patch(
|
2022-12-22 21:47:02 +00:00
|
|
|
'powerdnsadmin.models.setting.Setting',
|
2019-12-06 03:32:57 +00:00
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
|
|
|
self.domain_model_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.models.domain.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
|
|
|
self.record_model_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.models.record.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
|
|
|
self.server_model_setting_patcher = patch(
|
|
|
|
'powerdnsadmin.models.server.Setting',
|
|
|
|
spec=powerdnsadmin.models.setting.Setting)
|
2022-12-22 21:47:02 +00:00
|
|
|
self.mock_user_patcher = patch(
|
|
|
|
'powerdnsadmin.decorators.User',
|
|
|
|
spec=powerdnsadmin.models.user.User)
|
2019-03-01 22:49:31 +00:00
|
|
|
self.mock_hist_patcher = patch(
|
2019-12-06 03:32:57 +00:00
|
|
|
'powerdnsadmin.routes.api.History',
|
|
|
|
spec=powerdnsadmin.models.history.History)
|
2022-12-22 21:47:02 +00:00
|
|
|
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.base_route_user_patcher = patch(
|
|
|
|
'powerdnsadmin.routes.base.User',
|
|
|
|
spec=powerdnsadmin.models.user.User)
|
2019-03-01 22:49:31 +00:00
|
|
|
|
2022-12-22 21:47:02 +00:00
|
|
|
with app.app_context():
|
|
|
|
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()
|
|
|
|
self.mock_models_setting = self.models_setting_patcher.start()
|
|
|
|
self.mock_domain_model_setting = self.domain_model_setting_patcher.start(
|
|
|
|
)
|
|
|
|
self.mock_record_model_setting = self.record_model_setting_patcher.start(
|
|
|
|
)
|
|
|
|
self.mock_server_model_setting = self.server_model_setting_patcher.start(
|
|
|
|
)
|
|
|
|
self.mock_user = self.mock_user_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_setting.return_value.get.side_effect = load_data
|
|
|
|
|
|
|
|
self.mockk = MagicMock()
|
|
|
|
self.mockk.role.name = "User"
|
|
|
|
|
|
|
|
self.mock_user.query.filter.return_value.first.return_value = self.mockk
|
|
|
|
self.mock_user.return_value.is_validate.return_value = True
|
|
|
|
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
|
2019-03-01 22:49:31 +00:00
|
|
|
|
2022-05-18 22:53:05 +00:00
|
|
|
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.mock_user_patcher,
|
|
|
|
self.mock_hist_patcher,
|
2022-12-22 21:47:02 +00:00
|
|
|
self.mock_setting_patcher,
|
|
|
|
self.mock_decorators_setting_patcher,
|
2022-05-18 22:53:05 +00:00
|
|
|
]:
|
|
|
|
patcher.stop()
|
|
|
|
|
|
|
|
|
2019-12-06 03:32:57 +00:00
|
|
|
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, \
|
2022-12-22 21:47:02 +00:00
|
|
|
patch('powerdnsadmin.routes.api.Domain') as mock_domain:
|
2019-03-01 22:49:31 +00:00
|
|
|
mock_post.return_value.status_code = 201
|
|
|
|
mock_post.return_value.content = json.dumps(created_zone_data)
|
|
|
|
mock_post.return_value.headers = {}
|
|
|
|
mock_domain.return_value.update.return_value = True
|
|
|
|
|
2019-12-06 03:32:57 +00:00
|
|
|
res = client.post("/api/v1/pdnsadmin/zones",
|
|
|
|
headers=basic_auth_user_headers,
|
|
|
|
data=json.dumps(zone_data),
|
|
|
|
content_type="application/json")
|
2019-03-01 22:49:31 +00:00
|
|
|
data = res.get_json(force=True)
|
|
|
|
data['rrsets'] = []
|
|
|
|
|
|
|
|
validate_zone(data)
|
|
|
|
assert res.status_code == 201
|
|
|
|
|
2019-12-06 03:32:57 +00:00
|
|
|
def test_get_multiple_zones(self, client, common_data_mock, zone_data,
|
|
|
|
basic_auth_user_headers):
|
|
|
|
with patch('powerdnsadmin.routes.api.get_user_domains') as mock_user_domains:
|
2019-03-01 22:49:31 +00:00
|
|
|
test_domain = Domain(1, name=zone_data['name'].rstrip("."))
|
2019-12-06 03:32:57 +00:00
|
|
|
mock_user_domains.return_value = [test_domain]
|
2022-12-22 21:47:02 +00:00
|
|
|
|
2019-12-06 03:32:57 +00:00
|
|
|
res = client.get("/api/v1/pdnsadmin/zones",
|
2022-12-22 21:47:02 +00:00
|
|
|
headers=basic_auth_user_headers)
|
2019-12-06 03:32:57 +00:00
|
|
|
data = res.get_json(force=True)
|
|
|
|
|
|
|
|
fake_domain = namedtuple("Domain", data[0].keys())(*data[0].values())
|
|
|
|
domain_schema = DomainSchema(many=True)
|
|
|
|
|
|
|
|
json.dumps(domain_schema.dump([fake_domain]))
|
|
|
|
assert res.status_code == 200
|
|
|
|
|
|
|
|
def test_delete_zone(self, client, common_data_mock, zone_data,
|
|
|
|
basic_auth_user_headers):
|
2022-12-22 21:47:02 +00:00
|
|
|
with patch('powerdnsadmin.lib.helper.requests.request') as mock_delete, \
|
|
|
|
patch('powerdnsadmin.routes.api.Domain') as mock_domain, \
|
|
|
|
patch('powerdnsadmin.routes.api.get_user_domains') as mock_user_domains:
|
2019-12-06 03:32:57 +00:00
|
|
|
test_domain = Domain(1, name=zone_data['name'].rstrip("."))
|
|
|
|
|
|
|
|
mock_domain.return_value.update.return_value = True
|
|
|
|
mock_user_domains.return_value = [test_domain]
|
2019-03-01 22:49:31 +00:00
|
|
|
mock_delete.return_value.status_code = 204
|
|
|
|
mock_delete.return_value.content = ''
|
|
|
|
|
|
|
|
zone_url_format = "/api/v1/pdnsadmin/zones/{0}"
|
|
|
|
zone_url = zone_url_format.format(zone_data['name'].rstrip("."))
|
2019-12-06 03:32:57 +00:00
|
|
|
res = client.delete(zone_url, headers=basic_auth_user_headers)
|
2019-03-01 22:49:31 +00:00
|
|
|
|
|
|
|
assert res.status_code == 204
|