def test_post_fails_with_bad_data(test_app, test_case, basic_case_definition):
    session = MagicMock()
    test_case['data']['case_definition_id'] = basic_case_definition.id
    service = ActivityDefinitionsService(json_args=test_case['data'],
                                         _session=session)
    g.request_user = UserFactory.build()

    response = service.post()

    response_json = response[0].get_json()
    assert response[1] == test_case['response_status']
    if test_case['response_message']:
        assert response_json['message'] == test_case['response_message']
def test_post_valid_activity_definition(test_app, basic_case_definition):
    activity_definition = {
        "name": "Test Activity Definition",
        "case_definition_id": basic_case_definition.id
    }

    session = MagicMock()
    service = ActivityDefinitionsService(json_args=activity_definition,
                                         _session=session)
    g.request_user = UserFactory.build()

    response = service.post()
    assert response is not None
    assert isinstance(response, models.ActivityDefinition)
def test_post_fails_with_invalid_case_definition_id(test_app):
    activity_definition = {
        "name": "Test Activity Definition",
        "case_definition_id": 99
    }
    session = MagicMock()
    service = ActivityDefinitionsService(json_args=activity_definition,
                                         _session=session)
    g.request_user = UserFactory.build()

    with patch.object(models.CaseDefinition.query, 'get_or_404') as get_or_404:
        get_or_404.side_effect = NotFound
        with pytest.raises(NotFound):
            service.post()
def test_post_fails_with_duplicate_name(test_app, basic_case_definition):
    activity_definition = {
        "name": "Test Case Definition",
        "case_definition_id": basic_case_definition.id
    }
    session = MagicMock()
    service = ActivityDefinitionsService(json_args=activity_definition,
                                         _session=session)
    g.request_user = UserFactory.build()

    with patch.object(service, '_is_name_unique') as _is_name_unique:
        _is_name_unique.return_value = False
        response = service.post()

    response_json = response[0].get_json()
    assert response[1] == 400
    assert response_json[
        'message'] == "Activity Definition names must be unique."
def get_custom_field(activity_definition_id, custom_field_id):
    current_app.logger.info(
        f"/activity_definitions/{activity_definition_id}/custom_fields/{custom_field_id} GET accessed by {g.request_user.email}"
    )
    service = ActivityDefinitionsService()
    return service.get_custom_field(activity_definition_id, custom_field_id)
def delete_activity_definition(activity_definition_id):
    current_app.logger.info(
        f"/activity_definitions/{activity_definition_id} DELETE accessed by {g.request_user.email}"
    )
    service = ActivityDefinitionsService()
    return service.delete(activity_definition_id)
def put_activity_definition(activity_definition_id):
    current_app.logger.info(
        f"/activity_definitions/{activity_definition_id} PUT accessed by {g.request_user.email}"
    )
    service = ActivityDefinitionsService(json_args=request.get_json())
    return service.put(activity_definition_id)
def get_activity_definitions():
    current_app.logger.info(
        f"/activity_definitions GET accessed by {g.request_user.email}")
    service = ActivityDefinitionsService()
    return service.get_all()
def post_custom_field(activity_definition_id):
    current_app.logger.info(
        f"/activity_definitions/{activity_definition_id}/custom_fields POST accessed by {g.request_user.email}"
    )
    service = ActivityDefinitionsService(json_args=request.get_json())
    return service.post_custom_field(activity_definition_id)
def post_activity_definition():
    current_app.logger.info(
        f"/activity_definitions POST accessed by {g.request_user.email}")
    service = ActivityDefinitionsService(json_args=request.get_json())
    activity_definition = service.post()
    return jsonify(activity_definition.__getstate__()), 200
def test_can_init():
    session = MagicMock()
    service = ActivityDefinitionsService(json_args={'foo': 'bar'},
                                         _session=session)
    assert service.session is session
    assert service.json_args['foo'] == 'bar'