Exemplo n.º 1
0
def test_get_logins_route(app, mocker, client):
    expected_status = 200
    expected_length = 25
    expected_limit = 25
    expected_page = 1
    expected_total = 30
    expected_next_uri = 'http://localhost/logins/2/25'

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

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.return_value = admin1

    auth_db_mock = mocker.patch('modules.administrators.authentication.db')
    auth_db_mock.add.return_value = None
    auth_db_mock.commit.return_value = None

    query_mock.return_value \
        .order_by.return_value \
        .limit.return_value \
        .offset.return_value \
        .__iter__.return_value = [Login()] * expected_length
    query_mock.return_value \
        .order_by.return_value \
        .count.return_value = expected_total

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.get("/logins?app_key=123",
                          headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert len(response.json['logins']) == expected_length
    assert response.json['limit'] == expected_limit
    assert response.json['page'] == expected_page
    assert response.json['total'] == expected_total
    assert response.json['next_uri'] == expected_next_uri
Exemplo n.º 2
0
def test_get_app_keys_empty_route(app, mocker, client):
    expected_status = 204
    expected_json = None

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

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.return_value = admin1

    auth_db_mock = mocker.patch('modules.administrators.authentication.db')
    auth_db_mock.add.return_value = None
    auth_db_mock.commit.return_value = None

    query_mock.return_value \
        .filter.return_value \
        .order_by.return_value \
        .limit.return_value \
        .offset.return_value \
        .__iter__.return_value = []
    query_mock.return_value \
        .filter.return_value \
        .order_by.return_value \
        .count.return_value = 15

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.get("/app_keys/3?app_key=123",
                          headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert response.json == expected_json
Exemplo n.º 3
0
def test_get_auth_token_route_ok(app, mocker, client):
    expected_status = 200
    expected_r_expiration = 14400
    expected_r_user_id = 2
    expected_r_username = "******"

    # mock db query
    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    role1 = Role()
    role1.id = 1
    role1.name = 'USER'
    role1.password_reset_days = 365
    role1.password_policy = True
    role1.password_reuse_history = 10

    user2 = User()
    user2.id = 2
    user2.username = expected_r_username
    user2.password = "******"
    user2.roles = [role1]

    # mock user login db query
    query_mock.return_value \
        .filter.return_value \
        .first.return_value = user2

    db_mock = mocker.patch('modules.users.authentication.db')
    db_mock.add.return_value = None
    db_mock.commit.return_value = None

    # mock user login
    auth_mock = mocker.patch('modules.users.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'user2:user2pass'.encode('ascii')).decode('utf-8')

    response = client.get("/token?app_key=123",
                          headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert response.json['expiration'] == expected_r_expiration
    assert response.json['user_id'] == expected_r_user_id
    assert response.json['username'] == expected_r_username
    assert 'token' in response.json
Exemplo n.º 4
0
def test_get_auth_token_route_ok(app, mocker, client):
    expected_status = 200
    expected_r_expiration = 14400
    expected_r_user_id = 1
    expected_r_username = "******"

    # mock db query
    query_mock = mocker.patch('flask_sqlalchemy._QueryProperty.__get__')

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.username = expected_r_username
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.return_value = admin1

    db_mock = mocker.patch('modules.administrators.authentication.db')
    db_mock.add.return_value = None
    db_mock.commit.return_value = None

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.get("/token?app_key=123",
                          headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert response.json['expiration'] == expected_r_expiration
    assert response.json['user_id'] == expected_r_user_id
    assert response.json['username'] == expected_r_username
    assert 'token' in response.json
Exemplo n.º 5
0
def test_put_terms_of_service_route_ok(app, mocker, client):
    expected_status = 200
    expected_m_length = 8
    expected_m_id = 1
    expected_m_text = "This is TOS 1a"
    expected_m_version = "1.0b"
    expected_m_publish_date = "2018-06-19T08:00:00+0000"
    expected_m_status = TermsOfService.STATUS_DISABLED
    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}$")

    data = {
        'text': expected_m_text,
        'version': expected_m_version,
        "publish_date": expected_m_publish_date,
        "status": expected_m_status,
    }

    tos_1 = TermsOfService()
    tos_1.id = expected_m_id

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

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.return_value = admin1

    auth_db_mock = mocker.patch('modules.administrators.authentication.db')
    auth_db_mock.add.return_value = None
    auth_db_mock.commit.return_value = None

    query_mock.return_value \
        .get.return_value = tos_1

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

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.put(
        "/terms_of_service/{}?app_key=123".format(expected_m_id),
        json=data,
        headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert 'terms_of_service' in response.json
    assert len(response.json['terms_of_service']) == expected_m_length
    assert response.json['terms_of_service']['id'] == expected_m_id
    assert response.json['terms_of_service']['text'] == expected_m_text
    assert response.json['terms_of_service']['version'] == \
        expected_m_version
    assert response.json['terms_of_service']['publish_date'] == \
        expected_m_publish_date
    assert response.json['terms_of_service']['status'] == expected_m_status
    assert bool(
        re_datetime.match(
            response.json['terms_of_service']['status_changed_at']))
    assert response.json['terms_of_service']['created_at'] == \
        expected_m_created_at
    assert response.json['terms_of_service']['updated_at'] == \
        expected_m_updated_at
Exemplo n.º 6
0
def test_post_user_route_ok(app, mocker, client):
    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}$")

    data = {
        '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 app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.return_value = admin1

    auth_db_mock = mocker.patch('modules.administrators.authentication.db')
    auth_db_mock.add.return_value = None
    auth_db_mock.commit.return_value = None

    # 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

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.post(
        "/user_profiles?app_key=123",
        json=data,
        headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert 'user_profile' in response.json
    assert len(response.json['user_profile']) == expected_m_length
    assert response.json['user_profile']['id'] == expected_m_id
    assert response.json['user_profile']['user_id'] == expected_m_user_id
    assert response.json['user_profile']['first_name'] == \
        expected_m_first_name
    assert response.json['user_profile']['last_name'] == expected_m_last_name
    assert response.json['user_profile']['joined_at'] == expected_m_joined_at
    assert response.json['user_profile']['status'] == expected_m_status
    assert bool(re_datetime.match(
        response.json['user_profile']['status_changed_at']))
    assert response.json['user_profile']['created_at'] == \
        expected_m_created_at
    assert response.json['user_profile']['updated_at'] == \
        expected_m_updated_at
Exemplo n.º 7
0
def test_post_app_key_route_ok(app, mocker, client):
    expected_status = 201
    expected_m_length = 7
    expected_m_application = 'Test Application'
    expected_m_id = None
    expected_m_key = 'B8CzqaJWs9TmffSJjxDCFrepzhvYzrKz'
    expected_m_status = AppKey.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}$")

    data = {
        'application': expected_m_application,
        'key': expected_m_key,
        "status": expected_m_status
    }

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

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock app key authorization db query
    query_mock.return_value \
        .filter.return_value \
        .one.return_value = AppKey()

    # mock user login db query
    role2 = Role()
    role2.id = 2
    role2.name = 'SUPER_ADMIN'
    role2.password_reset_days = 365

    admin1 = Administrator()
    admin1.id = 1
    admin1.password = '******'
    admin1.roles = [role2]

    query_mock.return_value \
        .filter.return_value \
        .first.side_effect = [admin1, None, None]

    auth_db_mock = mocker.patch('modules.administrators.authentication.db')
    auth_db_mock.add.return_value = None
    auth_db_mock.commit.return_value = None

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

    # mock user login
    auth_mock = mocker.patch(
        'modules.administrators.Authentication.is_account_locked')
    auth_mock.return_value = False

    credentials = base64.b64encode(
        'admin1:admin1pass'.encode('ascii')).decode('utf-8')

    response = client.post(
        "/app_keys?app_key=123",
        json=data,
        headers={"Authorization": f"Basic {credentials}"})

    assert response.status_code == expected_status
    assert 'app_key' in response.json
    assert len(response.json['app_key']) == expected_m_length
    assert response.json['app_key']['application'] == expected_m_application
    assert response.json['app_key']['id'] == expected_m_id
    assert response.json['app_key']['key'] == expected_m_key
    assert response.json['app_key']['status'] == expected_m_status
    assert bool(re_datetime.match(
        response.json['app_key']['status_changed_at']))
    assert response.json['app_key']['created_at'] == expected_m_created_at
    assert response.json['app_key']['updated_at'] == expected_m_updated_at