def test_create_update_delete_api_key(client: TestClient, superuser_token_headers): api_key = ApiKeyCreate( name="Test Key", all_farms=True, farm_id=[0,1,2], notes="Some notes", enabled=True ) r = client.post( f"{settings.API_V1_STR}/api-keys/", headers=superuser_token_headers, data=api_key.json() ) assert 200 <= r.status_code < 300 key = r.json() assert key assert "id" in key assert "key" in key assert key["enabled"] == api_key.enabled assert key["farm_id"] == api_key.farm_id assert key["all_farms"] == api_key.all_farms assert key["name"] == api_key.name assert key["notes"] == api_key.notes # Update the key key_update = ApiKeyUpdate( name="My New Name", enabled=False, notes="Updated notes", all_farms=False, farm_id=[] ) r = client.put( f"{settings.API_V1_STR}/api-keys/{key['id']}", headers=superuser_token_headers, data=key_update.json() ) assert 200 <= r.status_code < 300 updated_key = r.json() assert updated_key assert "key" in updated_key assert updated_key["enabled"] == key_update.enabled assert updated_key["name"] == key_update.name assert updated_key["notes"] == key_update.notes # Check that these values did not change. assert updated_key["farm_id"] == api_key.farm_id assert updated_key["all_farms"] == api_key.all_farms # Delete API Key. r = client.delete( f"{settings.API_V1_STR}/api-keys/{key['id']}", headers=superuser_token_headers, ) assert 200 <= r.status_code < 300
def get_api_key_headers(client: TestClient, api_key_params: ApiKeyCreate): r = client.post(f"{settings.API_V1_STR}/api-keys/", headers=get_superuser_token_headers(client=client), data=api_key_params.json()) api_key = r.json() key = api_key["key"] headers = {"api-key": f"{key}"} return headers
def get_api_key_headers(api_key_params: ApiKeyCreate): server_api = get_server_api() r = requests.post(f"{server_api}{settings.API_V1_STR}/api-keys", headers=get_superuser_token_headers(), data=api_key_params.json()) api_key = r.json() key = api_key["key"] headers = {"api-key": f"{key}"} return headers
def create(db_session: Session, api_key_in: ApiKeyCreate): # Generate the JWT token that will be saved as the 'key'. key = create_api_key(farm_id=api_key_in.farm_id, all_farms=api_key_in.all_farms, scopes=api_key_in.scopes) db_item = ApiKey(key=key, **api_key_in.dict()) db_session.add(db_item) db_session.commit() logger.debug("Created API Key: " + key.decode()) db_session.refresh(db_item) return db_item
def test_read_farms_no_scope_api_key(client: TestClient, test_farm): test_api_key = ApiKeyCreate(name="Test Key", enabled=True, all_farms=True, scopes=[]) farm_id = test_farm.id r = client.get( f"{settings.API_V1_STR}/farms/{farm_id}", headers=get_api_key_headers(client=client, api_key_params=test_api_key), ) assert r.status_code == 401
def test_read_farms_no_scope_api_key(test_farm): test_api_key = ApiKeyCreate(name="Test Key", enabled=True, all_farms=True, scopes=[]) server_api = get_server_api() farm_id = test_farm.id r = requests.get( f"{server_api}{settings.API_V1_STR}/farms/{farm_id}", headers=get_api_key_headers(test_api_key), ) assert r.status_code == 401
def test_read_farms_one_farm_id_api_key(client: TestClient, test_farm): test_api_key = ApiKeyCreate(name="Test Key", enabled=True, farm_id=[test_farm.id], scopes=["farm:read"]) farm_id = test_farm.id r = client.get( f"{settings.API_V1_STR}/farms/{farm_id}", headers=get_api_key_headers(client=client, api_key_params=test_api_key), ) assert 200 <= r.status_code < 300 response = r.json() farm = crud.farm.get_by_id(db_session, farm_id=response['id']) assert farm.farm_name == response["farm_name"]
def test_read_farms_all_farms_api_key(test_farm): test_api_key = ApiKeyCreate(name="Test Key", enabled=True, all_farms=True, scopes=["farm:read"]) server_api = get_server_api() farm_id = test_farm.id r = requests.get( f"{server_api}{settings.API_V1_STR}/farms/{farm_id}", headers=get_api_key_headers(test_api_key), ) assert 200 <= r.status_code < 300 response = r.json() farm = crud.farm.get_by_id(db_session, farm_id=response['id']) assert farm.farm_name == response["farm_name"]