Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
Merge pull request #1443 from diox/convert-testreceiptresource-to-drf
Browse files Browse the repository at this point in the history
Convert TestReceiptResource to DRF (bug 910582)
  • Loading branch information
diox committed Nov 28, 2013
2 parents edfa43b + b0fdb64 commit 254c6aa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 38 deletions.
35 changes: 13 additions & 22 deletions mkt/receipts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,12 @@
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response

from tastypie.authorization import Authorization
from tastypie.validation import CleanedDataFormValidation


from constants.payments import CONTRIB_NO_CHARGE
from lib.cef_loggers import receipt_cef
from market.models import AddonPurchase
from mkt.api.authentication import (RestOAuthAuthentication,
OptionalOAuthAuthentication,
RestSharedSecretAuthentication)
from mkt.api.base import cors_api_view, CORSResource, MarketplaceResource
from mkt.api.base import cors_api_view
from mkt.constants import apps
from mkt.installs.utils import install_type, record
from mkt.receipts.forms import ReceiptForm, TestInstall
Expand Down Expand Up @@ -82,23 +77,19 @@ def install_record(obj, request, install_type):
return create_receipt(installed)


class TestReceiptResource(CORSResource, MarketplaceResource):

class Meta(MarketplaceResource.Meta):
always_return_data = True
authentication = OptionalOAuthAuthentication()
authorization = Authorization()
detail_allowed_methods = []
list_allowed_methods = ['post']
object_class = dict
resource_name = 'test'
validation = CleanedDataFormValidation(form_class=TestInstall)
@cors_api_view(['POST'])
@permission_classes((AllowAny,))
def test_receipt(request):
form = TestInstall(request.DATA)
if not form.is_valid():
return Response({'error_message': form.errors}, status=400)

def obj_create(self, bundle, request=None, **kwargs):
receipt_cef.log(request, None, 'sign', 'Test receipt signing')
bundle.data = {'receipt': create_test_receipt(
bundle.data['root'], bundle.data['receipt_type'])}
return bundle
receipt_cef.log(request._request, None, 'sign', 'Test receipt signing')
data = {
'receipt': create_test_receipt(form.cleaned_data['root'],
form.cleaned_data['receipt_type'])
}
return Response(data, status=201)


@cors_api_view(['POST'])
Expand Down
15 changes: 5 additions & 10 deletions mkt/receipts/tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import json

from django.conf import settings
from django.contrib.auth.models import AnonymousUser
from django.core.urlresolvers import reverse

import mock
from nose.tools import eq_, ok_
from receipts.receipts import Receipt
from tastypie import http
from tastypie.bundle import Bundle
from test_utils import RequestFactory

import amo.tests

from addons.models import Addon, AddonUser
from constants.payments import CONTRIB_NO_CHARGE
from devhub.models import AppLog
from mkt.api.base import list_url
from mkt.api.tests.test_oauth import BaseOAuth, RestOAuth
from mkt.api.tests.test_oauth import RestOAuth
from mkt.constants import apps
from mkt.site.fixtures import fixture
from users.models import UserProfile
Expand Down Expand Up @@ -91,17 +86,17 @@ def test_log_metrics(self, cef):

@mock.patch.object(settings, 'WEBAPPS_RECEIPT_KEY',
amo.tests.AMOPaths.sample_key())
class TestDevhubAPI(BaseOAuth):
class TestDevhubAPI(RestOAuth):
fixtures = fixture('user_2519', 'webapp_337141')

def setUp(self):
super(TestDevhubAPI, self).setUp(api_name='receipts')
super(TestDevhubAPI, self).setUp()
self.data = json.dumps({'manifest_url': 'http://foo.com',
'receipt_type': 'expired'})
self.url = list_url('test')
self.url = reverse('receipt.test')

def test_has_cors(self):
self.assertCORS(self.client.get(self.url), 'post')
self.assertCORS(self.client.post(self.url), 'post')

def test_decode(self):
res = self.anon.post(self.url, data=self.data)
Expand Down
8 changes: 2 additions & 6 deletions mkt/receipts/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
from django.conf.urls import include, patterns, url

from tastypie.api import Api
from django.conf.urls import patterns, url

import amo
from . import api, views

receipt = Api(api_name='receipts')
receipt.register(api.TestReceiptResource())

# Note: this URL is embedded in receipts, if you change the URL, make sure
# that you put a redirect in.
Expand All @@ -25,8 +21,8 @@
)

receipt_api_patterns = patterns('',
url(r'^', include(receipt.urls)),
url(r'^receipts/install/', api.install, name='receipt.install'),
url(r'^receipts/test/', api.test_receipt, name='receipt.test'),
url(r'^receipts/reissue/', api.reissue, name='receipt.reissue')
)

Expand Down

0 comments on commit 254c6aa

Please sign in to comment.