Merge lp:~ricardokirkner/ols-store-tests/snap-purchases-api into lp:~ubuntuone-pqm-team/ols-store-tests/store-acceptance-tests

Proposed by Ricardo Kirkner
Status: Merged
Merged at revision: 33
Proposed branch: lp:~ricardokirkner/ols-store-tests/snap-purchases-api
Merge into: lp:~ubuntuone-pqm-team/ols-store-tests/store-acceptance-tests
Diff against target: 341 lines (+286/-4)
6 files modified
.bzrignore (+2/-0)
Makefile (+21/-3)
dependencies.txt (+1/-1)
requirements.txt (+1/-0)
tests/api/snap/test_snap_purchases_customers.py (+133/-0)
tests/api/snap/test_snap_purchases_orders.py (+128/-0)
To merge this branch: bzr merge lp:~ricardokirkner/ols-store-tests/snap-purchases-api
Reviewer Review Type Date Requested Status
Vincent Ladeuil (community) Approve
Ubuntu One PQM Team Pending
Review via email: mp+305946@code.launchpad.net

Commit message

added tests for new snap purchases api

To post a comment you must log in.
32. By Ricardo Kirkner

make sure store-versions is called after the branch has been updated

33. By Ricardo Kirkner

use wheels for dependencies instead of hitting pypi

34. By Ricardo Kirkner

pass STRIPE_PUBLISHABLE_KEY to vm environment

Revision history for this message
Vincent Ladeuil (vila) wrote :

I didn't review the tests, for the "infra" bits, all look sane.

Thanks for the great work ;)

review: Approve
35. By Ricardo Kirkner

