Guest User

Untitled

a guest
Jul 12th, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. import re
  2.  
  3. from django.test import TestCase, Client
  4. from django.contrib.auth.models import User
  5.  
  6.  
  7. LINK_PATTERN = re.compile(r'<a href="(/haldus/[^ ]+)"')
  8.  
  9.  
  10. class TestDjangoAdmin(TestCase):
  11.  
  12. def test_if_admin_pages_work(self):
  13. """
  14. If there is something wrong with migrations,
  15. related admin pages are not accessable.
  16. Checking whether the admin pages work is thus
  17. a good opportunity to control whether
  18. the structure of the database is OK.
  19. """
  20. # Create user and log in
  21. password = 'adminadminadmin'
  22. user = User.objects.create_superuser('myuser', 'myemail@test.com', password)
  23. client = Client()
  24. login = client.login(username=user.username, password=password)
  25. self.assertEqual(login, True)
  26.  
  27. # Go to admin homepage
  28. homepage_response = client.get('/haldus/')
  29. self.assertEqual(homepage_response.status_code, 200)
  30.  
  31. # # Find all links on that page
  32. html = homepage_response.content.decode('utf-8')
  33. matches = LINK_PATTERN.finditer(html)
  34.  
  35. # Clear results to get URLs
  36. # There are a lot duplicate URLs on the page. To remove them, we use a set instead of a list.
  37. # We do not want to change password or log out, therefore theese URLs are excluded.
  38. urls = {match.groups()[0] for match in matches} - {'/haldus/password_change/', '/haldus/logout/'}
  39.  
  40. # Visit URLs and check response.status_code
  41. for url in urls:
  42. response = client.get(url)
  43. self.assertEqual(response.status_code, 200, msg=f"This url cannot be accessed: '{url}'")
Add Comment
Please, Sign In to add comment