]>
Commit | Line | Data |
---|---|---|
e2dba705 | 1 | import json |
1a152698 | 2 | import requests |
e2dba705 | 3 | import unittest |
7c876c30 | 4 | from test_helper import ApiTestCase, unique_zone_name, isRecursor |
1a152698 CH |
5 | |
6 | ||
7c876c30 | 7 | @unittest.skipIf(isRecursor(), "Not implemented yet") |
1a152698 CH |
8 | class Servers(ApiTestCase): |
9 | ||
10 | def test_ListZones(self): | |
11 | r = self.session.get(self.url("/servers/localhost/zones")) | |
12 | self.assertSuccessJson(r) | |
45de6290 | 13 | domains = r.json() |
1a152698 CH |
14 | example_com = [domain for domain in domains if domain['name'] == u'example.com'] |
15 | self.assertEquals(len(example_com), 1) | |
16 | example_com = example_com[0] | |
418aa246 | 17 | for k in ('id', 'url', 'name', 'masters', 'kind', 'last_check', 'notified_serial', 'serial'): |
1a152698 | 18 | self.assertIn(k, example_com) |
e2dba705 | 19 | |
bee2acae CH |
20 | def create_zone(self, name=None, nameservers=None): |
21 | if name is None: | |
22 | name = unique_zone_name() | |
e2dba705 | 23 | payload = { |
bee2acae | 24 | 'name': name, |
e2dba705 | 25 | 'kind': 'Native', |
bee2acae | 26 | 'nameservers': ['ns1.example.com', 'ns2.example.com'] |
e2dba705 | 27 | } |
bee2acae CH |
28 | if nameservers is not None: |
29 | payload['nameservers'] = nameservers | |
e2dba705 CH |
30 | r = self.session.post( |
31 | self.url("/servers/localhost/zones"), | |
32 | data=json.dumps(payload), | |
33 | headers={'content-type': 'application/json'}) | |
34 | self.assertSuccessJson(r) | |
bee2acae CH |
35 | return (payload, r.json()) |
36 | ||
37 | def test_CreateZone(self): | |
38 | payload, data = self.create_zone() | |
418aa246 | 39 | for k in ('id', 'url', 'name', 'masters', 'kind', 'last_check', 'notified_serial', 'serial'): |
e2dba705 CH |
40 | self.assertIn(k, data) |
41 | if k in payload: | |
42 | self.assertEquals(data[k], payload[k]) | |
05776d2f | 43 | |
00a9b229 CH |
44 | @unittest.expectedFailure |
45 | def test_CreateZoneWithSymbols(self): | |
bee2acae CH |
46 | payload, data = self.create_zone(name='foo/bar.'+unique_zone_name()) |
47 | name = payload['name'] | |
48 | expected_id = name.replace('/', '\\047') | |
00a9b229 CH |
49 | for k in ('id', 'url', 'name', 'masters', 'kind', 'last_check', 'notified_serial', 'serial'): |
50 | self.assertIn(k, data) | |
51 | if k in payload: | |
52 | self.assertEquals(data[k], payload[k]) | |
bee2acae | 53 | self.assertEquals(data['id'], expected_id) |
00a9b229 | 54 | |
05776d2f CH |
55 | def test_GetZone(self): |
56 | r = self.session.get(self.url("/servers/localhost/zones")) | |
57 | domains = r.json() | |
58 | example_com = [domain for domain in domains if domain['name'] == u'example.com'][0] | |
59 | r = self.session.get(self.url("/servers/localhost/zones/" + example_com['id'])) | |
60 | self.assertSuccessJson(r) | |
61 | data = r.json() | |
62 | for k in ('id', 'url', 'name', 'masters', 'kind', 'last_check', 'notified_serial', 'serial'): | |
63 | self.assertIn(k, data) | |
64 | self.assertEquals(data['name'], 'example.com') | |
7c0ba3d2 CH |
65 | |
66 | def test_UpdateZone(self): | |
bee2acae CH |
67 | payload, zone = self.create_zone() |
68 | name = payload['name'] | |
7c0ba3d2 CH |
69 | # update, set as Master |
70 | payload = { | |
71 | 'kind': 'Master', | |
72 | 'masters': ['192.0.2.1','192.0.2.2'] | |
73 | } | |
74 | r = self.session.put( | |
75 | self.url("/servers/localhost/zones/" + name), | |
76 | data=json.dumps(payload), | |
77 | headers={'content-type': 'application/json'}) | |
78 | self.assertSuccessJson(r) | |
79 | data = r.json() | |
80 | for k in payload.keys(): | |
81 | self.assertIn(k, data) | |
82 | self.assertEquals(data[k], payload[k]) | |
83 | # update, back to Native | |
84 | payload = { | |
85 | 'kind': 'Native' | |
86 | } | |
87 | r = self.session.put( | |
88 | self.url("/servers/localhost/zones/" + name), | |
89 | data=json.dumps(payload), | |
90 | headers={'content-type': 'application/json'}) | |
91 | self.assertSuccessJson(r) | |
92 | data = r.json() | |
93 | for k in payload.keys(): | |
94 | self.assertIn(k, data) | |
95 | self.assertEquals(data[k], payload[k]) | |
b3905a3d CH |
96 | |
97 | def test_ZoneRRUpdate(self): | |
bee2acae CH |
98 | payload, zone = self.create_zone() |
99 | name = payload['name'] | |
b3905a3d CH |
100 | # do a replace (= update) |
101 | payload = { | |
102 | 'changetype': 'replace', | |
103 | 'name': name, | |
104 | 'type': 'NS', | |
105 | 'records': [ | |
106 | { | |
107 | "name": name, | |
108 | "type": "NS", | |
109 | "priority": 0, | |
110 | "ttl": 3600, | |
cea26350 CH |
111 | "content": "ns1.bar.com", |
112 | "disabled": False | |
113 | }, | |
114 | { | |
115 | "name": name, | |
116 | "type": "NS", | |
117 | "priority": 0, | |
118 | "ttl": 1800, | |
119 | "content": "ns2-disabled.bar.com", | |
120 | "disabled": True | |
b3905a3d CH |
121 | } |
122 | ] | |
123 | } | |
124 | r = self.session.patch( | |
125 | self.url("/servers/localhost/zones/" + name + "/rrset"), | |
126 | data=json.dumps(payload), | |
127 | headers={'content-type': 'application/json'}) | |
128 | self.assertSuccessJson(r) | |
129 | # verify that (only) the new record is there | |
130 | r = self.session.get( | |
131 | self.url("/servers/localhost/zones/" + name), | |
132 | data=json.dumps(payload), | |
133 | headers={'content-type': 'application/json'}) | |
134 | data = r.json()['records'] | |
135 | recs = [rec for rec in data if rec['type'] == payload['type'] and rec['name'] == payload['name']] | |
136 | self.assertEquals(recs, payload['records']) | |
137 | ||
138 | def test_ZoneRRDelete(self): | |
bee2acae CH |
139 | payload, zone = self.create_zone() |
140 | name = payload['name'] | |
b3905a3d CH |
141 | # do a delete of all NS records (these are created with the zone) |
142 | payload = { | |
143 | 'changetype': 'delete', | |
144 | 'name': name, | |
145 | 'type': 'NS' | |
146 | } | |
147 | r = self.session.patch( | |
148 | self.url("/servers/localhost/zones/" + name + "/rrset"), | |
149 | data=json.dumps(payload), | |
150 | headers={'content-type': 'application/json'}) | |
151 | self.assertSuccessJson(r) | |
152 | # verify that the records are gone | |
153 | r = self.session.get( | |
154 | self.url("/servers/localhost/zones/" + name), | |
155 | data=json.dumps(payload), | |
156 | headers={'content-type': 'application/json'}) | |
157 | data = r.json()['records'] | |
158 | recs = [rec for rec in data if rec['type'] == payload['type'] and rec['name'] == payload['name']] | |
159 | self.assertEquals(recs, []) | |
cea26350 CH |
160 | |
161 | def test_ZoneDisableReenable(self): | |
162 | payload, zone = self.create_zone() | |
163 | name = payload['name'] | |
164 | # disable zone by disabling SOA | |
165 | payload = { | |
166 | 'changetype': 'replace', | |
167 | 'name': name, | |
168 | 'type': 'SOA', | |
169 | 'records': [ | |
170 | { | |
171 | "name": name, | |
172 | "type": "SOA", | |
173 | "priority": 0, | |
174 | "ttl": 3600, | |
175 | "content": "ns1.bar.com hostmaster.foo.org 1 1 1 1 1", | |
176 | "disabled": True | |
177 | } | |
178 | ] | |
179 | } | |
180 | r = self.session.patch( | |
181 | self.url("/servers/localhost/zones/" + name + "/rrset"), | |
182 | data=json.dumps(payload), | |
183 | headers={'content-type': 'application/json'}) | |
184 | self.assertSuccessJson(r) | |
185 | # make sure it's still in zone list | |
186 | r = self.session.get(self.url("/servers/localhost/zones")) | |
187 | domains = r.json() | |
188 | self.assertEquals(len([domain for domain in domains if domain['name'] == name]), 1) | |
189 | # verify that modifying it still works | |
190 | payload['records'][0]['disabled'] = False | |
191 | r = self.session.patch( | |
192 | self.url("/servers/localhost/zones/" + name + "/rrset"), | |
193 | data=json.dumps(payload), | |
194 | headers={'content-type': 'application/json'}) | |
195 | self.assertSuccessJson(r) |