updated snap purchases api urls

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file '.bzrignore'
--- .bzrignore 1970-01-01 00:00:00 +0000
+++ .bzrignore 2016-09-19 13:38:27 +0000
@@ -0,0 +1,2 @@
1env
2branches/
03
=== modified file 'Makefile'
--- Makefile 2016-09-19 12:16:07 +0000
+++ Makefile 2016-09-19 13:38:27 +0000
@@ -1,11 +1,28 @@
1# Copyright (C) 2016 Canonical Ltd.1# Copyright (C) 2016 Canonical Ltd.
22
3ENV = $(CURDIR)/env
4PIP = pip3
3PYTHON = python35PYTHON = python3
4VM = store-acceptance-tests6VM = store-acceptance-tests
5VSSH = ols-vms shell ${VM}7VSSH = ols-vms shell ${VM}
6TEST_TARGET ?= discover tests/api8TEST_TARGET ?= discover tests/api
79PROJECT_NAME = ols-store-tests
8vm-setup: ols-vms.conf10WHEELS_DIR = branches/wheels
11WHEELS_BRANCH_URL ?= lp:~ubuntuone-pqm-team/$(PROJECT_NAME)/dependencies
12
13bootstrap: $(ENV) wheels install-wheels
14
15$(ENV):
16 pyvenv --clear --system-site-packages $(ENV)
17
18wheels:
19 [ -d $(WHEELS_DIR) ] && (cd $(WHEELS_DIR) && bzr pull) || (bzr branch $(WHEELS_BRANCH_URL) $(WHEELS_DIR))
20
21install-wheels: ARGS=-r requirements.txt
22install-wheels: $(ENV)
23 $(PIP) install --find-links=$(WHEELS_DIR) --no-index $(ARGS)
24
25vm-setup: ols-vms.conf dependencies.txt
9 if [ `ols-vms status ${VM}` = 'RUNNING' ] ; then ols-vms stop ${VM} ; fi26 if [ `ols-vms status ${VM}` = 'RUNNING' ] ; then ols-vms stop ${VM} ; fi
10 ols-vms setup ${VM}27 ols-vms setup ${VM}
11 touch vm-setup28 touch vm-setup
@@ -30,7 +47,8 @@
30 TEST_USER_EMAIL=$(TEST_USER_EMAIL) \47 TEST_USER_EMAIL=$(TEST_USER_EMAIL) \
31 TEST_USER_PASSWORD=$(TEST_USER_PASSWORD) \48 TEST_USER_PASSWORD=$(TEST_USER_PASSWORD) \
32 TEST_USER_NAMESPACE=$(TEST_USER_NAMESPACE) \49 TEST_USER_NAMESPACE=$(TEST_USER_NAMESPACE) \
50 STRIPE_PUBLISHABLE_KEY=$(STRIPE_PUBLISHABLE_KEY) \
33 make api-tests'51 make api-tests'
3452
35api-tests:53api-tests: bootstrap
36 $(PYTHON) -u -m unittest $(TEST_TARGET)54 $(PYTHON) -u -m unittest $(TEST_TARGET)
3755
=== added directory 'branches'
=== modified file 'dependencies.txt'
--- dependencies.txt 2016-08-18 12:49:56 +0000
+++ dependencies.txt 2016-09-19 13:38:27 +0000
@@ -1,6 +1,6 @@
1make1make
2squashfs-tools2squashfs-tools
3snapcraft3snapcraft
4python3-pip
4python3-pymacaroons5python3-pymacaroons
5python3-requests6python3-requests
6
77
=== added file 'requirements.txt'
--- requirements.txt 1970-01-01 00:00:00 +0000
+++ requirements.txt 2016-09-19 13:38:27 +0000
@@ -0,0 +1,1 @@
1stripe==1.36.0
02
=== added file 'tests/api/snap/test_snap_purchases_customers.py'
--- tests/api/snap/test_snap_purchases_customers.py 1970-01-01 00:00:00 +0000
+++ tests/api/snap/test_snap_purchases_customers.py 2016-09-19 13:38:27 +0000
@@ -0,0 +1,133 @@
1from __future__ import unicode_literals
2
3import json
4import os
5
6import requests # fades
7import stripe # fades
8
9from .helpers import (
10 SCA_ROOT_URL,
11 APITestCase,
12 authenticate_with_macaroon,
13)
14
15
16def create_customer(stripe_token, auth=None):
17 headers = {
18 'Content-Type': 'application/json',
19 }
20 if auth is not None:
21 headers['Authorization'] = auth
22 data = {}
23 if stripe_token is not None:
24 data['stripe_token'] = stripe_token
25 url = '{}/purchases/v1/customers'.format(SCA_ROOT_URL)
26 response = requests.post(url, data=json.dumps(data), headers=headers)
27 return response
28
29
30def get_stripe_token():
31 stripe.api_key = os.getenv('STRIPE_PUBLISHABLE_KEY', None)
32 token = stripe.Token.create(card={
33 'number': '4111111111111111',
34 'cvc': '1234',
35 'exp_year': 2032,
36 'exp_month': 12,
37 })
38 return token.id
39
40
41def get_customer(auth=None):
42 headers = {
43 'Content-Type': 'application/json',
44 'Cache-Control': 'no-cache',
45 }
46 if auth is not None:
47 headers['Authorization'] = auth
48 url = '{}/purchases/v1/customers/me'.format(SCA_ROOT_URL)
49 response = requests.get(url, headers=headers)
50 return response
51
52
53class CreateCustomerTestCase(APITestCase):
54
55 @classmethod
56 def setUpClass(cls):
57 super(CreateCustomerTestCase, cls).setUpClass()
58
59 cls.auth = authenticate_with_macaroon()
60
61 def assert_invalid_request(self, response, message=None, extra=None):
62 self.assertEqual(response.status_code, 400)
63 body = response.json()
64 if message is None:
65 message = "The 'stripe_token' field is required."
66 if extra is None:
67 extra = {'field': 'stripe_token'}
68 self.assertEqual(body, {
69 'error_list': [
70 {'code': 'invalid-field',
71 'message': message,
72 'extra': extra},
73 ]
74 })
75
76 def assert_customer(self, response):
77 self.assertEqual(response.status_code, 200)
78 body = response.json()
79 self.assertIn('accepted_tos_date', body)
80 self.assertIn('latest_tos_date', body)
81 self.assertIn('latest_tos_accepted', body)
82
83 def test_require_authentication(self):
84 response = create_customer(stripe_token='token')
85 self.assert_require_auth(response)
86
87 def test_create_customer_missing_token(self):
88 response = create_customer(stripe_token=None, auth=self.auth)
89 self.assert_invalid_request(response)
90
91 def test_create_customer_invalid_token(self):
92 response = create_customer(stripe_token='', auth=self.auth)
93 message = "The 'stripe_token' field can not be empty."
94 self.assert_invalid_request(response, message)
95
96 def test_create_customer_bad_token(self):
97 response = create_customer(stripe_token='invalid', auth=self.auth)
98 message = "'invalid' is not a valid stripe_token."
99 extra = {'field': 'stripe_token', 'value': 'invalid'}
100 self.assert_invalid_request(response, message, extra)
101
102 def test_create_customer_success(self):
103 token = get_stripe_token()
104 response = create_customer(stripe_token=token, auth=self.auth)
105 self.assert_customer(response)
106
107
108class GetCustomerTestCase(APITestCase):
109
110 @classmethod
111 def setUpClass(cls):
112 super(GetCustomerTestCase, cls).setUpClass()
113
114 cls.auth = authenticate_with_macaroon()
115
116 def assert_customer(self, response):
117 data = response.json()
118 self.assertIn('latest_tos_date', data)
119 self.assertIn('accepted_tos_date', data)
120 self.assertIn('latest_tos_accepted', data)
121
122 def test_require_authentication(self):
123 response = get_customer()
124 self.assert_require_auth(response)
125
126 def test_get_customer_successful(self):
127 response = get_customer(auth=self.auth)
128 self.assert_success(response)
129 self.assert_customer(response)
130
131 def test_get_customer_bad_auth(self):
132 response = get_customer(auth='invalid')
133 self.assert_require_auth(response)
0134
=== added file 'tests/api/snap/test_snap_purchases_orders.py'
--- tests/api/snap/test_snap_purchases_orders.py 1970-01-01 00:00:00 +0000
+++ tests/api/snap/test_snap_purchases_orders.py 2016-09-19 13:38:27 +0000
@@ -0,0 +1,128 @@
1from __future__ import unicode_literals
2
3import json
4
5import requests # fades
6
7from .helpers import (
8 SCA_ROOT_URL,
9 APITestCase,
10 authenticate_with_macaroon,
11)
12from .test_snap_purchases_customers import (
13 create_customer,
14 get_stripe_token,
15)
16
17
18SNAP_PACKAGE_SNAP_ID = 'A0KkM9O8Aoht03uQqKwGB7JEZAbTPwnu'
19
20
21def create_order(snap_id, currency=None, amount=None, auth=None):
22 headers = {
23 'Content-Type': 'application/json',
24 }
25 if auth is not None:
26 headers['Authorization'] = auth
27 data = {}
28 if snap_id is not None:
29 data['snap_id'] = snap_id
30 if currency is not None:
31 data['currency'] = currency
32 if amount is not None:
33 data['amount'] = amount
34 url = '{}/purchases/v1/orders'.format(SCA_ROOT_URL)
35 response = requests.post(url, data=json.dumps(data), headers=headers)
36 return response
37
38
39def get_orders(auth=None):
40 headers = {
41 'Content-Type': 'application/json',
42 }
43 if auth is not None:
44 headers['Authorization'] = auth
45 url = '{}/purchases/v1/orders'.format(SCA_ROOT_URL)
46 response = requests.get(url, headers=headers)
47 return response
48
49
50class OrdersTestCase(APITestCase):
51
52 @classmethod
53 def setUpClass(cls):
54 super(OrdersTestCase, cls).setUpClass()
55
56 cls.auth = authenticate_with_macaroon(permissions=['package_purchase'])
57
58 def assert_order_data(self, order):
59 self.assertEqual(order['snap_id'], SNAP_PACKAGE_SNAP_ID)
60 self.assertEqual(order['state'], 'Complete')
61 self.assertEqual(order['currency'], 'USD')
62 self.assertIn('amount', order)
63 self.assertIsNotNone(order['amount'])
64
65
66class CreateOrderTestCase(OrdersTestCase):
67
68 def assert_invalid_request(self, response, message=None, extra=None):
69 self.assertEqual(response.status_code, 400)
70 body = response.json()
71 if message is None:
72 message = "The 'snap_id' field is required."
73 if extra is None:
74 extra = {'field': 'snap_id'}
75 self.assertEqual(body, {
76 'error_list': [
77 {'code': 'invalid-field',
78 'message': message,
79 'extra': extra}
80 ]
81 })
82
83 def assert_order(self, response):
84 self.assertEqual(response.status_code, 200)
85 body = response.json()
86 self.assert_order_data(body)
87
88 def test_require_authentication(self):
89 response = create_order(snap_id='snap-id')
90 self.assert_require_auth(response)
91
92 def test_create_order_missing_snap_id(self):
93 response = create_order(snap_id=None, auth=self.auth)
94 self.assert_invalid_request(response)
95
96 def test_create_order_invalid_currency(self):
97 response = create_order(snap_id='snap-id', currency='EUR',
98 auth=self.auth)
99 message = "'EUR' is not a valid currency."
100 extra = {'field': 'currency', 'value': 'EUR'}
101 self.assert_invalid_request(response, message, extra)
102
103 def test_create_order_success(self):
104 token = get_stripe_token()
105 response = create_customer(stripe_token=token, auth=self.auth)
106 assert response.status_code == 200
107 response = create_order(snap_id=SNAP_PACKAGE_SNAP_ID, auth=self.auth)
108 self.assert_order(response)
109
110
111class GetOrdersTestCase(OrdersTestCase):
112
113 def assert_orders(self, response):
114 self.assertEqual(response.status_code, 200)
115 body = response.json()
116 self.assertIn('orders', body)
117 orders = body['orders']
118 if orders:
119 order = body['orders'][-1]
120 self.assert_order_data(order)
121
122 def test_require_authentication(self):
123 response = get_orders()
124 self.assert_require_auth(response)
125
126 def test_get_orders(self):
127 response = get_orders(auth=self.auth)
128 self.assert_orders(response)

Subscribers

People subscribed via source and target branches

to all changes: