Skip to content

Commit

Permalink
Fix support for multiple interactions in init_test_data
Browse files Browse the repository at this point in the history
Add config.archive-url.json to test data
  • Loading branch information
TylerZeroMaster committed Oct 30, 2024
1 parent 89d93fc commit 658888e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 17 deletions.
1 change: 1 addition & 0 deletions backend/app/tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def pytest_configure(config):
)
init_test_data = config.getoption("--init-test-data")
if init_test_data:
os.environ.setdefault("VCR_RECORD", "1")
token = config.getoption("--github-token")
if token is not None:
from tests.unit.init_test_data import main
Expand Down
18 changes: 18 additions & 0 deletions backend/app/tests/unit/data/get_file_content.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interactions:
- request:
body: ''
headers:
host:
- api.github.com
method: GET
uri: https://api.github.com/repos/openstax/rex-web/contents/src/config.archive-url.json
response:
content: '{"name":"config.archive-url.json","path":"src/config.archive-url.json","sha":"ddd2befbcff5578dda01dda94cd6c2474999d876","size":95,"url":"https://api.github.com/repos/openstax/rex-web/contents/src/config.archive-url.json?ref=main","html_url":"https://github.com/openstax/rex-web/blob/main/src/config.archive-url.json","git_url":"https://api.github.com/repos/openstax/rex-web/git/blobs/ddd2befbcff5578dda01dda94cd6c2474999d876","download_url":"https://raw.githubusercontent.com/openstax/rex-web/main/src/config.archive-url.json","type":"file","content":"ewogICJSRUFDVF9BUFBfQVJDSElWRSI6ICIyMDI0MDkxMC4xNjEyMjciLAog\nICJSRUFDVF9BUFBfQVJDSElWRV9VUkxfQkFTRSI6ICIvYXBwcy9hcmNoaXZl\nLyIKfQo=\n","encoding":"base64","_links":{"self":"https://api.github.com/repos/openstax/rex-web/contents/src/config.archive-url.json?ref=main","git":"https://api.github.com/repos/openstax/rex-web/git/blobs/ddd2befbcff5578dda01dda94cd6c2474999d876","html":"https://github.com/openstax/rex-web/blob/main/src/config.archive-url.json"}}'
headers:
Content-Type:
- application/json; charset=utf-8
Server:
- github.com
http_version: HTTP/1.1
status_code: 200
version: 1
64 changes: 47 additions & 17 deletions backend/app/tests/unit/init_test_data.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
import asyncio
import os
import shutil
import sys
from pathlib import Path
from typing import cast

import vcr
from httpx import AsyncClient
from vcr.record_mode import RecordMode

import app.core.config as config
from app.github import (
AuthenticatedClient,
get_book_repository,
get_collections,
get_file_content,
get_user,
get_user_repositories,
get_user_teams,
)

vcr_args = {
"record_mode": RecordMode.NEW_EPISODES
if os.getenv("VCR_RECORD") == "1"
else RecordMode.NONE
}


def apply_key_whitelist(d, whitelist):
whitelist = [k.lower() for k in whitelist]
Expand All @@ -31,13 +41,14 @@ def __init__(self, vcr) -> None:
self.vcr = vcr

def transform(self, d):
request_headers = d["interactions"][0]["request"]["headers"]
response_headers = d["interactions"][0]["response"]["headers"]
for headers, whitelist in (
(request_headers, ("host",)),
(response_headers, ("content-type", "server")),
):
apply_key_whitelist(headers, whitelist)
for interaction in d["interactions"]:
request_headers = interaction["request"]["headers"]
response_headers = interaction["response"]["headers"]
for headers, whitelist in (
(request_headers, ("host",)),
(response_headers, ("content-type", "server")),
):
apply_key_whitelist(headers, whitelist)

def serialize(self, d, *args, **kwargs):
self.transform(d)
Expand All @@ -58,11 +69,12 @@ def transform(self, d):
import json

super().transform(d)
body = json.loads(d["interactions"][0]["response"]["content"])
# Keep exactly what is used by the backend, delete extra data
apply_key_whitelist(body, ("login", "avatar_url", "id"))
# HACK: Set the content to save from the response
d["interactions"][0]["response"]["content"] = json.dumps(body)
for interaction in d["interactions"]:
body = json.loads(interaction["response"]["content"])
# Keep exactly what is used by the backend, delete extra data
apply_key_whitelist(body, ("login", "avatar_url", "id"))
# HACK: Set the content to save from the response
interaction["response"]["content"] = json.dumps(body)


DATA_DIR = str(Path(__file__).parent / "data")
Expand All @@ -73,12 +85,14 @@ def transform(self, d):
my_vcr.register_serializer("user_sanitizer", UserSanitizer(my_vcr))


@my_vcr.use_cassette("get_user.yaml", serializer="user_sanitizer")
@my_vcr.use_cassette("get_user.yaml", serializer="user_sanitizer", **vcr_args)
async def mock_get_user(client, access_token):
return await get_user(client, access_token)


@my_vcr.use_cassette("get_user_teams.yaml", serializer="base_sanitizer")
@my_vcr.use_cassette(
"get_user_teams.yaml", serializer="base_sanitizer", **vcr_args
)
async def mock_get_user_teams(client, user):
import app.github.api

Expand All @@ -88,21 +102,34 @@ async def mock_get_user_teams(client, user):
return teams


@my_vcr.use_cassette("get_user_repositories.yaml", serializer="base_sanitizer")
@my_vcr.use_cassette(
"get_user_repositories.yaml", serializer="base_sanitizer", **vcr_args
)
async def mock_get_user_repositories(client, query):
return await get_user_repositories(client, query)


@my_vcr.use_cassette("get_book_repository.yaml", serializer="base_sanitizer")
@my_vcr.use_cassette(
"get_book_repository.yaml", serializer="base_sanitizer", **vcr_args
)
async def mock_get_book_repository(client, repo_name, repo_owner, version):
return await get_book_repository(client, repo_name, repo_owner, version)


@my_vcr.use_cassette("get_collections.yaml", serializer="base_sanitizer")
@my_vcr.use_cassette(
"get_collections.yaml", serializer="base_sanitizer", **vcr_args
)
async def mock_get_collections(client, repo_name, repo_owner, commit_sha):
return await get_collections(client, repo_name, repo_owner, commit_sha)


@my_vcr.use_cassette(
"get_file_content.yaml", serializer="base_sanitizer", **vcr_args
)
async def mock_get_file_content(client, owner, repo, path, ref=None):
return await get_file_content(client, owner, repo, path, ref)


async def async_main(access_token: str):
async with AsyncClient() as client:
client.headers = {"authorization": f"Bearer {access_token}"}
Expand All @@ -114,6 +141,9 @@ async def async_main(access_token: str):
)
await mock_get_book_repository(client, "tiny-book", "openstax", "main")
await mock_get_collections(client, "tiny-book", "openstax", "main")
await mock_get_file_content(
client, *config.REX_WEB_ARCHIVE_CONFIG.split(":", 2)
)


def main(access_token):
Expand Down

0 comments on commit 658888e

Please sign in to comment.