forked from City-of-Helsinki/parkkihubi
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api/enforcement: Add endpoint for operators
Add API endpoint for listing the operators. Also add operator identifier to the valid_parking endpoint so that it is possible to link them properly (not just by name).
- Loading branch information
1 parent
e79bcf4
commit 4716c3b
Showing
5 changed files
with
98 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from rest_framework import permissions, serializers, viewsets | ||
|
||
from ...authentication import ApiKeyAuthentication | ||
from ...models import Operator | ||
|
||
|
||
class OperatorSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Operator | ||
fields = [ | ||
'id', | ||
'created_at', | ||
'modified_at', | ||
'name', | ||
] | ||
|
||
|
||
class OperatorViewSet(viewsets.ReadOnlyModelViewSet): | ||
queryset = Operator.objects.order_by('name') | ||
serializer_class = OperatorSerializer | ||
authentication_classes = [ApiKeyAuthentication] | ||
permission_classes = [permissions.IsAdminUser] |
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ class Meta: | |
'time_start', | ||
'time_end', | ||
'zone', | ||
'operator', | ||
'operator_name', | ||
] | ||
|
||
|
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,71 @@ | ||
import pytest | ||
from django.core.urlresolvers import reverse | ||
from rest_framework.status import HTTP_401_UNAUTHORIZED, HTTP_403_FORBIDDEN | ||
|
||
from parkings.models import Operator | ||
|
||
from ..utils import ALL_METHODS, check_list_endpoint_base_fields, check_method_status_codes, get | ||
|
||
list_url = reverse('enforcement:v1:operator-list') | ||
|
||
|
||
def get_url(kind, operator): | ||
if kind == 'list': | ||
return list_url | ||
elif kind == 'detail': | ||
return reverse('enforcement:v1:operator-detail', | ||
kwargs={'pk': operator.pk}) | ||
|
||
|
||
ALL_URL_KINDS = ['list', 'detail'] | ||
|
||
|
||
@pytest.mark.parametrize('url_kind', ALL_URL_KINDS) | ||
def test_permission_checks(api_client, operator_api_client, operator, url_kind): | ||
url = get_url(url_kind, operator) | ||
check_method_status_codes( | ||
api_client, [url], ALL_METHODS, HTTP_401_UNAUTHORIZED) | ||
check_method_status_codes( | ||
operator_api_client, [url], ALL_METHODS, HTTP_403_FORBIDDEN, | ||
error_code='permission_denied') | ||
|
||
|
||
@pytest.mark.parametrize('url_kind', ALL_URL_KINDS) | ||
def test_disallowed_methods(staff_api_client, operator, url_kind): | ||
url = get_url(url_kind, operator) | ||
disallowed_methods = ('post', 'put', 'patch', 'delete') | ||
check_method_status_codes( | ||
staff_api_client, [url], disallowed_methods, 405) | ||
|
||
|
||
def test_list_endpoint_base_fields(staff_api_client): | ||
operator_data = get(staff_api_client, list_url) | ||
check_list_endpoint_base_fields(operator_data) | ||
|
||
|
||
def test_list_endpoint_data(staff_api_client, operator): | ||
assert Operator.objects.count() == 1 | ||
data = get(staff_api_client, list_url) | ||
assert len(data['results']) == 1 | ||
operator_data = data['results'][0] | ||
check_operator_data_keys(operator_data) | ||
check_operator_data_matches_operator_object(data['results'][0], operator) | ||
|
||
|
||
def check_operator_data_keys(operator_data): | ||
assert set(operator_data.keys()) == { | ||
'id', 'created_at', 'modified_at', 'name'} | ||
|
||
|
||
def check_operator_data_matches_operator_object(operator_data, operator_obj): | ||
""" | ||
Check that a operator data dict and an actual Operator object match. | ||
""" | ||
assert operator_data['id'] == str(operator_obj.id) # UUID -> str | ||
assert operator_data['created_at'] == iso8601_us(operator_obj.created_at) | ||
assert operator_data['modified_at'] == iso8601_us(operator_obj.modified_at) | ||
assert operator_data['name'] == operator_obj.name | ||
|
||
|
||
def iso8601_us(dt): | ||
return dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ') |
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