Permalink
Browse files

make makepremium idempotent (bug 824751)

  • Loading branch information...
1 parent 79386a5 commit 854187f13e6b4fc01332646dda25607a58f96d12 @andymckay andymckay committed Dec 27, 2012
Showing with 44 additions and 7 deletions.
  1. +1 −0 lib/bango/constants.py
  2. +1 −1 lib/bango/errors.py
  3. +18 −0 lib/bango/resources/simple.py
  4. +24 −6 lib/bango/tests/test_resources.py
View
@@ -1,6 +1,7 @@
import re
ACCESS_DENIED = 'ACCESS_DENIED'
+BANGO_ALREADY_PREMIUM_ENABLED = 'BANGO_ALREADY_PREMIUM_ENABLED'
BANK_DETAILS_EXIST = 'BANK_DETAILS_EXIST'
INTERNAL_ERROR = 'INTERNAL_ERROR'
# There is one of these for every field.
View
@@ -1,6 +1,6 @@
class BangoError(Exception):
def __init__(self, id, message):
- self.id = type
+ self.id = id
self.message = message
def __str__(self):
@@ -1,5 +1,10 @@
+from tastypie.exceptions import ImmediateHttpResponse
+from tastypie.http import HttpNoContent
+
from cached import SimpleResource
+from lib.bango.constants import BANGO_ALREADY_PREMIUM_ENABLED
+from lib.bango.errors import BangoError
from lib.bango.forms import (CreateBankDetailsForm, MakePremiumForm,
UpdateRatingForm)
@@ -26,3 +31,16 @@ class Meta(SimpleResource.Meta):
resource_name = 'premium'
simple_form = MakePremiumForm
simple_api = 'MakePremiumPerAccess'
+
+ def obj_create(self, *args, **kwargs):
+ try:
+ res = super(MakePremiumResource, self).obj_create(*args, **kwargs)
+ except BangoError, exc:
+ # No need to fail if this is called twice, just catch and continue.
+ if exc.id == BANGO_ALREADY_PREMIUM_ENABLED:
+ # Normally this method will return a 201 created. I'm not sure
+ # what status code best represents this, but a 204 is still a
+ # success and allows you to distinguish it, should you want to.
+ raise ImmediateHttpResponse(response=HttpNoContent())
+ raise
+ return res
@@ -11,7 +11,9 @@
from lib.transactions.models import Transaction
from solitude.base import APITest
+from ..constants import BANGO_ALREADY_PREMIUM_ENABLED
from ..client import ClientMock
+from ..errors import BangoError
from ..resources.cached import SimpleResource
import samples
@@ -201,21 +203,37 @@ def setUp(self):
super(TestBangoMarkPremium, self).setUp()
self.list_url = self.get_list_url('premium')
- def test_mark(self):
- self.create()
+ def create(self):
+ super(TestBangoMarkPremium, self).create()
data = samples.good_make_premium.copy()
data['seller_product_bango'] = self.seller_product_bango_uri
- res = self.client.post(self.list_url, data=data)
+ return data
+
+ def test_mark(self):
+ res = self.client.post(self.list_url, data=self.create())
eq_(res.status_code, 201)
def test_fail(self):
- self.create()
- data = samples.good_make_premium.copy()
+ data = self.create()
data['currencyIso'] = 'FOO'
- data['seller_product_bango'] = self.seller_product_bango_uri
res = self.client.post(self.list_url, data=data)
eq_(res.status_code, 400)
+ @mock.patch.object(ClientMock, 'mock_results')
+ def test_other_error(self, mock_results):
+ data = self.create()
+ mock_results.return_value = {'responseCode': 'wat?'}
+ with self.assertRaises(BangoError):
+ self.client.post(self.list_url, data=data)
+
+ @mock.patch.object(ClientMock, 'mock_results')
+ def test_done_twice(self, mock_results):
+ data = self.create()
+ mock_results.return_value = {'responseCode':
+ BANGO_ALREADY_PREMIUM_ENABLED}
+ res = self.client.post(self.list_url, data=data)
+ eq_(res.status_code, 204)
+
@mock.patch.object(settings, 'BANGO_MOCK', True)
class TestBangoUpdateRating(SellerProductBangoBase):

0 comments on commit 854187f

Please sign in to comment.