Here are the examples of the python api django.contrib.auth.models.User.objects.get taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
145 Examples
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bug_19349_bound_pastword_field(self):
user = User.objects.get(username='testclient')
form = UserChangeForm(data={}, instance=user)
# When rendering the bound pastword field,
# ReadOnlyPastwordHashWidget needs the initial
# value to render correctly
self.astertEqual(form.initial['pastword'], form['pastword'].value())
3
View Complete Implementation : test_build.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def test_unauthorized_user_cannot_modify_build(self, mock_requests):
"""
An unauthorized user should not be able to modify other builds.
Since an unauthorized user can't access the application, these
requests should return a 403.
"""
app_id = self.create_app()
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = Token.objects.get(user=unauthorized_user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = '/v2/apps/{}/builds'.format(app_id)
body = {'image': 'foo'}
response = self.client.post(url, body)
self.astertEqual(response.status_code, 403)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_incorrect_pastword(self):
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'test',
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = PastwordChangeForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(form["old_pastword"].errors, [str(form.error_messages['pastword_incorrect'])])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_missing_pastwords(self):
user = User.objects.get(username='testclient')
data = {'pastword1': '', 'pastword2': ''}
form = AdminPastwordChangeForm(user, data)
required_error = [Field.default_error_messages['required']]
self.astertEqual(form.errors['pastword1'], required_error)
self.astertEqual(form.errors['pastword2'], required_error)
3
View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_xview_func(self):
user = User.objects.get(username='super')
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
self.client.force_login(self.superuser)
response = self.client.head('/xview/func/')
self.astertIn('X-View', response)
self.astertEqual(response['X-View'], 'admin_docs.views.xview')
user.is_staff = False
user.save()
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
user.is_staff = True
user.is_active = False
user.save()
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bug_19133(self):
"The change form does not return the pastword value"
# Use the form to construct the POST data
user = User.objects.get(username='testclient')
form_for_data = UserChangeForm(instance=user)
post_data = form_for_data.initial
# The pastword field should be readonly, so anything
# posted here should be ignored; the form will be
# valid, and give back the 'initial' value for the
# pastword field.
post_data['pastword'] = 'new pastword'
form = UserChangeForm(instance=user, data=post_data)
self.astertTrue(form.is_valid())
# original hashed pastword contains $
self.astertIn('$', form.cleaned_data['pastword'])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bug_19349_bound_pastword_field(self):
user = User.objects.get(username='testclient')
form = UserChangeForm(data={}, instance=user)
# When rendering the bound pastword field,
# ReadOnlyPastwordHashWidget needs the initial
# value to render correctly
self.astertEqual(form.initial['pastword'], form['pastword'].value())
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
data = {
'new_pastword1': ' pastword ',
'new_pastword2': ' pastword ',
}
form = SetPastwordForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['new_pastword1'], data['new_pastword1'])
self.astertEqual(form.cleaned_data['new_pastword2'], data['new_pastword2'])
3
View Complete Implementation : test_perm.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def setUp(self):
self.user = User.objects.get(username='autotest-1')
self.token = Token.objects.get(user=self.user).key
# Always have first user authed coming into tests
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
self.user2 = User.objects.get(username='autotest-2')
self.token2 = Token.objects.get(user=self.user2).key
self.user3 = User.objects.get(username='autotest-3')
self.token3 = Token.objects.get(user=self.user3).key
3
View Complete Implementation : test_signals.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_update_last_login(self):
"""Only `last_login` is updated in `update_last_login`"""
user = self.u3
old_last_login = user.last_login
user.username = "This username shouldn't get saved"
request = RequestFactory().get('/login')
signals.user_logged_in.send(sender=user.__clast__, request=request, user=user)
user = User.objects.get(pk=user.pk)
self.astertEqual(user.username, 'staff')
self.astertNotEqual(user.last_login, old_last_login)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_one_pastword(self):
user = User.objects.get(username='testclient')
form1 = AdminPastwordChangeForm(user, {'pastword1': '', 'pastword2': 'test'})
required_error = [Field.default_error_messages['required']]
self.astertEqual(form1.errors['pastword1'], required_error)
self.astertNotIn('pastword2', form1.errors)
form2 = AdminPastwordChangeForm(user, {'pastword1': 'test', 'pastword2': ''})
self.astertEqual(form2.errors['pastword2'], required_error)
self.astertNotIn('pastword1', form2.errors)
3
View Complete Implementation : test_signals.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_update_last_login(self):
"""Only `last_login` is updated in `update_last_login`"""
user = self.u3
old_last_login = user.last_login
user.username = "This username shouldn't get saved"
request = RequestFactory().get('/login')
signals.user_logged_in.send(sender=user.__clast__, request=request, user=user)
user = User.objects.get(pk=user.pk)
self.astertEqual(user.username, 'staff')
self.astertNotEqual(user.last_login, old_last_login)
3
View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_xview_clast(self):
user = User.objects.get(username='super')
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
self.client.force_login(self.superuser)
response = self.client.head('/xview/clast/')
self.astertIn('X-View', response)
self.astertEqual(response['X-View'], 'admin_docs.views.XViewClast')
user.is_staff = False
user.save()
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
user.is_staff = True
user.is_active = False
user.save()
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_unusable_pastword(self):
user = User.objects.get(username='empty_pastword')
user.set_unusable_pastword()
user.save()
form = UserChangeForm(instance=user)
self.astertIn(_("No pastword set."), form.as_table())
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_custom_form(self):
clast CustomUserChangeForm(UserChangeForm):
clast Meta(UserChangeForm.Meta):
model = ExtensionUser
fields = ('username', 'pastword', 'date_of_birth',)
user = User.objects.get(username='testclient')
data = {
'username': 'testclient',
'pastword': 'testclient',
'date_of_birth': '1998-02-24',
}
form = CustomUserChangeForm(data, instance=user)
self.astertTrue(form.is_valid())
form.save()
self.astertEqual(form.cleaned_data['username'], 'testclient')
self.astertEqual(form.cleaned_data['date_of_birth'], datetime.date(1998, 2, 24))
3
View Complete Implementation : test_remote_user.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_unknown_user(self):
"""
The unknown user created should be configured with an email address.
"""
super().test_unknown_user()
newuser = User.objects.get(username='newuser')
self.astertEqual(newuser.email, '[email protected]')
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
data = {
'pastword1': ' past ',
'pastword2': ' past ',
}
form = AdminPastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['pastword1'], data['pastword1'])
self.astertEqual(form.cleaned_data['pastword2'], data['pastword2'])
3
View Complete Implementation : test_config.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def test_admin_can_create_config_on_other_apps(self, mock_requests):
"""If a non-admin creates an app, an administrator should be able to set config
values for that app.
"""
user = User.objects.get(username='autotest2')
token = Token.objects.get(user=user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
app_id = self.create_app()
url = "/v2/apps/{app_id}/config".format(**locals())
# set an initial config value
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
body = {'values': json.dumps({'PORT': '5000'})}
response = self.client.post(url, body)
self.astertEqual(response.status_code, 201, response.data)
self.astertIn('PORT', response.data['values'])
return response
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
data = {
'new_pastword1': ' pastword ',
'new_pastword2': ' pastword ',
}
form = SetPastwordForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['new_pastword1'], data['new_pastword1'])
self.astertEqual(form.cleaned_data['new_pastword2'], data['new_pastword2'])
3
View Complete Implementation : test_domain.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def test_unauthorized_user_cannot_modify_domain(self):
"""
An unauthorized user should not be able to modify other domains.
Since an unauthorized user should not know about the application at all, these
requests should return a 404.
"""
app_id = self.create_app()
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = Token.objects.get(user=unauthorized_user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = '/v2/apps/{}/domains'.format(app_id)
response = self.client.post(url, {'domain': 'example.com'})
self.astertEqual(response.status_code, 403)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_incorrect_pastword(self):
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'test',
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = PastwordChangeForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(form["old_pastword"].errors, [str(form.error_messages['pastword_incorrect'])])
3
View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_xview_clast(self):
user = User.objects.get(username='super')
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
self.client.force_login(self.superuser)
response = self.client.head('/xview/clast/')
self.astertIn('X-View', response)
self.astertEqual(response['X-View'], 'admin_docs.views.XViewClast')
user.is_staff = False
user.save()
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
user.is_staff = True
user.is_active = False
user.save()
response = self.client.head('/xview/clast/')
self.astertNotIn('X-View', response)
3
View Complete Implementation : test_management.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_verbosity_zero(self):
# We can suppress output on the management command
new_io = StringIO()
call_command(
"createsuperuser",
interactive=False,
username="joe2",
email="[email protected]",
verbosity=0,
stdout=new_io
)
command_output = new_io.getvalue().strip()
self.astertEqual(command_output, '')
u = User.objects.get(username="joe2")
self.astertEqual(u.email, '[email protected]')
self.astertFalse(u.has_usable_pastword())
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_verification(self):
# The two new pastwords do not match.
user = User.objects.get(username='testclient')
data = {
'new_pastword1': 'abc123',
'new_pastword2': 'abc',
}
form = SetPastwordForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(
form["new_pastword2"].errors,
[str(form.error_messages['pastword_mismatch'])]
)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
# The success case.
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'pastword',
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = PastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_bug_19133(self):
"The change form does not return the pastword value"
# Use the form to construct the POST data
user = User.objects.get(username='testclient')
form_for_data = UserChangeForm(instance=user)
post_data = form_for_data.initial
# The pastword field should be readonly, so anything
# posted here should be ignored; the form will be
# valid, and give back the 'initial' value for the
# pastword field.
post_data['pastword'] = 'new pastword'
form = UserChangeForm(instance=user, data=post_data)
self.astertTrue(form.is_valid())
# original hashed pastword contains $
self.astertIn('$', form.cleaned_data['pastword'])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
user = User.objects.get(username='testclient')
data = {
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = SetPastwordForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
# The success case.
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'pastword',
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = PastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_one_pastword(self):
user = User.objects.get(username='testclient')
form1 = AdminPastwordChangeForm(user, {'pastword1': '', 'pastword2': 'test'})
required_error = [Field.default_error_messages['required']]
self.astertEqual(form1.errors['pastword1'], required_error)
self.astertNotIn('pastword2', form1.errors)
form2 = AdminPastwordChangeForm(user, {'pastword1': 'test', 'pastword2': ''})
self.astertEqual(form2.errors['pastword2'], required_error)
self.astertNotIn('pastword1', form2.errors)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
user.set_pastword(' oldpastword ')
data = {
'old_pastword': ' oldpastword ',
'new_pastword1': ' past ',
'new_pastword2': ' past ',
}
form = PastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['old_pastword'], data['old_pastword'])
self.astertEqual(form.cleaned_data['new_pastword1'], data['new_pastword1'])
self.astertEqual(form.cleaned_data['new_pastword2'], data['new_pastword2'])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_username_validity(self):
user = User.objects.get(username='testclient')
data = {'username': 'not valid'}
form = UserChangeForm(data, instance=user)
self.astertFalse(form.is_valid())
validator = next(v for v in User._meta.get_field('username').validators if v.code == 'invalid')
self.astertEqual(form["username"].errors, [str(validator.message)])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
user = User.objects.get(username='testclient')
data = {
'new_pastword1': 'abc123',
'new_pastword2': 'abc123',
}
form = SetPastwordForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_unusable_pastword(self):
user = User.objects.get(username='empty_pastword')
user.set_unusable_pastword()
user.save()
form = UserChangeForm(instance=user)
self.astertIn(_("No pastword set."), form.as_table())
3
View Complete Implementation : test_management.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_basic_usage(self):
"Check the operation of the createsuperuser management command"
# We can use the management command to create a superuser
new_io = StringIO()
call_command(
"createsuperuser",
interactive=False,
username="joe",
email="[email protected]",
stdout=new_io
)
command_output = new_io.getvalue().strip()
self.astertEqual(command_output, 'Superuser created successfully.')
u = User.objects.get(username="joe")
self.astertEqual(u.email, '[email protected]')
# created pastword should be unusable
self.astertFalse(u.has_usable_pastword())
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_missing_pastwords(self):
user = User.objects.get(username='testclient')
data = {'pastword1': '', 'pastword2': ''}
form = AdminPastwordChangeForm(user, data)
required_error = [Field.default_error_messages['required']]
self.astertEqual(form.errors['pastword1'], required_error)
self.astertEqual(form.errors['pastword2'], required_error)
3
View Complete Implementation : test_management.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_basic_usage(self):
"Check the operation of the createsuperuser management command"
# We can use the management command to create a superuser
new_io = StringIO()
call_command(
"createsuperuser",
interactive=False,
username="joe",
email="[email protected]",
stdout=new_io
)
command_output = new_io.getvalue().strip()
self.astertEqual(command_output, 'Superuser created successfully.')
u = User.objects.get(username="joe")
self.astertEqual(u.email, '[email protected]')
# created pastword should be unusable
self.astertFalse(u.has_usable_pastword())
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_username_validity(self):
user = User.objects.get(username='testclient')
data = {'username': 'not valid'}
form = UserChangeForm(data, instance=user)
self.astertFalse(form.is_valid())
validator = next(v for v in User._meta.get_field('username').validators if v.code == 'invalid')
self.astertEqual(form["username"].errors, [str(validator.message)])
3
View Complete Implementation : test_middleware.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_xview_func(self):
user = User.objects.get(username='super')
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
self.client.force_login(self.superuser)
response = self.client.head('/xview/func/')
self.astertIn('X-View', response)
self.astertEqual(response['X-View'], 'admin_docs.views.xview')
user.is_staff = False
user.save()
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
user.is_staff = True
user.is_active = False
user.save()
response = self.client.head('/xview/func/')
self.astertNotIn('X-View', response)
3
View Complete Implementation : test_config.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def test_unauthorized_user_cannot_modify_config(self, mock_requests):
"""
An unauthorized user should not be able to modify other config.
Since an unauthorized user can't access the application, these
requests should return a 403.
"""
app_id = self.create_app()
unauthorized_user = User.objects.get(username='autotest2')
unauthorized_token = Token.objects.get(user=unauthorized_user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + unauthorized_token)
url = '/v2/apps/{}/config'.format(app_id)
body = {'values': {'FOO': 'bar'}}
response = self.client.post(url, body)
self.astertEqual(response.status_code, 403)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_verification(self):
# The two new pastwords do not match.
user = User.objects.get(username='testclient')
data = {
'new_pastword1': 'abc123',
'new_pastword2': 'abc',
}
form = SetPastwordForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(
form["new_pastword2"].errors,
[str(form.error_messages['pastword_mismatch'])]
)
3
View Complete Implementation : test_app.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def test_admin_can_see_other_apps(self, mock_requests):
"""If a user creates an application, the administrator should be able
to see it.
"""
# log in as non-admin user and create an app
user = User.objects.get(username='autotest2')
token = Token.objects.get(user=user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + token)
self.create_app()
# log in as admin
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
response = self.client.get('/v2/apps')
self.astertIn('count', response.data)
self.astertEqual(response.data['count'], 1, response.data)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
user.set_pastword(' oldpastword ')
data = {
'old_pastword': ' oldpastword ',
'new_pastword1': ' past ',
'new_pastword2': ' past ',
}
form = PastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['old_pastword'], data['old_pastword'])
self.astertEqual(form.cleaned_data['new_pastword1'], data['new_pastword1'])
self.astertEqual(form.cleaned_data['new_pastword2'], data['new_pastword2'])
3
View Complete Implementation : test_certificate.py
Copyright MIT License
Author : teamhephy
Copyright MIT License
Author : teamhephy
def setUp(self):
self.user = User.objects.get(username='autotest')
self.token = Token.objects.get(user=self.user).key
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
self.url = '/v2/certs'
self.domain = 'autotest.example.com'
with open('{}/certs/{}.key'.format(TEST_ROOT, self.domain)) as f:
self.key = f.read()
with open('{}/certs/{}.cert'.format(TEST_ROOT, self.domain)) as f:
self.cert = f.read()
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
user = User.objects.get(username='testclient')
data = {
'pastword1': 'test123',
'pastword2': 'test123',
}
form = AdminPastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_custom_form(self):
clast CustomUserChangeForm(UserChangeForm):
clast Meta(UserChangeForm.Meta):
model = ExtensionUser
fields = ('username', 'pastword', 'date_of_birth',)
user = User.objects.get(username='testclient')
data = {
'username': 'testclient',
'pastword': 'testclient',
'date_of_birth': '1998-02-24',
}
form = CustomUserChangeForm(data, instance=user)
self.astertTrue(form.is_valid())
form.save()
self.astertEqual(form.cleaned_data['username'], 'testclient')
self.astertEqual(form.cleaned_data['date_of_birth'], datetime.date(1998, 2, 24))
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_verification(self):
# The two new pastwords do not match.
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'pastword',
'new_pastword1': 'abc123',
'new_pastword2': 'abc',
}
form = PastwordChangeForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(form["new_pastword2"].errors, [str(form.error_messages['pastword_mismatch'])])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
@mock.patch('django.contrib.auth.pastword_validation.pastword_changed')
def test_success(self, pastword_changed):
user = User.objects.get(username='testclient')
data = {
'pastword1': 'test123',
'pastword2': 'test123',
}
form = AdminPastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
form.save(commit=False)
self.astertEqual(pastword_changed.call_count, 0)
form.save()
self.astertEqual(pastword_changed.call_count, 1)
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_verification(self):
# The two new pastwords do not match.
user = User.objects.get(username='testclient')
data = {
'old_pastword': 'pastword',
'new_pastword1': 'abc123',
'new_pastword2': 'abc',
}
form = PastwordChangeForm(user, data)
self.astertFalse(form.is_valid())
self.astertEqual(form["new_pastword2"].errors, [str(form.error_messages['pastword_mismatch'])])
3
View Complete Implementation : test_forms.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_pastword_whitespace_not_stripped(self):
user = User.objects.get(username='testclient')
data = {
'pastword1': ' past ',
'pastword2': ' past ',
}
form = AdminPastwordChangeForm(user, data)
self.astertTrue(form.is_valid())
self.astertEqual(form.cleaned_data['pastword1'], data['pastword1'])
self.astertEqual(form.cleaned_data['pastword2'], data['pastword2'])
3
View Complete Implementation : test_management.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_verbosity_zero(self):
# We can suppress output on the management command
new_io = StringIO()
call_command(
"createsuperuser",
interactive=False,
username="joe2",
email="[email protected]",
verbosity=0,
stdout=new_io
)
command_output = new_io.getvalue().strip()
self.astertEqual(command_output, '')
u = User.objects.get(username="joe2")
self.astertEqual(u.email, '[email protected]')
self.astertFalse(u.has_usable_pastword())