コード例 #1
0
 def test_questions_can_be_100_words(self):
     question = " ".join(["word"] * 100)
     with self.app.app_context():
         question = BriefClarificationQuestion(brief=self.brief,
                                               question=question,
                                               answer="Because")
         question.validate()
コード例 #2
0
 def test_answers_can_be_100_words(self):
     answer = " ".join(["word"] * 100)
     with self.app.app_context():
         question = BriefClarificationQuestion(brief=self.brief,
                                               question="Why?",
                                               answer=answer)
         question.validate()
コード例 #3
0
    def test_answer_must_not_be_empty(self):
        with self.app.app_context(), pytest.raises(ValidationError) as e:
            question = BriefClarificationQuestion(brief=self.brief,
                                                  question="Why?",
                                                  answer="")
            question.validate()

        assert e.value.message["answer"] == "answer_required"
コード例 #4
0
    def test_answer_must_not_be_more_than_5000_characters(self):
        long_answer = "a" * 5001
        with self.app.app_context(), pytest.raises(ValidationError) as e:
            question = BriefClarificationQuestion(brief=self.brief,
                                                  question="Why?",
                                                  answer=long_answer)
            question.validate()

        assert e.value.message["answer"] == "under_character_limit"
コード例 #5
0
    def test_answers_must_not_be_more_than_100_words(self):
        long_answer = " ".join(["word"] * 101)
        with self.app.app_context(), pytest.raises(ValidationError) as e:
            question = BriefClarificationQuestion(brief=self.brief,
                                                  question="Why?",
                                                  answer=long_answer)
            question.validate()

        assert e.value.message["answer"] == "under_100_words"
コード例 #6
0
    def test_answer_must_not_be_null(self):
        with self.app.app_context(), pytest.raises(IntegrityError):
            question = BriefClarificationQuestion(brief=self.brief,
                                                  question="Why?")

            db.session.add(question)
            db.session.commit()
コード例 #7
0
def publish_answer(current_user, brief_id, data):
    brief = briefs.get(brief_id)
    if not brief:
        raise NotFoundError("Invalid brief id '{}'".format(brief_id))

    if not briefs.has_permission_to_brief(current_user.id, brief.id):
        raise UnauthorisedError('Unauthorised to publish answer')

    publish_question = data.get('question')
    if not publish_question:
        raise ValidationError('Question is required')

    answer = data.get('answer')
    if not answer:
        raise ValidationError('Answer is required')

    brief_clarification_question = brief_clarification_question_service.save(
        BriefClarificationQuestion(_brief_id=brief.id,
                                   question=publish_question,
                                   answer=answer,
                                   user_id=current_user.id))

    question_id = data.get('questionId')
    if question_id:
        question = brief_question_service.get(question_id)
        if question.brief_id == brief.id:
            question.answered = True
            brief_question_service.save(question)

    audit_service.log_audit_event(
        audit_type=audit_types.create_brief_clarification_question,
        user=current_user.email_address,
        data={'briefId': brief.id},
        db_object=brief_clarification_question)
コード例 #8
0
    def test_cannot_update_brief_by_id(self):
        with self.app.app_context(), pytest.raises(ValidationError) as e:
            BriefClarificationQuestion(brief_id=self.brief.id,
                                       question="Why?",
                                       answer="Because")

        assert str(
            e.value.message
        ) == "Cannot update brief_id directly, use brief relationship"
コード例 #9
0
    def setup_dummy_brief(self,
                          id=None,
                          user_id=1,
                          status=None,
                          data=None,
                          published_at=None,
                          withdrawn_at=None,
                          cancelled_at=None,
                          unsuccessful_at=None,
                          framework_slug='digital-outcomes-and-specialists',
                          lot_slug='digital-specialists',
                          add_clarification_question=False):
        if published_at is not None and status is not None:
            raise ValueError('Cannot provide both status and published_at')
        if withdrawn_at is not None and published_at is None:
            raise ValueError(
                'If setting withdrawn_at then published_at must also be set')
        if not published_at:
            if status == 'closed':
                published_at = datetime.utcnow() - timedelta(days=1000)
            elif status == 'withdrawn':
                published_at = datetime.utcnow() - timedelta(days=1000)
                withdrawn_at = datetime.utcnow()
            elif status == 'cancelled':
                published_at = datetime.utcnow() - timedelta(days=1000)
                cancelled_at = datetime.utcnow()
            elif status == 'unsuccessful':
                published_at = datetime.utcnow() - timedelta(days=1000)
                unsuccessful_at = datetime.utcnow()
            else:
                published_at = None if status == 'draft' else datetime.utcnow()
        framework = Framework.query.filter(
            Framework.slug == framework_slug).first()
        lot = Lot.query.filter(Lot.slug == lot_slug).first()

        brief = Brief(id=id,
                      data=data,
                      framework=framework,
                      lot=lot,
                      users=[User.query.get(user_id)],
                      published_at=published_at,
                      withdrawn_at=withdrawn_at,
                      cancelled_at=cancelled_at,
                      unsuccessful_at=unsuccessful_at)

        db.session.add(brief)
        if add_clarification_question:
            db.session.add(
                BriefClarificationQuestion(
                    brief=brief,
                    question=
                    "What is the answer to the meaning of life, the Universe and everything?",
                    answer="42"))

        db.session.commit()
        return brief
コード例 #10
0
    def test_published_at_is_set_on_creation(self):
        with self.app.app_context():
            question = BriefClarificationQuestion(brief=self.brief,
                                                  question="Why?",
                                                  answer="Because")

            db.session.add(question)
            db.session.commit()

            assert isinstance(question.published_at, datetime)
コード例 #11
0
    def test_brief_must_be_live(self):
        with self.app.app_context():
            brief = Brief(data={},
                          framework=self.framework,
                          lot=self.lot,
                          status="draft")
            with pytest.raises(ValidationError) as e:
                BriefClarificationQuestion(brief=brief,
                                           question="Why?",
                                           answer="Because")

            assert str(
                e.value.message) == "Brief status must be 'live', not 'draft'"