Beispiel #1
0
    def test_new_revisions_are_not_created_on_each_authentication(self):
        staff = UserFactory(is_staff=True, is_superuser=True)
        staff_password = User.objects.make_random_password()
        staff.set_password(staff_password)
        staff.save()
        self.assertTrue(
            self.client.login(username=staff.username,
                              password=staff_password))

        url = '/admin/core/user/add/'
        user_password = User.objects.make_random_password()
        self.client.post(
            url,
            {
                'username': '******',
                'password1': user_password,
                'password2': user_password,
            },
        )
        user = User.objects.get(username='******')
        ct = ContentType.objects.get_for_model(user)
        self.assertEqual(
            Version.objects.filter(object_id=user.id, content_type=ct).count(),
            1)

        user.is_staff = True
        user.save()
        self.assertTrue(
            self.client.login(username=user.username, password=user_password))
        self.assertEqual(
            Version.objects.filter(object_id=user.id, content_type=ct).count(),
            1)
Beispiel #2
0
class GenericRelatedFieldTest(APITransactionTestCase):
    def setUp(self):
        from waldur_core.structure.tests.factories import UserFactory

        self.user = UserFactory(is_staff=True)
        self.request = APIRequestFactory().get('/')
        self.request.user = self.user

        self.field = GenericRelatedField(related_models=get_loggable_models())
        self.field.root._context = {'request': self.request}

    def test_if_related_object_exists_it_is_deserialized(self):
        from waldur_core.structure.tests.factories import CustomerFactory

        customer = CustomerFactory()
        valid_url = CustomerFactory.get_url(customer)
        self.assertEqual(self.field.to_internal_value(valid_url), customer)

    def test_if_related_object_does_not_exist_validation_error_is_raised(self):
        from waldur_core.structure.tests.factories import CustomerFactory

        customer = CustomerFactory()
        valid_url = CustomerFactory.get_url(customer)
        customer.delete()
        self.assertRaises(serializers.ValidationError,
                          self.field.to_internal_value, valid_url)

    def test_if_user_does_not_have_permissions_for_related_object_validation_error_is_raised(
        self, ):
        from waldur_core.structure.tests.factories import CustomerFactory

        customer = CustomerFactory()
        valid_url = CustomerFactory.get_url(customer)
        self.user.is_staff = False
        self.user.save()
        self.assertRaises(serializers.ValidationError,
                          self.field.to_internal_value, valid_url)

    def test_if_uuid_is_invalid_validation_error_is_raised(self):
        invalid_url = 'https://example.com/api/customers/invalid/'
        self.assertRaises(serializers.ValidationError,
                          self.field.to_internal_value, invalid_url)