From 230f3234f547be858722458ffa60e2bdea48d3b6 Mon Sep 17 00:00:00 2001 From: Benjamin Bertrand <benjamin.bertrand@esss.se> Date: Fri, 23 Feb 2018 11:32:56 +0100 Subject: [PATCH] Add /network/domains endpoint --- app/api/network.py | 25 +++++++++++++++++++++++++ tests/functional/test_api.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/app/api/network.py b/app/api/network.py index d20aa52..104a4cb 100644 --- a/app/api/network.py +++ b/app/api/network.py @@ -164,3 +164,28 @@ def create_macs(): """ return create_generic_model(models.Mac, mandatory_fields=('address',)) + + +@bp.route('/domains') +@jwt_required +def get_domains(): + """Return domains + + .. :quickref: Network; Get domains + """ + return get_generic_model(models.Domain, + order_by=models.Domain.name) + + +@bp.route('/domains', methods=['POST']) +@jwt_required +@jwt_groups_accepted('admin') +def create_domain(): + """Create a new domain + + .. :quickref: Network; Create new domain + + :jsonparam name: domain name + """ + return create_generic_model(models.Domain, + mandatory_fields=('name',)) diff --git a/tests/functional/test_api.py b/tests/functional/test_api.py index 6954f28..2909023 100644 --- a/tests/functional/test_api.py +++ b/tests/functional/test_api.py @@ -27,6 +27,7 @@ ENDPOINT_MODEL = { 'network/interfaces': models.Interface, 'network/hosts': models.Host, 'network/macs': models.Mac, + 'network/domains': models.Domain, } GENERIC_GET_ENDPOINTS = [key for key in ENDPOINT_MODEL.keys() if key.startswith('inventory') and key != 'inventory/items'] @@ -765,3 +766,31 @@ def test_get_user_profile(client, readonly_token): assert user['username'] == 'user_ro' assert user['display_name'] == 'User RO' assert user['email'] == 'user_ro@example.com' + + +def test_get_domains(client, domain_factory, readonly_token): + # Create some domains + domain1 = domain_factory() + domain2 = domain_factory() + response = get(client, f'{API_URL}/network/domains', token=readonly_token) + assert response.status_code == 200 + assert len(response.json) == 2 + check_input_is_subset_of_response(response, (domain1.to_dict(), domain2.to_dict())) + + +def test_create_domain(client, admin_token): + # check that name is mandatory + response = post(client, f'{API_URL}/network/domains', data={}, token=admin_token) + check_response_message(response, "Missing mandatory field 'name'", 422) + + data = {'name': 'tn.esss.lu.se'} + response = post(client, f'{API_URL}/network/domains', data=data, token=admin_token) + assert response.status_code == 201 + assert {'id', 'name', 'scopes', + 'networks', 'created_at', + 'updated_at', 'user'} == set(response.json.keys()) + assert response.json['name'] == data['name'] + + # Check that name shall be unique + response = post(client, f'{API_URL}/network/domains', data=data, token=admin_token) + check_response_message(response, '(psycopg2.IntegrityError) duplicate key value violates unique constraint', 422) -- GitLab