Esempio n. 1
0
    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)
Esempio n. 2
0
    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,
            ),
        )
Esempio n. 3
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,
            )
Esempio n. 4
0
    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,
            )
Esempio n. 5
0
    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)
Esempio n. 6
0
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)
Esempio n. 7
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)
Esempio n. 8
0
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)
Esempio n. 9
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)
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 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,
        )
Esempio n. 13
0
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,
        )
Esempio n. 14
0
    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,
            )
Esempio n. 15
0
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,
        )
    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,)
Esempio n. 17
0
class SendGroupReminderEmailsTest(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=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_send_reminders_to_all(self):
        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        # Assert emails were sent to both
        self.assertEqual(len(mail.outbox), 2)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)
        self.assertTrue(self.lewis_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_not_email_when_grouped(self):
        self.lewis_feedback_request.email_when_grouped = False
        self.lewis_feedback_request.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        # Assert email was only sent to graham
        self.assertEqual(len(mail.outbox), 1)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)
        self.assertFalse(self.lewis_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_already_sent(self):
        self.lewis_feedback_request.reminder_email_sent = True
        self.lewis_feedback_request.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        # Assert email was only sent to graham
        self.assertEqual(len(mail.outbox), 1)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_too_soon(self):
        self.lewis_user.send_reminder_emails = False
        self.lewis_user.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        # Assert email was only sent to graham
        self.assertEqual(len(mail.outbox), 1)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)
        self.assertFalse(self.lewis_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_user_disabled_send_reminder_emails(self):
        self.lewis_user.send_reminder_emails = False
        self.lewis_user.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        self.assertEqual(len(mail.outbox), 1)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)
        self.assertFalse(self.lewis_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_unassigned(self):
        self.lewis_feedback_request.feedback_group = None
        self.lewis_feedback_request.save()
        self.graham_feedback_request.feedback_group = None
        self.graham_feedback_request.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        self.assertEqual(len(mail.outbox), 0)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertFalse(self.graham_feedback_request.reminder_email_sent)
        self.assertFalse(self.lewis_feedback_request.reminder_email_sent)

    def test_do_not_send_reminder_if_response_submitted(self):
        self.lewis_feedback_response.submitted = True
        self.lewis_feedback_response.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        self.assertEqual(len(mail.outbox), 1)

        self.graham_feedback_request.refresh_from_db()
        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.graham_feedback_request.reminder_email_sent)
        self.assertFalse(self.lewis_feedback_request.reminder_email_sent)

    def test_trackless_request(self):
        self.lewis_feedback_request.media_url = None
        self.lewis_feedback_request.save()

        with patch("django.utils.timezone.now", Mock(return_value=FUTURE)):
            call_command("send_group_reminder_emails")

        self.assertEqual(len(mail.outbox), 2)

        self.lewis_feedback_request.refresh_from_db()
        self.assertTrue(self.lewis_feedback_request.reminder_email_sent)
        # Really crude way to check that the trackless email template was used.
        self.assertTrue(
            "Don't forget to check out your feedback group and write feedback for its other members!"
            in mail.outbox[1].body)