Skip to content

Commit

Permalink
Fix API intercept
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanceM committed Sep 6, 2023
1 parent f380c7e commit aa6cfba
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
30 changes: 29 additions & 1 deletion test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from _pytest.fixtures import SubRequest
from pytest_base_url.plugin import base_url
from playwright.sync_api import Playwright, APIRequestContext, Route, Page, Browser
import json

@fixture
def get_test_dir() -> Path:
Expand Down Expand Up @@ -151,4 +152,31 @@ def expect_nav_items() -> List[Dict[str,str]]:
# {'title': 'Archive', 'href': '/archive'},
# failing on tests here: https://github.com/JupiterBroadcasting/jupiterbroadcasting.com/runs/8254156209?check_suite_focus=true#step:9:26
{'title': 'Contact', 'href': '/contact/'},
]
]

@fixture
def set_live() -> Callable:
return _replace_live_event

@staticmethod
def _replace_live_event(page: Page) -> None:
def handle_route(route: Route) -> None:
# fetch original response
response = page.request.fetch(route.request)
json_data = response.json()

# override response values
if json_data.get('data'):
json_data['data'][0]['isLive'] = True
json_data['total'] = 1

# setting live event
route.fulfill(
# Pass all fields from the response
response=response,
body=json.dumps(json_data),
)
page.route(
"https://jupiter.tube/api/v1/video-channels/live/**",
handle_route
)
18 changes: 7 additions & 11 deletions test/e2e/test_live.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@
from typing import Tuple, Callable
from playwright.sync_api import Page, expect, Locator, FrameLocator, Route

def handle_peertube_response(route: Route):
response = route.fulfill()
json = response.json()
json.data[0].isLive = True
# Fulfill using the original response, while patching the response body
route.fulfill(response=response, json=json)

def test_live_indicator(
page: Page,
set_live: Callable,
screenshot_dir: Path,
):
# intercepting reponses for live event, and make live
set_live(page)

# go to the live page
page.goto("/live")

page.route("https://jupiter.tube/api/v1/video-channels/live/videos?isLive=true&skipCount=false&count=1&sort=-createdAt", handle_peertube_response)

# validate live button is red
expect(page.locator("#mainnavigation.is-live").locator("#livebutton"),).to_have_css(
name="background-color",
Expand All @@ -37,6 +31,7 @@ def test_live_indicator(

def test_mobile_live_indicator(
mobile_device_tuple: Tuple[Page, str],
set_live: Callable,
screenshot_dir: Path,
):
# set mobile page to variable
Expand All @@ -46,9 +41,10 @@ def test_mobile_live_indicator(
screenshot_dir / f"mobile/{mobile_device_tuple[1].replace(' ','-')}/"
)

mobile_device.goto("/live")
# intercepting reponses for live event, and make live
set_live(mobile_device)

mobile_device.route("https://jupiter.tube/api/v1/video-channels/live/videos?isLive=true&skipCount=false&count=1&sort=-createdAt", handle_peertube_response)
mobile_device.goto("/live")

navbar: Locator = mobile_device.locator("#mainnavigation.is-live",).locator(
".navbar-burger",
Expand Down

0 comments on commit aa6cfba

Please sign in to comment.