Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_login_admin_as_user(self):
"""
checks that an admin user can impersonate a regular user via impostor backend
and that the login get reflected into the ImpostorLog table
:return:
"""
assert ImpostorLog.objects.count() == 0
composed_username = '{} as {}'.format(admin_username, user_username)
u = authenticate(username=composed_username, password=admin_pass)
real_user = get_user_model().objects.get(username=user_username)
assert u == real_user
# Check if logs contain an entry now
logs_entries = ImpostorLog.objects.all()
assert len(logs_entries) == 1
entry = logs_entries[0]
# today = datetime.date.today()
# lin = entry.logged_in
assert entry.impostor.username == admin_username
assert entry.imposted_as.username == user_username
:param first_user:
:param password:
:param impersonated_user:
:param expected:
:param custom_settings:
:param rf:
:return:
"""
setattr(rf, 'META', {})
rf.META['HTTP_X_FORWARDED_FOR'] = '127.0.0.1,192.168.0.1'
assert ImpostorLog.objects.count() == 0
composed_username = '{} as {}'.format(first_user.username, impersonated_user.username)
authenticated_user = authenticate(request=rf, username=composed_username, password=password)
if expected == 'ok':
assert authenticated_user == impersonated_user
assert ImpostorLog.objects.count() == 1
log = ImpostorLog.objects.first()
assert log.impostor == first_user
assert log.imposted_as == impersonated_user
assert log.impostor_ip == '127.0.0.1'
else:
assert authenticated_user is None