-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #121 from ebridges/feature/confirm_upload_processing
feature/confirm upload processing
- Loading branch information
Showing
6 changed files
with
107 additions
and
4 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
57 changes: 57 additions & 0 deletions
57
application/media_items/tests/test_media_processor_check.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,57 @@ | ||
from uuid import uuid4 | ||
from unittest.mock import MagicMock | ||
from django.http import HttpRequest | ||
from django.contrib.auth.models import AnonymousUser | ||
|
||
from pytest import raises, mark | ||
|
||
from base.views.errors import MethodNotAllowedException, ForbiddenException | ||
from media_items.views.media_processor_check import validate, check_exists | ||
from users.models import CustomUser | ||
|
||
|
||
@mark.django_db | ||
def test_check_exists_normal_case(media_item_factory): | ||
mi = media_item_factory() | ||
response = check_exists(mi.id) | ||
assert response.status_code == 200 | ||
|
||
|
||
@mark.django_db | ||
def test_check_exists_normal_case(): | ||
response = check_exists(uuid4()) | ||
assert response.status_code == 404 | ||
|
||
|
||
@mark.django_db | ||
def test_validate_request_normal_case(user_factory): | ||
expected_id = uuid4() | ||
u = user_factory() | ||
under_test = setup_validate_request_mock(u) | ||
assert validate(under_test, u.id) is None | ||
|
||
|
||
@mark.django_db | ||
def test_validate_request_invalid_method(user_factory): | ||
u = user_factory() | ||
under_test = setup_validate_request_mock(u, method='POST') | ||
with raises(MethodNotAllowedException): | ||
validate(under_test, u.id) | ||
|
||
|
||
def test_validate_request_forbidden(): | ||
u = AnonymousUser() | ||
under_test = setup_validate_request_mock(u) | ||
with raises(ForbiddenException): | ||
validate(under_test, u.id) | ||
|
||
under_test = setup_validate_request_mock(u) | ||
with raises(ForbiddenException): | ||
validate(under_test, uuid4()) | ||
|
||
|
||
def setup_validate_request_mock(user, method='HEAD'): | ||
mock_request = HttpRequest() | ||
mock_request.user = user | ||
mock_request.method = method | ||
return mock_request |
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,38 @@ | ||
from logging import info | ||
from django.shortcuts import get_object_or_404 | ||
from django.http import HttpResponse | ||
from rest_framework.decorators import api_view | ||
|
||
from base.views.errors import ( | ||
MethodNotAllowedException, | ||
ForbiddenException, | ||
exceptions_to_api_response, | ||
) | ||
from media_items.models import MediaItem | ||
|
||
|
||
@api_view(http_method_names=['HEAD']) | ||
@exceptions_to_api_response | ||
def confirm_upload(request, uid, iid, ext): | ||
info(f'confirm processing completed for {uid}/{iid}.{ext}') | ||
validate(request, uid) | ||
return check_exists(iid) | ||
|
||
|
||
def check_exists(iid): | ||
if MediaItem.objects.filter(pk=iid).exists(): | ||
return HttpResponse(status=200) | ||
else: | ||
return HttpResponse(status=404) | ||
|
||
|
||
def validate(request, owner_id): | ||
if request.method != 'HEAD': | ||
raise MethodNotAllowedException('HEAD') | ||
|
||
user = request.user | ||
if not user.is_authenticated: | ||
raise ForbiddenException('Authentication is required.') | ||
|
||
if not user.id == owner_id: | ||
raise ForbiddenException('Permission denied') |
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