-
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.
✨(service_providers) add API endpoints
This allow to display service providers in the frontend. Not used yet, but will allow to manage organization and teams related service providers.
- Loading branch information
Showing
7 changed files
with
252 additions
and
0 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,3 @@ | ||
""" | ||
Test for the service providers viewset. | ||
""" |
91 changes: 91 additions & 0 deletions
91
src/backend/core/tests/service_providers/test_core_api_service_providers_list.py
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,91 @@ | ||
""" | ||
Tests for Service Provider API endpoint in People's core app: list | ||
""" | ||
|
||
import pytest | ||
from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED | ||
|
||
from core import factories | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_api_service_providers_list_anonymous(client): | ||
"""Anonymous users should not be allowed to list service providers.""" | ||
factories.ServiceProviderFactory.create_batch(2) | ||
|
||
response = client.get("/api/v1.0/service-providers/") | ||
|
||
assert response.status_code == HTTP_401_UNAUTHORIZED | ||
assert response.json() == { | ||
"detail": "Authentication credentials were not provided." | ||
} | ||
|
||
|
||
def test_api_service_providers_list_authenticated(client): | ||
""" | ||
Authenticated users should be able to list service providers | ||
of their organization. | ||
""" | ||
user = factories.UserFactory(with_organization=True) | ||
client.force_login(user) | ||
|
||
service_provider_1 = factories.ServiceProviderFactory( | ||
name="A", organizations=[user.organization] | ||
) | ||
service_provider_2 = factories.ServiceProviderFactory( | ||
name="B", organizations=[user.organization] | ||
) | ||
|
||
# Generate some not fetched data | ||
factories.ServiceProviderFactory.create_batch( | ||
2, organizations=[factories.OrganizationFactory(with_registration_id=True)] | ||
) # Other service providers | ||
factories.TeamFactory( | ||
users=[user], service_providers=[factories.ServiceProviderFactory()] | ||
) | ||
|
||
response = client.get( | ||
"/api/v1.0/service-providers/", | ||
) | ||
|
||
assert response.status_code == HTTP_200_OK | ||
assert response.json() == { | ||
"count": 2, | ||
"next": None, | ||
"previous": None, | ||
"results": [ | ||
{ | ||
"audience_id": str(service_provider_1.audience_id), | ||
"id": str(service_provider_1.pk), | ||
"name": "A", | ||
}, | ||
{ | ||
"audience_id": str(service_provider_2.audience_id), | ||
"id": str(service_provider_2.pk), | ||
"name": "B", | ||
}, | ||
], | ||
} | ||
|
||
|
||
def test_api_service_providers_order(client): | ||
"""Test that the service providers are sorted as requested.""" | ||
user = factories.UserFactory(with_organization=True) | ||
factories.ServiceProviderFactory(name="A", organizations=[user.organization]) | ||
factories.ServiceProviderFactory(name="B", organizations=[user.organization]) | ||
|
||
client.force_login(user) | ||
|
||
# Test ordering by name descending | ||
response = client.get("/api/v1.0/service-providers/?ordering=-name") | ||
assert response.status_code == 200 | ||
response_data = response.json()["results"] | ||
assert response_data[0]["name"] == "B" | ||
assert response_data[1]["name"] == "A" | ||
|
||
# Test ordering by creation date ascending | ||
response = client.get("/api/v1.0/service-providers/?ordering=created_at") | ||
response_data = response.json()["results"] | ||
assert response_data[0]["name"] == "A" | ||
assert response_data[1]["name"] == "B" |
84 changes: 84 additions & 0 deletions
84
src/backend/core/tests/service_providers/test_core_api_service_providers_retrieve.py
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,84 @@ | ||
""" | ||
Tests for Service Provider API endpoint in People's core app: retrieve | ||
""" | ||
|
||
import pytest | ||
from rest_framework.status import HTTP_200_OK, HTTP_401_UNAUTHORIZED, HTTP_404_NOT_FOUND | ||
|
||
from core import factories | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
def test_api_service_providers_retrieve_anonymous(client): | ||
"""Anonymous users should not be allowed to retrieve service providers.""" | ||
service_provider = factories.ServiceProviderFactory() | ||
|
||
response = client.get(f"/api/v1.0/service-providers/{service_provider.pk}/") | ||
|
||
assert response.status_code == HTTP_401_UNAUTHORIZED | ||
assert response.json() == { | ||
"detail": "Authentication credentials were not provided." | ||
} | ||
|
||
|
||
def test_api_service_providers_retrieve_authenticated_allowed(client): | ||
""" | ||
Authenticated users should be able to retrieve service providers | ||
of their organization. | ||
""" | ||
user = factories.UserFactory(with_organization=True) | ||
client.force_login(user) | ||
|
||
service_provider = factories.ServiceProviderFactory( | ||
organizations=[user.organization] | ||
) | ||
|
||
response = client.get(f"/api/v1.0/service-providers/{service_provider.pk}/") | ||
|
||
assert response.status_code == HTTP_200_OK | ||
assert response.json() == { | ||
"audience_id": str(service_provider.audience_id), | ||
"id": str(service_provider.pk), | ||
"name": service_provider.name, | ||
} | ||
|
||
|
||
def test_api_service_providers_retrieve_authenticated_other_organization(client): | ||
""" | ||
Authenticated users should not be able to retrieve service providers | ||
of other organization. | ||
""" | ||
user = factories.UserFactory(with_organization=True) | ||
client.force_login(user) | ||
|
||
service_provider = factories.ServiceProviderFactory( | ||
organizations=[factories.OrganizationFactory(with_registration_id=True)] | ||
) | ||
|
||
response = client.get(f"/api/v1.0/service-providers/{service_provider.pk}/") | ||
|
||
assert response.status_code == HTTP_404_NOT_FOUND | ||
assert response.json() == {"detail": "No ServiceProvider matches the given query."} | ||
|
||
|
||
def test_api_service_providers_retrieve_authenticated_on_teams(client): | ||
""" | ||
Authenticated users should not be able to retrieve service providers | ||
of because of their teams (might change later if needed). | ||
""" | ||
user = factories.UserFactory(with_organization=True) | ||
client.force_login(user) | ||
|
||
other_organization = factories.OrganizationFactory(with_registration_id=True) | ||
service_provider = factories.ServiceProviderFactory() | ||
factories.TeamFactory( | ||
users=[user], | ||
organization=other_organization, | ||
service_providers=[service_provider], | ||
) | ||
|
||
response = client.get(f"/api/v1.0/service-providers/{service_provider.pk}/") | ||
|
||
assert response.status_code == HTTP_404_NOT_FOUND | ||
assert response.json() == {"detail": "No ServiceProvider matches the given query."} |
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