test_login_to_admin.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from django.urls import reverse
  2. from ...test import assert_contains
  3. from ..auth import is_admin_session
  4. admin_link = reverse("misago:admin:index")
  5. def test_login_form_is_displayed(db, client):
  6. response = client.get(admin_link)
  7. assert response.status_code == 200
  8. assert_contains(response, "Administration")
  9. assert_contains(response, "Sign in")
  10. assert_contains(response, "Username or e-mail")
  11. assert_contains(response, "Password")
  12. def test_attempt_to_login_using_invalid_credentials_fails(db, client):
  13. response = client.post(admin_link, {"username": "no", "password": "no"})
  14. assert_contains(response, "Login or password is incorrect.")
  15. def test_attempt_to_login_using_invalid_password_fails(client, superuser):
  16. response = client.post(
  17. admin_link, {"username": superuser.username, "password": "no"}
  18. )
  19. assert_contains(response, "Login or password is incorrect.")
  20. def test_attempt_to_login_without_staff_status_fails(client, user, user_password):
  21. response = client.post(
  22. admin_link, {"username": user.username, "password": user_password}
  23. )
  24. assert_contains(response, "Your account does not have admin privileges.")
  25. def test_attempt_to_login_as_superuser_without_staff_status_fails(
  26. client, user, user_password
  27. ):
  28. user.is_superuser = True
  29. user.save()
  30. response = client.post(
  31. admin_link, {"username": user.username, "password": user_password}
  32. )
  33. assert_contains(response, "Your account does not have admin privileges.")
  34. def test_user_with_staff_status_is_logged_to_admin(client, staffuser, user_password):
  35. response = client.post(
  36. admin_link, {"username": staffuser.username, "password": user_password}
  37. )
  38. assert is_admin_session(response.wsgi_request)
  39. assert response.wsgi_request.user == staffuser
  40. def test_login_form_redirects_user_to_admin_index_after_successful_login(
  41. client, staffuser, user_password
  42. ):
  43. response = client.post(
  44. admin_link, {"username": staffuser.username, "password": user_password}
  45. )
  46. assert response["location"] == admin_link