def test_prevents_unauthorized_updates(self): user = factory.make_User() domain = Domain.objects.get_default_domain() new_name = factory.make_hostname() handler = DomainHandler(user, {}, None) with ExpectedException(HandlerPermissionError): handler.update({"id": domain.id, "name": new_name})
def test__delete_as_admin_success(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() handler.delete({"id": domain.id}) domain = reload_object(domain) self.assertThat(domain, Equals(None))
def test_update_returns_model_object(self): user = factory.make_admin() domain = Domain.objects.get_default_domain() new_name = factory.make_hostname() handler = DomainHandler(user, {}, None) returned_domain = handler.update({"id": domain.id, "name": new_name}) domain = reload_object(domain) self.assertThat(self.dehydrate_domain(domain), Equals(returned_domain))
def test_update_allows_domain_name_change(self): user = factory.make_admin() domain = Domain.objects.get_default_domain() new_name = factory.make_hostname() handler = DomainHandler(user, {}, None) handler.update({"id": domain.id, "name": new_name}) domain = reload_object(domain) self.assertThat(domain.name, Equals(new_name))
def test_update_allows_default_ttl_change(self): user = factory.make_admin() domain = Domain.objects.get_default_domain() handler = DomainHandler(user, {}, None) new_ttl = randint(1, 3600) handler.update({"id": domain.id, "ttl": new_ttl}) domain = reload_object(domain) self.assertThat(domain.ttl, Equals(new_ttl))
def test_update_allows_authoritative_change(self): user = factory.make_admin() domain = factory.make_Domain(authoritative=choice([True, False])) handler = DomainHandler(user, {}, None) new_authoritative = not domain.authoritative handler.update({"id": domain.id, "authoritative": new_authoritative}) domain = reload_object(domain) self.assertThat(domain.authoritative, Equals(new_authoritative))
def test__delete_as_non_admin_asserts(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() with ExpectedException(HandlerPermissionError): handler.delete({ "id": domain.id, })
def test__delete_default_domain_fails(self): domain = Domain.objects.get_default_domain() user = factory.make_admin() handler = DomainHandler(user, {}, None) with ExpectedException(ValidationError): handler.delete({ "id": domain.id, })
def test__set_default_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() with ExpectedException(HandlerPermissionError): handler.set_default({ 'domain': domain.id, })
def test__delete_resource_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() resource = factory.make_DNSResource(domain=domain) with ExpectedException(HandlerPermissionError): handler.delete_address_record( {"domain": domain.id, "dnsresource": resource.id} )
def test__delete_dnsdata(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() dnsresource = factory.make_DNSResource(domain=domain) dnsdata = factory.make_DNSData(dnsresource) handler.delete_dnsdata({"domain": domain.id, "dnsdata_id": dnsdata.id}) dnsdata = reload_object(dnsdata) self.assertThat(dnsdata, Is(None))
def test__delete_resource(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() resource = factory.make_DNSResource(domain=domain) handler.delete_dnsresource( {"domain": domain.id, "dnsresource_id": resource.id} ) self.assertThat(reload_object(resource), Is(None))
def test__set_default_sets_default(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) factory.make_Domain() domain2 = factory.make_Domain() self.assertThat(domain2.is_default(), Equals(False)) handler.set_default({"domain": domain2.id}) domain2 = reload_object(domain2) self.assertThat(domain2.is_default(), Equals(True))
def test_get(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() factory.make_DNSData(domain=domain) factory.make_DNSResource(domain=domain) factory.make_DNSResource(domain=domain, address_ttl=randint(0, 300)) self.assertEqual(self.dehydrate_domain(domain), handler.get({"id": domain.id}))
def test_update_raises_validaitonerror_for_empty_name(self): user = factory.make_admin() domain = factory.make_Domain(authoritative=choice([True, False])) handler = DomainHandler(user, {}, None) with ExpectedException(HandlerValidationError): handler.update({ "id": domain.id, "name": "", })
def test__delete_resource_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}) domain = factory.make_Domain() resource = factory.make_DNSResource(domain=domain) with ExpectedException(HandlerPermissionError): handler.delete_dnsresource({ 'domain': domain.id, 'dnsresource': resource.id, })
def test_list(self): user = factory.make_User() handler = DomainHandler(user, {}, None) Domain.objects.get_default_domain() factory.make_Domain() expected_domains = [ self.dehydrate_domain(domain, for_list=True) for domain in Domain.objects.all() ] self.assertItemsEqual(expected_domains, handler.list({}))
def test__add_address_without_ip_addresses_fails(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() with ExpectedException(ValidationError): handler.create_address_record({ 'domain': domain.id, 'name': name, 'rrtype': choice(['A', 'AAAA']), 'ip_addresses': [''] })
def test__add_address_without_ip_addresses_fails(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() with ExpectedException(ValidationError): handler.create_address_record({ "domain": domain.id, "name": name, "rrtype": choice(["A", "AAAA"]), "ip_addresses": [""], })
def test__delete_dnsdata_fails_for_non_admin(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() dnsresource = factory.make_DNSResource(domain=domain) dnsdata = factory.make_DNSData(dnsresource) with ExpectedException(HandlerPermissionError): handler.delete_dnsdata( {"domain": domain.id, "dnsdata_id": dnsdata.id} ) dnsdata = reload_object(dnsdata) self.assertThat(dnsdata, Not(Is(None)))
def test__add_address_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.create_address_record({ 'domain': domain.id, 'name': name, 'address_ttl': ttl, 'ip_addresses': ['127.0.0.1'] })
def test__delete_address_deletes_single_address(self): user = factory.make_admin() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() resource = factory.make_DNSResource( domain=domain, name=name, ip_addresses=["127.0.0.1", "127.0.0.2"]) handler.delete_address_record({ 'domain': domain.id, 'dnsresource_id': resource.id, 'rrdata': "127.0.0.1" }) self.assertThat(resource.get_addresses(), Equals(["127.0.0.2"]))
def test__add_address_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.create_address_record({ "domain": domain.id, "name": name, "address_ttl": ttl, "ip_addresses": ["127.0.0.1"], })
def test__update_dnsdata(self): user = factory.make_admin() handler = DomainHandler(user, {}) domain = factory.make_Domain() dnsresource = factory.make_DNSResource(domain=domain) dnsdata = factory.make_DNSData( dnsresource, rrtype="TXT", rrdata="original") handler.update_dnsdata({ 'domain': domain.id, 'dnsresource_id': dnsresource.id, 'dnsdata_id': dnsdata.id, 'rrdata': 'updated', }) dnsdata = reload_object(dnsdata) self.assertThat(dnsdata.rrdata, Equals("updated"))
def test__update_address_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() resource = factory.make_DNSResource(domain=domain) new_name = factory.make_hostname() new_ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.update_address_record({ 'domain': domain.id, 'dnsresource': resource.id, 'name': new_name, 'address_ttl': new_ttl, 'ip_addresses': ['127.0.0.1'], })
def test__update_address_as_non_admin_fails(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() resource = factory.make_DNSResource(domain=domain) new_name = factory.make_hostname() new_ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.update_address_record({ "domain": domain.id, "dnsresource": resource.id, "name": new_name, "address_ttl": new_ttl, "ip_addresses": ["127.0.0.1"], })
def test__add_data_for_existing_resource_name_fails_for_non_admin(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() dnsresource = factory.make_DNSResource(domain=domain, name=name) ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.create_dnsdata({ 'domain': domain.id, 'name': name, 'ttl': ttl, 'rrtype': 'TXT', 'rrdata': 'turtles all the way down', }) self.expectThat(dnsresource.dnsdata_set.all(), HasLength(0))
def test__add_data_for_new_resource_name_fails_for_non_admin(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.create_dnsdata({ 'domain': domain.id, 'name': name, 'ttl': ttl, 'rrtype': 'TXT', 'rrdata': 'turtles all the way down', }) with ExpectedException(DNSResource.DoesNotExist): DNSResource.objects.get(domain=domain, name=name)
def test_add_data_for_new_resource_name_fails_for_non_admin(self): user = factory.make_User() handler = DomainHandler(user, {}, None) domain = factory.make_Domain() name = factory.make_hostname() ttl = randint(1, 3600) with ExpectedException(HandlerPermissionError): handler.create_dnsdata({ "domain": domain.id, "name": name, "ttl": ttl, "rrtype": "TXT", "rrdata": "turtles all the way down", }) with ExpectedException(DNSResource.DoesNotExist): DNSResource.objects.get(domain=domain, name=name)
def test_create_raises_validation_error_for_missing_name(self): user = factory.make_admin() handler = DomainHandler(user, {}) params = {"name": ""} error = self.assertRaises(ValidationError, handler.create, params) self.assertThat(error.message_dict, Equals({'name': ['This field cannot be blank.']}))