|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +import sys |
| 4 | + |
| 5 | +import pytest |
| 6 | +from faker import Faker |
| 7 | +from httpx import AsyncClient |
| 8 | + |
| 9 | +sys.path.append('../../') |
| 10 | + |
| 11 | +from backend.app.core.conf import settings # noqa: E402 |
| 12 | +from backend.app.main import app # noqa: E402 |
| 13 | +from backend.app.common.redis import redis_client # noqa: E402 |
| 14 | + |
| 15 | + |
| 16 | +class TestAuth: |
| 17 | + pytestmark = pytest.mark.anyio |
| 18 | + faker = Faker(locale='zh_CN') |
| 19 | + users_api_base_url = f'http://{settings.UVICORN_HOST}:{settings.UVICORN_PORT}/v1/auth/users' |
| 20 | + |
| 21 | + @property |
| 22 | + async def get_token(self): |
| 23 | + token = await redis_client.get('test_token') |
| 24 | + return token |
| 25 | + |
| 26 | + async def test_login(self): |
| 27 | + async with AsyncClient( |
| 28 | + app=app, headers={'accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'} |
| 29 | + ) as client: |
| 30 | + response = await client.post( |
| 31 | + url=f'{self.users_api_base_url}/login', data={'username': '1', 'password': '1'} |
| 32 | + ) |
| 33 | + assert response.status_code == 200 |
| 34 | + assert response.json()['token_type'] == 'Bearer' |
| 35 | + |
| 36 | + async def test_register(self): |
| 37 | + async with AsyncClient( |
| 38 | + app=app, headers={'accept': 'application/json', 'Content-Type': 'application/json'} |
| 39 | + ) as client: |
| 40 | + response = await client.post( |
| 41 | + url=f'{self.users_api_base_url}/register', |
| 42 | + json={ |
| 43 | + 'username': f'{self.faker.user_name()}', |
| 44 | + 'password': f'{self.faker.password()}', |
| 45 | + 'email': f'{self.faker.email()}', |
| 46 | + }, |
| 47 | + ) |
| 48 | + assert response.status_code == 200 |
| 49 | + r_json = response.json() |
| 50 | + assert r_json['code'] == 200 |
| 51 | + assert r_json['msg'] == 'Success' |
| 52 | + |
| 53 | + async def test_get_userinfo(self): |
| 54 | + async with AsyncClient( |
| 55 | + app=app, headers={'accept': 'application/json', 'Authorization': f'Bearer {await self.get_token}'} |
| 56 | + ) as client: |
| 57 | + response = await client.get(url=f'{self.users_api_base_url}/1') |
| 58 | + assert response.status_code == 200 |
| 59 | + r_json = response.json() |
| 60 | + assert r_json['code'] == 200 |
| 61 | + assert r_json['msg'] == 'Success' |
| 62 | + |
| 63 | + async def test_get_all_users(self): |
| 64 | + async with AsyncClient( |
| 65 | + app=app, headers={'accept': 'application/json', 'Authorization': f'Bearer {await self.get_token}'} |
| 66 | + ) as client: |
| 67 | + response = await client.get(url=f'{self.users_api_base_url}?page=1&size=20') |
| 68 | + assert response.status_code == 200 |
| 69 | + r_json = response.json() |
| 70 | + assert isinstance(r_json['data'], list) |
| 71 | + assert isinstance(r_json['links'], dict) |
| 72 | + assert isinstance(r_json['links']['self'], str) |
0 commit comments