def test_search_users(self): Profile.create_new_user('uniquetestuser', '*****@*****.**', 'password1', date.today()) found = Profile.search('uniquetest') #for profile in found: # user = profile.user self.assertTrue(len(found) > 0)
def test_search_for_profile_0_chars(self): Profile.create_new_user('uniquetestuser', '*****@*****.**', 'password1', date.today()) with self.assertRaises( InvalidSearchException ): # Verify that exception is raised, as it should be Profile.search( '') # A search with 0 characters should throw an exception
def test_delete_review_user_is_not_admin(self): profile_author = Profile.create_new_user('test11', '*****@*****.**', 'test11', date.today()) profile_nonauthor = Profile.create_new_user('test22', '*****@*****.**', 'test22', date.today()) movie = Movie.get_details(5) new_review = Review() new_review.review_title = 'Test review' new_review.review_body = 'body' new_review.date_created = date.today() new_review.user = profile_author new_review.movie = movie new_review.save() with self.assertRaises(Exception): # Verify that error is raised when trying to do this since user is not admin new_review.delete(profile_nonauthor.user)
def test_delete_review_user_is_not_admin(self): profile_author = Profile.create_new_user('test11', '*****@*****.**', 'test11', date.today()) profile_nonauthor = Profile.create_new_user('test22', '*****@*****.**', 'test22', date.today()) movie = Movie.get_details(5) new_review = Review() new_review.review_title = 'Test review' new_review.review_body = 'body' new_review.date_created = date.today() new_review.user = profile_author new_review.movie = movie new_review.save() with self.assertRaises( Exception ): # Verify that error is raised when trying to do this since user is not admin new_review.delete(profile_nonauthor.user)
def test_get_overall_most_popular(self): profile = Profile.create_new_user('testing5', '*****@*****.**', 'testing5', date.today()) movie = Movie.get_details(11) Rating.set_rating_for_user(movie,4,profile) movie = Movie.get_details(12) Rating.set_rating_for_user(movie,5,profile) movie = Movie.get_details(13) Rating.set_rating_for_user(movie,2,profile) results = Movie.get_popular(min_rating=3) self.assertTrue(results['total_items'] == 2)
def test_delete_review_user_is_writer_of_review(self): profile = Profile.create_new_user('test11', '*****@*****.**', 'test11', date.today()) movie = Movie.get_details(5) new_review = Review() new_review.review_title = 'Test review' new_review.review_body = 'body' new_review.date_created = date.today() new_review.user = profile new_review.movie = movie new_review.save() new_review.delete(profile.user)
def test_get_overall_most_popular(self): profile = Profile.create_new_user('testing5', '*****@*****.**', 'testing5', date.today()) movie = Movie.get_details(11) Rating.set_rating_for_user(movie, 4, profile) movie = Movie.get_details(12) Rating.set_rating_for_user(movie, 5, profile) movie = Movie.get_details(13) Rating.set_rating_for_user(movie, 2, profile) results = Movie.get_popular(min_rating=3) self.assertTrue(results['total_items'] == 2)
def rate(request, rating_id, approve): target_user = Profile.find(request.GET['review_of']) target_movie = Movie.objects.filter(m_id=int(rating_id))[0] review_of = Review.objects.filter(movie=target_movie, user=target_user)[0] user = Profile.get(request.user) already_exists = ReviewRating.objects.filter(review=review_of, user=user) """for entry in already_exists: entry.delete()""" if not len(already_exists): review_rating = ReviewRating(review=review_of, user=user, vote=approve) review_rating.save() else: if already_exists[0].vote == int(approve): already_exists[0].delete() else: already_exists[0].vote = approve already_exists[0].save() return HttpResponseRedirect(request.META['HTTP_REFERER'])
def test_get_popular_movies_min_5_stars(self): profile = Profile.create_new_user('testing1', '*****@*****.**', 'testing1', date.today()) movie = Movie.get_details(11) Rating.set_rating_for_user(movie, 1, profile) movie = Movie.get_details(12) Rating.set_rating_for_user(movie, 2, profile) movie = Movie.get_details(13) Rating.set_rating_for_user(movie, 3, profile) movie = Movie.get_details(513) Rating.set_rating_for_user(movie, 4, profile) movie = Movie.get_details(5) Rating.set_rating_for_user(movie, 5, profile) # There should only be one result when getting movies with rating 5 or higher self.assertTrue(Movie.get_popular(min_rating=5)['total_items'] == 1)
def test_get_popular_movies_min_1_star(self): profile = Profile.create_new_user('testing1', '*****@*****.**', 'testing1', date.today()) movie = Movie.get_details(11) Rating.set_rating_for_user(movie, 1, profile) movie = Movie.get_details(12) Rating.set_rating_for_user(movie, 2, profile) movie = Movie.get_details(13) Rating.set_rating_for_user(movie, 3, profile) movie = Movie.get_details(513) Rating.set_rating_for_user(movie, 4, profile) movie = Movie.get_details(5) Rating.set_rating_for_user(movie, 5, profile) # All 5 movies should show up as results when the minimum rating required for a movie to be 'popular' is a 1 self.assertTrue(Movie.get_popular(min_rating=1)['total_items'] == 5)
def authentication_info(request): ''' This context processor adds all the user info into the page that is needed for the login form and signup form that appear in the header of every page, as well as other basic information about the state of the current user. This way the code is DRY-er: we don't need to copy these fields into every page handler. ''' profile = Profile.get(request.user) return { 'login_form': AuthenticationForm(), 'signup_form': CreateAccountForm(), 'username': request.user.username, 'is_authenticated': request.user.is_active and request.user.is_authenticated(), 'is_admin': profile.user.is_superuser if profile is not None else None }
def edit_review(request, review_id): if request.method == 'POST': new_title = request.POST['new_review_title'] new_body = request.POST['new_review_body'] current_user = Profile.get(request.user) current_movie = Movie.objects.filter(m_id=review_id)[0] review = Review.objects.filter(user=current_user, movie=current_movie)[0] review.approved = False review.review_title = new_title review.review_body = new_body review.date_edited = datetime.today() review.save() return HttpResponseRedirect(request.META['HTTP_REFERER']) else: return HttpResponse("Unknown edit review request")
def test_search_for_profile_2000_chars(self): Profile.create_new_user('uniquetestuser', '*****@*****.**', 'password1', date.today()) profile = Profile.search('t' * 2000) self.assertTrue(len(profile) == 0)
def test_find_user_when_does_not_exist(self): profile = Profile.create_new_user('testuser1', '*****@*****.**', 'password1', date.today()) profile_found = Profile.find('auserthatdoesntexist') self.assertTrue(profile_found is None)
def test_find_user_when_exists(self): profile = Profile.create_new_user('testuser1', '*****@*****.**', 'password1', date.today()) profile_found = Profile.find('testuser1') self.assertTrue(profile_found == profile)
def test_search_for_profile_9999999_chars(self): Profile.create_new_user('uniquetestuser', '*****@*****.**', 'password1', date.today()) with self.assertRaises(InvalidSearchException): Profile.search('t' * 9999999)
def test_delete_review_no_user_passed_in(self): profile = Profile.create_new_user('test11', '*****@*****.**', 'test11', date.today()) movie = Movie.get_details(5) new_review = Review() new_review.review_title = 'Test review' new_review.review_body = 'body' new_review.date_created = date.today() new_review.user = profile new_review.movie = movie new_review.save() with self.assertRaises(Exception): # Verify that error is raised when trying to do this since user is not admin new_review.delete() # class BrowserTests(TestCase): # """ # Tests urls and views code. # """ # @classmethod # def setUpClass(cls): # cls.browser = webdriver.Chrome() # cls.uname = uuid1().hex[:30] # @classmethod # def tearDownClass(cls): # cls.browser.close() # # Check the spotlight redirects to the appropriate movie detail page # def spotlight(self): # self.browser.get("http://127.0.0.1:8000/") # self.browser.find_element_by_id("movie-spotlight").find_element_by_tag_name("img").click() # self.assertEqual(self.browser.current_url, "http://127.0.0.1:8000/movie/detail/5/") # # Metamorphic Property: if the user is not logged in, he should not be able to rate movies # def rating_absence(self): # self.browser.get("http://127.0.0.1:8000/movie/detail/5/") # self.assertFalse(self.browser.find_elements_by_id("rating-stars")) # # Tests properties that must be done independent of login # def test_misc(self): # self.spotlight() # self.rating_absence() # # Metamorphic Property: if the user is not logged in, he should not be able to rate movies # def signup(self): # self.browser.get("http://127.0.0.1:8000") # self.browser.find_element_by_class_name("signup-link").click() # form = self.browser.find_element_by_id("signupForm") # form.find_element_by_id("id_email_address").send_keys("foo@foo") # form.find_element_by_id("id_username").send_keys(self.uname) # form.find_element_by_id("id_password1").send_keys("foofoo") # form.find_element_by_id("id_password2").send_keys("foofoo") # form.submit() # sleep(1) # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1].click() # expected_url = u"http://127.0.0.1:8000/profile/%s/" % self.uname # self.assertEqual(self.browser.current_url, expected_url) # # Metamorphic Property: if the user is not logged in, the middle button in the header should # # be the Log In button # def logout(self): # self.browser.get("http://127.0.0.1:8000/") # sleep(1) # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[2].click() # sleep(1) # mid_button = self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1] # self.assertTrue(mid_button.text, "Log In") # # Metamorphic Property: if the user is not logged in, the middle button in the header should # # be the Profile button # def login(self): # self.browser.get("http://127.0.0.1:8000") # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1].click() # form = self.browser.find_element_by_id("loginForm") # form.find_element_by_id("id_username").send_keys(self.uname) # form.find_element_by_id("id_password").send_keys("foofoo") # form.submit() # sleep(1) # mid_button = self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1] # self.assertTrue(mid_button.text, "Profile") # # Tests user creation as well as login/logout # def test_user(self): # self.signup() # self.logout() # self.login() # self.logout()
def test_search_for_profile_0_chars(self): Profile.create_new_user('uniquetestuser', '*****@*****.**', 'password1', date.today()) with self.assertRaises(InvalidSearchException): # Verify that exception is raised, as it should be Profile.search('') # A search with 0 characters should throw an exception
def test_delete_review_no_user_passed_in(self): profile = Profile.create_new_user('test11', '*****@*****.**', 'test11', date.today()) movie = Movie.get_details(5) new_review = Review() new_review.review_title = 'Test review' new_review.review_body = 'body' new_review.date_created = date.today() new_review.user = profile new_review.movie = movie new_review.save() with self.assertRaises( Exception ): # Verify that error is raised when trying to do this since user is not admin new_review.delete() # class BrowserTests(TestCase): # """ # Tests urls and views code. # """ # @classmethod # def setUpClass(cls): # cls.browser = webdriver.Chrome() # cls.uname = uuid1().hex[:30] # @classmethod # def tearDownClass(cls): # cls.browser.close() # # Check the spotlight redirects to the appropriate movie detail page # def spotlight(self): # self.browser.get("http://127.0.0.1:8000/") # self.browser.find_element_by_id("movie-spotlight").find_element_by_tag_name("img").click() # self.assertEqual(self.browser.current_url, "http://127.0.0.1:8000/movie/detail/5/") # # Metamorphic Property: if the user is not logged in, he should not be able to rate movies # def rating_absence(self): # self.browser.get("http://127.0.0.1:8000/movie/detail/5/") # self.assertFalse(self.browser.find_elements_by_id("rating-stars")) # # Tests properties that must be done independent of login # def test_misc(self): # self.spotlight() # self.rating_absence() # # Metamorphic Property: if the user is not logged in, he should not be able to rate movies # def signup(self): # self.browser.get("http://127.0.0.1:8000") # self.browser.find_element_by_class_name("signup-link").click() # form = self.browser.find_element_by_id("signupForm") # form.find_element_by_id("id_email_address").send_keys("foo@foo") # form.find_element_by_id("id_username").send_keys(self.uname) # form.find_element_by_id("id_password1").send_keys("foofoo") # form.find_element_by_id("id_password2").send_keys("foofoo") # form.submit() # sleep(1) # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1].click() # expected_url = u"http://127.0.0.1:8000/profile/%s/" % self.uname # self.assertEqual(self.browser.current_url, expected_url) # # Metamorphic Property: if the user is not logged in, the middle button in the header should # # be the Log In button # def logout(self): # self.browser.get("http://127.0.0.1:8000/") # sleep(1) # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[2].click() # sleep(1) # mid_button = self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1] # self.assertTrue(mid_button.text, "Log In") # # Metamorphic Property: if the user is not logged in, the middle button in the header should # # be the Profile button # def login(self): # self.browser.get("http://127.0.0.1:8000") # self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1].click() # form = self.browser.find_element_by_id("loginForm") # form.find_element_by_id("id_username").send_keys(self.uname) # form.find_element_by_id("id_password").send_keys("foofoo") # form.submit() # sleep(1) # mid_button = self.browser.find_element_by_id("header-buttons").find_elements_by_class_name("header-button")[1] # self.assertTrue(mid_button.text, "Profile") # # Tests user creation as well as login/logout # def test_user(self): # self.signup() # self.logout() # self.login() # self.logout()