import json import logging from logging import getLogger import pytest from sqlalchemy import create_engine from sqlalchemy_utils import drop_database LOG = getLogger(__name__) @pytest.fixture(scope='session') def app(): """ Create a Flask app context for the tests. """ from delphi_api.core.app import get_app return get_app() @pytest.fixture(scope='session') def database(app, request): """ Create Postgres database and seed with some test data """ from delphi_api.const import SQLALCHEMY_DATABASE_URI engine = create_engine(SQLALCHEMY_DATABASE_URI) @request.addfinalizer def drop(): drop_database(engine.url) from delphi_api.v3.data_models.postgres_db import db db.init_app(app.app) return db @pytest.fixture(scope='session') def _db(database): """ Provide the transactional fixtures with access to the database via a Flask-SQLAlchemy database connection. """ return database @pytest.fixture(scope='session', autouse=True) def disable_logging(): logging.disable(logging.ERROR) MOCK_ISRCS = [ 'AAzzz0123456', 'BBzzz0123456', 'CCzzz0123456', 'DDzzz0123456', 'EEzzz0123456', 'FFzzz0123456', ] MOCK_PLAYLIST_IDS = [ 'PL-01', 'PL-02', 'PL-03', 'PL-04', 'PL-05', 'PL-06', ] MOCK_RANGE_KEYS = [ '2019-01-01_us', '2019-01-02_us', '2019-01-03_us', '2019-01-04_us', '2019-01-05_us', '2019-01-06_us', ] MOCK_APPLE_CONTAINER_IDS = [ '1111111', '2222222', '3333333', '4444444', '5555555', '6666666', ] MOCK_DATE_RANGE = [ '2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05', '2019-01-06', ] MOCK_DSP_SLUGS = [ 'alpha', 'beta', 'gamma', ] # --- These should match what is in the test data seeds --- # TEST_ARTIST_IDS = [ 'GRAS_7513', ] TEST_DSPS = [ 'apple', 'spotify', ] TEST_TRACK_IDS = [ 'GRAS_A10328E00092855030', 'GRAS_A10328E0001321390L', ] TEST_ISRCS = [ 'USSM11914699', 'USSM10010774', ] TEST_PLAYLIST_IDS = [ 'apple_pl.0001620d82ce4ebb9570ceb57123af1d', 'spotify_02C8uKtc6CoBpD0IdqOZdT', ] TEST_PRODUCT_IDS = [ 'GRAS_G0100042483587', 'GRAS_G010000912970T', ] TEST_CHART_IDS = [ 'apple_top100_daily_us', 'spotify_top200_daily_us', ] # --------------------------------------------------------- # @pytest.fixture def mock_hash_keys_list(): return MOCK_ISRCS @pytest.fixture def mock_hash_keys_set(): return frozenset(MOCK_ISRCS) @pytest.fixture def mock_range_keys_list(): return MOCK_RANGE_KEYS @pytest.fixture def mock_range_keys_set(): return frozenset(MOCK_RANGE_KEYS) @pytest.fixture def mock_dsp_slugs_set(): return frozenset(MOCK_DSP_SLUGS) @pytest.fixture def mock_item_keys(): out = [] for range_key in MOCK_RANGE_KEYS: for hash_key in MOCK_ISRCS: out.append((hash_key, range_key)) return out @pytest.fixture def mock_item_keys_level_3(): out = [] for range_key in MOCK_RANGE_KEYS: for hash_key in MOCK_ISRCS: for other_key in MOCK_DSP_SLUGS: out.append((hash_key, range_key, other_key)) return out class FakeResponse(object): def __init__(self, status_code, text): """ :type status_code: int :type text: ste """ self.status_code = status_code self.text = text self.ok = status_code == 200 def json(self): return json.loads(self.text) @pytest.fixture(scope='class') def oauth_requests(request, monkeypatch): def fake_get(url, params=None, headers=None, timeout=None): headers = headers or {} token = headers.get('Authorization', 'invalid').split()[-1] if token in ["100", "has_myscope"]: return FakeResponse(200, '{"uid": "test-user", "scope": ["myscope"]}') if token in ["200", "has_wrongscope"]: return FakeResponse(200, '{"uid": "test-user", "scope": ["wrongscope"]}') if token == "has_myscope_otherscope": return FakeResponse(200, '{"uid": "test-user", "scope": ["myscope", "otherscope"]}') if token in ["300", "is_not_invalid"]: return FakeResponse(404, '') if token == "has_scopes_in_scopes_with_s": return FakeResponse(200, '{"uid": "test-user", "scopes": ["myscope", "otherscope"]}') return url monkeypatch.setattr('connexion.decorators.security.session.get', fake_get)