mirror of
https://github.com/cwinfo/powerdns-admin.git
synced 2025-07-30 17:23:45 +00:00
Update tests fix
Fix migration init_db 'id' Handle app context when needed Fix conftest fixtures Rearrange test Dockerfiles Hide DeprecationWarning during pytest execution Upgrade all python packages
This commit is contained in:
@@ -1,26 +1,28 @@
|
||||
import os
|
||||
import pytest
|
||||
import flask_migrate
|
||||
from base64 import b64encode
|
||||
|
||||
from powerdnsadmin import create_app
|
||||
from powerdnsadmin.models.base import db
|
||||
from powerdnsadmin.models.user import User
|
||||
from powerdnsadmin.models.setting import Setting
|
||||
from powerdnsadmin.models.api_key import ApiKey
|
||||
import pytest
|
||||
from flask_migrate import upgrade as flask_migrate_upgrade
|
||||
|
||||
app = create_app('../configs/test.py')
|
||||
ctx = app.app_context()
|
||||
ctx.push()
|
||||
from powerdnsadmin import create_app
|
||||
from powerdnsadmin.models.api_key import ApiKey
|
||||
from powerdnsadmin.models.base import db
|
||||
from powerdnsadmin.models.setting import Setting
|
||||
from powerdnsadmin.models.user import User
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def app():
|
||||
app = create_app('../configs/test.py')
|
||||
yield app
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
def client(app):
|
||||
app.config['TESTING'] = True
|
||||
client = app.test_client()
|
||||
yield client
|
||||
|
||||
|
||||
def load_data(setting_name, *args, **kwargs):
|
||||
if setting_name == 'maintenance':
|
||||
return 0
|
||||
@@ -41,17 +43,17 @@ def load_data(setting_name, *args, **kwargs):
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_admin_user():
|
||||
def test_admin_user(app):
|
||||
return app.config.get('TEST_ADMIN_USER')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_user():
|
||||
def test_user(app):
|
||||
return app.config.get('TEST_USER')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def basic_auth_admin_headers():
|
||||
def basic_auth_admin_headers(app):
|
||||
test_admin_user = app.config.get('TEST_ADMIN_USER')
|
||||
test_admin_pass = app.config.get('TEST_ADMIN_PASSWORD')
|
||||
user_pass = "{0}:{1}".format(test_admin_user, test_admin_pass)
|
||||
@@ -63,7 +65,7 @@ def basic_auth_admin_headers():
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def basic_auth_user_headers():
|
||||
def basic_auth_user_headers(app):
|
||||
test_user = app.config.get('TEST_USER')
|
||||
test_user_pass = app.config.get('TEST_USER_PASSWORD')
|
||||
user_pass = "{0}:{1}".format(test_user, test_user_pass)
|
||||
@@ -75,7 +77,8 @@ def basic_auth_user_headers():
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def initial_data():
|
||||
def initial_data(app):
|
||||
|
||||
pdns_proto = os.environ['PDNS_PROTO']
|
||||
pdns_host = os.environ['PDNS_HOST']
|
||||
pdns_port = os.environ['PDNS_PORT']
|
||||
@@ -85,46 +88,44 @@ def initial_data():
|
||||
api_key_setting = Setting('pdns_api_key', os.environ['PDNS_API_KEY'])
|
||||
allow_create_domain_setting = Setting('allow_user_create_domain', True)
|
||||
|
||||
try:
|
||||
flask_migrate.upgrade()
|
||||
with app.app_context():
|
||||
try:
|
||||
flask_migrate_upgrade(directory="migrations")
|
||||
db.session.add(api_url_setting)
|
||||
db.session.add(api_key_setting)
|
||||
db.session.add(allow_create_domain_setting)
|
||||
|
||||
db.session.add(api_url_setting)
|
||||
db.session.add(api_key_setting)
|
||||
db.session.add(allow_create_domain_setting)
|
||||
test_user = app.config.get('TEST_USER')
|
||||
test_user_pass = app.config.get('TEST_USER_PASSWORD')
|
||||
test_admin_user = app.config.get('TEST_ADMIN_USER')
|
||||
test_admin_pass = app.config.get('TEST_ADMIN_PASSWORD')
|
||||
|
||||
test_user = app.config.get('TEST_USER')
|
||||
test_user_pass = app.config.get('TEST_USER_PASSWORD')
|
||||
test_admin_user = app.config.get('TEST_ADMIN_USER')
|
||||
test_admin_pass = app.config.get('TEST_ADMIN_PASSWORD')
|
||||
admin_user = User(username=test_admin_user,
|
||||
plain_text_password=test_admin_pass,
|
||||
email="admin@admin.com")
|
||||
ret = admin_user.create_local_user()
|
||||
|
||||
admin_user = User(username=test_admin_user,
|
||||
plain_text_password=test_admin_pass,
|
||||
email="admin@admin.com")
|
||||
msg = admin_user.create_local_user()
|
||||
if not ret['status']:
|
||||
raise Exception("Error occurred creating user {0}".format(ret['msg']))
|
||||
|
||||
if not msg:
|
||||
raise Exception("Error occurred creating user {0}".format(msg))
|
||||
ordinary_user = User(username=test_user,
|
||||
plain_text_password=test_user_pass,
|
||||
email="test@test.com")
|
||||
ret = ordinary_user.create_local_user()
|
||||
|
||||
ordinary_user = User(username=test_user,
|
||||
plain_text_password=test_user_pass,
|
||||
email="test@test.com")
|
||||
msg = ordinary_user.create_local_user()
|
||||
if not ret['status']:
|
||||
raise Exception("Error occurred creating user {0}".format(ret['msg']))
|
||||
|
||||
if not msg:
|
||||
raise Exception("Error occurred creating user {0}".format(msg))
|
||||
|
||||
except Exception as e:
|
||||
print("Unexpected ERROR: {0}".format(e))
|
||||
raise e
|
||||
except Exception as e:
|
||||
print("Unexpected ERROR: {0}".format(e))
|
||||
raise e
|
||||
|
||||
yield
|
||||
|
||||
db.session.close()
|
||||
os.unlink(app.config['TEST_DB_LOCATION'])
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def initial_apikey_data():
|
||||
def initial_apikey_data(app):
|
||||
pdns_proto = os.environ['PDNS_PROTO']
|
||||
pdns_host = os.environ['PDNS_HOST']
|
||||
pdns_port = os.environ['PDNS_PORT']
|
||||
@@ -135,42 +136,40 @@ def initial_apikey_data():
|
||||
allow_create_domain_setting = Setting('allow_user_create_domain', True)
|
||||
allow_remove_domain_setting = Setting('allow_user_remove_domain', True)
|
||||
|
||||
try:
|
||||
flask_migrate.upgrade()
|
||||
with app.app_context():
|
||||
try:
|
||||
flask_migrate_upgrade(directory="migrations")
|
||||
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)
|
||||
|
||||
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')
|
||||
|
||||
test_user_apikey = app.config.get('TEST_USER_APIKEY')
|
||||
test_admin_apikey = app.config.get('TEST_ADMIN_APIKEY')
|
||||
dummy_apikey = ApiKey(desc="dummy", role_name="Administrator")
|
||||
|
||||
dummy_apikey = ApiKey(desc="dummy", role_name="Administrator")
|
||||
admin_key = dummy_apikey.get_hashed_password(
|
||||
plain_text_password=test_admin_apikey).decode('utf-8')
|
||||
|
||||
admin_key = dummy_apikey.get_hashed_password(
|
||||
plain_text_password=test_admin_apikey).decode('utf-8')
|
||||
admin_apikey = ApiKey(key=admin_key,
|
||||
desc="test admin apikey",
|
||||
role_name="Administrator")
|
||||
admin_apikey.create()
|
||||
|
||||
admin_apikey = ApiKey(key=admin_key,
|
||||
desc="test admin apikey",
|
||||
role_name="Administrator")
|
||||
admin_apikey.create()
|
||||
user_key = dummy_apikey.get_hashed_password(
|
||||
plain_text_password=test_user_apikey).decode('utf-8')
|
||||
|
||||
user_key = dummy_apikey.get_hashed_password(
|
||||
plain_text_password=test_user_apikey).decode('utf-8')
|
||||
user_apikey = ApiKey(key=user_key,
|
||||
desc="test user apikey",
|
||||
role_name="User")
|
||||
user_apikey.create()
|
||||
|
||||
user_apikey = ApiKey(key=user_key,
|
||||
desc="test user apikey",
|
||||
role_name="User")
|
||||
user_apikey.create()
|
||||
|
||||
except Exception as e:
|
||||
print("Unexpected ERROR: {0}".format(e))
|
||||
raise e
|
||||
except Exception as e:
|
||||
print("Unexpected ERROR: {0}".format(e))
|
||||
raise e
|
||||
|
||||
yield
|
||||
|
||||
db.session.close()
|
||||
os.unlink(app.config['TEST_DB_LOCATION'])
|
||||
|
||||
|
||||
@@ -187,61 +186,51 @@ def zone_data():
|
||||
@pytest.fixture
|
||||
def created_zone_data():
|
||||
data = {
|
||||
'url':
|
||||
'/api/v1/servers/localhost/zones/example.org.',
|
||||
'soa_edit_api':
|
||||
'DEFAULT',
|
||||
'last_check':
|
||||
0,
|
||||
'url': '/api/v1/servers/localhost/zones/example.org.',
|
||||
'soa_edit_api': 'DEFAULT',
|
||||
'last_check': 0,
|
||||
'masters': [],
|
||||
'dnssec':
|
||||
False,
|
||||
'notified_serial':
|
||||
0,
|
||||
'nsec3narrow':
|
||||
False,
|
||||
'serial':
|
||||
2019013101,
|
||||
'nsec3param':
|
||||
'',
|
||||
'soa_edit':
|
||||
'',
|
||||
'api_rectify':
|
||||
False,
|
||||
'kind':
|
||||
'Native',
|
||||
'dnssec': False,
|
||||
'notified_serial': 0,
|
||||
'nsec3narrow': False,
|
||||
'serial': 2019013101,
|
||||
'nsec3param': '',
|
||||
'soa_edit': '',
|
||||
'api_rectify': False,
|
||||
'kind': 'Native',
|
||||
'rrsets': [{
|
||||
'comments': [],
|
||||
'type':
|
||||
'SOA',
|
||||
'name':
|
||||
'example.org.',
|
||||
'ttl':
|
||||
3600,
|
||||
'type': 'SOA',
|
||||
'name': 'example.org.',
|
||||
'ttl': 3600,
|
||||
'records': [{
|
||||
'content':
|
||||
'a.misconfigured.powerdns.server. hostmaster.example.org. 2019013101 10800 3600 604800 3600',
|
||||
'content': 'a.misconfigured.powerdns.server. hostmaster.example.org. 2019013101 10800 3600 604800 3600',
|
||||
'disabled': False
|
||||
}]
|
||||
}, {
|
||||
'comments': [],
|
||||
'type':
|
||||
'NS',
|
||||
'name':
|
||||
'example.org.',
|
||||
'ttl':
|
||||
3600,
|
||||
'type': 'NS',
|
||||
'name': 'example.org.',
|
||||
'ttl': 3600,
|
||||
'records': [{
|
||||
'content': 'ns1.example.org.',
|
||||
'disabled': False
|
||||
}]
|
||||
}],
|
||||
'name':
|
||||
'example.org.',
|
||||
'account':
|
||||
'',
|
||||
'id':
|
||||
'example.org.'
|
||||
'name': 'example.org.',
|
||||
'account': '',
|
||||
'id': 'example.org.'
|
||||
}
|
||||
return data
|
||||
|
||||
|
||||
def user_data(app):
|
||||
test_user = app.config.get('TEST_USER')
|
||||
test_user_pass = app.config.get('TEST_USER_PASSWORD')
|
||||
data = {
|
||||
"username": test_user,
|
||||
"plain_text_password": test_user_pass,
|
||||
"email": "test@test.com"
|
||||
}
|
||||
return data
|
||||
|
||||
@@ -261,37 +250,39 @@ def admin_apikey_data():
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def user_apikey_integration():
|
||||
def user_apikey_integration(app):
|
||||
test_user_apikey = app.config.get('TEST_USER_APIKEY')
|
||||
headers = create_apikey_headers(test_user_apikey)
|
||||
return headers
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def admin_apikey_integration():
|
||||
def admin_apikey_integration(app):
|
||||
test_user_apikey = app.config.get('TEST_ADMIN_APIKEY')
|
||||
headers = create_apikey_headers(test_user_apikey)
|
||||
return headers
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def user_apikey():
|
||||
data = user_apikey_data()
|
||||
api_key = ApiKey(desc=data['description'],
|
||||
role_name=data['role'],
|
||||
domains=[])
|
||||
headers = create_apikey_headers(api_key.plain_key)
|
||||
return headers
|
||||
def user_apikey(app):
|
||||
with app.app_context():
|
||||
data = user_apikey_data()
|
||||
api_key = ApiKey(desc=data['description'],
|
||||
role_name=data['role'],
|
||||
domains=[])
|
||||
headers = create_apikey_headers(api_key.plain_key)
|
||||
return headers
|
||||
|
||||
|
||||
@pytest.fixture(scope='module')
|
||||
def admin_apikey():
|
||||
data = admin_apikey_data()
|
||||
api_key = ApiKey(desc=data['description'],
|
||||
role_name=data['role'],
|
||||
domains=[])
|
||||
headers = create_apikey_headers(api_key.plain_key)
|
||||
return headers
|
||||
def admin_apikey(app):
|
||||
with app.app_context():
|
||||
data = admin_apikey_data()
|
||||
api_key = ApiKey(desc=data['description'],
|
||||
role_name=data['role'],
|
||||
domains=[])
|
||||
headers = create_apikey_headers(api_key.plain_key)
|
||||
return headers
|
||||
|
||||
|
||||
def create_apikey_headers(passw):
|
||||
|
Reference in New Issue
Block a user