Exemple #1
0
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(notify_api, notify_db, notify_db_session,
                                                                        sample_service):
    try:
        get_model_api_keys(sample_service.id, id=123)
        fail("Should have thrown a NoResultFound exception")
    except NoResultFound:
        pass
Exemple #2
0
def test_save_api_key_should_update_the_api_key(notify_api, notify_db, notify_db_session, sample_api_key):
    now = datetime.utcnow()
    saved_api_key = get_model_api_keys(service_id=sample_api_key.service_id, id=sample_api_key.id)
    save_model_api_key(saved_api_key, update_dict={'id': saved_api_key.id, 'expiry_date': now})
    all_api_keys = get_model_api_keys(service_id=sample_api_key.service_id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0].expiry_date == now
    assert all_api_keys[0].secret == saved_api_key.secret
    assert all_api_keys[0].id == saved_api_key.id
    assert all_api_keys[0].service_id == saved_api_key.service_id
Exemple #3
0
def get_api_keys(service_id, key_id=None):
    dao_fetch_service_by_id(service_id=service_id)

    try:
        if key_id:
            api_keys = [get_model_api_keys(service_id=service_id, id=key_id)]
        else:
            api_keys = get_model_api_keys(service_id=service_id)
    except NoResultFound:
        error = "API key not found for id: {}".format(service_id)
        raise InvalidRequest(error, status_code=404)

    return jsonify(apiKeys=api_key_schema.dump(api_keys, many=True).data), 200
Exemple #4
0
 def from_service_id(cls, service_id):
     keys = [{
         k: getattr(key, k)
         for k in SerialisedAPIKey.ALLOWED_PROPERTIES
     } for key in get_model_api_keys(service_id)]
     db.session.commit()
     return cls(keys)
Exemple #5
0
def test_save_api_key_should_create_new_api_key(notify_api, notify_db, notify_db_session, sample_service):
    api_key = ApiKey(**{'service_id': sample_service.id, 'name': sample_service.name})
    save_model_api_key(api_key)

    all_api_keys = get_model_api_keys(service_id=sample_service.id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0] == api_key
Exemple #6
0
def get_api_keys(service_id, key_id=None):
    try:
        service = get_model_services(service_id=service_id)
    except DataError:
        return jsonify(result="error", message="Invalid service id"), 400
    except NoResultFound:
        return jsonify(result="error", message="Service not found"), 404

    try:
        if key_id:
            api_keys = [get_model_api_keys(service_id=service_id, id=key_id)]
        else:
            api_keys = get_model_api_keys(service_id=service_id)
    except DAOException as e:
        return jsonify(result='error', message=str(e)), 400
    except NoResultFound:
        return jsonify(result="error", message="API key not found"), 404

    return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200
def get_api_keys(service_id, key_id=None):
    try:
        service = get_model_services(service_id=service_id)
    except DataError:
        return jsonify(result="error", message="Invalid service id"), 400
    except NoResultFound:
        return jsonify(result="error", message="Service not found"), 404

    try:
        if key_id:
            api_keys = [get_model_api_keys(service_id=service_id, id=key_id)]
        else:
            api_keys = get_model_api_keys(service_id=service_id)
    except DAOException as e:
        return jsonify(result='error', message=str(e)), 400
    except NoResultFound:
        return jsonify(result="error", message="API key not found"), 404

    return jsonify(apiKeys=api_keys_schema.dump(api_keys).data), 200
def revoke_api_key(service_id, api_key_id):
    try:
        service_api_key = get_model_api_keys(service_id=service_id, id=api_key_id)
    except DataError:
        return jsonify(result="error", message="Invalid  api key for service"), 400
    except NoResultFound:
        return jsonify(result="error", message="Api key not found for service"), 404

    save_model_api_key(service_api_key, update_dict={'id': service_api_key.id, 'expiry_date': datetime.utcnow()})
    return jsonify(), 202
Exemple #9
0
def test_should_not_return_revoked_api_keys_older_than_7_days(
        sample_service, days_old, expected_length):
    expired_api_key = ApiKey(
        **{
            'service': sample_service,
            'name': sample_service.name,
            'created_by': sample_service.created_by,
            'key_type': KEY_TYPE_NORMAL,
            'expiry_date': datetime.utcnow() - timedelta(days=days_old)
        })
    save_model_api_key(expired_api_key)

    all_api_keys = get_model_api_keys(service_id=sample_service.id)

    assert len(all_api_keys) == expected_length
