Skip to content

Commit

Permalink
Add more tests for OONI Run v2
Browse files Browse the repository at this point in the history
* Reach 100% code coverage
  • Loading branch information
hellais committed Feb 28, 2024
1 parent a748463 commit c1266dc
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 53 deletions.
51 changes: 22 additions & 29 deletions api/fastapi/oonidataapi/routers/oonirun.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,37 +326,30 @@ def list_oonirun_descriptors(
account_id = get_account_id_or_none(authorization)

q = db.query(models.OONIRunLink)
try:
if only_latest:
subquery = (
db.query(
models.OONIRunLink.oonirun_link_id,
sqlalchemy.func.max(models.OONIRunLink.revision).label("revision"),
)
.group_by(models.OONIRunLink.oonirun_link_id)
.subquery("latest_link")
)
q = q.filter(
sqlalchemy.tuple_(
models.OONIRunLink.oonirun_link_id,
models.OONIRunLink.revision,
).in_(subquery)
)
if not include_expired:
q = q.filter(
models.OONIRunLink.expiration_date > datetime.now(timezone.utc)
)
if only_mine:
q = q.filter(models.OONIRunLink.creator_account_id == account_id)

if oonirun_link_id:
q = q.filter(
models.OONIRunLink.oonirun_link_id.in_(commasplit(oonirun_link_id))
if only_latest:
subquery = (
db.query(
models.OONIRunLink.oonirun_link_id,
sqlalchemy.func.max(models.OONIRunLink.revision).label("revision"),
)
.group_by(models.OONIRunLink.oonirun_link_id)
.subquery("latest_link")
)
q = q.filter(
sqlalchemy.tuple_(
models.OONIRunLink.oonirun_link_id,
models.OONIRunLink.revision,
).in_(subquery)
)
if not include_expired:
q = q.filter(models.OONIRunLink.expiration_date > datetime.now(timezone.utc))
if only_mine:
q = q.filter(models.OONIRunLink.creator_account_id == account_id)

except Exception as e:
log.debug(f"list_oonirun_descriptors: invalid parameter. {e}")
raise HTTPException(status_code=400, detail="Incorrect parameter used")
if oonirun_link_id:
q = q.filter(
models.OONIRunLink.oonirun_link_id.in_(commasplit(oonirun_link_id))
)

descriptors = []
for row in q.all():
Expand Down
108 changes: 84 additions & 24 deletions api/fastapi/oonidataapi/tests/test_oonirun.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,52 @@ def test_oonirun_validation(client, client_with_user_role, client_with_admin_rol
assert r.status_code == 422, "empty name_intl should be rejected"


def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_role):
def test_oonirun_not_found(client, client_with_user_role, client_with_admin_role):
z = deepcopy(SAMPLE_OONIRUN)
### Create descriptor as user
z["name"] = "integ-test name in English"
z["name_intl"]["it"] = "integ-test nome in italiano"
r = client_with_user_role.post("/api/v2/oonirun", json=z)
assert r.status_code == 200, r.json()
j = r.json()
assert str(j["oonirun_link_id"]).endswith("00")
oonirun_link_id = int(r.json()["oonirun_link_id"])

j["expiration_date"] = (
datetime.now(timezone.utc) + timedelta(minutes=-1)
).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
r = client_with_user_role.put(f"/api/v2/oonirun/{oonirun_link_id}", json=j)
assert r.status_code == 200, r.json()

not_existing_link_id = "1234676871672836187"
r = client_with_user_role.put(f"/api/v2/oonirun/{not_existing_link_id}", json=j)
assert r.status_code == 404, r.json()

r = client.get(f"/api/v2/oonirun/{not_existing_link_id}")
assert r.status_code == 404, r.json()

r = client_with_user_role.put(f"/api/v2/oonirun/{oonirun_link_id}", json=j)
assert r.status_code == 403, "expired link cannot be edited"


def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_role):
z = deepcopy(SAMPLE_OONIRUN)
### Create 2 descriptors as user
z["name"] = "integ-test name in English"
z["name_intl"]["it"] = "integ-test nome in italiano"
r = client_with_user_role.post("/api/v2/oonirun", json=z)
assert r.status_code == 200, r.json()
assert str(r.json()["oonirun_link_id"]).endswith("00")
ooni_run_link_id = int(r.json()["oonirun_link_id"])
oonirun_link_id = int(r.json()["oonirun_link_id"])

z["name"] = "second descriptor in English"
z["name_intl"]["it"] = "second integ-test nome in italiano"
r = client_with_user_role.post("/api/v2/oonirun", json=z)
assert r.status_code == 200, r.json()
assert str(r.json()["oonirun_link_id"]).endswith("00")
oonirun_link_id = int(r.json()["oonirun_link_id"])

r = client_with_user_role.get(f"/api/v2/oonirun/{ooni_run_link_id}")
r = client_with_user_role.get(f"/api/v2/oonirun/{oonirun_link_id}")
assert r.status_code == 200, r.json()

j = r.json()
Expand All @@ -99,7 +134,7 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_
assert j["revision"] == 1

## Fetch by revision
r = client_with_user_role.get(f"/api/v2/oonirun/{ooni_run_link_id}?revision=1")
r = client_with_user_role.get(f"/api/v2/oonirun/{oonirun_link_id}?revision=1")
assert r.status_code == 200, r.json()

j = r.json()
Expand Down Expand Up @@ -132,7 +167,7 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_

found = False
for d in j["descriptors"]:
if d["oonirun_link_id"] == ooni_run_link_id:
if d["oonirun_link_id"] == oonirun_link_id:
found = True
assert sorted(d.keys()) == sorted(EXPECTED_OONIRUN_LINK_PUBLIC_KEYS)
assert found == True
Expand All @@ -146,7 +181,7 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_

found = False
for d in j["descriptors"]:
if d["oonirun_link_id"] == ooni_run_link_id:
if d["oonirun_link_id"] == oonirun_link_id:
found = True
assert sorted(d.keys()) == sorted(EXPECTED_OONIRUN_LINK_PUBLIC_KEYS)
assert found == True
Expand All @@ -164,7 +199,7 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_

found = False
for d in j["descriptors"]:
if d["oonirun_link_id"] == ooni_run_link_id:
if d["oonirun_link_id"] == oonirun_link_id:
found = True
assert d["is_mine"] == False
assert d["is_expired"] == False
Expand All @@ -174,12 +209,12 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_

### "update" the oonirun by creating a new version, changing the inputs
z["nettests"][0]["inputs"].append("https://foo.net/")
r = client_with_user_role.put(f"/api/v2/oonirun/{ooni_run_link_id}", json=z)
r = client_with_user_role.put(f"/api/v2/oonirun/{oonirun_link_id}", json=z)
assert r.status_code == 200, r.json()
assert r.json()["oonirun_link_id"] == ooni_run_link_id
assert r.json()["oonirun_link_id"] == oonirun_link_id

## Fetch it back
r = client_with_user_role.get(f"/api/v2/oonirun/{ooni_run_link_id}")
r = client_with_user_role.get(f"/api/v2/oonirun/{oonirun_link_id}")
assert r.status_code == 200, r.json()

j = r.json()
Expand All @@ -188,14 +223,16 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_
assert j["revision"] > 1, r.json()

## List descriptors as admin and find we have 2 versions now
r = client_with_admin_role.get(f"/api/v2/oonirun/?ids={ooni_run_link_id}")
r = client_with_admin_role.get(
f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}"
)
assert r.status_code == 200, r.json()
descs = r.json()["descriptors"]
assert len(descs) == 2, r.json()

## List descriptors using more params
r = client_with_user_role.get(
f"/api/v2/oonirun/?ids={ooni_run_link_id}&only_mine=True"
f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}&only_mine=True"
)
assert r.status_code == 200, r.json()
descs = r.json()["descriptors"]
Expand All @@ -216,15 +253,15 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_
# We need to pause 1 second for the update time to be different
time.sleep(1)
z["description_intl"]["it"] = "integ-test *nuova* descrizione in italiano"
r = client_with_user_role.put(f"/api/v2/oonirun/{ooni_run_link_id}", json=z)
r = client_with_user_role.put(f"/api/v2/oonirun/{oonirun_link_id}", json=z)
assert r.status_code == 200, r.json()

## previous id and descriptor_creation_time, not changed
assert r.json()["oonirun_link_id"] == ooni_run_link_id
assert r.json()["oonirun_link_id"] == oonirun_link_id
# assert creation_time == r.json()["descriptor_creation_time"]

## Fetch latest and find descriptor_creation_time has not changed
r = client_with_user_role.get(f"/api/v2/oonirun/{ooni_run_link_id}")
r = client_with_user_role.get(f"/api/v2/oonirun/{oonirun_link_id}")
assert r.status_code == 200, r.json()

j = r.json()
Expand Down Expand Up @@ -253,28 +290,41 @@ def test_oonirun_full_workflow(client, client_with_user_role, client_with_admin_
edit_req["expiration_date"] = (
datetime.now(timezone.utc) + timedelta(minutes=-1)
).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
r = client_with_user_role.put(f"/api/v2/oonirun/{ooni_run_link_id}", json=edit_req)
r = client_with_user_role.put(f"/api/v2/oonirun/{oonirun_link_id}", json=edit_req)
j = r.json()
assert r.status_code == 200, r.json()
assert j["is_expired"] == True

## List descriptors after expiration
## List descriptors after expiration filtering by ID
r = client_with_user_role.get(
f"/api/v2/oonirun/?ids={ooni_run_link_id}&include_expired=True"
f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}&include_expired=True"
)
j = r.json()
assert r.status_code == 200, r.json()
descs = j["descriptors"]
assert len(descs) == 2, r.json()

