Skip to content

Commit

Permalink
Add test coverage around searching -
Browse files Browse the repository at this point in the history
  • Loading branch information
pattisdr committed Jul 25, 2024
1 parent a8a16d3 commit 96ec9ac
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/fixtures/application_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1752,6 +1752,36 @@ def privacy_request_with_custom_fields(db: Session, policy: Policy) -> PrivacyRe
privacy_request.delete(db)


@pytest.fixture(scope="function")
def privacy_request_with_custom_array_fields(
db: Session, policy: Policy
) -> PrivacyRequest:
privacy_request = PrivacyRequest.create(
db=db,
data={
"external_id": f"ext-{str(uuid4())}",
"started_processing_at": datetime(2021, 10, 1),
"finished_processing_at": datetime(2021, 10, 3),
"requested_at": datetime(2021, 10, 1),
"status": PrivacyRequestStatus.complete,
"origin": f"https://example.com/",
"policy_id": policy.id,
"client_id": policy.client_id,
},
)
privacy_request.persist_custom_privacy_request_fields(
db=db,
custom_privacy_request_fields={
"device_ids": CustomPrivacyRequestField(
label="Device Ids", value=["device_1", "device_2", "device_3"]
),
},
)
privacy_request.save(db)
yield privacy_request
privacy_request.delete(db)


@pytest.fixture(scope="function")
def privacy_request_with_email_identity(db: Session, policy: Policy) -> PrivacyRequest:
privacy_request = PrivacyRequest.create(
Expand Down
59 changes: 59 additions & 0 deletions tests/ops/api/v1/endpoints/test_privacy_request_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,65 @@ def test_privacy_request_search_with_custom_fields(
assert resp["items"][0]["id"] == privacy_request.id
assert resp["items"][0].get("custom_privacy_request_fields") is None

def test_privacy_request_search_by_custom_fields_and_array_fields(
self,
api_client: TestClient,
url,
generate_auth_header,
privacy_request_with_custom_fields,
privacy_request_with_custom_array_fields,
):
privacy_request = privacy_request_with_custom_fields
auth_header = generate_auth_header(scopes=[PRIVACY_REQUEST_READ])
response = api_client.post(
url,
headers=auth_header,
json={
"custom_privacy_request_fields": {"first_name": "John"},
"include_custom_privacy_request_fields": True,
},
)
assert 200 == response.status_code

resp = response.json()
assert len(resp["items"]) == 1
assert resp["items"][0]["id"] == privacy_request.id
assert (
resp["items"][0]["custom_privacy_request_fields"]
== privacy_request.get_persisted_custom_privacy_request_fields()
)
assert resp["items"][0]["policy"]["key"] == privacy_request.policy.key
assert resp["items"][0]["policy"]["name"] == privacy_request.policy.name

# List fields are not indexed for search. privacy_request_with_custom_array_fields has a list of custom
# privacy request fields but it will not be returned here
response = api_client.post(
url,
headers=auth_header,
json={
"custom_privacy_request_fields": {"device_id": "device_1"},
"include_custom_privacy_request_fields": True,
},
)
assert 200 == response.status_code

resp = response.json()
assert len(resp["items"]) == 0

# If a list field is sent in as the query param, we ignore the list field in search, it does not become a filter -
response = api_client.post(
url,
headers=auth_header,
json={
"custom_privacy_request_fields": {"device_id": ["device_1"]},
"include_custom_privacy_request_fields": True,
},
)
assert 200 == response.status_code

resp = response.json()
assert len(resp["items"]) == 2

def test_privacy_request_search_by_action(
self,
api_client: TestClient,
Expand Down

0 comments on commit 96ec9ac

Please sign in to comment.