/
test_microservice.py
135 lines (94 loc) · 12.7 KB
/
test_microservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pytest
from run import app, create_tables
@pytest.fixture
def client():
app.config['TESTING'] = True
client = app.test_client()
with app.app_context():
create_tables()
yield client
def test_login(client):
resp = client.post('/login', json=dict(username='nevil', password='longbottom'))
assert resp.status_code == 200
def test_login_wrong_password(client):
resp = client.post('/login', json=dict(username='nevil', password='granger'))
assert resp.status_code == 401
def test_login_wrong_username(client):
username = 'hermioneeeee'
resp = client.post('/login', json=dict(username=username, password='granger'))
assert resp.status_code == 200
assert resp.json == {'message': f'User {username} doesn\'t exist'}
def test_login_missing_data(client):
resp = client.post('/login', json=dict(username='hermione'))
assert resp.status_code == 400
def test_access_token_wrong(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
refresh_token = resp.json['refresh_token']
resp = client.post('/jsonpatch',
headers={'Authorization': f'Bearer {refresh_token}'},
json=dict(json_object='{"foo": "bar"}',
json_patch='[{"op": "add", "path": "/baz", "value": "qux"}]'))
assert resp.status_code == 422
def test_refresh_token(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
refresh_token = resp.json['refresh_token']
resp = client.post('/token/refresh',
headers={'Authorization': f'Bearer {refresh_token}'})
assert resp.status_code == 200
assert access_token != resp.json['access_token']
def test_refresh_wrong(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
refresh_token = resp.json['refresh_token']
resp = client.post('/token/refresh',
headers={'Authorization': f'Bearer {access_token}'})
assert resp.status_code == 422
def test_json_patch(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
resp = client.post('/jsonpatch',
headers={'Authorization': f'Bearer {access_token}'},
json=dict(json_object='{"foo": "bar"}',
json_patch='[{"op": "add", "path": "/baz", "value": "qux"}]'))
assert resp.status_code == 200
assert resp.json == {'foo': 'bar', 'baz': 'qux'}
def test_json_patch_invalid_patch(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
resp = client.post('/jsonpatch',
headers={'Authorization': f'Bearer {access_token}'},
json=dict(json_object='{"foo": "bar"}',
json_patch='[{"op": "add", "path": "baz", "value": "qux"}]'))
assert resp.status_code == 400
assert resp.get_data() == b'Invalid JSON Patch'
def test_thumbnail_png(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
resp = client.post('/thumbnail',
headers={'Authorization': f'Bearer {access_token}'},
json=dict(image_url='https://miro.medium.com/max/569/1*0G5zu7CnXdMT9pGbYUTQLQ.png'))
assert resp.status_code == 200
img_data = resp.get_data()
# with open('image.png', 'wb') as f:
# f.write(img_data)
assert img_data == b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x002\x00\x00\x00\x1b\x08\x02\x00\x00\x00\xb1\xfeK\x18\x00\x00\x06\xb9IDATx\x9c\xcd\x96YL\x13]\x1b\xc7\xcfL[\xba\xa66\x01j\xb5\xa1"[\x94\xcdP\t\x10-KH\nh"\xa8$FE\x03b\x887%D\x91\x0b\xb9 \x045!n7\x12.\x88\xa0&DSP\xa21m#EQ\xa3\x05W\n%m\x81\xd6\x14\xdaB\xb1\xad],\xb6\xd3e\xcew1o\x88/`>\xde7|\xf8\xfd\xaff\x9es\xe6\x9c\xdf<\xcf\xff,\x00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e9tO\x9f>\xc5q<\x12\x89\xd8\xed\xf6\xe1\xe1\xe1\xdf\r\xfa\xe2\xc5\x0b.\x97\xbb\xb4\xb4\x04!\x0c\x85B\xdd\xdd\xdd\x10B\x8b\xc5\x92\x94\x94d\xb3\xd9\xd6\x83\x05!\x14\x89D}}}\xab\xe3hnn\xae@ \xa0\xd1h"\x91\xa8\xb2\xb2\xf2\xe1\xc3\x87;v\xec@\x10\x04EQ&\x93\xf9\xfd\xfb\xf7\xdf\x96\x1fA(\x14\n\xf1\x8caXmm-QM\x14E\xd7m!\xb0<\xc2\n\x91\xe3\xe2\xe2\xf2\xf3\xf3\x95J\xa5V\xab\x95\xc9d\x05\x05\x05\x1c\x0e\xc7\xe5r\xcd\xce\xceF"\x11\x9f\xcf\x07!D\x10dM\xac`0866\x86 \x88L&kkk#\x91H\x08\x82D"\x91p8\x0c\x00\xf0\xfb\xfdr\xb9\\\xadV\xb3\xd9\xec\x86\x86\x86\xa8\xa8(\xb7\xdb\xfd\xe0\xc1\x03\xab\xd5\x1a\x1d\x1d\xdd\xd0\xd0@"\x91 \x84d2Y\xab\xd5>{\xf6\x8cN\xa7\x8bD"\xa1P\x08\x00@Q\x14\x15\x8b\xc5\xfb\xf7\xef7\x1a\x8d\x00\x80\xc9\xc9\xc9\xf6\xf6\xf6\xce\xceN\x04A\xacV+\x89D"L\xbd&\x16\x86a*\x95\xaa\xbf\xbf\x7fjj\x8aD"\x11\xf1\xbf\xcc\x01@WW\x97B\xa1\xa8\xae\xae\xee\xe8\xe8\x98\x98\x98\x00\x00\\\xbf~}ffF"\x918\x9d\xce\xe5:P\xa9T\x8f\xc7322\x92\x92\x92"\x10\x08\xc0\xf2(ccc\x87\x0e\x1d\x8a\x8a\x8a*--\xad\xa8\xa88\x7f\xfe\xbc\xd7\xeb\x85\x10\xe28>::j0\x18\xd6\xb4\xc5\xcb\x97/\xf9|>\x86a\x10\xc2\xd7\xaf_\x13A\xb3\xd9\x9c\x90\x90`6\x9b\t\xc3\xd9\xed\xf6\xb7o\xdf&&&\x8e\x8e\x8eB\x08{{{\xf9|\xfe\x85\x0b\x17\xe6\xe6\xe6\x88\xfeb\xb1\xb8\xb2\xb22%%evv\xf6o\xde\x02\x00ddddggS\xa9\xd4\xf7\xef\xdf\x97\x95\x95edd\x10\x85 \x96\xc2\xd4\xd4\xd4\x9a\xd9"\xd2\x13\n\x85\x00\x00\x05\x05\x05D\x10E\xd1e\xcfuww_\xbdz5\x10\x080\x18\x0c:\x9d\x0e\x00\xa8\xaa\xaa\x1a\x18\x18\xf8\xfa\xf5kQQ\x11q\xb2\x85\xc3\xe1\xe4\xe4\xe4\x9c\x9c\x9c\xc6\xc6\xc6_\x07G\x89\t\xca\xcb\xcb\x05\x02\x81\xd7\xeb\xed\xe8\xe8\xd0\xe9t\'O\x9e\xac\xae\xae\x9e\x98\x98`\xb3\xd9\xcf\x9f?\xf7\xf9|+\x98"\x91\x88^\xaf\xb7\xdb\xed\x93\x93\x93\x18\x86-\xc7\x17\x16\x16l6\x9bF\xa3\xc10\xec\xf2\xe5\xcb111[\xb7n\xf5x<R\xa9\xd4\xe5r]\xbcx\x91L&\xdf\xbe}\x9bF\xa3\xb9\xddn\x9f\xcf777\x17\x1b\x1b{\xe3\xc6\r\x8dFs\xf6\xecY\xa3\xd1\x88\xe3\xf8_X\x00\x80\xcc\xcc\xcc\xaa\xaa*\x81@0==\x1d\x0c\x06\x0b\x0b\x0b\x1f=z\xa4\xd3\xe98\x1cN~~\xfe\xabW\xafV`---\xe18\xde\xde\xde\xfe\xe9\xd3\'\x87\xc3\xb1\x1c\xb7Z\xad---\x06\x83!\x10\x08tww;\x9dN\x83\xc1p\xe5\xca\x15>\x9fO\xa3\xd1\xf2\xf2\xf2\xa4RiOO\xcf\xcd\x9b7SSS\xb5Z\xed\xf1\xe3\xc7\x11\x04\xf1z\xbd2\x99\x8c\xc9d\x0e\r\r\x11\xcb\x05,\x97\xd3`0\xd4\xd6\xd6\xb2\xd9\xec}\xfb\xf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3\xad\x0b\xebW\x85\xc3a\x0c\xc3\xc2\xe1p \x10\xc00,\x14\nEEEm\xd9\xb2\x85\xc5b\xfd\xa3\xfb\xcc\x06cm\x9a\xfe\x03\xaa\xb8Q\xa3s\xc2\x19$\x00\x00\x00\x00IEND\xaeB`\x82'
def test_thumbnail_jpg(client):
resp = client.post('/login', json=dict(username='hermione', password='granger'))
assert resp.status_code == 200
access_token = resp.json['access_token']
resp = client.post('/thumbnail',
headers={'Authorization': f'Bearer {access_token}'},
json=dict(image_url='https://www.fullstackpython.com/img/logos/flask.jpg'))
assert resp.status_code == 200
img_data = resp.get_data()
# with open('image.jpg', 'wb') as f:
# f.write(img_data)
assert img_data == b'\xff\xd8\xff\xe0\x00\x10JFIF\x00\x01\x01\x00\x00\x01\x00\x01\x00\x00\xff\xdb\x00C\x00\x08\x06\x06\x07\x06\x05\x08\x07\x07\x07\t\t\x08\n\x0c\x14\r\x0c\x0b\x0b\x0c\x19\x12\x13\x0f\x14\x1d\x1a\x1f\x1e\x1d\x1a\x1c\x1c $.\' ",#\x1c\x1c(7),01444\x1f\'9=82<.342\xff\xdb\x00C\x01\t\t\t\x0c\x0b\x0c\x18\r\r\x182!\x1c!22222222222222222222222222222222222222222222222222\xff\xc0\x00\x11\x08\x00\x13\x002\x03\x01"\x00\x02\x11\x01\x03\x11\x01\xff\xc4\x00\x1f\x00\x00\x01\x05\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x10\x00\x02\x01\x03\x03\x02\x04\x03\x05\x05\x04\x04\x00\x00\x01}\x01\x02\x03\x00\x04\x11\x05\x12!1A\x06\x13Qa\x07"q\x142\x81\x91\xa1\x08#B\xb1\xc1\x15R\xd1\xf0$3br\x82\t\n\x16\x17\x18\x19\x1a%&\'()*456789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xc4\x00\x1f\x01\x00\x03\x01\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\xff\xc4\x00\xb5\x11\x00\x02\x01\x02\x04\x04\x03\x04\x07\x05\x04\x04\x00\x01\x02w\x00\x01\x02\x03\x11\x04\x05!1\x06\x12AQ\x07aq\x13"2\x81\x08\x14B\x91\xa1\xb1\xc1\t#3R\xf0\x15br\xd1\n\x16$4\xe1%\xf1\x17\x18\x19\x1a&\'()*56789:CDEFGHIJSTUVWXYZcdefghijstuvwxyz\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x92\x93\x94\x95\x96\x97\x98\x99\x9a\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xff\xda\x00\x0c\x03\x01\x00\x02\x11\x03\x11\x00?\x00\xe9<?\xae\xeb\xda\xe7\x8b5\x9dN\xd2\xe9\xe2\x86)v=\x9f\x93\x94E\x8b\x82\x87q\x04\xb9\xeeT\x1c\x1flW\xa3\xea-t|3x\xf2H \xb9\x16\xd26\xfbg?+\x05$\x15$};W\x9b\xf8\xae;\xbf\x02x\xb6\xe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