|
17 | 17 | import os |
18 | 18 |
|
19 | 19 | import main |
20 | | -from testing import CloudTest |
21 | | - |
22 | | - |
23 | | -class PubSubTest(CloudTest): |
24 | | - def setUp(self): |
25 | | - super(PubSubTest, self).setUp() |
26 | | - main.app.testing = True |
27 | | - self.client = main.app.test_client() |
28 | | - |
29 | | - def test_index(self): |
30 | | - r = self.client.get('/') |
31 | | - self.assertEqual(r.status_code, 200) |
32 | | - |
33 | | - def test_post_index(self): |
34 | | - r = self.client.post('/', data={'payload': 'Test payload'}) |
35 | | - self.assertEqual(r.status_code, 200) |
36 | | - |
37 | | - def test_push_endpoint(self): |
38 | | - url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN'] |
39 | | - |
40 | | - r = self.client.post( |
41 | | - url, |
42 | | - data=json.dumps({ |
43 | | - "message": { |
44 | | - "data": base64.b64encode( |
45 | | - u'Test message'.encode('utf-8') |
46 | | - ).decode('utf-8') |
47 | | - } |
48 | | - }) |
49 | | - ) |
50 | | - |
51 | | - self.assertEqual(r.status_code, 200) |
52 | | - |
53 | | - # Make sure the message is visible on the home page. |
54 | | - r = self.client.get('/') |
55 | | - self.assertEqual(r.status_code, 200) |
56 | | - self.assertTrue('Test message' in r.data.decode('utf-8')) |
57 | | - |
58 | | - def test_push_endpoint_errors(self): |
59 | | - # no token |
60 | | - r = self.client.post('/pubsub/push') |
61 | | - self.assertEqual(r.status_code, 400) |
62 | | - |
63 | | - # invalid token |
64 | | - r = self.client.post('/pubsub/push?token=bad') |
65 | | - self.assertEqual(r.status_code, 400) |
| 20 | +import pytest |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def client(): |
| 25 | + main.app.testing = True |
| 26 | + return main.app.test_client() |
| 27 | + |
| 28 | + |
| 29 | +def test_index(client): |
| 30 | + r = client.get('/') |
| 31 | + assert r.status_code == 200 |
| 32 | + |
| 33 | + |
| 34 | +def test_post_index(client): |
| 35 | + r = client.post('/', data={'payload': 'Test payload'}) |
| 36 | + assert r.status_code == 200 |
| 37 | + |
| 38 | + |
| 39 | +def test_push_endpoint(client): |
| 40 | + url = '/pubsub/push?token=' + os.environ['PUBSUB_VERIFICATION_TOKEN'] |
| 41 | + |
| 42 | + r = client.post( |
| 43 | + url, |
| 44 | + data=json.dumps({ |
| 45 | + "message": { |
| 46 | + "data": base64.b64encode( |
| 47 | + u'Test message'.encode('utf-8') |
| 48 | + ).decode('utf-8') |
| 49 | + } |
| 50 | + }) |
| 51 | + ) |
| 52 | + |
| 53 | + assert r.status_code == 200 |
| 54 | + |
| 55 | + # Make sure the message is visible on the home page. |
| 56 | + r = client.get('/') |
| 57 | + assert r.status_code == 200 |
| 58 | + assert 'Test message' in r.data.decode('utf-8') |
| 59 | + |
| 60 | + |
| 61 | +def test_push_endpoint_errors(client): |
| 62 | + # no token |
| 63 | + r = client.post('/pubsub/push') |
| 64 | + assert r.status_code == 400 |
| 65 | + |
| 66 | + # invalid token |
| 67 | + r = client.post('/pubsub/push?token=bad') |
| 68 | + assert r.status_code == 400 |
0 commit comments