|
4 | 4 |
|
5 | 5 | from dnsimple import DNSimpleException
|
6 | 6 | from dnsimple.struct.zone import Zone
|
| 7 | +from dnsimple.struct.batch_change_zone_records import ( |
| 8 | + BatchChangeZoneRecordsInput, |
| 9 | + BatchChangeZoneRecordsUpdateInput, |
| 10 | + BatchChangeZoneRecordsDeleteInput, |
| 11 | + BatchChangeZoneRecordsResponse |
| 12 | +) |
| 13 | +from dnsimple.struct.zone_record import ZoneRecordInput |
| 14 | +from dnsimple.struct.zone_record import ZoneRecord |
7 | 15 | from tests.helpers import DNSimpleTest, DNSimpleMockResponse
|
8 | 16 |
|
9 | 17 |
|
@@ -126,6 +134,105 @@ def test_check_zone_distribution_failure(self):
|
126 | 134 | self.assertEqual('Could not query zone, connection time out', dnse.message)
|
127 | 135 | self.assertIsInstance(dnse, DNSimpleException)
|
128 | 136 |
|
| 137 | + @responses.activate |
| 138 | + def test_batch_change_records_success(self): |
| 139 | + responses.add(DNSimpleMockResponse(method=responses.POST, |
| 140 | + path='/1010/zones/example.com/batch', |
| 141 | + fixture_name='batchChangeZoneRecords/success')) |
| 142 | + |
| 143 | + batch_change = BatchChangeZoneRecordsInput( |
| 144 | + creates=[ |
| 145 | + ZoneRecordInput('ab', 'A', '3.2.3.4'), |
| 146 | + ZoneRecordInput('ab', 'A', '4.2.3.4') |
| 147 | + ], |
| 148 | + updates=[ |
| 149 | + BatchChangeZoneRecordsUpdateInput(67622534, content='3.2.3.40'), |
| 150 | + BatchChangeZoneRecordsUpdateInput(67622537, content='5.2.3.40') |
| 151 | + ], |
| 152 | + deletes=[ |
| 153 | + BatchChangeZoneRecordsDeleteInput(67622509), |
| 154 | + BatchChangeZoneRecordsDeleteInput(67622527) |
| 155 | + ] |
| 156 | + ) |
| 157 | + |
| 158 | + response = self.zones.batch_change_records(1010, 'example.com', batch_change) |
| 159 | + result = response.data |
| 160 | + |
| 161 | + self.assertIsInstance(result, BatchChangeZoneRecordsResponse) |
| 162 | + |
| 163 | + self.assertEqual(2, len(result.creates)) |
| 164 | + self.assertIsInstance(result.creates[0], ZoneRecord) |
| 165 | + self.assertEqual(67623409, result.creates[0].id) |
| 166 | + self.assertEqual('ab', result.creates[0].name) |
| 167 | + self.assertEqual('3.2.3.4', result.creates[0].content) |
| 168 | + self.assertEqual('A', result.creates[0].type) |
| 169 | + |
| 170 | + self.assertEqual(2, len(result.updates)) |
| 171 | + self.assertIsInstance(result.updates[0], ZoneRecord) |
| 172 | + self.assertEqual(67622534, result.updates[0].id) |
| 173 | + self.assertEqual('3.2.3.40', result.updates[0].content) |
| 174 | + |
| 175 | + self.assertEqual(2, len(result.deletes)) |
| 176 | + self.assertEqual(67622509, result.deletes[0].id) |
| 177 | + self.assertEqual(67622527, result.deletes[1].id) |
| 178 | + |
| 179 | + @responses.activate |
| 180 | + def test_batch_change_records_create_validation_failed(self): |
| 181 | + responses.add(DNSimpleMockResponse(method=responses.POST, |
| 182 | + path='/1010/zones/example.com/batch', |
| 183 | + fixture_name='batchChangeZoneRecords/error_400_create_validation_failed')) |
| 184 | + |
| 185 | + batch_change = BatchChangeZoneRecordsInput( |
| 186 | + creates=[ |
| 187 | + ZoneRecordInput('test', 'SPF', 'v=spf1 -all') |
| 188 | + ] |
| 189 | + ) |
| 190 | + |
| 191 | + try: |
| 192 | + self.zones.batch_change_records(1010, 'example.com', batch_change) |
| 193 | + except DNSimpleException as dnse: |
| 194 | + self.assertEqual('Validation failed', dnse.message) |
| 195 | + self.assertIsInstance(dnse, DNSimpleException) |
| 196 | + self.assertEqual('The SPF record type has been discontinued', dnse.attribute_errors['creates'][0]['message']) |
| 197 | + |
| 198 | + @responses.activate |
| 199 | + def test_batch_change_records_update_validation_failed(self): |
| 200 | + responses.add(DNSimpleMockResponse(method=responses.POST, |
| 201 | + path='/1010/zones/example.com/batch', |
| 202 | + fixture_name='batchChangeZoneRecords/error_400_update_validation_failed')) |
| 203 | + |
| 204 | + batch_change = BatchChangeZoneRecordsInput( |
| 205 | + updates=[ |
| 206 | + BatchChangeZoneRecordsUpdateInput(99999999, content='1.2.3.4') |
| 207 | + ] |
| 208 | + ) |
| 209 | + |
| 210 | + try: |
| 211 | + self.zones.batch_change_records(1010, 'example.com', batch_change) |
| 212 | + except DNSimpleException as dnse: |
| 213 | + self.assertEqual('Validation failed', dnse.message) |
| 214 | + self.assertIsInstance(dnse, DNSimpleException) |
| 215 | + self.assertEqual('Record not found ID=99999999', dnse.attribute_errors['updates'][0]['message']) |
| 216 | + |
| 217 | + @responses.activate |
| 218 | + def test_batch_change_records_delete_validation_failed(self): |
| 219 | + responses.add(DNSimpleMockResponse(method=responses.POST, |
| 220 | + path='/1010/zones/example.com/batch', |
| 221 | + fixture_name='batchChangeZoneRecords/error_400_delete_validation_failed')) |
| 222 | + |
| 223 | + batch_change = BatchChangeZoneRecordsInput( |
| 224 | + deletes=[ |
| 225 | + BatchChangeZoneRecordsDeleteInput(67622509) |
| 226 | + ] |
| 227 | + ) |
| 228 | + |
| 229 | + try: |
| 230 | + self.zones.batch_change_records(1010, 'example.com', batch_change) |
| 231 | + except DNSimpleException as dnse: |
| 232 | + self.assertEqual('Validation failed', dnse.message) |
| 233 | + self.assertIsInstance(dnse, DNSimpleException) |
| 234 | + self.assertEqual('Record not found ID=67622509', dnse.attribute_errors['deletes'][0]['message']) |
| 235 | + |
129 | 236 |
|
130 | 237 | if __name__ == '__main__':
|
131 | 238 | unittest.main()
|
0 commit comments