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_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_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_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_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,
            ),
        )
    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,
        )