def test_save_api_key_should_create_new_api_key_and_history(sample_service):
    api_key = ApiKey(**{'service': sample_service,
                        'name': sample_service.name,
                        'created_by': sample_service.created_by,
                        'key_type': KEY_TYPE_NORMAL})
    save_model_api_key(api_key)

    all_api_keys = get_model_api_keys(service_id=sample_service.id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0] == api_key
    assert api_key.version == 1

    all_history = api_key.get_history_model().query.all()
    assert len(all_history) == 1
    assert all_history[0].id == api_key.id
    assert all_history[0].version == api_key.version
def test_expire_api_key_should_update_the_api_key_and_create_history_record(notify_api,
                                                                            sample_api_key):
    expire_api_key(service_id=sample_api_key.service_id, api_key_id=sample_api_key.id)
    all_api_keys = get_model_api_keys(service_id=sample_api_key.service_id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0].expiry_date <= datetime.utcnow()
    assert all_api_keys[0].secret == sample_api_key.secret
    assert all_api_keys[0].id == sample_api_key.id
    assert all_api_keys[0].service_id == sample_api_key.service_id

    all_history = sample_api_key.get_history_model().query.all()
    assert len(all_history) == 2
    assert all_history[0].id == sample_api_key.id
    assert all_history[1].id == sample_api_key.id
    sorted_all_history = sorted(all_history, key=lambda hist: hist.version)
    sorted_all_history[0].version = 1
    sorted_all_history[1].version = 2
Exemple #12
0
def revoke_api_key(service_id, api_key_id):
    try:
        service_api_key = get_model_api_keys(service_id=service_id,
                                             id=api_key_id)
    except DataError:
        return jsonify(result="error",
                       message="Invalid  api key for service"), 400
    except NoResultFound:
        return jsonify(result="error",
                       message="Api key not found for service"), 404

    save_model_api_key(service_api_key,
                       update_dict={
                           'id': service_api_key.id,
                           'expiry_date': datetime.utcnow()
                       })
    return jsonify(), 202
Exemple #13
0
def test_expire_api_key_should_update_the_api_key_and_create_history_record(
        notify_api, sample_api_key):
    expire_api_key(service_id=sample_api_key.service_id,
                   api_key_id=sample_api_key.id)
    all_api_keys = get_model_api_keys(service_id=sample_api_key.service_id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0].expiry_date <= datetime.utcnow()
    assert all_api_keys[0].secret == sample_api_key.secret
    assert all_api_keys[0].id == sample_api_key.id
    assert all_api_keys[0].service_id == sample_api_key.service_id

    all_history = sample_api_key.get_history_model().query.all()
    assert len(all_history) == 2
    assert all_history[0].id == sample_api_key.id
    assert all_history[1].id == sample_api_key.id
    sorted_all_history = sorted(all_history, key=lambda hist: hist.version)
    sorted_all_history[0].version = 1
    sorted_all_history[1].version = 2
Exemple #14
0
def test_save_api_key_should_create_new_api_key_and_history(sample_service):
    api_key = ApiKey(
        **{
            'service': sample_service,
            'name': sample_service.name,
            'created_by': sample_service.created_by,
            'key_type': KEY_TYPE_NORMAL
        })
    save_model_api_key(api_key)

    all_api_keys = get_model_api_keys(service_id=sample_service.id)
    assert len(all_api_keys) == 1
    assert all_api_keys[0] == api_key
    assert api_key.version == 1

    all_history = api_key.get_history_model().query.all()
    assert len(all_history) == 1
    assert all_history[0].id == api_key.id
    assert all_history[0].version == api_key.version
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(sample_service, fake_uuid):
    with pytest.raises(NoResultFound):
        get_model_api_keys(sample_service.id, id=fake_uuid)
def test_should_return_api_key_for_service(notify_api, notify_db, notify_db_session, sample_api_key):
    api_key = get_model_api_keys(service_id=sample_api_key.service_id, id=sample_api_key.id)
    assert api_key == sample_api_key
Exemple #17
0
def test_should_return_api_key_for_service(notify_api, notify_db,
                                           notify_db_session, sample_api_key):
    api_key = get_model_api_keys(service_id=sample_api_key.service_id,
                                 id=sample_api_key.id)
    assert api_key == sample_api_key
Exemple #18
0
def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(
        sample_service, fake_uuid):
    with pytest.raises(NoResultFound):
        get_model_api_keys(sample_service.id, id=fake_uuid)