Skip to content
Merged
5 changes: 4 additions & 1 deletion backend/btrixcloud/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,10 @@ async def do_commit_to_profile(
existing_profile.resource.size if existing_profile.resource else 0
)

origins = existing_profile.origins
# only set origins from existing profile if browser
# actually launched with that profile (eg. not a reset)
if metadata.baseprofile == profileid:
origins = existing_profile.origins

else:
profileid = metadata.profileid
Expand Down
59 changes: 30 additions & 29 deletions backend/test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,33 +639,30 @@ def url_list_config_id(crawler_auth_headers, default_org_id):

@pytest.fixture(scope="session")
def profile_browser_id(admin_auth_headers, default_org_id):
return _create_profile_browser(admin_auth_headers, default_org_id)
return create_profile_browser(admin_auth_headers, default_org_id)


@pytest.fixture(scope="session")
def profile_browser_2_id(admin_auth_headers, default_org_id):
return _create_profile_browser(
return create_profile_browser(
admin_auth_headers, default_org_id, "https://specs.webrecorder.net"
)


@pytest.fixture(scope="session")
def profile_browser_3_id(admin_auth_headers, default_org_id):
return _create_profile_browser(admin_auth_headers, default_org_id)


@pytest.fixture(scope="session")
def profile_browser_4_id(admin_auth_headers, default_org_id):
return _create_profile_browser(admin_auth_headers, default_org_id)


def _create_profile_browser(
headers: Dict[str, str], oid: UUID, url: str = "https://old.webrecorder.net"
def create_profile_browser(
headers: Dict[str, str],
oid: UUID,
url="https://old.webrecorder.net",
baseprofile="",
):
data = {"url": url}
if baseprofile:
data["profileId"] = baseprofile

r = requests.post(
f"{API_PREFIX}/orgs/{oid}/profiles/browser",
headers=headers,
json={"url": url},
json=data,
)
assert r.status_code == 200
browser_id = r.json()["browserid"]
Expand Down Expand Up @@ -711,10 +708,7 @@ def echo_server():


def prepare_browser_for_profile_commit(
browser_id: str,
headers: Dict[str, str],
oid: UUID,
url="https://old.webrecorder.net/tools",
browser_id: str, headers: Dict[str, str], oid: UUID, url=None
) -> None:
# Ping to make sure it doesn't expire
r = requests.post(
Expand All @@ -740,14 +734,15 @@ def prepare_browser_for_profile_commit(
assert data["scale"]
assert data["oid"] == oid

# Navigate to new URL
r = requests.post(
f"{API_PREFIX}/orgs/{oid}/profiles/browser/{browser_id}/navigate",
headers=headers,
json={"url": url},
)
assert r.status_code == 200
assert r.json()["success"]
# Navigate to new URL, if provided
if url:
r = requests.post(
f"{API_PREFIX}/orgs/{oid}/profiles/browser/{browser_id}/navigate",
headers=headers,
json={"url": url},
)
assert r.status_code == 200
assert r.json()["success"]

# Ping browser until ready
max_attempts = 20
Expand All @@ -770,7 +765,10 @@ def prepare_browser_for_profile_commit(
@pytest.fixture(scope="session")
def profile_id(admin_auth_headers, default_org_id, profile_browser_id):
prepare_browser_for_profile_commit(
profile_browser_id, admin_auth_headers, default_org_id
profile_browser_id,
admin_auth_headers,
default_org_id,
url="https://old.webrecorder.net/tools",
)

# Create profile
Expand Down Expand Up @@ -862,7 +860,10 @@ def profile_config_id(admin_auth_headers, default_org_id, profile_id):
@pytest.fixture(scope="session")
def profile_2_id(admin_auth_headers, default_org_id, profile_browser_2_id):
prepare_browser_for_profile_commit(
profile_browser_2_id, admin_auth_headers, default_org_id
profile_browser_2_id,
admin_auth_headers,
default_org_id,
url="https://old.webrecorder.net/tools",
)

# Create profile
Expand Down
77 changes: 66 additions & 11 deletions backend/test/test_profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
PROFILE_2_TAGS,
PROFILE_TAGS_UPDATED,
prepare_browser_for_profile_commit,
create_profile_browser,
)


Expand Down Expand Up @@ -253,7 +254,7 @@ def test_update_profile_metadata(crawler_auth_headers, default_org_id, profile_i


def test_commit_browser_to_existing_profile(
admin_auth_headers, default_org_id, profile_browser_3_id, profile_id
admin_auth_headers, default_org_id, profile_id
):
# Get original modified time
r = requests.get(
Expand All @@ -265,11 +266,20 @@ def test_commit_browser_to_existing_profile(
original_created = data["created"]
original_modified = data["modified"]

url = "https://example-com.webrecorder.net/"

# create browser with existing profile
browser_id = create_profile_browser(
admin_auth_headers,
default_org_id,
url=url,
baseprofile=profile_id,
)

prepare_browser_for_profile_commit(
profile_browser_3_id,
browser_id,
admin_auth_headers,
default_org_id,
url="https://example-com.webrecorder.net",
)

time.sleep(10)
Expand All @@ -280,7 +290,7 @@ def test_commit_browser_to_existing_profile(
f"{API_PREFIX}/orgs/{default_org_id}/profiles/{profile_id}",
headers=admin_auth_headers,
json={
"browserid": profile_browser_3_id,
"browserid": browser_id,
"name": PROFILE_NAME_UPDATED,
"description": PROFILE_DESC_UPDATED,
"tags": PROFILE_TAGS_UPDATED,
Expand Down Expand Up @@ -315,6 +325,53 @@ def test_commit_browser_to_existing_profile(
]


def test_commit_reset_browser_to_existing_profile(
admin_auth_headers, default_org_id, profile_id
):
url = "https://example-com.webrecorder.net/"

# create new browser w/o existing profile to reset
browser_id = create_profile_browser(admin_auth_headers, default_org_id, url=url)

prepare_browser_for_profile_commit(
browser_id, admin_auth_headers, default_org_id, url=url
)

time.sleep(10)

# Commit new browser to existing profile
while True:
r = requests.patch(
f"{API_PREFIX}/orgs/{default_org_id}/profiles/{profile_id}",
headers=admin_auth_headers,
json={
"browserid": browser_id,
"name": PROFILE_NAME_UPDATED,
"description": PROFILE_DESC_UPDATED,
"tags": PROFILE_TAGS_UPDATED,
},
)
assert r.status_code == 200
if r.json().get("detail") == "waiting_for_browser":
time.sleep(5)
continue

break

assert r.json()["updated"]

r = requests.get(
f"{API_PREFIX}/orgs/{default_org_id}/profiles/{profile_id}",
headers=admin_auth_headers,
)
assert r.status_code == 200
data = r.json()

assert data.get("origins") == [
"https://example-com.webrecorder.net",
]


@pytest.mark.parametrize(
"sort_by,sort_direction,profile_1_index,profile_2_index",
[
Expand Down Expand Up @@ -419,9 +476,7 @@ def test_delete_profile(admin_auth_headers, default_org_id, profile_2_id):
assert r.json()["detail"] == "profile_not_found"


def test_create_profile_read_only_org(
admin_auth_headers, default_org_id, profile_browser_4_id
):
def test_create_profile_read_only_org(admin_auth_headers, default_org_id):
# Set org to read-only
r = requests.post(
f"{API_PREFIX}/orgs/{default_org_id}/read-only",
Expand All @@ -430,9 +485,9 @@ def test_create_profile_read_only_org(
)
assert r.json()["updated"]

prepare_browser_for_profile_commit(
profile_browser_4_id, admin_auth_headers, default_org_id
)
browser_id = create_profile_browser(admin_auth_headers, default_org_id)

prepare_browser_for_profile_commit(browser_id, admin_auth_headers, default_org_id)

# Try to create profile, verify we get 403 forbidden
start_time = time.monotonic()
Expand All @@ -443,7 +498,7 @@ def test_create_profile_read_only_org(
f"{API_PREFIX}/orgs/{default_org_id}/profiles",
headers=admin_auth_headers,
json={
"browserid": profile_browser_4_id,
"browserid": browser_id,
"name": "uncreatable",
"description": "because org is read-only",
},
Expand Down
Loading
Loading