def test_generate_eq_url_seft(self, mock_request):
        # Given all external services are mocked and we have seft collection instrument
        mock_request.get(url_get_collection_exercise, json=collection_exercise)
        mock_request.get(url_get_collection_exercise_events,
                         json=collection_exercise_events)
        mock_request.get(url_get_business_party, json=business_party)
        mock_request.get(url_get_survey, json=survey)
        mock_request.get(url_get_ci, json=collection_instrument_seft)
        mock_request.get(url_banner_api, status_code=404)

        # When create_payload is called
        # Then an InvalidEqPayLoad is raised
        with app.app_context():
            with self.assertRaises(InvalidEqPayLoad) as e:
                EqPayload().create_payload(
                    case,
                    collection_exercise,
                    party_id=respondent_party["id"],
                    business_party_id=business_party["id"],
                    survey=survey_eq,
                    version=None,
                )
        self.assertEqual(
            e.exception.message,
            "Collection instrument 68ad4018-2ddd-4894-89e7-33f0135887a2 type is not EQ"
        )
    def test_generate_eq_url_no_eq_id(self, mock_request):
        # Given all external services are mocked and we have an EQ collection instrument without an EQ ID
        with open(
                "tests/test_data/collection_instrument/collection_instrument_eq_no_eq_id.json"
        ) as json_data:
            collection_instrument_eq_no_eq_id = json.load(json_data)

        mock_request.get(url_get_ci, json=collection_instrument_eq_no_eq_id)
        mock_request.get(url_banner_api, status_code=404)

        # When create_payload is called
        # Then an InvalidEqPayLoad is raised
        with app.app_context():
            with self.assertRaises(InvalidEqPayLoad) as e:
                EqPayload().create_payload(
                    case,
                    collection_exercise,
                    party_id=respondent_party["id"],
                    business_party_id=business_party["id"],
                    survey=survey_eq,
                    version=None,
                )
        self.assertEqual(
            e.exception.message,
            "Collection instrument 68ad4018-2ddd-4894-89e7-33f0135887a2 "
            "classifiers are incorrect or missing",
        )
    def test_generate_eq_url_missing_mandatory_event_date(self):
        # Given a mandatory event date does not exist
        collex_events_dates = [
            {
                "id": "e82e7ec9-b14e-412c-813e-edfd2e03e773",
                "collectionExerciseId": "8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0",
                "tag": "return_by",
                "timestamp": "2018-03-27T01:00:00.000+01:00",
            },
            {
                "id": "8a24731e-3d79-4f3c-b6eb-3b199f53694f",
                "collectionExerciseId": "8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0",
                "tag": "reminder",
                "timestamp": "2018-04-03T01:00:00.000+01:00",
            },
        ]

        # When find_event_date_by_tag is called with a search param
        # Then an InvalidEqPayLoad is raised

        with self.assertRaises(InvalidEqPayLoad) as e:
            EqPayload()._find_event_date_by_tag("return by",
                                                collex_events_dates, "123",
                                                True)
        self.assertEqual(
            e.exception.message,
            "Mandatory event not found for collection 123 for search param return by"
        )
    def test_generate_eq_url_for_response_id_for_v2(self, mock_request):
        # Given all external services are mocked and we have an EQ collection instrument without an EQ ID
        mock_request.get(url_get_collection_exercise_events,
                         json=collection_exercise_events)
        mock_request.get(url_get_business_party, json=business_party)
        with open(
                "tests/test_data/collection_instrument/collection_instrument_eq.json"
        ) as json_data:
            collection_instrument_eq = json.load(json_data)

        mock_request.get(url_get_ci, json=collection_instrument_eq)
        mock_request.get(url_banner_api, status_code=404)

        # When create_payload is called
        # Then an InvalidEqPayLoad is raised
        with app.app_context():
            payload = EqPayload().create_payload(
                case,
                collection_exercise,
                party_id=respondent_party["id"],
                business_party_id=business_party["id"],
                survey=survey_eq,
                version="v2",
            )
        self.assertFalse("response_id" in payload)
    def test_generate_eq_url_missing_mandatory_event_date(self):

        # Given a mandatory event date does not exist
        collex_events_dates = [{
            'id': 'e82e7ec9-b14e-412c-813e-edfd2e03e773',
            'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0',
            'tag': 'return_by',
            'timestamp': '2018-03-27T01:00:00.000+01:00'
        }, {
            'id': '8a24731e-3d79-4f3c-b6eb-3b199f53694f',
            'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0',
            'tag': 'reminder',
            'timestamp': '2018-04-03T01:00:00.000+01:00'
        }]

        # When find_event_date_by_tag is called with a search param
        # Then an InvalidEqPayLoad is raised

        with self.assertRaises(InvalidEqPayLoad) as e:
            EqPayload()._find_event_date_by_tag('return by',
                                                collex_events_dates, '123',
                                                True)
        self.assertEqual(
            e.exception.message,
            'Mandatory event not found for collection 123 for search param return by'
        )
    def test_generate_eq_url_iso8601_date_format(self):
        # Given a valid date
        date = "2007-01-25T12:00:00Z"

        # When format_string_long_date_time_to_short_date is called
        # Then the correct date is returned
        result = EqPayload()._format_string_long_date_time_to_short_date(date)
        self.assertEqual(result, "2007-01-25")
    def test_string_date_time_adjusts_to_local_time_iso_format(self):
        # Given a valid date in tz -1hr before midnight
        date = "2007-01-25T23:59:59-0100"

        # When format_date is called
        result = EqPayload()._format_string_long_date_time_to_iso_format(date)

        # Then the date is localised to the next day
        self.assertEqual(result, "2007-01-26T00:59:59+00:00")
    def test_generate_eq_url_incorrect_date_format(self):
        # Given an invalid date
        date = "invalid"

        # When format_string_long_date_time_to_short_date is called
        # Then an InvalidEqPayLoad is raised
        with self.assertRaises(InvalidEqPayLoad) as e:
            EqPayload()._format_string_long_date_time_to_short_date(date)
        self.assertEqual(e.exception.message, "Unable to format invalid")
    def test_generate_eq_url_non_mandatory_event_date_is_none(self):

        # Given a non mandatory event date does not exist
        collex_events_dates = []
        # When find_event_date_by_tag is called with a search param
        # Then a None response is returned and no exception is raised

        response = EqPayload()._find_event_date_by_tag('employment', collex_events_dates, '123', False)
        self.assertEqual(response, None)
    def test_iso8601_adjusts_to_local_time(self):
        # Given a valid date in tz -1hr before midnight
        date = '2007-01-25T23:59:59-0100'

        # When format_date is called
        result = EqPayload()._format_string_long_date_time_to_short_date(date)

        # Then the date is localised to the next day
        self.assertEqual(result, '2007-01-26')
    def test_generate_eq_url_non_mandatory_event_date_is_returned(self):

        # Given a non mandatory event date exists
        collex_events_dates = [{'id': 'e82e7ec9-b14e-412c-813e-edfd2e03e773',
                                'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0',
                                'tag': 'return_by', 'timestamp': '2018-03-27T01:00:00.000+01:00'},
                               {'id': '8a24731e-3d79-4f3c-b6eb-3b199f53694f',
                                'collectionExerciseId': '8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0',
                                'tag': 'employment', 'timestamp': '2018-04-03T01:00:00.000+01:00'}]
        # When find_event_date_by_tag is called with a search param
        # Then the formatted date is returned

        response = EqPayload()._find_event_date_by_tag('employment', collex_events_dates, '123', False)
        self.assertEqual(response, '2018-04-03')