## List descriptors
r = client_with_user_role.get(f"/api/v2/oonirun/?ids={ooni_run_link_id}")
## List descriptors after expiration NOT filtering by ID
r = client_with_user_role.get(f"/api/v2/oonirun/?&include_expired=True")
j = r.json()
assert r.status_code == 200, r.json()
descs = j["descriptors"]
assert len(descs) == 3, r.json()

## List descriptors filtered by ID
r = client_with_user_role.get(f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}")
assert r.status_code == 200, r.json()
descs = r.json()["descriptors"]
assert len(descs) == 0, r.json()

## "Fetch latest and find that it's archived
r = client_with_user_role.get(f"/api/v2/oonirun/{ooni_run_link_id}")
## List descriptors unfiltered by ID
r = client_with_user_role.get(f"/api/v2/oonirun/")
assert r.status_code == 200, r.json()
descs = r.json()["descriptors"]
assert len(descs) == 1, r.json()

## Fetch latest and find that it's archived
r = client_with_user_role.get(f"/api/v2/oonirun/{oonirun_link_id}")
assert r.status_code == 200, r.json()
assert r.json()["is_expired"] == True, r.json()

Expand Down Expand Up @@ -325,19 +375,29 @@ def test_oonirun_expiration(client, client_with_user_role):
assert r.json()["is_expired"] == True, r.json()

## List descriptors after expiration
r = client_with_user_role.get(f"/api/v2/oonirun/?ids={oonirun_link_id}")
r = client_with_user_role.get(f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}")
j = r.json()
assert r.status_code == 200, r.json()
descs = j["descriptors"]
assert len(descs) == 0, r.json()

## List descriptors after expiration
r = client_with_user_role.get(
f"/api/v2/oonirun/?ids={oonirun_link_id}&include_expired=True"
f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}&include_expired=True"
)
j = r.json()
assert r.status_code == 200, r.json()
descs = j["descriptors"]
assert len(descs) == 2, r.json()
for d in descs:
assert d["is_expired"] == True, "is_expired should be True"

r = client_with_user_role.get(
f"/api/v2/oonirun/?oonirun_link_id={oonirun_link_id}&include_expired=True&only_latest=True"
)
j = r.json()
assert r.status_code == 200, r.json()
descs = j["descriptors"]
assert len(descs) == 1, r.json()
for d in descs:
assert d["is_expired"] == True, "is_expired should be True"

0 comments on commit c1266dc

Please sign in to comment.