Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

import html
import re
import urllib.parse

import pytest
import requests

from live_cloud import find_resource_key, no_resource_key, read_environment

Expand Down Expand Up @@ -69,6 +71,28 @@ def credentials(keys):
return keys


def key_carries(api, resource_key, name):
"""
Whether the resource key carries the property, from the cloud's own
list of the properties the key carries. A test asserts on a value only
when the key carries its property, because the cloud leaves out a
property the key does not carry.
"""

answer = requests.request(
"GET",
api + "accessibleproperties?resource=" +
urllib.parse.quote(resource_key, safe=""),
timeout=30)
if answer.status_code != 200:
raise ValueError(f"HTTP {answer.status_code}: {answer.text[:200]}")
products = answer.json().get("Products") or {}
return any(
str(item.get("Name", "")).lower() == name.lower()
for product in products.values()
for item in (product or {}).get("Properties") or [])


def cell_after(page, label):
"""
The text a browser shows in the table cell after the one holding label,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
the example asks the cloud service about before anything else, and these
tests skip themselves when no key is set or the key does not carry it.

HumanConfidenceSource is a property of its own, which a key can carry or
not. The example gives no source for a key without it, so the passive test
expects Passive only when the key carries it.

Only the passive step is run here. The active step needs the browser tests,
which only a browser runs.
"""
Expand All @@ -36,6 +40,7 @@
app as example)
from fiftyone_pipeline_examples.example_utils import ExampleUtils

from helpers import key_carries
from live_cloud import RESOURCE_KEY_NAMES, find_resource_key

DESKTOP_CHROME = (
Expand All @@ -44,7 +49,9 @@

HEADERS = {"User-Agent": DESKTOP_CHROME}

LEVELS = ("High", "Medium", "Low")
# Every value the property has. Unknown is the one the cloud gives when it
# has too little to rate the request.
LEVELS = ("High", "Medium", "Low", "Unknown")


@pytest.fixture(scope="module")
Expand Down Expand Up @@ -80,11 +87,14 @@ def test_page_is_served_and_never_kept(client, key):
assert key in response.get_data(as_text=True)


def test_passive_level_comes_from_the_cloud(client):
def test_passive_level_comes_from_the_cloud(client, key, api):
answer = client.get("/passive", headers=HEADERS).get_json()
assert "error" not in answer, answer
assert answer["source"] == "Passive"
assert answer["humanConfidence"] in LEVELS
if key_carries(api, key, "HumanConfidenceSource"):
assert answer["source"] == "Passive"
else:
assert answer["source"] is None


def test_licence_key_never_reaches_the_browser(client):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
app as example)
from fiftyone_pipeline_examples.example_utils import ExampleUtils

from helpers import key_carries

API = "https://cloud.example/api/v4/"
RESOURCE_KEY = "a-resource-key"
LICENCE_KEY = "a-licence-key"
Expand Down Expand Up @@ -259,6 +261,33 @@ def test_passive_without_the_properties_returns_neither(cloud):
"source": None, "humanConfidence": None}


# What the live tests ask of the key before they assert on a value.

def test_key_carries_a_property_the_cloud_lists_for_it(cloud):
cloud(properties=("HumanConfidence",))

assert key_carries(API, RESOURCE_KEY, "HumanConfidence")
assert key_carries(API, RESOURCE_KEY, "humanconfidence")


def test_key_does_not_carry_a_property_the_cloud_leaves_out(cloud):
stub = cloud(properties=("HumanConfidence",))

assert not key_carries(API, RESOURCE_KEY, "HumanConfidenceSource")
method, url, _ = stub.requests[0]
assert method == "GET"
assert url == API + "accessibleproperties?resource=" + RESOURCE_KEY


def test_key_carries_stops_when_the_cloud_refuses_the_key(cloud):
stub = cloud()
stub.instead["accessibleproperties?resource=" + RESOURCE_KEY] = \
StubResponse('{"errors":["The resource key is not valid."]}', 400)

with pytest.raises(ValueError, match="The resource key is not valid."):
key_carries(API, RESOURCE_KEY, "HumanConfidence")


def test_passive_says_what_the_cloud_refused(cloud):
stub = cloud()
stub.instead[".json"] = StubResponse(
Expand Down
Loading