Exemple #12
0
def get_eq_url(case_id, party_id, business_party_id, survey_short_name):
    logger.info('Attempting to generate EQ URL',
                case_id=case_id,
                party_id=party_id)

    case = get_case_by_case_id(case_id)

    valid_enrolment = party_controller.is_respondent_enrolled(
        party_id, business_party_id, survey_short_name, return_survey=True)
    if not valid_enrolment:
        raise NoSurveyPermission(party_id, case_id)

    if case['caseGroup']['caseGroupStatus'] in ('COMPLETE', 'COMPLETEDBYPHONE',
                                                'NOLONGERREQUIRED'):
        logger.info(
            'The case group status is complete, opening an EQ is forbidden',
            case_id=case_id,
            party_id=party_id)
        abort(403)

    payload = EqPayload().create_payload(case, party_id, business_party_id,
                                         valid_enrolment['survey'])

    json_secret_keys = app.config['JSON_SECRET_KEYS']
    encrypter = Encrypter(json_secret_keys)
    token = encrypter.encrypt(payload)

    eq_url = app.config['EQ_URL'] + token

    category = 'EQ_LAUNCH'
    ci_id = case['collectionInstrumentId']
    post_case_event(
        case_id,
        party_id=party_id,
        category=category,
        description=
        f"Instrument {ci_id} launched by {party_id} for case {case_id}")

    logger.info('Successfully generated EQ URL',
                case_id=case_id,
                ci_id=ci_id,
                party_id=party_id,
                business_party_id=business_party_id,
                survey_short_name=survey_short_name,
                tx_id=payload['tx_id'])
    return eq_url
    def test_generate_eq_url_no_form_type(self, mock_request):

        # Given all external services are mocked and we have an EQ collection instrument without a Form_type
        with open('tests/test_data/collection_instrument/collection_instrument_eq_no_form_type.json') as json_data:
            collection_instrument_eq_no_form_type = json.load(json_data)

        mock_request.get(url_get_ci, json=collection_instrument_eq_no_form_type)

        # When create_payload is called
        # Then an InvalidEqPayLoad is raised
        with app.app_context():
            with self.assertRaises(InvalidEqPayLoad) as e:
                EqPayload().create_payload(case, party_id=respondent_party['id'], business_party_id=business_party['id'],
                                           survey=survey_eq)
        self.assertEqual(e.exception.message,
                         'Collection instrument 68ad4018-2ddd-4894-89e7-33f0135887a2 '
                         'classifiers are incorrect or missing')
    def test_generate_eq_url_non_mandatory_event_date_is_returned(self):
        # Given a non mandatory event date exists
        collex_events_dates = [
            {
                "id": "e82e7ec9-b14e-412c-813e-edfd2e03e773",
                "collectionExerciseId": "8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0",
                "tag": "return_by",
                "timestamp": "2018-03-27T01:00:00.000+01:00",
            },
            {
                "id": "8a24731e-3d79-4f3c-b6eb-3b199f53694f",
                "collectionExerciseId": "8d926ae3-fb3c-4c25-9f0f-356ded7d1ac0",
                "tag": "employment",
                "timestamp": "2018-04-03T01:00:00.000+01:00",
            },
        ]
        # When find_event_date_by_tag is called with a search param
        # Then the formatted date is returned

        response = EqPayload()._find_event_date_by_tag("employment",
                                                       collex_events_dates,
                                                       "123", False)
        self.assertEqual(response, "2018-04-03")
Exemple #15
0
            raise ValueError(f"The eq version [{version}] is not supported")

    if case["caseGroup"]["caseGroupStatus"] in ("COMPLETE", "COMPLETEDBYPHONE",
                                                "NOLONGERREQUIRED"):
        logger.info(
            "The case group status is complete, opening an EQ is forbidden",
            case_id=case_id,
            party_id=party_id)
        abort(403)

    survey = survey_controller.get_survey_by_short_name(survey_short_name)
    if not party_controller.is_respondent_enrolled(party_id, business_party_id,
                                                   survey):
        raise NoSurveyPermission(party_id, case_id)

    payload = EqPayload().create_payload(case, collection_exercise, party_id,
                                         business_party_id, survey, version)

    json_secret_keys = app.config["JSON_SECRET_KEYS"]
    encrypter = Encrypter(json_secret_keys)

    if version == "v2":
        token = encrypter.encrypt(payload, "eq")
        eq_url = app.config["EQ_URL"] + token
    elif version == "v3":
        token = encrypter.encrypt(payload, "eq_v3")
        eq_url = app.config["EQ_V3_URL"] + token

    category = "EQ_LAUNCH"
    ci_id = case["collectionInstrumentId"]
    post_case_event(