Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 LoginViewTest(restframework.APIViewTestCase,
assertions.StatusCodeAssertionsMixin,
assertions.InstanceAssertionsMixin):
view_class = views.ObtainTokenView
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,
'client': 'my-device',
}
user_logged_in.connect(self.signal_receiver)
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)
self.assert_instance_exists(get_user_model(), username=data['username'])
user = get_user_model().objects.get(username=data['username'])
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)
request = self.factory.post(data=data)
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,
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)
self.assert_instance_exists(get_user_model(), username=data['username'])
user = get_user_model().objects.get(username=data['username'])
'email': 'john@beatles.com',
'password': 'secret',
}
request = self.factory.post(data=data)
response = self.view(request)
self.assert_status_equal(response, status.HTTP_400_BAD_REQUEST)
self.assertEqual(
response.data, [djoser.constants.CANNOT_CREATE_USER_ERROR]
)
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)
request = self.factory.post(data=data)
>>> with self.assert_instance_deleted(Article, slug='lorem-ipsum'):
... Article.objects.get(slug='lorem-ipsum').delete()
"""
return _InstanceContext(
self.assert_instance_exists,
self.assert_instance_does_not_exist,
model_class,
**kwargs
)
class CompleteAssertionsMixin(
StatusCodeAssertionsMixin,
EmailAssertionsMixin,
MessagesAssertionsMixin,
InstanceAssertionsMixin,
):
pass