def test_post_user_profiles_type_fail(app, mocker):
    expected_status = 400
    expected_json = {
        "error": {
            'user_id': ['Not a valid integer.'],
            'first_name': ['Not a valid string.'],
            'joined_at': ['Not a valid datetime.'],
            'last_name': ['Not a valid string.'],
            'status': ['Not a valid integer.'],
        }
    }

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {
        'user_id': 'bad',
        'first_name': 123,
        'joined_at': 123,
        'last_name': 123,
        'status': "bad",
    }

    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock exists() validation
    query_mock.return_value \
        .get.return_value = None

    result = post_user_profiles()

    assert result[1] == expected_status
    assert result[0].json == expected_json
def test_post_user_profiles_max_fail(app, mocker):
    expected_status = 400
    expected_json = {'error': {
        'first_name': ["Value must be between 1 and 40 characters long."],
        'last_name': ["Value must be between 2 and 40 characters long."],
    }}

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {
        'user_id': 9,
        'first_name': "LRUNzhfsbfrfZ4BT9N6R3SNYVfAAuQdQdTSmrwFew",
        'last_name': "z3Sytm4QrL8g7J4kgugEABnhwXZAnCZmrngUCeeXm",
        'joined_at': "2019-02-04T00:00:00+0000",
        'status': UserProfile.STATUS_ENABLED
    }

    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock exists() validation
    user_9 = User()
    user_9.id = 9
    query_mock.return_value \
        .get.return_value = user_9

    result = post_user_profiles()

    assert result[1] == expected_status
    assert result[0].json == expected_json
def test_post_user_profiles_min_fail(app, mocker):
    expected_status = 400
    expected_json = {'error': {
        'first_name': ["Value must be between 1 and 40 characters long."],
        'last_name': ["Value must be between 2 and 40 characters long."],
    }}

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {
        'user_id': 9,
        'first_name': "",
        'last_name': "A",
        'joined_at': "2019-02-04T00:00:00+0000",
        'status': UserProfile.STATUS_ENABLED
    }

    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock exists() validation
    user_9 = User()
    user_9.id = 9
    query_mock.return_value \
        .get.return_value = user_9

    result = post_user_profiles()

    assert result[1] == expected_status
    assert result[0].json == expected_json
def test_post_user_profile_ok(app, mocker):
    expected_status = 201
    expected_m_length = 9
    expected_m_id = None
    expected_m_user_id = 9
    expected_m_first_name = "Service"
    expected_m_last_name = "Account"
    expected_m_joined_at = "2019-02-04T00:00:00+0000"
    expected_m_status = UserProfile.STATUS_ENABLED
    expected_m_created_at = None
    expected_m_updated_at = None
    # @todo: timezone
    re_datetime = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$")

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {
        'user_id': expected_m_user_id,
        'first_name': expected_m_first_name,
        'last_name': expected_m_last_name,
        'joined_at': expected_m_joined_at,
        'status': expected_m_status,
    }

    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock exists() validation
    user_9 = User()
    user_9.id = 9
    query_mock.return_value \
        .get.return_value = user_9

    db_mock = mocker.patch('modules.user_profiles.routes_admin.db')
    db_mock.add.return_value = None
    db_mock.commit.return_value = None

    result = post_user_profiles()

    assert result[1] == expected_status
    assert 'user_profile' in result[0].json
    assert len(result[0].json['user_profile']) == expected_m_length
    assert result[0].json['user_profile']['id'] == expected_m_id
    assert result[0].json['user_profile']['user_id'] == expected_m_user_id
    assert result[0].json['user_profile']['first_name'] == \
        expected_m_first_name
    assert result[0].json['user_profile']['last_name'] == expected_m_last_name
    assert result[0].json['user_profile']['joined_at'] == expected_m_joined_at
    assert result[0].json['user_profile']['status'] == expected_m_status
    assert bool(re_datetime.match(
        result[0].json['user_profile']['status_changed_at']))
    assert result[0].json['user_profile']['created_at'] == \
        expected_m_created_at
    assert result[0].json['user_profile']['updated_at'] == \
        expected_m_updated_at
def test_post_user_profiles_required_fail(app, mocker):
    expected_status = 400
    expected_json = {
        'error': {
            'user_id': ['Missing data for required field.'],
            'first_name': ['Missing data for required field.'],
            'foo': ['Unknown field.'],
            'joined_at': ['Missing data for required field.'],
            'last_name': ['Missing data for required field.'],
            'status': ['Missing data for required field.'],
        }
    }

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {'foo': "bar"}

    result = post_user_profiles()

    assert result[1] == expected_status
    assert result[0].json == expected_json
def test_post_user_profiles_user_exists_fail(app, mocker):
    expected_status = 400
    expected_json = {'error': {
        'user_id': ['Invalid value.']}}

    request_mock = mocker.patch('modules.user_profiles.routes_admin.request')
    request_mock.json = {
        'user_id': 250,
        'first_name': "Service",
        'last_name': "Account",
        'joined_at': "2019-02-04T00:00:00+0000",
        'status': UserProfile.STATUS_ENABLED
    }

    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock exists() validation
    query_mock.return_value \
        .get.return_value = None

    result = post_user_profiles()

    assert result[1] == expected_status
    assert result[0].json == expected_json