How to use the djet.assertions function in djet

To help you get started, we’ve selected a few djet examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sunscrapers / django-rest-multitoken / testproject / testapp / tests.py View on Github external
'password': 'wrong password',
            'client': 'my-device',
        }
        user_logged_in.connect(self.signal_receiver)
        request = self.factory.post(data=data)

        response = self.view(request)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        with self.assertRaises(models.Token.DoesNotExist):
            user.auth_tokens.get()
        self.assertFalse(self.signal_sent)


class LogoutViewTest(restframework.APIViewTestCase,
                     assertions.StatusCodeAssertionsMixin):
    view_class = views.InvalidateTokenView

    def test_post_should_logout_logged_in_user(self):
        user = create_user()
        token = models.Token.objects.create(user=user, client='my-device')

        request = self.factory.post(user=user, token=token)
        response = self.view(request)

        self.assert_status_equal(response, status.HTTP_200_OK)
        self.assertEqual(response.data, None)
        with self.assertRaises(models.Token.DoesNotExist):
            utils.refresh(token)

    def test_post_should_deny_logging_out_when_user_not_logged_in(self):
        create_user()
github sunscrapers / djoser / testproject / testapp / tests.py View on Github external
'username': 'john',
        'password': 'secret',
        'email': 'john@beatles.com',
    }
    data.update(kwargs)
    user = get_user_model().objects.create_user(**data)
    user.raw_password = data['password']
    return user


def perform_create_mock(x):
    raise IntegrityError


class RootViewTest(restframework.APIViewTestCase,
                   assertions.StatusCodeAssertionsMixin):
    view_class = djoser.views.RootView

    def test_get_should_return_urls_mapping(self):
        request = self.factory.get()
        view_object = self.create_view_object(request)

        response = view_object.dispatch(request)

        self.assert_status_equal(response, status.HTTP_200_OK)
        for key in view_object.get_urls_mapping().keys():
            self.assertIn(key, response.data)


class RegistrationViewTest(restframework.APIViewTestCase,
                           assertions.StatusCodeAssertionsMixin,
                           assertions.EmailAssertionsMixin,
github mapoio / TaskApp / TaskApp / tests.py View on Github external
def test_post_should_not_register_if_fails_password_validation(self):
        data = {
            'username': 'john',
            'password': '',
            'csrftoken': 'asdf',
        }
        request = self.factory.post(data=data)

        response = self.view(request)

        self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
        self.assertEqual(response.data, {'password': ['该字段不能为空。']})


class LoginViewTest(restframework.APIViewTestCase,
                    assertions.StatusCodeAssertionsMixin,
                    assertions.InstanceAssertionsMixin):
    view_class = djoser.views.LoginView

    def setUp(self):
        self.signal_sent = False

    def signal_receiver(self, *args, **kwargs):
        self.signal_sent = True

    def test_post_should_login_user(self):
        user = create_user()
        data = {
            'username': user.username,
            'password': user.raw_password,
        }
        user_logged_in.connect(self.signal_receiver)
github mapoio / TaskApp / company / tests.py View on Github external
data = {
        'username': 'admin',
        'password': 'password123',
        'email': 'john@beatles.com',
        'is_superuser': True,
        'is_active': True,
        'is_staff': True,
    }
    data.update(kwargs)
    user = get_user_model().objects.create_user(**data)
    user.raw_password = data['password']
    return user


class CompanyViewTest(APITestCase,
                      assertions.StatusCodeAssertionsMixin):
    def setUp(self):
        user = create_user()
        self.user = user


    def test_get_context_should_data(self):
        url = reverse('company-list')
        response = self.client.get(url)

        self.assert_status_equal(response,status.HTTP_200_OK)
        self.assertEqual(0,response.data['count'])

    def test_post_un_login_should_403(self):
        url = reverse('company-list')
        response = self.client.post(url)
github mapoio / TaskApp / TaskApp / tests.py View on Github external
import djoser.serializers

def create_user(**kwargs):
    data = {
        'username': 'john',
        'password': 'secret',
        'email': 'john@beatles.com',
    }
    data.update(kwargs)
    user = get_user_model().objects.create_user(**data)
    user.raw_password = data['password']
    return user


class RegistrationViewTest(restframework.APIViewTestCase,
                           assertions.StatusCodeAssertionsMixin,
                           assertions.EmailAssertionsMixin,
                           assertions.InstanceAssertionsMixin):
    view_class = djoser.views.RegistrationView

    def test_post_should_create_user_without_login(self):
        data = {
            'username': 'john',
            'password': 'secret',
            'csrftoken': 'asdf',
        }
        request = self.factory.post(data=data)

        response = self.view(request)

        self.assert_status_equal(response, status.HTTP_201_CREATED)
        self.assertTrue('password' not in response.data)