Skip to content

Commit

Permalink
Merge pull request #121 from ebridges/feature/confirm_upload_processing
Browse files Browse the repository at this point in the history
feature/confirm upload processing
  • Loading branch information
ebridges authored Sep 11, 2020
2 parents f5285ca + 9b90515 commit 7215a94
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 4 deletions.
4 changes: 2 additions & 2 deletions application/elektrum/deploy/task_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
get_tag_commit,
)

ELEKTRUM_APPLICATION_VERSION = {'development': '0.7.1', 'staging': '0.7.1', 'production': '0.7.1'}
ELEKTRUM_APPLICATION_VERSION = {'development': '0.7.2', 'staging': '0.7.2', 'production': '0.7.2'}
ELEKTRUM_PROCESSOR_VERSION = {'development': '1.1.12', 'staging': '1.1.12', 'production': '1.1.12'}
ELEKTRUM_THUMBNAIL_VERSION = {'development': '1.4.0', 'staging': '1.4.0', 'production': '1.4.0'}
ELEKTRUM_THUMBNAIL_VERSION = {'development': '1.5.0', 'staging': '1.5.0', 'production': '1.5.0'}


def service():
Expand Down
57 changes: 57 additions & 0 deletions application/media_items/tests/test_media_processor_check.py
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
2 changes: 2 additions & 0 deletions application/media_items/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from media_items.views.media_item_view import media_item_view
from media_items.views.media_item_upload_view import media_item_upload_view
from media_items.views.upload_request import upload_request_web, upload_request_api
from media_items.views.media_processor_check import confirm_upload


# noinspection PyMethodMayBeStatic
Expand Down Expand Up @@ -37,6 +38,7 @@ def to_url(self, value):
urlpatterns = [
path('upload-request/', upload_request_web, name='upload-request'),
path('api/upload-request/', upload_request_api, name='upload-request-api'),
path('api/confirm-upload/<uuid:uid>/<uuid:iid>.<ext>', confirm_upload, name='confirm-upload'),
path('<uuid:owner_id>/upload/', media_item_upload_view, name='media-item-upload-view'),
path('<uuid:owner_id>/<uuid:image_id>/', media_item_view, name='media-item-view'),
path('<uuid:owner_id>/<yyyy:year>/<yyyy-mm-dd:date>/', media_list_view, name='media-list-view'),
Expand Down
38 changes: 38 additions & 0 deletions application/media_items/views/media_processor_check.py
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')
8 changes: 7 additions & 1 deletion application/media_items/views/upload_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@
from django.http import HttpResponse

from media_items.views.upload_util import create_signed_upload_url, supported_upload_types
from base.views.errors import *
from base.views.errors import (
MethodNotAllowedException,
ForbiddenException,
BadRequestException,
exceptions_to_web_response,
exceptions_to_api_response,
)


@exceptions_to_web_response
Expand Down
2 changes: 1 addition & 1 deletion network/roles/lam/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
action: lambda:InvokeFunction
principal: s3.amazonaws.com
function_name: "{{ media_processor_lambda_name }}"
statement_id: "{{ media_processor_execution_role_policy }}"
statement_id: "{{service_name}}-{{operating_env}}-invoke"
source_arn: "arn:aws:s3:{{aws_region}}:{{aws_account_id}}:{{ media_upload_bucket_name }}"
source_account: "{{aws_account_id}}"
register: lambda_policy
Expand Down

0 comments on commit 7215a94

Please sign in to comment.