Skip to content

Commit

Permalink
[BE] fix redeem_by timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
poxip committed Sep 8, 2017
1 parent 2461c0e commit 8e5d20b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
9 changes: 5 additions & 4 deletions aa_stripe/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.contrib.contenttypes.models import ContentType
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from django.utils import timezone
from django.utils import dateformat, timezone
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField

Expand Down Expand Up @@ -178,8 +178,9 @@ def update_from_stripe_data(self, stripe_coupon, exclude_fields=None, commit=Tru
"""
fields_to_update = self.STRIPE_FIELDS - set(exclude_fields or [])
update_data = {key: stripe_coupon[key] for key in fields_to_update}
if update_data.get("created"):
update_data["created"] = timestamp_to_timezone_aware_date(update_data["created"])
for field in ["created", "redeem_by"]:
if update_data.get(field):
update_data[field] = timestamp_to_timezone_aware_date(update_data[field])

if update_data.get("amount_off"):
update_data["amount_off"] = Decimal(update_data["amount_off"]) / 100
Expand Down Expand Up @@ -245,7 +246,7 @@ def save(self, force_retrieve=False, *args, **kwargs):
max_redemptions=self.max_redemptions,
metadata=self.metadata,
percent_off=self.percent_off,
redeem_by=self.redeem_by
redeem_by=int(dateformat.format(self.redeem_by, "U")) if self.redeem_by else None
)
# stripe will generate coupon_id if none was specified in the request
if not self.coupon_id:
Expand Down
22 changes: 14 additions & 8 deletions tests/test_coupons.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import time
from datetime import datetime
from datetime import datetime, timedelta
from decimal import Decimal

import requests_mock
import simplejson as json
from django.contrib.auth import get_user_model
from django.core.management import call_command
from django.utils import timezone
from django.utils import dateformat, timezone
from freezegun import freeze_time
from rest_framework.reverse import reverse
from tests.test_utils import BaseTestCase

from aa_stripe.forms import StripeCouponForm
from aa_stripe.models import StripeCoupon
from aa_stripe.utils import timestamp_to_timezone_aware_date

UserModel = get_user_model()

Expand All @@ -21,31 +22,34 @@ class TestCoupons(BaseTestCase):
@freeze_time("2016-01-01 00:00:00")
def test_create(self):
# test creating simple coupon with no coupon_id specified (will be generated by Stripe)
created = int(time.mktime(datetime.now().timetuple()))
stripe_response = {
"id": "25OFF",
"object": "coupon",
"amount_off": 1,
"created": int(time.mktime(datetime.now().timetuple())),
"created": created,
"currency": "usd",
"duration": StripeCoupon.DURATION_FOREVER,
"duration_in_months": None,
"livemode": False,
"max_redemptions": None,
"metadata": {},
"percent_off": 25,
"redeem_by": None,
"redeem_by": created + 60,
"times_redeemed": 0,
"valid": True
}
with requests_mock.Mocker() as m:
m.register_uri("POST", "https://api.stripe.com/v1/coupons", text=json.dumps(stripe_response))
coupon = StripeCoupon.objects.create(
duration=StripeCoupon.DURATION_FOREVER,
percent_off=25
percent_off=25,
redeem_by=timezone.now() + timedelta(seconds=60)
)
self.assertEqual(coupon.coupon_id, stripe_response["id"])
self.assertEqual(coupon.created, timezone.make_aware(datetime.fromtimestamp(stripe_response["created"])))
self.assertEqual(coupon.created, timestamp_to_timezone_aware_date(stripe_response["created"]))
self.assertEqual(coupon.stripe_response, stripe_response)
self.assertEqual(m.last_request.text.split("redeem_by=")[1], dateformat.format(coupon.redeem_by, "U"))

# test creating coupon with coupon_id
stripe_response["id"] = "YOLO1"
Expand All @@ -58,19 +62,20 @@ def test_create(self):

def test_update(self):
with requests_mock.Mocker() as m:
created = int(time.mktime(datetime.now().timetuple()))
stripe_response = {
"id": "25OFF",
"object": "coupon",
"amount_off": 1,
"created": int(time.mktime(datetime.now().timetuple())),
"created": created,
"currency": "usd",
"duration": StripeCoupon.DURATION_FOREVER,
"duration_in_months": None,
"livemode": False,
"max_redemptions": None,
"metadata": {},
"percent_off": 25,
"redeem_by": None,
"redeem_by": created + 60,
"times_redeemed": 0,
"valid": True
}
Expand All @@ -96,6 +101,7 @@ def test_update(self):
coupon.save()
coupon.refresh_from_db()
self.assertNotEqual(coupon.duration, StripeCoupon.DURATION_ONCE)
self.assertEqual(coupon.redeem_by, timestamp_to_timezone_aware_date(stripe_response["redeem_by"]))

def test_delete(self):
coupon = self._create_coupon(coupon_id="CPON", amount_off=1, duration=StripeCoupon.DURATION_FOREVER)
Expand Down

0 comments on commit 8e5d20b

Please sign in to comment.