/zebra_sample_project/marty/tests.py

https://github.com/leetrout/django-zebra · Python · 365 lines · 292 code · 63 blank · 10 comment · 0 complexity · 66b666cb3df5d1318db38f65b9fc1c8c MD5 · raw file

  1. import unittest
  2. from django.conf import settings
  3. from django.test.client import Client
  4. from zebra.signals import *
  5. from django.utils import simplejson
  6. from django.core.urlresolvers import reverse
  7. class TestWebhooks(unittest.TestCase):
  8. def setUp(self):
  9. self.signal_kwargs = None
  10. def _signal_reciever(self, **kwargs):
  11. self.signal_kwargs = kwargs
  12. def _customized_signal_reciever(self, **kwargs):
  13. self.customer = kwargs["customer"]
  14. self.full_json = kwargs["full_json"]
  15. def test_recurring_payment_failed_signal_fired(self):
  16. zebra_webhook_recurring_payment_failed.connect(self._signal_reciever)
  17. self.assertEqual(self.signal_kwargs, None)
  18. # Pulled directly from the stripe docs
  19. test_post_data = {'json': simplejson.dumps({
  20. "customer":1083,
  21. "livemode": True,
  22. "event": "recurring_payment_failed",
  23. "attempt": 2,
  24. "invoice": {
  25. "attempted": True,
  26. "charge": "ch_sUmNHkMiag",
  27. "closed": False,
  28. "customer": "1083",
  29. "date": 1305525584,
  30. "id": "in_jN6A1g8N76",
  31. "object": "invoice",
  32. "paid": True,
  33. "period_end": 1305525584,
  34. "period_start": 1305525584,
  35. "subtotal": 2000,
  36. "total": 2000,
  37. "lines": {
  38. "subscriptions": [
  39. {
  40. "period": {
  41. "start": 1305525584,
  42. "end": 1308203984
  43. },
  44. "plan": {
  45. "object": "plan",
  46. "name": "Premium plan",
  47. "id": "premium",
  48. "interval": "month",
  49. "amount": 2000
  50. },
  51. "amount": 2000
  52. }
  53. ]
  54. }
  55. },
  56. "payment": {
  57. "time": 1297887533,
  58. "card": {
  59. "type": "Visa",
  60. "last4": "4242"
  61. },
  62. "success": False
  63. }
  64. }) }
  65. c = Client()
  66. response = c.post(reverse("zebra:webhooks"), test_post_data)
  67. self.assertEqual(response.status_code, 200)
  68. self.assertEqual(self.signal_kwargs["full_json"]["customer"], 1083)
  69. def test_invoice_ready_signal_fired(self):
  70. zebra_webhook_invoice_ready.connect(self._signal_reciever)
  71. self.assertEqual(self.signal_kwargs, None)
  72. # Pulled directly from the stripe docs
  73. test_post_data = {'json': simplejson.dumps(
  74. {
  75. "customer":1083,
  76. "event":"invoice_ready",
  77. "invoice": {
  78. "total": 1500,
  79. "subtotal": 3000,
  80. "lines": {
  81. "invoiceitems": [
  82. {
  83. "id": "ii_N17xcRJUtn",
  84. "amount": 1000,
  85. "date": 1303586118,
  86. "currency": "usd",
  87. "description": "One-time setup fee"
  88. }
  89. ],
  90. "subscriptions": [
  91. {
  92. "amount": 2000,
  93. "period": {
  94. "start": 1304588585,
  95. "end": 1307266985
  96. },
  97. "plan": {
  98. "amount": 2000,
  99. "interval": "month",
  100. "object": "plan",
  101. "id": "p_Mr2NgWECmJ",
  102. "id": "premium"
  103. }
  104. }
  105. ]
  106. },
  107. "object": "invoice",
  108. "discount": {
  109. "code": "50OFF",
  110. "percent_off": 50
  111. },
  112. "date": 1304588585,
  113. "period_start": 1304588585,
  114. "id": "in_jN6A1g8N76",
  115. "period_end": 1304588585
  116. }
  117. }
  118. ) }
  119. c = Client()
  120. response = c.post(reverse("zebra:webhooks"), test_post_data)
  121. self.assertEqual(response.status_code, 200)
  122. self.assertEqual(self.signal_kwargs["full_json"]["invoice"]["date"], 1304588585)
  123. def test_recurring_payment_succeeded_signal_fired(self):
  124. zebra_webhook_recurring_payment_succeeded.connect(self._signal_reciever)
  125. self.assertEqual(self.signal_kwargs, None)
  126. # Pulled directly from the stripe docs
  127. test_post_data = {'json': simplejson.dumps(
  128. {
  129. "customer":"1083",
  130. "livemode": True,
  131. "event":"recurring_payment_succeeded",
  132. "invoice": {
  133. "total": 2000,
  134. "subtotal": 2000,
  135. "lines": {
  136. "subscriptions": [
  137. {
  138. "amount": 2000,
  139. "period": {
  140. "start": 1304588585,
  141. "end": 1307266985
  142. },
  143. "plan": {
  144. "amount": 2000,
  145. "interval": "month",
  146. "object": "plan",
  147. "id": "premium",
  148. "name": "Premium plan"
  149. }
  150. }
  151. ]
  152. },
  153. "object": "invoice",
  154. "date": 1304588585,
  155. "period_start": 1304588585,
  156. "id": "in_jN6A1g8N76",
  157. "period_end": 1304588585
  158. },
  159. "payment": {
  160. "time": 1297887533,
  161. "card":
  162. {
  163. "type": "Visa",
  164. "last4": "4242"
  165. },
  166. "success": True
  167. }
  168. }
  169. ) }
  170. c = Client()
  171. response = c.post(reverse("zebra:webhooks"), test_post_data)
  172. self.assertEqual(response.status_code, 200)
  173. self.assertEqual(self.signal_kwargs["full_json"]["payment"]["time"], 1297887533)
  174. def test_subscription_trial_ending_signal_fired(self):
  175. zebra_webhook_subscription_trial_ending.connect(self._signal_reciever)
  176. self.assertEqual(self.signal_kwargs, None)
  177. # Pulled directly from the stripe docs
  178. test_post_data = {'json': simplejson.dumps(
  179. {
  180. "customer":1083,
  181. "event":"subscription_trial_ending",
  182. "subscription":
  183. {
  184. "trial_start": 1304627445,
  185. "trial_end": 1307305845,
  186. "plan": {
  187. "trial_period_days": 31,
  188. "amount": 2999,
  189. "interval": "month",
  190. "id": "silver",
  191. "name": "Silver"
  192. },
  193. }
  194. }
  195. ) }
  196. c = Client()
  197. response = c.post(reverse("zebra:webhooks"), test_post_data)
  198. self.assertEqual(response.status_code, 200)
  199. self.assertEqual(self.signal_kwargs["full_json"]["subscription"]["trial_end"], 1307305845)
  200. def test_subscription_final_payment_attempt_failed_signal_fired(self):
  201. zebra_webhook_subscription_final_payment_attempt_failed.connect(self._signal_reciever)
  202. self.assertEqual(self.signal_kwargs, None)
  203. # Pulled directly from the stripe docs
  204. test_post_data = {'json': simplejson.dumps(
  205. {
  206. "customer":1083,
  207. "event":"subscription_final_payment_attempt_failed",
  208. "subscription": {
  209. "status": "canceled",
  210. "start": 1304585542,
  211. "plan": {
  212. "amount": 2000,
  213. "interval": "month",
  214. "object": "plan",
  215. "id": "p_ag2NgWECmJ",
  216. "id": "silver"
  217. },
  218. "canceled_at": 1304585552,
  219. "ended_at": 1304585552,
  220. "object": "subscription",
  221. "current_period_end": 1307263942,
  222. "id": "sub_kP4M63kFrb",
  223. "current_period_start": 1304585542
  224. }
  225. }
  226. ) }
  227. c = Client()
  228. response = c.post(reverse("zebra:webhooks"), test_post_data)
  229. self.assertEqual(response.status_code, 200)
  230. self.assertEqual(self.signal_kwargs["full_json"]["subscription"]["start"], 1304585542)
  231. def test_webhooks_return_valid_customer_obj(self):
  232. zebra_webhook_subscription_trial_ending.connect(self._signal_reciever)
  233. from zebra.models import Customer
  234. cust = Customer.objects.create()
  235. # since ZEBRA_AUTO_CREATE_STRIPE_CUSTOMERS is on (default), this creates a customer
  236. cust.stripe_customer
  237. self.assertEqual(self.signal_kwargs, None)
  238. # Pulled directly from the stripe docs
  239. test_post_data = {'json': simplejson.dumps(
  240. {
  241. "customer":cust.stripe_customer_id,
  242. "event":"subscription_trial_ending",
  243. "subscription":
  244. {
  245. "trial_start": 1304627445,
  246. "trial_end": 1307305845,
  247. "plan": {
  248. "trial_period_days": 31,
  249. "amount": 2999,
  250. "interval": "month",
  251. "id": "silver",
  252. "name": "Silver"
  253. },
  254. }
  255. }
  256. ) }
  257. c = Client()
  258. response = c.post(reverse("zebra:webhooks"), test_post_data)
  259. self.assertEqual(response.status_code, 200)
  260. self.assertEqual(self.signal_kwargs["customer"], cust)
  261. def test_webhooks_return_valid_customer_obj_as_an_arg(self):
  262. zebra_webhook_subscription_trial_ending.connect(self._customized_signal_reciever)
  263. from zebra.models import Customer
  264. cust = Customer.objects.create()
  265. # since ZEBRA_AUTO_CREATE_STRIPE_CUSTOMERS is on (default), this creates a customer
  266. cust.stripe_customer
  267. self.assertEqual(self.signal_kwargs, None)
  268. # Pulled directly from the stripe docs
  269. test_post_data = {'json': simplejson.dumps(
  270. {
  271. "customer":cust.stripe_customer_id,
  272. "event":"subscription_trial_ending",
  273. "subscription":
  274. {
  275. "trial_start": 1304627445,
  276. "trial_end": 1307305845,
  277. "plan": {
  278. "trial_period_days": 31,
  279. "amount": 2999,
  280. "interval": "month",
  281. "id": "silver",
  282. "name": "Silver"
  283. },
  284. }
  285. }
  286. ) }
  287. c = Client()
  288. response = c.post(reverse("zebra:webhooks"), test_post_data)
  289. self.assertEqual(response.status_code, 200)
  290. self.assertEqual(self.customer, cust)
  291. def test_ping_webhook_signal_fired(self):
  292. zebra_webhook_subscription_ping_sent.connect(self._signal_reciever)
  293. self.assertEqual(self.signal_kwargs, None)
  294. # Pulled directly from the stripe docs
  295. test_post_data = {'json': simplejson.dumps(
  296. {
  297. "event":"ping",
  298. }
  299. ) }
  300. c = Client()
  301. response = c.post(reverse("zebra:webhooks"), test_post_data)
  302. self.assertEqual(response.status_code, 200)