From: Peter van Dijk Date: Wed, 15 Dec 2021 14:05:33 +0000 (+0100) Subject: auth: add autoprimary API test X-Git-Tag: auth-4.7.0-alpha1~83^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F11102%2Fhead;p=thirdparty%2Fpdns.git auth: add autoprimary API test --- diff --git a/regression-tests.api/test_Servers.py b/regression-tests.api/test_Servers.py index 4547a59de9..47122ebb15 100644 --- a/regression-tests.api/test_Servers.py +++ b/regression-tests.api/test_Servers.py @@ -1,6 +1,8 @@ +import json +import operator import requests import unittest -from test_helper import ApiTestCase, is_auth, is_recursor +from test_helper import ApiTestCase, is_auth, is_recursor, is_auth_lmdb class Servers(ApiTestCase): @@ -102,3 +104,64 @@ class Servers(ApiTestCase): r = requests.get(self.url("/api/v1/servers/localhost/statistics"), auth=('admin', self.server_web_password)) self.assertEqual(r.status_code, requests.codes.ok) self.assert_success_json(r) + + @unittest.skipIf(is_recursor(), "Not applicable") + @unittest.skipIf(is_auth_lmdb(), "No autoprimary management in LMDB yet") + def test_autoprimaries(self): + # verify that we have zero autoprimaries + res = self.session.get(self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) + self.assertEqual(res.status_code, requests.codes.ok) + self.assertEqual(res.json(), []) + + # add one + payload = { + 'ip': '192.0.2.1', + 'nameserver': 'ns.example.com' + } + + res = self.session.post( + self.url("/api/v1/servers/localhost/autoprimaries"), + data=json.dumps(payload), + headers={'content-type': 'application/json'}) + + self.assertEqual(res.status_code, 201) + + # check that it's there + res = self.session.get(self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) + self.assertEqual(res.status_code, requests.codes.ok) + self.assertEqual(res.json(), [{'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com'}]) + + # add another one, this time with an account field + payload = { + 'ip': '192.0.2.2', + 'nameserver': 'ns.example.org', + 'account': 'test' + } + + res = self.session.post( + self.url("/api/v1/servers/localhost/autoprimaries"), + data=json.dumps(payload), + headers={'content-type': 'application/json'}) + + self.assertEqual(res.status_code, 201) + + # check that both are there + res = self.session.get(self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) + self.assertEqual(res.status_code, requests.codes.ok) + self.assertEqual(len(res.json()), 2) + self.assertEqual(sorted(res.json(), key=operator.itemgetter('ip')), [ + {'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com'}, + {'account': 'test', 'ip': '192.0.2.2', 'nameserver': 'ns.example.org'} + ]) + + # remove one + res = self.session.delete( + self.url("/api/v1/servers/localhost/autoprimaries/192.0.2.2/ns.example.org"), + headers={'content-type': 'application/json"'}) + + self.assertEqual(res.status_code, 204) + + # check that we are back to just one + res = self.session.get(self.url("/api/v1/servers/localhost/autoprimaries"), auth=('whatever', self.webServerBasicAuthPassword), timeout=2.0) + self.assertEqual(res.status_code, requests.codes.ok) + self.assertEqual(res.json(), [{'account': '', 'ip': '192.0.2.1', 'nameserver': 'ns.example.com'}])