This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
import json
|
||||
import base64
|
||||
import time
|
||||
import uuid
|
||||
|
||||
url = 'http://localhost:8080/api/v1'
|
||||
|
||||
def call(m, p, b=None, h=None):
|
||||
req = urllib.request.Request(url+p, method=m)
|
||||
if b:
|
||||
req.data = json.dumps(b).encode()
|
||||
req.add_header('Content-Type','application/json')
|
||||
if h:
|
||||
for k,v in h.items(): req.add_header(k,v)
|
||||
try:
|
||||
return urllib.request.urlopen(req).status, json.loads(urllib.request.urlopen(req).read()) if m=='GET' else None
|
||||
except urllib.error.HTTPError as e:
|
||||
return e.code, None
|
||||
|
||||
u = f'test_{uuid.uuid4().hex[:6]}'
|
||||
call('POST', '/users', {'username':u, 'display_name':'Test', 'password':'TestPassword123!'})
|
||||
cred = base64.b64encode(f'{u}:TestPassword123!'.encode()).decode()
|
||||
|
||||
req = urllib.request.Request(url+'/auth/sessions', method='POST', headers={'Authorization': f'Basic {cred}'})
|
||||
res = json.loads(urllib.request.urlopen(req).read())
|
||||
token = res['token']
|
||||
print(f'Токен получен: {token[:10]}...')
|
||||
print('Ждем 3 секунды...')
|
||||
time.sleep(3)
|
||||
|
||||
try:
|
||||
req2 = urllib.request.Request(url+'/users/me', headers={'Authorization': f'Bearer {token}'})
|
||||
urllib.request.urlopen(req2)
|
||||
print('FAIL: Токен всё еще работает (200)')
|
||||
except urllib.error.HTTPError as e:
|
||||
if e.code == 401:
|
||||
print('PASS: Токен истек, получили 401!')
|
||||
else:
|
||||
print(f'FAIL: Ожидали 401, получили {e.code}')
|
||||
Reference in New Issue
Block a user