]> git.ipfire.org Git - thirdparty/pdns.git/blob - regression-tests.recursor-dnssec/test_API.py
Merge pull request #9088 from neheb/nbm
[thirdparty/pdns.git] / regression-tests.recursor-dnssec / test_API.py
1 import os
2 import requests
3
4 from recursortests import RecursorTest
5
6 class APIRecursorTest(RecursorTest):
7
8 @classmethod
9 def setUpClass(cls):
10
11 # we don't need all the auth stuff
12 cls.setUpSockets()
13 cls.startResponders()
14
15 confdir = os.path.join('configs', cls._confdir)
16 cls.createConfigDir(confdir)
17
18 cls.generateRecursorConfig(confdir)
19 cls.startRecursor(confdir, cls._recursorPort)
20
21 @classmethod
22 def tearDownClass(cls):
23 cls.tearDownRecursor()
24
25 class APIAllowedRecursorTest(APIRecursorTest):
26 _confdir = 'API'
27 _wsPort = 8042
28 _wsTimeout = 2
29 _wsPassword = 'secretpassword'
30 _apiKey = 'secretapikey'
31
32 _config_template = """
33 webserver=yes
34 webserver-port=%d
35 webserver-address=127.0.0.1
36 webserver-password=%s
37 webserver-allow-from=127.0.0.1
38 api-key=%s
39 """ % (_wsPort, _wsPassword, _apiKey)
40
41 def testAPI(self):
42 headers = {'x-api-key': self._apiKey}
43 url = 'http://127.0.0.1:' + str(self._wsPort) + '/api/v1/servers/localhost/statistics'
44 r = requests.get(url, headers=headers, timeout=self._wsTimeout)
45 self.assertTrue(r)
46 self.assertEquals(r.status_code, 200)
47 self.assertTrue(r.json())
48
49 class APIDeniedRecursorTest(APIRecursorTest):
50 _confdir = 'API'
51 _wsPort = 8042
52 _wsTimeout = 2
53 _wsPassword = 'secretpassword'
54 _apiKey = 'secretapikey'
55
56 _config_template = """
57 webserver=yes
58 webserver-port=%d
59 webserver-address=127.0.0.1
60 webserver-password=%s
61 webserver-allow-from=192.0.2.1
62 api-key=%s
63 """ % (_wsPort, _wsPassword, _apiKey)
64
65 def testAPI(self):
66 headers = {'x-api-key': self._apiKey}
67 url = 'http://127.0.0.1:' + str(self._wsPort) + '/api/v1/servers/localhost/statistics'
68 try:
69 r = requests.get(url, headers=headers, timeout=self._wsTimeout)
70 self.assertTrue(False)
71 except requests.exceptions.ConnectionError as exp:
72 pass