This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial attempt at a data model and API for capturing availability.
- Models for representing availability reports and availability windows. - REST API endpoint for writing those availabiltiy reports. - Basic test that the API endpoint works.
- Loading branch information
Showing
12 changed files
with
538 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{ | ||
"location": "recaQlVkkI1rNarvx", | ||
"input": { | ||
"feed_update": { | ||
"uuid": "02d63a35-5dbc-4ac8-affb-14603bf6eb2e", | ||
"github_url": "https://example.com", | ||
"feed_provider": "test_provider" | ||
}, | ||
"location": "116", | ||
"availability_windows": [ | ||
{ | ||
"starts_at": "2021-02-28T10:00:00Z", | ||
"ends_at": "2021-02-28T11:00:00Z", | ||
"slots": 25, | ||
"additional_restrictions": [] | ||
}, | ||
{ | ||
"starts_at": "2021-02-28T11:00:00Z", | ||
"ends_at": "2021-02-28T12:00:00Z", | ||
"slots": 18, | ||
"additional_restrictions": ["vaccinating_65_plus"] | ||
} | ||
] | ||
}, | ||
"expected_status": 201, | ||
"expected_fields": { | ||
"location__public_id": "recaQlVkkI1rNarvx" | ||
}, | ||
"expected_windows": [ | ||
{ | ||
"expected_fields": | ||
{ | ||
"slots": 25 | ||
}, | ||
"expected_additional_restrictions": [] | ||
}, | ||
{ | ||
"expected_fields": | ||
{ | ||
"slots": 18 | ||
}, | ||
"expected_additional_restrictions": ["vaccinating_65_plus"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from config.settings import SCRAPER_API_KEY | ||
from core.models import Report, Location, FeedProvider, AppointmentAvailabilityWindow, AppointmentAvailabilityReport, \ | ||
LocationFeedConcordance | ||
from api.models import ApiLog | ||
import json | ||
import pathlib | ||
import pytest | ||
|
||
tests_dir = pathlib.Path(__file__).parent / "test-data" / "submitAvailabilityReport" | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_submit_availability_report_api_bad_token(client): | ||
response = client.post("/api/submitAvailabilityReport") | ||
assert response.json() == {"error": "Authorization header must start with 'Bearer'"} | ||
assert response.status_code == 403 | ||
last_log = ApiLog.objects.order_by("-id")[0] | ||
assert { | ||
"method": "POST", | ||
"path": "/api/submitAvailabilityReport", | ||
"query_string": "", | ||
"remote_ip": "127.0.0.1", | ||
"response_status": 403, | ||
"created_report_id": None, | ||
}.items() <= last_log.__dict__.items() | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_submit_report_api_invalid_json(client): | ||
response = client.post( | ||
"/api/submitAvailabilityReport", | ||
"This is bad JSON", | ||
content_type="text/plain", | ||
HTTP_AUTHORIZATION="Bearer {}".format(SCRAPER_API_KEY) | ||
) | ||
assert response.status_code == 400 | ||
assert response.json()["error"] == "Expecting value: line 1 column 1 (char 0)" | ||
|
||
|
||
@pytest.mark.django_db | ||
@pytest.mark.parametrize("json_path", tests_dir.glob("*.json")) | ||
def test_submit_report_api_example(client, json_path): | ||
fixture = json.load(json_path.open()) | ||
assert Report.objects.count() == 0 | ||
# Ensure location exists | ||
location, _ = Location.objects.get_or_create( | ||
public_id=fixture["location"], | ||
defaults={ | ||
"latitude": 0, | ||
"longitude": 0, | ||
"location_type_id": 1, | ||
"state_id": 1, | ||
"county_id": 1, | ||
}, | ||
) | ||
# Ensure feed provider exists | ||
provider, _ = FeedProvider.objects.get_or_create( | ||
name="Test feed", | ||
slug=fixture["input"]["feed_update"]["feed_provider"] | ||
) | ||
# Create concordance | ||
LocationFeedConcordance.objects.create( | ||
feed_provider=provider, | ||
location=location, | ||
provider_id=fixture["input"]["location"] | ||
) | ||
|
||
response = client.post( | ||
"/api/submitAvailabilityReport", | ||
fixture["input"], | ||
content_type="application/json", | ||
HTTP_AUTHORIZATION="Bearer {}".format(SCRAPER_API_KEY), | ||
) | ||
assert response.status_code == fixture["expected_status"] | ||
# Load new report from DB and check it | ||
report = AppointmentAvailabilityReport.objects.order_by("-id")[0] | ||
expected_field_values = AppointmentAvailabilityReport.objects.filter(pk=report.pk).values( | ||
*list(fixture["expected_fields"].keys()) | ||
)[0] | ||
assert expected_field_values == fixture["expected_fields"] | ||
|
||
# Check the windows | ||
for window, expected_window in zip(report.windows.all(), fixture["expected_windows"]): | ||
expected_field_values = AppointmentAvailabilityWindow.objects.filter(pk=window.pk).values( | ||
*list(expected_window["expected_fields"].keys()) | ||
)[0] | ||
assert expected_field_values == expected_window["expected_fields"] | ||
|
||
actual_tags = [tag.slug for tag in window.additional_restrictions.all()] | ||
assert actual_tags == expected_window["expected_additional_restrictions"] |
Oops, something went wrong.