def test_calculate_user_rating_with_many_ratings(self): user = self.users[0] for i in range(0, MAX_RATINGS_TO_CONSIDER * 2): feedback_request = FeedbackRequest( user=self.users[i], media_url="https://soundcloud.com/ruairidx/bruno", ) feedback_request.save() # Old rates will be good, new ratings will be a little worse. # The newer ratings should prevail. if i < MAX_RATINGS_TO_CONSIDER: rating = 5 else: rating = 3 FeedbackResponse( feedback_request=feedback_request, user=user, feedback="jery get ipad", submitted=True, time_submitted=datetime.fromtimestamp(i, tz=pytz.UTC), rating=rating, ).save() call_command("calculate_user_ratings") updated_user = FeedbackGroupsUser.objects.filter(id=user.id,).first() # Calculated from most recent ratings, ignoring old ones. self.assertEquals(updated_user.rating, 3)
def setUp(self): self.request_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.request_user.save() self.response_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.response_user.save() self.feedback_request = FeedbackRequest( user=self.request_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", genre=GenreChoice.HIPHOP.name, ) self.feedback_request.save() self.feedback_response = FeedbackResponse( user=self.response_user, feedback_request=self.feedback_request, ) self.feedback_response.save()
def test_assign_groups_ignore_old_requests(self): # Assert that we ignore feedback requests that already have feedback groups users = self.users[:4] old_feedback_group = FeedbackGroup(name="nothing really") old_feedback_group.save() for user in users: # First user's request already has a feedback group. if user == users[0]: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", feedback_group=old_feedback_group, email_when_grouped=True, ).save() else: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, ).save() call_command("assign_groups") # Assert one group was created (excluding old group) and that responses were # created for everyone in the group. self.assertEqual(FeedbackGroup.objects.count(), 2) self.assertEqual(FeedbackResponse.objects.count(), 6) # New group, not old_feedback_group feedback_group = FeedbackGroup.objects.last() # Check we didn't overwrite the old request or anything stupid. self.assertEqual( FeedbackRequest.objects.filter( user=users[0], feedback_group=old_feedback_group).count(), 1, ) for user in users[1:4]: self.assertEqual( FeedbackRequest.objects.filter( user=user, feedback_group=feedback_group).count(), 1, ) # assert correct emails were sent (not to user in old group) self.assertEqual(len(mail.outbox), 3) for i in range(0, 3): email = mail.outbox[i] self.assertEqual(email.subject, "your new feedback group") self.assertEqual(len(email.recipients()), 1) self.assertEqual(email.recipients()[0], users[i + 1].email) self.assertTrue( "https://www.howsmytrack.com/group/2" in email.body) # Verify that HTML content was sent in the email as well self.assertTrue(len(email.alternatives) > 0) self.assertTrue( len(email.alternatives[0][0]) > 0) # Message content self.assertEqual(email.alternatives[0][1], "text/html")
def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() with patch("django.utils.timezone.now", Mock(return_value=DEFAULT_DATETIME)): self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.graham_feedback_request.save() self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, ) self.lewis_feedback_response = FeedbackResponse( feedback_request=self.graham_feedback_request, user=self.lewis_user, feedback="lewisfeedback", submitted=True, rating=3, allow_replies=True, ) self.graham_feedback_response.save() self.lewis_feedback_response.save()
def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, feedback="grahamfeedback", submitted=True, rating=4, allow_replies=True, ) self.graham_feedback_response.save() self.lewis_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.lewis_user, text="love from lewis", allow_replies=True, ) self.lewis_reply_to_graham.save() self.graham_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.graham_user, text="i don't want your love", allow_replies=False, ) self.graham_reply_to_graham.save()
def test_user_details_logged_in_unread_reply(self): other_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) other_user.save() feedback_group = FeedbackGroup(name="name") feedback_group.save() feedback_request = FeedbackRequest( user=other_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=feedback_group, email_when_grouped=True, genre=GenreChoice.NO_GENRE, ) feedback_request.save() feedback_response = FeedbackResponse( feedback_request=feedback_request, user=self.user, feedback="feedback", submitted=True, ) feedback_response.save() feedback_response_reply = FeedbackResponseReply( feedback_response=feedback_response, user=other_user, text="some reply", ) feedback_response_reply.save() info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_query_type().graphene_type().resolve_user_details( info=info, )) self.assertEqual( result, UserType( username="******", rating=4.5, notifications=1, send_reminder_emails=True, ), )
def setUp(self): self.user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.user.save() self.existing_request = FeedbackRequest( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.existing_request.save()
def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() with patch("django.utils.timezone.now", Mock(return_value=GROUP_TIME_CREATED)): self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, ) self.graham_feedback_request.save() self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, submitted=False, ) self.lewis_feedback_response = FeedbackResponse( feedback_request=self.graham_feedback_request, user=self.lewis_user, submitted=False, ) self.graham_feedback_response.save() self.lewis_feedback_response.save()
def test_assign_groups_uneven_groups_advanced(self): # Groups should be a minimum of 3 members # (unless this is literally impossible e.g. 2 or 5 requests) users = self.users[:10] for user in users: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, ).save() call_command("assign_groups") # Assert three groups were created of the expected sizes. self.assertEqual(FeedbackRequest.objects.count(), 10) self.assertEqual(FeedbackGroup.objects.count(), 3) all_feedback_groups = FeedbackGroup.objects.all() self.assertEqual( all_feedback_groups[0].feedback_requests.count(), 4, ) self.assertEqual( all_feedback_groups[1].feedback_requests.count(), 3, ) self.assertEqual( all_feedback_groups[2].feedback_requests.count(), 3, ) self.assertEqual(FeedbackResponse.objects.count(), 12 + 6 + 6) users.sort(key=lambda user: user.rating, reverse=True) for user in users[0:4]: # Top four users by rating should be in first group self.assertEqual( FeedbackRequest.objects.filter( user=user, feedback_group=all_feedback_groups[0]).count(), 1, ) for user in users[4:7]: # Next three users by rating should be in second group self.assertEqual( FeedbackRequest.objects.filter( user=user, feedback_group=all_feedback_groups[1]).count(), 1, ) for user in users[7:10]: # Bottom three users by rating should be in third group self.assertEqual( FeedbackRequest.objects.filter( user=user, feedback_group=all_feedback_groups[2]).count(), 1, )
def test_calculate_user_rating_without_enough_rating(self): user = self.users[0] for i in range(0, MIN_RATINGS_TO_CONSIDER - 1): feedback_request = FeedbackRequest( user=self.users[i], media_url="https://soundcloud.com/ruairidx/bruno", ) feedback_request.save() FeedbackResponse( feedback_request=feedback_request, user=user, feedback="jery get ipad", submitted=True, time_submitted=datetime.fromtimestamp(i, tz=pytz.UTC), rating=5, ).save() call_command("calculate_user_ratings") updated_user = FeedbackGroupsUser.objects.filter(id=user.id,).first() self.assertEquals(updated_user.rating, 0)
def test_trackless_requests_without_matching_genre(self): """ If we have a trackless request without a group containing tracks of the request's genre, just pick the group with the fewest requests. """ genres_with_tracks = [ GenreChoice.NO_GENRE.name, GenreChoice.NO_GENRE.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, ] genres_without_tracks = [ GenreChoice.HIPHOP.name, ] feedback_requests_with_tracks = [] for i in range(0, len(genres_with_tracks)): feedback_request = FeedbackRequest( user=self.users[i], media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, genre=genres_with_tracks[i], ) feedback_request.save() feedback_requests_with_tracks.append(feedback_request) feedback_requests_without_tracks = [] for i in range(0, len(genres_without_tracks)): feedback_request = FeedbackRequest( user=self.users[len(genres_with_tracks) + i], media_url=None, email_when_grouped=True, genre=genres_without_tracks[i], ) feedback_request.save() feedback_requests_without_tracks.append(feedback_request) call_command("assign_groups") self.assertEqual(FeedbackGroup.objects.count(), 2) self.assertEqual(FeedbackResponse.objects.count(), 10) for feedback_group in FeedbackGroup.objects.all(): self.assertEqual( feedback_group.feedback_requests.count(), 3, )
def setUp(self): # graham's request will be assigned, but not lewis's self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=None, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.graham_feedback_request.save() self.lewis_feedback_request.save()
def test_existing_assigned_request(self): # Add a previous assigned request, user should be able to add another. feedback_group = FeedbackGroup(name="name") feedback_group.save() FeedbackRequest( user=self.user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ).save() info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_mutation_type().fields["createFeedbackRequest"]. resolver( self=Mock(), info=info, media_url="https://www.dropbox.com/s/nonsense/file.wav", feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, )) self.assertEqual( result, CreateFeedbackRequest( success=True, error=None, invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://www.dropbox.com/s/nonsense/file.wav", media_type=MediaTypeChoice.DROPBOX.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).count(), 1, )
def test_trackless_requests_with_uneven_groups(self): """In the event that we have uneven groups, smaller groups are prioritised to get additional trackless requests.""" genres_with_tracks = [ GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, ] genres_without_tracks = [ GenreChoice.ELECTRONIC.name, ] feedback_requests_with_tracks = [] for i in range(0, len(genres_with_tracks)): feedback_request = FeedbackRequest( user=self.users[i], media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, genre=genres_with_tracks[i], ) feedback_request.save() feedback_requests_with_tracks.append(feedback_request) feedback_requests_without_tracks = [] for i in range(0, len(genres_without_tracks)): feedback_request = FeedbackRequest( user=self.users[len(genres_with_tracks) + i], media_url=None, email_when_grouped=True, genre=genres_without_tracks[i], ) feedback_request.save() feedback_requests_without_tracks.append(feedback_request) call_command("assign_groups") self.assertEqual(FeedbackGroup.objects.count(), 2) self.assertEqual(FeedbackResponse.objects.count(), 10) for feedback_group in FeedbackGroup.objects.all(): self.assertEqual( feedback_group.feedback_requests.count(), 3, )
def test_assign_groups_singleton(self): # Use feedback requests equal to a multiple of 4 i.e. evenly sized groups users = self.users[:1] for user in users: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, ).save() call_command("assign_groups") # Assert no groups were created for just one user. self.assertEqual(FeedbackGroup.objects.count(), 0) # Assert no emails were sent self.assertEqual(len(mail.outbox), 0)
def test_same_url_different_account(self): self.another_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.another_user.save() # Add a previous unassigned request, should prevent adding another. FeedbackRequest( user=self.another_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).save() info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_mutation_type().fields["createFeedbackRequest"]. resolver( self=Mock(), info=info, media_url="https://soundcloud.com/ruairidx/bruno", feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, )) self.assertEqual( result, CreateFeedbackRequest( success=False, error="A request for this track is already pending.", invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter( media_url="https://soundcloud.com/ruairidx/bruno", user=self.user, ).count(), 0, )
def test_no_email_if_email_when_grouped_false(self): # Use feedback requests equal to a multiple of 4 i.e. evenly sized groups users = self.users[:4] for user in users: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=False, ).save() call_command("assign_groups") # Assert one group was created and that responses were # created for everyone in the group. self.assertEqual(FeedbackGroup.objects.count(), 1) self.assertEqual(FeedbackResponse.objects.count(), 12) self.assertEqual(FeedbackRequest.objects.count(), 4) # assert no emails were sent even though users were grouped self.assertEqual(len(mail.outbox), 0)
def test_assign_groups_even_groups(self): # Use feedback requests equal to a multiple of 4 i.e. evenly sized groups users = self.users[:4] for user in users: FeedbackRequest( user=user, media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, ).save() call_command("assign_groups") # Assert one group was created and that responses were # created for everyone in the group. self.assertEqual(FeedbackGroup.objects.count(), 1) self.assertEqual(FeedbackResponse.objects.count(), 12) self.assertEqual(FeedbackRequest.objects.count(), 4) feedback_group = FeedbackGroup.objects.first() feedback_requests = FeedbackRequest.objects.all() for feedback_request in feedback_requests: self.assertEqual(feedback_request.feedback_group, feedback_group) for user in users: self.assertEqual( FeedbackResponse.objects.filter(user=user, ).count(), 3, ) # assert correct emails were sent self.assertEqual(len(mail.outbox), 4) for i in range(0, 4): email = mail.outbox[i] self.assertEqual(email.subject, "your new feedback group") self.assertEqual(len(email.recipients()), 1) self.assertEqual(email.recipients()[0], users[i].email) self.assertTrue( "https://www.howsmytrack.com/group/1" in email.body)
def test_existing_unassigned_request(self): # Add a previous unassigned request, should prevent adding another. FeedbackRequest( user=self.user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=None, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).save() info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_mutation_type().fields["createFeedbackRequest"]. resolver( self=Mock(), info=info, media_url="https://www.dropbox.com/s/nonsense/file.wav", feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, )) self.assertEqual( result, CreateFeedbackRequest( success=False, error= "You have an unassigned feedback request. Once that request has been assigned to a feedback group, you will be eligible to submit another request.", invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter(user=self.user, ).count(), 1, )
class RepliesTest(TestCase): def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, feedback="grahamfeedback", submitted=True, rating=4, allow_replies=True, ) self.graham_feedback_response.save() self.lewis_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.lewis_user, text="love from lewis", allow_replies=True, ) self.lewis_reply_to_graham.save() self.graham_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.graham_user, text="i don't want your love", allow_replies=False, ) self.graham_reply_to_graham.save() def test_logged_out(self): info = Mock() result = (schema.get_query_type().graphene_type().resolve_replies( info=info, feedback_response_id=self.graham_feedback_response.id, )) self.assertIs(result, None) def test_bad_id(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = (schema.get_query_type().graphene_type().resolve_replies( info=info, feedback_response_id=1901, )) self.assertIs(result, None) def test_unauthorised(self): other_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) other_user.save() info = Mock() info.context = Mock() info.context.user = other_user.user result = (schema.get_query_type().graphene_type().resolve_replies( info=info, feedback_response_id=self.graham_feedback_response.id, )) self.assertIs(result, None) def test_logged_in(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = (schema.get_query_type().graphene_type().resolve_replies( info=info, feedback_response_id=self.graham_feedback_response.id, )) expected = FeedbackResponseRepliesType( allow_further_replies=False, replies=[ FeedbackResponseReplyType( id=1, username="******", text="love from lewis", allow_replies=True, time_created=self.lewis_reply_to_graham.time_created, ), FeedbackResponseReplyType( id=2, username="******", text="i don't want your love", allow_replies=False, time_created=self.graham_reply_to_graham.time_created, ), ], ) self.assertEqual(result, expected) def test_logged_in_unrated(self): self.graham_feedback_response.rating = None self.graham_feedback_response.save() info = Mock() info.context = Mock() info.context.user = self.lewis_user.user result = (schema.get_query_type().graphene_type().resolve_replies( info=info, feedback_response_id=self.graham_feedback_response.id, )) self.assertIs(result, None)
def test_trackless_requests_with_few_groups(self): """In the event that we have more trackless requests than groups, groups get more FeedbackResponses. Hooray!""" genres_with_tracks = [ GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.HIPHOP.name, GenreChoice.HIPHOP.name, ] genres_without_tracks = [ GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.ELECTRONIC.name, GenreChoice.HIPHOP.name, GenreChoice.HIPHOP.name, GenreChoice.HIPHOP.name, ] feedback_requests_with_tracks = [] for i in range(0, len(genres_with_tracks)): feedback_request = FeedbackRequest( user=self.users[i], media_url="https://soundcloud.com/ruairidx/grey", email_when_grouped=True, genre=genres_with_tracks[i], ) feedback_request.save() feedback_requests_with_tracks.append(feedback_request) feedback_requests_without_tracks = [] for i in range(0, len(genres_without_tracks)): feedback_request = FeedbackRequest( user=self.users[len(genres_with_tracks) + i], media_url=None, email_when_grouped=True, genre=genres_without_tracks[i], ) feedback_request.save() feedback_requests_without_tracks.append(feedback_request) call_command("assign_groups") self.assertEqual(FeedbackGroup.objects.count(), 2) self.assertEqual(FeedbackResponse.objects.count(), 16) for feedback_group in FeedbackGroup.objects.all(): self.assertEqual( feedback_group.feedback_requests.count(), 5, ) self.assertEqual( feedback_group.feedback_requests.filter( media_url__isnull=False).count(), 2, ) self.assertEqual( feedback_group.feedback_requests.filter( media_url__isnull=True).count(), 3, ) self.assertEqual( FeedbackResponse.objects.filter( feedback_request__feedback_group=feedback_group).count(), 8, ) # Trackless requests should have one response assigned to them but a response from a tracked user and multiple trackless users. for feedback_request_with_track in feedback_requests_with_tracks: feedback_request_with_track.refresh_from_db() self.assertEqual( FeedbackResponse.objects.filter( user=feedback_request_with_track.user).count(), 1, ) self.assertEqual( FeedbackResponse.objects.filter( feedback_request=feedback_request_with_track).count(), 4, ) # Trackless requests should have two responses assigned to them but no responses of their own (since they have no track) for feedback_request_without_track in feedback_requests_without_tracks: feedback_request_without_track.refresh_from_db() self.assertEqual( FeedbackResponse.objects.filter( user=feedback_request_without_track.user).count(), 2, ) self.assertEqual( FeedbackResponse.objects.filter( feedback_request=feedback_request_without_track).count(), 0, )
class UnassignedRequestTest(TestCase): def setUp(self): # graham's request will be assigned, but not lewis's self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=None, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.graham_feedback_request.save() self.lewis_feedback_request.save() def test_logged_out(self): info = Mock() result = ( schema.get_query_type().graphene_type().resolve_unassigned_request( info=info, )) self.assertIs(result, None) def test_logged_in_without_unassigned_request(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_unassigned_request( info=info, )) self.assertIs(result, None) def test_logged_in_with_unassigned_request(self): info = Mock() info.context = Mock() info.context.user = self.lewis_user.user result = ( schema.get_query_type().graphene_type().resolve_unassigned_request( info=info, )) expected = FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.assertEqual(result, expected)
class FeedbackGroupsTest(TestCase): def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() with patch("django.utils.timezone.now", Mock(return_value=DEFAULT_DATETIME)): self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.graham_feedback_request.save() self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, ) self.lewis_feedback_response = FeedbackResponse( feedback_request=self.graham_feedback_request, user=self.lewis_user, feedback="lewisfeedback", submitted=True, rating=3, allow_replies=True, ) self.graham_feedback_response.save() self.lewis_feedback_response.save() def test_logged_out(self): info = Mock() result = ( schema.get_query_type().graphene_type().resolve_feedback_groups( info=info, )) self.assertEqual(result, []) def test_logged_in(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_groups( info=info, )) expected = [ FeedbackGroupType( id=self.feedback_group.id, name="name", media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), time_created=DEFAULT_DATETIME, members=1, trackless_members=0, feedback_responses=[ FeedbackResponseType( id=1, feedback_request=FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ), feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, replies=0, unread_replies=0, ) ], user_feedback_responses=[ FeedbackResponseType( id=2, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), feedback="lewisfeedback", submitted=True, rating=3, allow_replies=True, replies=0, unread_replies=0, ) ], user_feedback_response_count=1, ) ] self.assertEqual(result, expected)
class SubmitFeedbackResponseTest(TestCase): def setUp(self): self.request_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.request_user.save() self.response_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.response_user.save() self.feedback_request = FeedbackRequest( user=self.request_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", genre=GenreChoice.HIPHOP.name, ) self.feedback_request.save() self.feedback_response = FeedbackResponse( user=self.response_user, feedback_request=self.feedback_request, ) self.feedback_response.save() def test_logged_out(self): info = Mock() result = (schema.get_mutation_type().fields["submitFeedbackResponse"]. resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, feedback="feedback", allow_replies=True, )) self.assertEqual( result, SubmitFeedbackResponse( success=False, error="Not logged in.", )) self.assertEqual( FeedbackResponse.objects.filter( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, ).count(), 0, ) def test_bad_id(self): info = Mock() info.context = Mock() info.context.user = self.response_user.user result = (schema.get_mutation_type().fields["submitFeedbackResponse"]. resolver( self=Mock(), info=info, feedback_response_id=1901, feedback="feedback", allow_replies=True, )) self.assertEqual( result, SubmitFeedbackResponse( success=False, error="Invalid feedback_response_id", ), ) self.assertEqual( FeedbackResponse.objects.filter( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, ).count(), 0, ) def test_already_submitted(self): self.feedback_response.submitted = True self.feedback_response.save() info = Mock() info.context = Mock() info.context.user = self.response_user.user result = (schema.get_mutation_type().fields["submitFeedbackResponse"]. resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, feedback="feedback", allow_replies=True, )) self.assertEqual( result, SubmitFeedbackResponse( success=False, error="Feedback has already been submitted", ), ) self.assertEqual( FeedbackResponse.objects.filter( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, ).count(), 0, ) def test_logged_in_unsubmitted(self): info = Mock() info.context = Mock() info.context.user = self.response_user.user result = (schema.get_mutation_type().fields["submitFeedbackResponse"]. resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, feedback="feedback", allow_replies=True, )) self.assertEqual(result, SubmitFeedbackResponse( success=True, error=None, )) self.assertEqual( FeedbackResponse.objects.filter( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, allow_replies=True, ).count(), 1, )
class AddFeedbackResponseReplyTest(TestCase): def setUp(self): self.request_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.request_user.save() self.response_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.response_user.save() self.feedback_request = FeedbackRequest( user=self.request_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", genre=GenreChoice.HIPHOP.name, ) self.feedback_request.save() self.feedback_response = FeedbackResponse( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, allow_replies=True, ) self.feedback_response.save() def test_logged_out(self): info = Mock() result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="Not logged in.", )) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 0, ) def test_bad_id(self): info = Mock() info.context = Mock() info.context.user = self.request_user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=1901, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="Invalid feedback_response_id", ), ) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 0, ) def test_invalid_user(self): unrelated_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) unrelated_user.save() info = Mock() info.context = Mock() info.context.user = unrelated_user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="You are not authorised to reply to this feedback.", ), ) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 0, ) def test_reject_reply_if_not_feedback_response_allow_replies(self): self.feedback_response.allow_replies = False self.feedback_response.save() info = Mock() info.context = Mock() info.context.user = self.feedback_request.user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="You cannot reply to this feedback.", ), ) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 0, ) def test_reject_reply_if_not_existing_reply_allow_replies(self): existing_reply = FeedbackResponseReply( feedback_response=self.feedback_response, user=self.feedback_request.user, text="danke mate", allow_replies=False, ) existing_reply.save() info = Mock() info.context = Mock() info.context.user = self.feedback_response.user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="You cannot reply to this feedback.", ), ) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 1, ) def test_reject_reply_if_response_is_unsubmitted(self): self.feedback_response.submitted = False self.feedback_response.save() info = Mock() info.context = Mock() info.context.user = self.feedback_response.user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( result, AddFeedbackResponseReply( reply=None, error="You cannot reply to this feedback.", ), ) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, ).count(), 0, ) def test_successful_reply(self): info = Mock() info.context = Mock() info.context.user = self.feedback_request.user.user result = (schema.get_mutation_type(). fields["addFeedbackResponseReply"].resolver( self=Mock(), info=info, feedback_response_id=self.feedback_response.id, text="thanks pal", allow_replies=True, )) self.assertEqual( FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, user=self.feedback_request.user, text="thanks pal", allow_replies=True, ).count(), 1, ) reply = FeedbackResponseReply.objects.filter( feedback_response=self.feedback_response, user=self.feedback_request.user, text="thanks pal", allow_replies=True, ).first() self.assertEqual( result, AddFeedbackResponseReply( reply=FeedbackResponseReplyType( id=1, username="******", text="thanks pal", allow_replies=True, time_created=reply.time_created, ), error=None, ), )
class MarkRepliesAsReadTest(TestCase): def setUp(self): self.request_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.request_user.save() self.response_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.response_user.save() self.feedback_request = FeedbackRequest( user=self.request_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", genre=GenreChoice.HIPHOP.name, ) self.feedback_request.save() self.feedback_response = FeedbackResponse( user=self.response_user, feedback_request=self.feedback_request, feedback="feedback", submitted=True, allow_replies=True, ) self.feedback_response.save() self.feedback_response_reply = FeedbackResponseReply( feedback_response=self.feedback_response, user=self.request_user, text="this is a reply", allow_replies=True, ) self.feedback_response_reply.save() def test_logged_out(self): info = Mock() result = ( schema.get_mutation_type() .fields["markRepliesAsRead"] .resolver(self=Mock(), info=info, reply_ids=[1],) ) self.assertEqual( result, MarkRepliesAsRead(success=False, error="Not logged in.",) ) self.assertEqual( FeedbackResponseReply.objects.filter(time_read__isnull=True,).count(), 1, ) def test_invalid_user(self): info = Mock() info.context = Mock() info.context.user = self.request_user.user result = ( schema.get_mutation_type() .fields["markRepliesAsRead"] .resolver(self=Mock(), info=info, reply_ids=[1],) ) self.assertEqual(result, MarkRepliesAsRead(success=True, error=None,)) self.assertEqual( FeedbackResponseReply.objects.filter(time_read__isnull=True,).count(), 1, ) def test_valid_user(self): info = Mock() info.context = Mock() info.context.user = self.response_user.user result = ( schema.get_mutation_type() .fields["markRepliesAsRead"] .resolver(self=Mock(), info=info, reply_ids=[1],) ) self.assertEqual(result, MarkRepliesAsRead(success=True, error=None,)) self.assertEqual( FeedbackResponseReply.objects.filter( time_read__isnull=False, id=1, ).count(), 1, )
class DeleteFeedbackRequestTest(TestCase): def setUp(self): self.user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.user.save() self.other_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.other_user.save() self.existing_request = FeedbackRequest( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.existing_request.save() def test_logged_out(self): info = Mock() result = (schema.get_mutation_type().fields["deleteFeedbackRequest"]. resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, )) self.assertEqual( result, DeleteFeedbackRequest( success=False, error="Not logged in.", )) self.assertEqual( FeedbackRequest.objects.count(), 1, ) def test_assigned_request(self): feedback_group = FeedbackGroup(name="name") feedback_group.save() self.existing_request.feedback_group = feedback_group self.existing_request.save() info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_mutation_type().fields["deleteFeedbackRequest"]. resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, )) self.assertEqual( result, DeleteFeedbackRequest( success=False, error= "This request has already been assigned to a feedback group and cannot be edited.", ), ) self.assertEqual( FeedbackRequest.objects.count(), 1, ) def test_unauthorised(self): info = Mock() info.context = Mock() info.context.user = self.other_user.user result = (schema.get_mutation_type().fields["deleteFeedbackRequest"]. resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, )) self.assertEqual( result, DeleteFeedbackRequest( success=False, error="You are not the owner of this feedback request.", ), ) self.assertEqual( FeedbackRequest.objects.count(), 1, ) def test_ok(self): info = Mock() info.context = Mock() info.context.user = self.user.user result = (schema.get_mutation_type().fields["deleteFeedbackRequest"]. resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, )) self.assertEqual(result, DeleteFeedbackRequest( success=True, error=None, )) self.assertEqual( FeedbackRequest.objects.count(), 0, )
class FeedbackGroupTest(TestCase): def setUp(self): self.graham_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.lewis_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.graham_user.save() self.lewis_user.save() with patch("django.utils.timezone.now", Mock(return_value=DEFAULT_DATETIME)): self.feedback_group = FeedbackGroup(name="name") self.feedback_group.save() self.graham_feedback_request = FeedbackRequest( user=self.graham_user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.lewis_feedback_request = FeedbackRequest( user=self.lewis_user, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", feedback_group=self.feedback_group, email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ) self.graham_feedback_request.save() self.lewis_feedback_request.save() self.graham_feedback_response = FeedbackResponse( feedback_request=self.lewis_feedback_request, user=self.graham_user, feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, ) self.lewis_feedback_response = FeedbackResponse( feedback_request=self.graham_feedback_request, user=self.lewis_user, feedback="lewisfeedback", submitted=True, rating=3, allow_replies=False, ) self.graham_feedback_response.save() self.lewis_feedback_response.save() def test_logged_out(self): info = Mock() result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=self.feedback_group.id, )) self.assertIs(result, None) def test_bad_id(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=1901, )) self.assertIs(result, None) def test_logged_in(self): info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=self.feedback_group.id, )) expected = FeedbackGroupType( id=self.feedback_group.id, name="name", media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), time_created=DEFAULT_DATETIME, members=1, trackless_members=0, feedback_responses=[ FeedbackResponseType( id=1, feedback_request=FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ), feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, replies=0, unread_replies=0, ) ], user_feedback_responses=[ FeedbackResponseType( id=2, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), feedback="lewisfeedback", submitted=True, rating=3, allow_replies=False, replies=0, unread_replies=0, ) ], user_feedback_response_count=1, ) self.assertEqual(result, expected) def test_logged_in_without_submitting_feedback(self): self.graham_feedback_response.submitted = False self.graham_feedback_response.save() info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=self.feedback_group.id, )) expected = FeedbackGroupType( id=self.feedback_group.id, name="name", media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), time_created=DEFAULT_DATETIME, members=1, trackless_members=0, feedback_responses=[ FeedbackResponseType( id=1, feedback_request=FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ), feedback="grahamfeedback", submitted=False, rating=4, allow_replies=False, replies=0, unread_replies=0, ) ], user_feedback_responses=None, # Should still show user that count is 1 even if we don't # send the actual response itself. user_feedback_response_count=1, ) self.assertEqual(result, expected) def test_logged_in_with_trackless_requests(self): self.lewis_feedback_response.delete() self.graham_feedback_request.media_url = None self.graham_feedback_request.save() info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=self.feedback_group.id, )) expected = FeedbackGroupType( id=self.feedback_group.id, name="name", media_url=None, media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_request=FeedbackRequestType( id=1, media_url=None, media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), time_created=DEFAULT_DATETIME, members=1, trackless_members=0, feedback_responses=[ FeedbackResponseType( id=1, feedback_request=FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ), feedback="grahamfeedback", submitted=True, rating=4, allow_replies=False, replies=0, unread_replies=0, ) ], user_feedback_responses=[], user_feedback_response_count=0, ) self.assertEqual(result, expected) def test_logged_in_with_replies(self): self.lewis_feedback_response.allow_replies = True self.lewis_feedback_response.save() self.graham_feedback_response.allow_replies = True self.graham_feedback_response.save() graham_reply_to_lewis = FeedbackResponseReply( feedback_response=self.lewis_feedback_response, user=self.graham_user, text="love from graham", allow_replies=False, ) graham_reply_to_lewis.save() lewis_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.lewis_user, text="love from lewis", allow_replies=True, ) lewis_reply_to_graham.save() graham_reply_to_graham = FeedbackResponseReply( feedback_response=self.graham_feedback_response, user=self.graham_user, text="i don't want your love", allow_replies=False, ) graham_reply_to_graham.save() info = Mock() info.context = Mock() info.context.user = self.graham_user.user result = ( schema.get_query_type().graphene_type().resolve_feedback_group( info=info, feedback_group_id=self.feedback_group.id, )) expected = FeedbackGroupType( id=self.feedback_group.id, name="name", media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), time_created=DEFAULT_DATETIME, members=1, trackless_members=0, feedback_responses=[ FeedbackResponseType( id=1, feedback_request=FeedbackRequestType( id=2, media_url="https://soundcloud.com/ruairidx/bruno", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.HIPHOP.name, ), feedback="grahamfeedback", submitted=True, rating=4, allow_replies=True, replies=2, unread_replies=1, ) ], user_feedback_responses=[ FeedbackResponseType( id=2, feedback_request=FeedbackRequestType( id=1, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ), feedback="lewisfeedback", submitted=True, rating=3, allow_replies=True, replies=1, unread_replies=0, ) ], user_feedback_response_count=1, ) self.assertEqual(result, expected)
def mutate( self, info, media_url=None, genre=None, email_when_grouped=False, feedback_prompt=None, ): user = info.context.user if user.is_anonymous: return CreateFeedbackRequest( success=False, error="Not logged in.", invalid_media_url=False, ) feedback_groups_user = FeedbackGroupsUser.objects.filter(user=user,).first() # Validate the media url, if one exists. media_type = None if media_url: try: media_type = validate_media_url(media_url) except ValidationError as e: return CreateFeedbackRequest( success=False, error=e.message, invalid_media_url=True, ) # Only create a new request if the user has an outstanding, ungrouped request # (should only happen if user's request is from within the last 24 hours or # the request is the only one submitted :cry: ) unassigned_requests = FeedbackRequest.objects.filter( user=feedback_groups_user, feedback_group=None, ).count() if unassigned_requests > 0: return CreateFeedbackRequest( success=False, error="You have an unassigned feedback request. Once that request has been assigned to a feedback group, you will be eligible to submit another request.", invalid_media_url=False, ) # Reject requests for the same URL (if the other submission hasn't been grouped yet) # This prevents users creating multiple accounts to request the same track. if media_url: existing_track_requests = FeedbackRequest.objects.filter( media_url=media_url, feedback_group=None, ).count() if existing_track_requests > 0: return CreateFeedbackRequest( success=False, error="A request for this track is already pending.", invalid_media_url=False, ) feedback_request = FeedbackRequest( user=feedback_groups_user, media_url=media_url, media_type=media_type, feedback_prompt=feedback_prompt, email_when_grouped=email_when_grouped, genre=genre, ) feedback_request.save() return CreateFeedbackRequest(success=True, error=None, invalid_media_url=False,)
class EditFeedbackRequestTest(TestCase): def setUp(self): self.user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.user.save() self.existing_request = FeedbackRequest( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ) self.existing_request.save() def test_logged_out(self): info = Mock() result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="https://soundcloud.com/ruairidx/bruno", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest( success=False, error="Not logged in.", invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://soundcloud.com/ruairidx/bruno", genre=GenreChoice.HIPHOP.name, ).count(), 0, ) def test_invalid_url(self): info = Mock() info.context = Mock() info.context.user = self.user.user result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="not a real url", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest( success=False, error=INVALID_MEDIA_URL_MESSAGE, invalid_media_url=True, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", genre=GenreChoice.ELECTRONIC.name, ).count(), 1, ) def test_unsupported_platform(self): info = Mock() info.context = Mock() info.context.user = self.user.user result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="https://twitter.com/ruairidx", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest( success=False, error=INVALID_MEDIA_URL_MESSAGE, invalid_media_url=True, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).count(), 1, ) def test_valid_url(self): info = Mock() info.context = Mock() info.context.user = self.user.user result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="https://www.dropbox.com/s/nonsense/file.wav", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest(success=True, error=None, invalid_media_url=False,), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://www.dropbox.com/s/nonsense/file.wav", media_type=MediaTypeChoice.DROPBOX.name, feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ).count(), 1, ) def test_assigned_request(self): feedback_group = FeedbackGroup(name="name") feedback_group.save() self.existing_request.feedback_group = feedback_group self.existing_request.save() info = Mock() info.context = Mock() info.context.user = self.user.user result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="https://www.dropbox.com/s/nonsense/file.wav", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest( success=False, error="This request has already been assigned to a feedback group and cannot be edited.", invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).count(), 1, ) def test_unauthorised(self): self.new_user = FeedbackGroupsUser.create( email="*****@*****.**", password="******", ) self.new_user.save() info = Mock() info.context = Mock() info.context.user = self.new_user.user result = ( schema.get_mutation_type() .fields["editFeedbackRequest"] .resolver( self=Mock(), info=info, feedback_request_id=self.existing_request.id, media_url="https://www.dropbox.com/s/nonsense/file.wav", feedback_prompt="feedback_prompt", email_when_grouped=False, genre=GenreChoice.HIPHOP.name, ) ) self.assertEqual( result, EditFeedbackRequest( success=False, error="You are not the owner of this feedback request.", invalid_media_url=False, ), ) self.assertEqual( FeedbackRequest.objects.filter( user=self.user, media_url="https://soundcloud.com/ruairidx/grey", media_type=MediaTypeChoice.SOUNDCLOUD.name, feedback_prompt="feedback_prompt", email_when_grouped=True, genre=GenreChoice.ELECTRONIC.name, ).count(), 1, )