Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.61 KB | None | 0 0
  1. from django.core.management import call_command
  2. from django.test import Client
  3. from django.test import TestCase
  4.  
  5.  
  6. class LibrarySystemTest(TestCase):
  7. def setUp(self):
  8. call_command('loaddata', 'library_server/fixtures/test_data.json', verbosity=0)
  9.  
  10. def test_database_and_user_login(self):
  11. self.assertTrue(self.client.login(username='jacob', password='password1'))
  12. self.assertFalse(self.client.login(username='jacob', password='wrong password'))
  13.  
  14. def test_user_login_successful(self):
  15. c = Client()
  16. response = c.post('/accounts/login/', {'username': 'jacob', 'password': 'password1'})
  17. self.assertRedirects(response, '/accounts/profile/')
  18. self.assertEqual(response.status_code, 302)
  19.  
  20. def test_user_login_failure(self):
  21. c = Client()
  22. response = c.post('/accounts/login/', {'password': 'wrong_password'})
  23. self.assertEqual(response.status_code, 200)
  24. self.assertTemplateUsed(response, 'registration/login.html')
  25. self.assertFormError(response, 'form', 'username', 'This field is required.')
  26. response = c.post('/accounts/login/', {'username': 'jacob'})
  27. self.assertEqual(response.status_code, 200)
  28. self.assertTemplateUsed(response, 'registration/login.html')
  29. self.assertFormError(response, 'form', 'password', 'This field is required.')
  30. response = c.post('/accounts/login/', {'username': 'jacob', 'password': 'wrong_password'})
  31. self.assertContains(response, "Your username and password didn't match. Please try again.", 1, 200)
  32.  
  33. def test_user_registration_for_authenticated_user(self):
  34. c = Client()
  35. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  36. response = c.get('/registration/')
  37. self.assertEqual(response.status_code, 302)
  38. self.assertRedirects(response, '/main_page')
  39.  
  40. def test_user_registration_for_unauthenticated_user(self):
  41. c = Client()
  42. response = c.get('/registration/')
  43. self.assertEqual(response.status_code, 200)
  44. self.assertTemplateUsed(response, 'library_server/registration.html')
  45.  
  46. def test_user_registration_form(self):
  47. c = Client()
  48. response = c.post('/registration/', {'username': 'jacob', 'email': 'dummy_mail'})
  49. self.assertFormError(response, 'form', 'email', 'Enter a valid email address.')
  50. response = c.post('/registration/', {'username': 'jacob', 'email': 'dummy_mail@yahoo.com'})
  51. self.assertFormError(response, 'form', 'password1', 'This field is required.')
  52. response = c.post('/registration/', {'username': 'jacob', 'email': 'dummy_mail@yahoo.com', 'password1': 'my_secret'})
  53. self.assertFormError(response, 'form', 'password2', 'This field is required.')
  54. response = c.post('/registration/',
  55. {'username': 'jacob', 'email': 'dummy_mail@yahoo.com', 'password1': 'my_secret', 'password2': 'not my_secret'})
  56. self.assertContains(response, "The two password fields didn't match.", 1, 200)
  57. self.assertContains(response, "A user with that username already exists.", 1, 200)
  58. response = c.post('/registration/',
  59. {'username': 'Nic', 'email': 'dummy_mail@yahoo.com', 'password1': 'my_secret', 'password2': 'my_secret'})
  60. self.assertEqual(response.status_code, 302)
  61. self.assertRedirects(response, '/main_page')
  62.  
  63. def test_user_borrow_for_unauthenticated_user(self):
  64. c = Client()
  65. response = c.get('/borrow/1/')
  66. self.assertEqual(response.status_code, 302)
  67. self.assertRedirects(response, '/accounts/login/?next=/borrow/1/')
  68.  
  69. def test_user_borrow_for_authenticated_user(self):
  70. c = Client()
  71. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  72. response = c.get('/borrow/200/')
  73. self.assertEqual(response.status_code, 404)
  74. response = c.get('/borrow/1/')
  75. self.assertContains(response, "book borrowing successful", 1, 200)
  76. response = c.get('/borrow/1/')
  77. self.assertContains(response, "Ali has already borrowed that book before", 1, 200)
  78.  
  79. def test_view_book_details_for_unauthenticated_user(self):
  80. c = Client()
  81. response = c.get('/books/200/')
  82. self.assertEqual(response.status_code, 302)
  83. self.assertRedirects(response, '/accounts/login/?next=/books/200/')
  84.  
  85. def test_view_book_details_for_authenticated_user(self):
  86. c = Client()
  87. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  88. response = c.get('/books/200/')
  89. self.assertEqual(response.status_code, 404)
  90. response = c.get('/books/2/')
  91. self.assertContains(response, "Title : Diary of a Wimpy Kid # 11: Double Down", 1, 200)
  92. self.assertContains(response, "ISBN : 978-1419723445", 1, 200)
  93.  
  94. def test_user_borrow_for_authenticated_user_no_avail_copies(self):
  95. c = Client()
  96. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  97. response = c.get('/borrow/3/')
  98. self.assertContains(response, "Sorry there is no available copies at the moment", 1, 200)
  99. self.assertContains(response, "Your request has been saved and will be updated automatically", 1, 200)
  100.  
  101. def test_user_return_book_for_authenticated_user(self):
  102. c = Client()
  103. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  104. response = c.get('/borrow/1/')
  105. self.assertContains(response, "book borrowing successful", 1, 200)
  106. response = c.get('/return/1/')
  107. self.assertContains(response, "book return successful", 1, 200)
  108.  
  109. def test_user_return_unborrowed_book_for_authenticated_user(self):
  110. c = Client()
  111. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  112. response = c.get('/return/1/')
  113. self.assertContains(response, "Ali didn't borrow that book", 1, 200)
  114.  
  115. def test_user_return_book_not_existing(self):
  116. c = Client()
  117. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  118. response = c.get('/return/10/')
  119. self.assertEqual(response.status_code, 404)
  120.  
  121.  
  122.  
  123. def test_user_return_book_for_unauthenticated_user(self):
  124. c = Client()
  125. response = c.get('/borrow/1/')
  126. self.assertEqual(response.status_code, 302)
  127. self.assertRedirects(response, '/accounts/login/?next=/borrow/1/')
  128. response = c.get('/return/1/')
  129. self.assertEqual(response.status_code, 302)
  130. self.assertRedirects(response, '/accounts/login/?next=/return/1/')
  131.  
  132. def test_user_search_for_book1(self):
  133. c = Client()
  134. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  135. response = c.get('/search/',{'search_box' : 'Whistler'})
  136. self.assertContains(response, "The Whistler", 1, 200)
  137.  
  138. def test_user_search_for_book2(self):
  139. c = Client()
  140. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  141. response = c.get('/search/',{'search_box' : '978-1481466226'})
  142. self.assertContains(response, "Take Heart My Child: A Mother's Dream", 1, 200)
  143.  
  144. def test_user_search_for_book3(self):
  145. c = Client()
  146. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  147. response = c.get('/search/',{'search_box' : 'Bill'})
  148. self.assertContains(response, "Killing the Rising Sun: How America Vanquished World War II Japan", 1, 200)
  149.  
  150. def test_user_search_for_book4(self):
  151. c = Client()
  152. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  153. response = c.get('/search/',{'search_box' : 'Revolution'})
  154. self.assertContains(response, "Hamilton: The Revolution", 1, 200)
  155. self.assertContains(response, "Our Revolution: A Future to Believe In", 1, 200)
  156. def test_user_search_for_book5(self):
  157. c = Client()
  158. c.post('/accounts/login/', {'username': 'Ali', 'password': 'password2'})
  159. response = c.get('/search/',{'search_box' : 'the'})
  160. self.assertContains(response, "Fantastic Beasts and Where to Find Them: The Original Screenplay", 1, 200)
  161. self.assertContains(response, "The Magnolia Story", 1, 200)
  162. self.assertContains(response, "Killing the Rising Sun: How America Vanquished World War II Japan", 1, 200)
  163. self.assertContains(response, "Take Heart My Child: A Mother's Dream", 1, 200)
  164. self.assertContains(response, "Hamilton: The Revolution", 1, 200)
  165. self.assertContains(response, "The Whistler", 1, 200)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement