mp_message/tests/test_auth.py

35 lines
946 B
Python

import threading
import jwt
import uvicorn
from config import Config
from main import app
from httpx import Client
server_thread = threading.Thread(target=uvicorn.run, args=(app,), daemon=True,
kwargs={"host": "127.0.0.1", "port": 8000, "reload": False})
server_thread.start()
client = Client(base_url="http://127.0.0.1:8000")
def test_auth_no_username():
resp = client.post('/api/v1/auth')
assert resp.status_code == 422
def test_auth_incorrect_username():
resp = client.post('/api/v1/auth', json={'username': -1})
assert resp.status_code == 401
def test_auth_correct_username():
resp = client.post('/api/v1/auth', json={'username': 1})
assert resp.status_code == 200
assert 'access_token' in resp.json()
assert len(resp.json()['access_token']) > 0
data = jwt.decode(resp.json()['access_token'], Config.secret, algorithms=['HS256'])
assert data["user_id"] == 2