def test_internal_error(monkeypatch):

    monkeypatch.setenv("CORS_ALLOW_ORIGIN", ORIGIN)

    response = InternalError({})
    response_json = response.to_json_string()

    assert response.statusCode == 500
    assert response.body == "An unexpected error occured."
def test_internal_error_message(monkeypatch):

    monkeypatch.setenv("CORS_ALLOW_ORIGIN", ORIGIN)

    MESSAGE = "message"
    response = InternalError({}, message=MESSAGE)
    response_json = response.to_json_string()

    assert response.statusCode == 500
    assert response.body is not None
    assert response.body == MESSAGE
def test_internal_error_exception(monkeypatch):

    monkeypatch.setenv("CORS_ALLOW_ORIGIN", ORIGIN)

    try:
        raise KeyError("Test")
    except Exception as e:
        response = InternalError({}, exception=e)

    response_json = response.to_json_string()

    assert response.statusCode == 500
    assert response.body is not None
    assert response_json is not None
def test_internal_error_message_and_exception(monkeypatch):

    monkeypatch.setenv("CORS_ALLOW_ORIGIN", ORIGIN)

    MESSAGE = "message"
    response = InternalError({}, message=MESSAGE, exception=KeyError())
    response_json = response.to_json_string()

    assert response.statusCode == 500
    assert response.body is not None
    assert response.body['message'] == MESSAGE
    assert response.body['exception'] is not None

    assert response_json is not None
Beispiel #5
0
def handle_item_closed(event, context):
    try:
        helper.log_method_initiated("Send notification", event, logger)

        if "detail" not in event or "item_id" not in event["detail"]:
            return BadRequest(event,
                              "Event contains no item_id.",
                              add_cors_headers=False).to_json_string()

        with Session() as session:
            item_id = event["detail"]["item_id"]
            item = item_handler.get_item_by_id(item_id, session)
            if item is None:
                return BadRequest(
                    event,
                    f"No item was found with the given item_id [{item_id}].",
                    add_cors_headers=False).to_json_string()
            item = item_handler.update_item_warning_tags(item, session)

            rating = int(item.result_score * 25)
            rating_text = get_rating_text(rating)

            parameters = dict(rating=rating,
                              rating_text=rating_text,
                              item_id=item.id,
                              content=item.content)

            for submission in item.submissions:
                if submission.mail is not None:
                    try:
                        mail_sender.send_notification("item_closed",
                                                      mail=submission.mail,
                                                      replacements=parameters)
                    except Exception as e:
                        logger.exception(e)

                if submission.telegram_id is not None:
                    try:
                        mail_sender.send_notification(
                            "item_closed",
                            telegram_id=submission.telegram_id,
                            replacements=parameters)
                    except Exception as e:
                        logger.exception(e)

            return Success(event, add_cors_headers=False).to_json_string()

    except Exception as e:
        return InternalError(event,
                             "Error sending notification",
                             e,
                             add_cors_headers=False).to_json_string()
Beispiel #6
0
def handle_item_rejected(event, context):
    try:
        helper.log_method_initiated("Handle item rejected", event, logger)

        if "detail" not in event or "item_id" not in event["detail"]:
            return BadRequest(event,
                              "Event contains no item_id.",
                              add_cors_headers=False).to_json_string()

        with Session() as session:

            item_id = event["detail"]["item_id"]
            item = item_handler.get_item_by_id(item_id, session)

            if item is None:
                return BadRequest(
                    event,
                    f"No item was found with the given item_id [{item_id}].",
                    add_cors_headers=False).to_json_string()

            parameters = dict(content=item.content)

            for submission in item.submissions:
                if submission.mail is not None:
                    try:
                        mail_sender.send_notification("item_rejected",
                                                      mail=submission.mail,
                                                      replacements=parameters)
                    except Exception as e:
                        logger.exception(e)

                if submission.telegram_id is not None:
                    try:
                        mail_sender.send_notification(
                            "item_rejected",
                            telegram_id=submission.telegram_id,
                            replacements=parameters)
                    except Exception as e:
                        logger.exception(e)

            return Success(event, add_cors_headers=False).to_json_string()
    except Exception as e:
        response = InternalError(event,
                                 "Error sending notification",
                                 e,
                                 add_cors_headers=False).to_json_string()
        logger.error(response)

        return response
Beispiel #7
0
def attach_iot_policy(event, context):
    try:
        helper.log_method_initiated("Attach IoT policy.", event, logger)

        try:
            cognito_identity = helper.get_cognito_identity_from_event(event)
        except Exception:
            logger.error(ERROR_MSG_IDENTIY_ID_MISSING)
            return BadRequest(event, ERROR_MSG_IDENTIY_ID_MISSING).to_json_string()

        try:
            user_id = helper.cognito_id_from_event(event)
        except Exception:
            logger.error(ERROR_MSG_USER_ID_MISSING)
            return BadRequest(event, ERROR_MSG_USER_ID_MISSING).to_json_string()

        client = BotoClientProvider().get_client('iot')

        # Get the policy for the current user or create it
        try:
            policy = client.get_policy(policyName=user_id)
        except client.exceptions.ResourceNotFoundException as e:
            resource = POLICY_DOCUMENT_STRING_FORMAT.format(user_id)

            policy_document['Statement'][0]['Resource'] = resource

            policy = client.create_policy(
                policyName=user_id, policyDocument=json.dumps(policy_document))

        attached_policies = client.list_attached_policies(
            target=cognito_identity)

        # If the policy is already attached to the current identity return
        if(policy['policyName'] in [attached_policy['policyName'] for attached_policy in attached_policies['policies']]):
            logger.info(INFO_MSG_POLICY_ALREADY_ATTACHED)
            return Success(event).to_json_string()

        client.attach_policy(
            policyName=policy['policyName'], target=cognito_identity)

        logger.info(INFO_MSG_POLICY_ATTACHED)
        return Success(event).to_json_string()

    except Exception as e:
        logger.exception(ERROR_MSG_EXCEPTION)
        return InternalError(event, ERROR_MSG_EXCEPTION, e).to_json_string()