def test_like_already_hated_movie(self): """ Ensure that user removes hate of a movie when opinion changes to like. """ users = get_sample_users() movies = get_sample_movies(users) movie = movies[0] not_owner = users[-1] set_sample_opinion(not_owner, movie, OPINION_HATE) self.assertEqual(movie.hates.count(), 1) self.assertEqual(movie.hates_counter, 1) url = reverse('movie-opinion', kwargs={'pk': movies[0].pk}) data = {'opinion': OPINION_LIKE} self.client.force_authenticate(user=not_owner) response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) movie.refresh_from_db() self.assertEqual(movie.likes.count(), 1) self.assertEqual(movie.likes_counter, 1) self.assertEqual(movie.hates_counter, 0) self.assertEqual(movie.hates.count(), 0)
def test_cannot_undo_hate_of_other_user(self): """ Ensure that owner of a movie cannot undo the hate of other user for a movie. """ users = get_sample_users() movies = get_sample_movies(users) movie = movies[0] not_owner = users[-1] other_user = users[-2] set_sample_opinion(other_user, movie, OPINION_HATE) self.assertEqual(movie.hates.count(), 1) self.assertEqual(movie.hates_counter, 1) url = reverse('movie-opinion', kwargs={'pk': movies[0].pk}) data = {'opinion': None} self.client.force_authenticate(user=not_owner) response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) movie.refresh_from_db() self.assertEqual(movie.hates_counter, 1) self.assertEqual(movie.hates.count(), 1)
def test_undo_hate_movie_not_owner(self): """ Ensure that not owner of a movie can undo the hate of a movie. """ users = get_sample_users() movies = get_sample_movies(users) movie = movies[0] not_owner = users[-1] set_sample_opinion(not_owner, movie, OPINION_HATE) self.assertEqual(movie.hates.count(), 1) self.assertEqual(movie.hates_counter, 1) url = reverse('movie-opinion', kwargs={'pk': movies[0].pk}) data = {'opinion': None} self.client.force_authenticate(user=not_owner) response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) movie.refresh_from_db() request = get_sample_request(not_owner) expected_response = MovieSerializer(instance=movie, context={ 'request': request }).data self.assertEqual(response.data, expected_response) self.assertEqual(movie.hates_counter, 0) self.assertEqual(movie.hates.count(), 0)
def test_undo_like_movie_owner(self): """ Ensure that owner of a movie cannot undo the like of a movie. """ users = get_sample_users() movies = get_sample_movies(users) movie = movies[0] not_owner = users[-1] set_sample_opinion(not_owner, movie, OPINION_LIKE) self.assertEqual(movie.likes.count(), 1) self.assertEqual(movie.likes_counter, 1) url = reverse('movie-opinion', kwargs={'pk': movies[0].pk}) data = {'opinion': None} self.client.force_authenticate(user=users[0]) response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) movie.refresh_from_db() self.assertEqual(movie.likes_counter, 1) self.assertEqual(movie.likes.count(), 1)
def test_cannot_like_already_liked_movie(self): """ Ensure that user cannot like a movie already liked by him. """ users = get_sample_users() movies = get_sample_movies(users) movie = movies[0] not_owner = users[-1] set_sample_opinion(not_owner, movie, OPINION_LIKE) self.assertEqual(movie.likes_counter, 1) self.assertEqual(movie.likes.count(), 1) url = reverse('movie-opinion', kwargs={'pk': movies[0].pk}) data = {'opinion': OPINION_LIKE} self.client.force_authenticate(user=not_owner) response = self.client.post(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(MovieOpinion.objects.count(), 1) movie.refresh_from_db() request = get_sample_request(not_owner) expected_response = MovieSerializer(instance=movie, context={ 'request': request }).data self.assertEqual(response.data, expected_response) self.assertEqual(movie.likes_counter, 1) self.assertEqual(movie.likes.count(), 1)
def test_liked_movie_is_marked(self): """ Ensure that movie is_liked attribute is marked for liked movie. """ users = get_sample_users() movie = get_sample_movies(users)[0] user = users[-1] set_sample_opinion(user, movie, OPINION_LIKE) request = get_sample_request(user) data = MovieSerializer(instance=movie, context={ 'request': request }).data self.assertTrue(data['is_liked'])
def test_not_hated_movie_is_not_marked(self): """ Ensure that movie is_hated attribute is not marked for movie that is not hated by that user. """ users = get_sample_users() movie = get_sample_movies(users)[0] user = users[-1] other_user = users[-2] set_sample_opinion(user, movie, OPINION_HATE) request = get_sample_request(other_user) data = MovieSerializer(instance=movie, context={ 'request': request }).data self.assertFalse(data['is_hated'])