def test_post_works_for_multiple_authenticated_user_that_has_not_already_voted(self): user1 = Helpers.getUser(Helpers.USER1) user2 = Helpers.getUser(Helpers.USER2) user3 = Helpers.getUser(Helpers.USER3) vote = self.createVote() vote_data1 = {"id": vote.id, "vote": True} vote_data2 = {"id": vote.id, "vote": False} vote_data3 = {"id": vote.id, "vote": True} url = self.url.format(vote.composition.id) self.client.login( username=user1.email, password=Helpers.USER1["password"]) response = self.client.post(url, vote_data1) self.client.logout() self.client.login( username=user2.email, password=Helpers.USER2["password"]) response = self.client.post(url, vote_data2) self.client.logout() self.client.login( username=user3.email, password=Helpers.USER3["password"]) response = self.client.post(url, vote_data3) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data, self.createResponse(vote.composition.id, 2, 1)) vote = self.refreshFromDB(vote) self.assertEqual(vote.positive, 2) self.assertEqual(vote.negative, 1) self.assertEqual(vote.voters.all().count(), 3)
def test_post_throws_forbidden_for_authenticated_user_not_himself(self): user = Helpers.getUser(Helpers.USER1) other_user = Helpers.getUser(Helpers.USER2) composition = Helpers.createComposition(user) bookmark_data = {'bookmarks': [composition.id]} url = self.url.format(user.id) self.client.login( username=other_user.email, password=Helpers.USER2["password"]) response = self.client.post(url, bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(user.bookmarks.all().count(), 0)
def test_get_throws_forbidden_when_user_not_himself(self): user = Helpers.getUser(Helpers.USER1) other_user = Helpers.getUser(Helpers.USER2) composition1 = Helpers.createComposition(user) composition2 = Helpers.createComposition(user) user.bookmarks.add(composition1, composition2) url = self.url.format(user.id) self.client.login( username=other_user.email, password=Helpers.USER2["password"]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_returns_forbidden_for_user_not_interpretationer(self): user1 = Helpers.getUser(user_data=Helpers.USER1) Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation = Interpretation.objects.create( user=user1, composition=composition, interpretation="interpretation1" ) url = "/compositions/{0}/interpretations/{1}".format(composition.id, interpretation.id) self.client.login(username="******", password="******") response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_post_for_authenticated_user_empty_comment(self): Helpers.getUser(Helpers.USER2) interpretation = Helpers.createInterpretation() comment_data = {'comment': ''} url = '/compositions/{0}/interpretations/{1}/comments'.format(interpretation.composition.id, interpretation.id) self.client.login(username='******', password='******') response = self.client.post(url, data=comment_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, {"comment": ["This field is required."]}) self.assertEqual(Comment.objects.all().count(), 0)
def test_post_for_authenticated_user_empty_interpretation(self): user1 = Helpers.getUser(user_data=Helpers.USER1) Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation_data = {'interpretation': ''} url = '/compositions/{0}/interpretations'.format(composition.id) self.client.login(username='******', password='******') response = self.client.post(url, data=interpretation_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, {"interpretation": ["This field is required."]})
def test_post_throws_forbidden_for_non_authenticated_user(self): Helpers.getUser(Helpers.USER1) vote = self.createVote() vote_data = {"id": vote.id, "vote": True} url = self.url.format(vote.interpretation.composition.id, vote.interpretation.id) response = self.client.post(url, vote_data) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) vote = self.refreshFromDB(vote) self.assertEqual(vote.positive, 0) self.assertEqual(vote.negative, 0) self.assertEqual(vote.voters.all().count(), 0)
def test_delete_for_autheticated_valid_interpretationer(self): user1 = Helpers.getUser(user_data=Helpers.USER1) Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation = Interpretation.objects.create( user=user1, composition=composition, interpretation="interpretation1" ) url = "/compositions/{0}/interpretations/{1}".format(composition.id, interpretation.id) self.client.login(username="******", password="******") response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(Interpretation.objects.all().count(), 0)
def test_post_authenticated_user_valid_post_data(self): user1 = Helpers.getUser(user_data=Helpers.USER1) user2 = Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation_data = {'interpretation': 'interpretation1'} url = '/compositions/{0}/interpretations'.format(composition.id) self.client.login(username='******', password='******') response = self.client.post(url, data=interpretation_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, self.createResponse( 1, 'interpretation1', user2.id, composition.id, datetime.now())) self.assertEqual(Interpretation.objects.get(pk=1).interpretation, interpretation_data["interpretation"]) self.assertEqual(Interpretation.objects.all().count(), 1)
def test_delete_returns_forbidden_for_user_not_commenter(self): user1 = Helpers.getUser(Helpers.USER1) Helpers.getUser(Helpers.USER2) interpretation = Helpers.createInterpretation() comment = Comment.objects.create( commenter=user1, interpretation=interpretation, comment='comment1') url = '/compositions/{0}/interpretations/{1}/comments/{2}'.format( interpretation.composition.id, interpretation.id, comment.id) self.client.login(username='******', password='******') response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_unread_returns_unread_notifications(self): notification1_data = self.createNotification("Action1") self.createNotification("Action2") notification3_data = self.createNotification("Action3") notification = Notification.objects.get(pk=2) notification.unread = False notification.save() user = Helpers.getUser(Helpers.USER2) url = self.url + "/unread" self.client.login( username=user.email, password=Helpers.USER2["password"]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = [self.createResponse(3, user.id, notification3_data["verb"], datetime.now(), notification3_data["actor"], notification3_data[ "action_object"], notification3_data["target"])] expected_response += [self.createResponse(1, user.id, notification1_data["verb"], datetime.now(), notification1_data["actor"], notification1_data[ "action_object"], notification1_data["target"])] self.assertEqual(response.data, expected_response)
def test_all_returns_all_notifications(self): notification1_data = self.createNotification() notification2_data = self.createNotification() user = Helpers.getUser(Helpers.USER2) self.client.login( username=user.email, password=Helpers.USER2["password"]) response = self.client.get(self.url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = [self.createResponse(2, user.id, notification2_data["verb"], datetime.now(), notification2_data["actor"], notification2_data[ "action_object"], notification2_data["target"])] expected_response += [self.createResponse(1, user.id, notification1_data["verb"], datetime.now(), notification1_data["actor"], notification1_data[ "action_object"], notification1_data["target"])] self.assertEqual(response.data, expected_response)
def test_get_throws_forbidden_when_user_not_authenticated(self): user = Helpers.getUser(Helpers.USER1) composition1 = Helpers.createComposition(user) composition2 = Helpers.createComposition(user) user.bookmarks.add(composition1, composition2) url = self.url.format(user.id) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_update_for_autheticated_valid_interpretationer(self): user1 = Helpers.getUser(user_data=Helpers.USER1) Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation = Interpretation.objects.create( user=user1, composition=composition, interpretation="interpretation1" ) interpretation_data = {"interpretation": "interpretation2"} url = "/compositions/{0}/interpretations/{1}".format(composition.id, interpretation.id) self.client.login(username="******", password="******") response = self.client.put(url, data=interpretation_data) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( response.data, self.createResponse(1, "interpretation2", user1.id, composition.id, interpretation.created) ) self.assertEqual(Interpretation.objects.all().count(), 1) self.assertEqual(Interpretation.objects.get(pk=1).interpretation, interpretation_data["interpretation"])
def test_post_returns_forbidden_for_not_authorized_user(self): user1 = Helpers.getUser(user_data=Helpers.USER1) composition = Helpers.getComposition(artist=user1) url = '/compositions/{0}/interpretations'.format(composition.id) response = self.client.post(url, data={}) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual( response.data, {"detail": "Authentication credentials were not provided."})
def createNotification(self, verb=None): if not verb: verb = "did something" user1 = Helpers.getUser(Helpers.USER1) user2 = Helpers.getUser(Helpers.USER2) comment = Helpers.createComment() action_object = comment target_object = comment.interpretation notification_data = {"actor": user1, "recipient": user2, "verb": verb, "action_object": action_object, "target": target_object} notify.send(user1, **notification_data) return notification_data
def test_check_for_notifications_returns_true_if_unread_notifications_present(self): self.createNotification() user = Helpers.getUser(Helpers.USER2) url = self.url + "/check" self.client.login( username=user.email, password=Helpers.USER2["password"]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, {'result': True})
def test_delete_return_forbidden_for_non_authenticated_user(self): user1 = Helpers.getUser(user_data=Helpers.USER1) composition = Helpers.getComposition(artist=user1) interpretation = Interpretation.objects.create( user=user1, composition=composition, interpretation="interpretation1" ) url = "/compositions/{0}/interpretations/{1}".format(composition.id, interpretation.id) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_delete_throws_forbidden_for_not_authenticated_user(self): user = Helpers.getUser(Helpers.USER1) composition1 = Helpers.createComposition(user) composition2 = Helpers.createComposition(user) user.bookmarks.add(composition1, composition2) bookmark_data = {'bookmarks': [composition2.id]} url = self.url.format(user.id) response = self.client.delete(url, data=bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(user.bookmarks.all().count(), 2)
def test_post_adds_bookmark_for_valid_authenticated_user(self): user = Helpers.getUser(Helpers.USER1) composition = Helpers.createComposition(user) bookmark_data = {'bookmarks': [composition.id]} url = self.url.format(user.id) self.client.login( username=user.email, password=Helpers.USER1["password"]) response = self.client.post(url, bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(user.bookmarks.all().count(), 1)
def test_post_throws_400_for_invalid_post_data(self): user = Helpers.getUser(Helpers.USER1) composition = Helpers.createComposition(user) url = self.url.format(user.id) self.client.login( username=user.email, password=Helpers.USER1["password"]) response = self.client.post(url) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual( response.data, {"bookmarks": "This field is required"}) self.assertEqual(user.bookmarks.all().count(), 0)
def test_mark_all_marks_all_notifications_read(self): self.createNotification() self.createNotification() self.createNotification() user = Helpers.getUser(Helpers.USER2) url = self.url + "/mark-all-as-read" self.client.login( username=user.email, password=Helpers.USER2["password"]) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(Notification.objects.filter(unread=True).count(), 0)
def test_post_for_already_added_bookmark_acts_same_as_adding_new(self): user = Helpers.getUser(Helpers.USER1) composition = Helpers.createComposition(user) user.bookmarks.add(composition) bookmark_data = {'bookmarks': [composition.id]} url = self.url.format(user.id) self.client.login( username=user.email, password=Helpers.USER1["password"]) response = self.client.post(url, bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(user.bookmarks.all().count(), 1)
def test_delete_return_forbidden_for_non_authenticated_user(self): user1 = Helpers.getUser(Helpers.USER1) interpretation = Helpers.createInterpretation() comment = Comment.objects.create( commenter=user1, interpretation=interpretation, comment='comment1') url = '/compositions/{0}/interpretations/{1}/comments/{2}'.format( interpretation.composition.id, interpretation.id, comment.id) response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_post_authenticated_user_valid_post_data(self): user2 = Helpers.getUser(Helpers.USER2) interpretation = Helpers.createInterpretation() comment_data = {'comment': 'comment1'} url = '/compositions/{0}/interpretations/{1}/comments'.format(interpretation.composition.id, interpretation.id) self.client.login(username='******', password='******') response = self.client.post(url, data=comment_data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, self.createResponse( 1, 'comment1', user2.id, interpretation.id, datetime.now())) self.assertEqual(Comment.objects.all().count(), 1)
def test_get_returns_all_interpretations_for_composition(self): user1 = Helpers.getUser(user_data=Helpers.USER1) user2 = Helpers.getUser(user_data=Helpers.USER2) composition = Helpers.getComposition(artist=user1) interpretation1 = Interpretation.objects.create( user=user1, composition=composition, interpretation='interpretation1') interpretation2 = Interpretation.objects.create( user=user1, composition=composition, interpretation='interpretation2') interpretation3 = Interpretation.objects.create( user=user2, composition=composition, interpretation='interpretation3') url = '/compositions/{0}/interpretations'.format(composition.id) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = [ self.createResponse(1, 'interpretation1', user1.id, composition.id, interpretation1.created)] expected_response.append( self.createResponse(2, 'interpretation2', user1.id, composition.id, interpretation2.created)) expected_response.append( self.createResponse(3, 'interpretation3', user2.id, composition.id, interpretation3.created)) self.assertEqual(response.data, expected_response)
def test_get_returns_all_comments_for_interpretation(self): user1 = Helpers.getUser(Helpers.USER1) user2 = Helpers.getUser(Helpers.USER1) interpretation = Helpers.createInterpretation() comment1 = Comment.objects.create( commenter=user1, interpretation=interpretation, comment='comment1') comment2 = Comment.objects.create( commenter=user1, interpretation=interpretation, comment='comment2') comment3 = Comment.objects.create( commenter=user2, interpretation=interpretation, comment='comment3') url = '/compositions/{0}/interpretations/{1}/comments'.format(interpretation.composition.id, interpretation.id) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = [ self.createResponse(1, 'comment1', user1.id, interpretation.id, comment1.created)] expected_response.append( self.createResponse(2, 'comment2', user1.id, interpretation.id, comment2.created)) expected_response.append( self.createResponse(3, 'comment3', user2.id, interpretation.id, comment3.created)) self.assertEqual(response.data, expected_response)
def test_delete_for_non_existing_bookmark_does_not_crash(self): user = Helpers.getUser(Helpers.USER1) composition1 = Helpers.createComposition(user) user.bookmarks.add(composition1) bookmark_data = {'bookmarks': [56, 89]} url = self.url.format(user.id) self.client.login( username=user.email, password=Helpers.USER1["password"]) response = self.client.delete(url, data=bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(user.bookmarks.all().count(), 1)
def test_delete_for_autheticated_valid_commenter(self): user1 = Helpers.getUser(Helpers.USER1) interpretation = Helpers.createInterpretation() comment = Comment.objects.create( commenter=user1, interpretation=interpretation, comment='comment1') url = '/compositions/{0}/interpretations/{1}/comments/{2}'.format( interpretation.composition.id, interpretation.id, comment.id) self.client.login(username='******', password='******') response = self.client.delete(url) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
def test_delete_removes_bookmark_for_valid_authenticated_user(self): user = Helpers.getUser(Helpers.USER1) composition1 = Helpers.createComposition(user) composition2 = Helpers.createComposition(user) user.bookmarks.add(composition1, composition2) bookmark_data = {'bookmarks': [composition2.id]} url = self.url.format(user.id) self.client.login( username=user.email, password=Helpers.USER1["password"]) response = self.client.delete(url, data=bookmark_data, format="json") self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(user.bookmarks.all().count(), 1)