test_views.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from django.test import LiveServerTestCase
  2. from model_mommy import mommy
  3. from selenium import webdriver
  4. from selenium.webdriver.support import expected_conditions as EC
  5. from selenium.webdriver.support.ui import WebDriverWait
  6. from users.models import CCUser
  7. from website.models import Artist, Album, Song, artist_path, album_path, song_path
  8. class ViewTests(LiveServerTestCase):
  9. def setUp(self):
  10. #self.selenium = webdriver.Remote('localhost:8000', webdriver.DesiredCapabilities.HTMLUNIT.copy())
  11. #self.selenium = webdriver.Firefox()
  12. self.selenium = webdriver.Chrome()
  13. self.selenium.maximize_window()
  14. super(ViewTests, self).setUp()
  15. self.testuser = mommy.prepare(CCUser, username='somedude')
  16. self.test_password = '1l1k3ccr4d10'
  17. self.user = CCUser.objects.create_user(username=self.testuser.username, email='myfake@email.com', password=self.test_password)
  18. self.admin = CCUser.objects.create_superuser(username='admin', email='admin@admin.com', password='admin')
  19. self.artist1 = mommy.make(Artist)
  20. self.album11 = mommy.make(Album, artist=self.artist1)
  21. self.song111 = mommy.make(Song, artist=self.artist1, album=self.album11)
  22. self.song112 = mommy.make(Song, artist=self.artist1, album=self.album11)
  23. self.album12 = mommy.make(Album, artist=self.artist1)
  24. self.song121 = mommy.make(Song, artist=self.artist1, album=self.album12)
  25. self.song122 = mommy.make(Song, artist=self.artist1, album=self.album12)
  26. self.artist2 = mommy.make(Artist)
  27. self.album21 = mommy.make(Album, artist=self.artist2)
  28. self.song211 = mommy.make(Song, artist=self.artist2, album=self.album21)
  29. self.song212 = mommy.make(Song, artist=self.artist2, album=self.album21)
  30. self.album22 = mommy.make(Album, artist=self.artist2)
  31. self.song221 = mommy.make(Song, artist=self.artist2, album=self.album22)
  32. self.song222 = mommy.make(Song, artist=self.artist2, album=self.album22)
  33. def tearDown(self):
  34. self.selenium.quit()
  35. super(ViewTests, self).tearDown()
  36. def test_create_user(self):
  37. selenium = self.selenium
  38. selenium.get(self.live_server_url + '/users/signup/')
  39. createduser = mommy.prepare(CCUser)
  40. try:
  41. #WebDriverWait(selenium, 10).until(EC.title_contains('Sign Up'))
  42. username = selenium.find_element_by_id('id_username')
  43. email = selenium.find_element_by_id('id_email')
  44. password1 = selenium.find_element_by_id('id_password1')
  45. password2 = selenium.find_element_by_id('id_password2')
  46. username.send_keys(createduser.username)
  47. email.send_keys(createduser.email)
  48. password1.send_keys(self.test_password)
  49. password2.send_keys(self.test_password)
  50. username.submit()
  51. #WebDriverWait(selenium, 10).until(EC.title_contains('Login'))
  52. self.assertTrue('Login' in selenium.title)
  53. finally:
  54. selenium.quit()
  55. def test_login(self):
  56. selenium = self.selenium
  57. selenium.get(self.live_server_url + '/users/login/')
  58. try:
  59. #WebDriverWait(selenium, 10).until(EC.login_contains('Login'))
  60. username = selenium.find_element_by_id('id_username')
  61. password = selenium.find_element_by_id('id_password')
  62. username.send_keys(self.testuser.username)
  63. password.send_keys(self.test_password)
  64. username.submit()
  65. WebDriverWait(selenium, 360).until(EC.title_contains('CCRadio'))
  66. # Assertion: User is taken back to welcome page
  67. self.assertTrue('CCRadio' in selenium.title)
  68. # Assertion: User's username is listed in navbar
  69. selenium.switch_to.frame('navbar')
  70. usermenu = selenium.find_element_by_id('user-menu')
  71. self.assertTrue(self.testuser.username in usermenu.text)
  72. # Assertion: Logout link is shown in navbar
  73. logout_link = usermenu.find_element_by_link_text('Logout')
  74. self.assertTrue(logout_link.is_displayed())
  75. finally:
  76. selenium.quit()
  77. def test_artists_page(self):
  78. self.artists_page(False)
  79. self.artists_page(True)
  80. def artists_page(self, logged_in):
  81. if(logged_in):
  82. self.client.login(username=self.testuser.username, password=self.test_password)
  83. selenium = self.selenium
  84. selenium.get(self.live_server_url + '/artists')
  85. WebDriverWait(selenium, 360).until(EC.title_contains('Artists'))
  86. pagebody = selenium.find_element_by_id('artists_body')
  87. # There should be two artists. Check that both are listed.
  88. self.assertTrue(self.artist1.name in pagebody.text)
  89. self.assertTrue(self.artist2.name in pagebody.text)
  90. def test_artist_page(self):
  91. self.artist_page(False)
  92. self.artist_page(True)
  93. def artist_page(self, logged_in):
  94. if(logged_in):
  95. self.client.login(username=self.testuser.username, password=self.test_password)
  96. for artist in Artist.objects.all():
  97. self.validate_artist_page(artist)
  98. def validate_artist_page(self, artist):
  99. selenium = self.selenium
  100. selenium.get(self.live_server_url + '/' + artist.handle)
  101. WebDriverWait(selenium, 360).until(EC.title_contains('Artist:'))
  102. pagebody = selenium.find_element_by_id('artist_body')
  103. for album in Album.objects.filter(artist=artist.handle):
  104. self.assertTrue(album.title in pagebody.text)
  105. def test_album_page(self):
  106. self.album_page(False)
  107. self.album_page(True)
  108. def album_page(self, logged_in):
  109. if(logged_in):
  110. self.client.login(username=self.testuser.username, password=self.test_password)
  111. for album in Album.objects.all():
  112. self.validate_album_page(album)
  113. def validate_album_page(self, album):
  114. selenium = self.selenium
  115. selenium.get(self.live_server_url + '/' + album.artist.handle + '/' + album.slug)
  116. WebDriverWait(selenium, 360).until(EC.title_contains('Album:'))
  117. pagebody = selenium.find_element_by_id('album_body')
  118. for song in Song.objects.filter(artist=album.artist, album=album):
  119. self.assertTrue(song.title in pagebody.text)