本文整理汇总了Python中user_management.models.tests.factories.UserFactory类的典型用法代码示例。如果您正苦于以下问题:Python UserFactory类的具体用法?Python UserFactory怎么用?Python UserFactory使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_put_invalid_token
def test_put_invalid_token(self):
user = UserFactory.create()
other_user = UserFactory.create()
token = default_token_generator.make_token(other_user)
uid = urlsafe_base64_encode(force_bytes(user.pk))
request = self.create_request('put', auth=False)
view = self.view_class.as_view()
response = view(request, uidb64=uid, token=token)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
示例2: test_post
def test_post(self):
"""Assert user can sign in"""
UserFactory.create(email=self.username, password=self.password)
request = self.create_request('post', auth=False, data=self.data)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(
response.status_code, status.HTTP_200_OK, msg=response.data)
# Ensure user has a token now
token = self.model.objects.get()
self.assertEqual(response.data['token'], token.key)
示例3: test_deserialize_update_email_in_use
def test_deserialize_update_email_in_use(self):
user = UserFactory.create()
other_user = UserFactory.create()
data = {
'name': "Robert'); DROP TABLE Students;--'",
'email': other_user.email,
'password': 'Sup3RSecre7paSSw0rD',
'password2': 'Sup3RSecre7paSSw0rD',
}
serializer = serializers.RegistrationSerializer(user, data=data)
self.assertFalse(serializer.is_valid())
self.assertEqual(
serializer._errors['email'],
['That email address has already been registered.'])
示例4: test_post_rate_limit
def test_post_rate_limit(self):
"""Ensure the POST requests are rate limited."""
email = '[email protected]'
UserFactory.create(email=email)
data = {'email': email}
request = self.create_request('post', data=data, auth=False)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
# request is throttled
response = view(request)
self.assertEqual(response.status_code, status.HTTP_429_TOO_MANY_REQUESTS)
示例5: test_post
def test_post(self):
username = '[email protected]'
password = 'myepicstrongpassword'
UserFactory.create(email=username.lower(), password=password)
data = {'username': username, 'password': password}
request = self.create_request('post', auth=False, data=data)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(
response.status_code, status.HTTP_200_OK, msg=response.data)
# Ensure user has a token now
token = self.model.objects.get(user__email=username.lower())
self.assertEqual(response.data['token'], token.key)
示例6: test_post_user_not_confirmed
def test_post_user_not_confirmed(self):
"""Assert non active users can not log in."""
UserFactory.create(email=self.username, password=self.password, is_active=False)
request = self.create_request('post', auth=False, data=self.data)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(
response.status_code,
status.HTTP_400_BAD_REQUEST,
msg=response.data,
)
expected = 'User account is disabled.'
self.assertIn(expected, response.data['non_field_errors'])
self.assertNotIn('token', response.data)
示例7: test_delete_without_avatar
def test_delete_without_avatar(self):
user = UserFactory.create()
request = self.create_request('delete', user=user)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
示例8: test_duplicate_email
def test_duplicate_email(self):
"""Emails should be unique regardless of case."""
# First create a user with the same email.
self.data.pop('password2')
UserFactory.create(**self.data)
# Just for kicks, lets try changing the email case.
self.data['email'] = self.data['email'].upper()
request = self.create_request('post', auth=False, data=self.data)
response = self.view_class.as_view()(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue('email' in response.data, msg=response.data)
self.assertEqual(User.objects.count(), 1)
示例9: test_existent_email
def test_existent_email(self):
email = '[email protected]'
user = UserFactory.create(email=email)
context = {}
site = Site.objects.get_current()
request = self.create_request(
'post',
data={'email': email},
auth=False,
)
view = self.view_class.as_view()
with patch(EMAIL_CONTEXT) as get_context:
get_context.return_value = context
with patch(SEND_METHOD) as send_email:
response = view(request)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
expected = {
'to': user.email,
'template_name': 'user_management/password_reset_email.txt',
'html_template_name': 'user_management/password_reset_email.html',
'subject': '{} password reset'.format(site.domain),
'context': context,
'headers': {},
}
send_email.assert_called_once_with(**expected)
示例10: test_deserialize
def test_deserialize(self):
user = UserFactory.build()
data = {
'name': 'New Name',
}
serializer = serializers.ProfileSerializer(user, data=data)
self.assertTrue(serializer.is_valid())
示例11: test_post_different_user_logged_in
def test_post_different_user_logged_in(self):
"""Assert a user can have amny accounts and verify one."""
user = UserFactory.create()
other_user = UserFactory.create()
token = user.generate_validation_token()
request = self.create_request('post', user=other_user)
view = self.view_class.as_view()
response = view(request, token=token)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
updated_user = User.objects.get(pk=user.pk)
self.assertTrue(updated_user.email_verified)
logged_in_user = User.objects.get(pk=other_user.pk)
self.assertFalse(logged_in_user.email_verified)
示例12: test_get
def test_get(self):
user = UserFactory.build(avatar=SIMPLE_PNG)
request = self.create_request(user=user)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['avatar'], SIMPLE_PNG.url)
示例13: test_get_no_avatar
def test_get_no_avatar(self):
user = UserFactory.build()
request = self.create_request(user=user)
view = self.view_class.as_view()
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['avatar'], None)
示例14: test_post_email_already_verified
def test_post_email_already_verified(self):
"""Assert email already verified does not trigger another email."""
user = UserFactory.create(email_verified=True)
request = self.create_request('post', auth=False, data={'email': user.email})
view = self.view_class.as_view()
response = view(request)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn('email', response.data)
示例15: test_patch
def test_patch(self):
user = UserFactory.build(is_staff=True)
other_user = UserFactory.create()
data = {'avatar': SIMPLE_PNG}
request = APIRequestFactory().patch('/', data=data)
request.user = user
force_authenticate(request, user)
view = self.view_class.as_view()
with patch('django.core.files.storage.Storage.url') as mocked_url:
mocked_url.return_value = 'mocked-url'
response = view(request, pk=other_user.pk)
self.assertEqual(response.status_code, 200)
SIMPLE_PNG.seek(0)
user = User.objects.get(pk=other_user.pk)
self.assertEqual(user.avatar.read(), SIMPLE_PNG.read())