Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

📼 VCR.py #46

Merged
merged 3 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ site/

# Ignore jupyter notebook session
session.ipynb

# Common location for sony ci credentials
ci.toml
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/gitguardian/ggshield
rev: v1.15.0
hooks:
- id: ggshield
language_version: python3
stages: [commit]
7 changes: 5 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"python.testing.pytestArgs": ["tests"],
"python.testing.pytestArgs": [
"tests",
"--vcr-record=none"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
}
7 changes: 1 addition & 6 deletions chowda/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

from enum import Enum


MediaUrl = stricturl(allowed_schemes=['video', 'audio', 'text'], tld_required=False)
"""Media url validator. Must have prefix of video, audio, or text. No TLD required.
Example:
Expand Down Expand Up @@ -76,12 +77,6 @@ class MediaFile(SQLModel, table=True):
)
clams_events: List['ClamsEvent'] = Relationship(back_populates='media_file')

async def __admin_repr__(self, request: Request):
return self.guid

async def __admin_select2_repr__(self, request: Request) -> str:
return f'<span><strong>{self.guid}</strong></span>'


mrharpo marked this conversation as resolved.
Show resolved Hide resolved
class Collection(SQLModel, table=True):
__tablename__ = 'collections'
Expand Down
398 changes: 381 additions & 17 deletions pdm.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ test = [
"pytest-xdist~=3.3",
"httpx~=0.24",
'nbmake~=1.4',
"vcrpy~=4.2",
"pytest-vcr~=1.0",
'urllib3~=1.26',
]
locust = [
"locust~=2.15",
"pydantic-factories~=1.17",
]


[tool.pdm.dev-dependencies]
dev = [
"uvicorn~=0.22",
"black~=23.3",
"ruff~=0.0",
]
dev = ['uvicorn~=0.21', 'black~=23.3', 'ruff~=0.0', 'pre-commit~=2.21']

docs = [
"mkdocs~=1.4",
"mkdocs-material~=9.1",
Expand Down
8 changes: 8 additions & 0 deletions tests/ci.test.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[sonyci]
username = 'aapb_notifications@wgbh.org'
password = 'DUMMY'
client_id = 'DUMMY'
client_secret = 'DUMMY'

# My Workspace - sandbox workspace used for testing for this account.
workspace_id = 'f44132c2393d470c88b9884f45877ebb'
46 changes: 45 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,47 @@
from os import environ
from json import dumps, loads
from json.decoder import JSONDecodeError
from os import environ, path
from pytest import fixture

# Set ENVIRONMENT env var to 'test' always. This serves as a flag for anywhere else in
# the application where we need to detect whether we are running tests or not.
environ['ENVIRONMENT'] = 'test'

# Set CI_CONFIG to use ./test/ci.test.toml *only* if it's not already set. We need to be
# able to set the CI_CONFIG to point to a real SonyCi account and workspace when we are
# recording our VCR cassette fixtures for testing.
if not environ.get('CI_CONFIG'):
environ['CI_CONFIG'] = './tests/ci.test.toml'


def clean_response(response: dict):
"""Replace secrets in response body with dummy values."""
try:
body = loads(response['body']['string'].decode())
except JSONDecodeError:
return response
if 'access_token' in body:
body['access_token'] = 'DUMMY_ACCESS_TOKEN'
if 'refresh_token' in body:
body['refresh_token'] = 'DUMMY_REFRESH_TOKEN'
response['body']['string'] = dumps(body).encode()
return response


@fixture(scope='module')
def vcr_config(request):
return {
# Save cassettes in tests/cassettes/<module_name>/<test_name>.yaml
'cassette_library_dir': path.join(
path.dirname(path.abspath(__file__)), 'cassettes', request.module.__name__
),
# Replace the Authorization request header with "DUMMY" in cassettes
'filter_headers': [('authorization', 'Bearer DUMMY')],
# Replace secrets in request body
'filter_post_data_parameters': [
('client_id', 'FAKE_CLIENT_ID'),
('client_secret', 'FAKE_CLIENT_SECRET'),
],
# Replace secrets in response body
'before_record_response': clean_response,
}