改用流式鉴权播放设备教程视频
This commit is contained in:
@@ -47,6 +47,51 @@ def auth(token: str) -> dict:
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
class AuthCookieApiTest(unittest.IsolatedAsyncioTestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.directory = Path(tempfile.gettempdir()) / f"worklog-test-cookie-{uuid4().hex}"
|
||||
cls.directory.mkdir()
|
||||
with patch.dict(os.environ, {
|
||||
"WORKLOG_DB": str(cls.directory / "test.db"),
|
||||
"WORKLOG_PORT": "34199",
|
||||
}):
|
||||
import server.main as m
|
||||
cls.m = m
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
import shutil
|
||||
if cls.directory.resolve().parent != Path(tempfile.gettempdir()).resolve():
|
||||
raise RuntimeError("Refusing to remove a test directory outside the temp root")
|
||||
shutil.rmtree(cls.directory)
|
||||
|
||||
def setUp(self):
|
||||
self.patch_dev = patch.object(self.m, "DEV_MODE", True)
|
||||
self.patch_dev.start()
|
||||
self.engine = self.m.create_async_engine("sqlite+aiosqlite:///:memory:", future=True)
|
||||
self.sessions = self.m.async_sessionmaker(self.engine, expire_on_commit=False)
|
||||
self.patch_session = patch.object(self.m, "SessionLocal", self.sessions)
|
||||
self.patch_session.start()
|
||||
|
||||
def tearDown(self):
|
||||
self.patch_session.stop()
|
||||
self.patch_dev.stop()
|
||||
|
||||
async def asyncSetUp(self):
|
||||
async with self.engine.begin() as conn:
|
||||
await conn.run_sync(self.m.metadata.create_all)
|
||||
self.client = AsyncClient(transport=ASGITransport(app=self.m.app), base_url="http://localhost")
|
||||
|
||||
async def asyncTearDown(self):
|
||||
await self.client.aclose()
|
||||
|
||||
async def test_bearer_or_cookie_can_authenticate(self):
|
||||
user = await register(self.client, 'cookietester')
|
||||
self.assertEqual((await self.client.get('/api/auth/me', headers=auth(user['token']))).status_code, 200)
|
||||
self.assertEqual((await self.client.get('/api/auth/me', cookies={'worklog_token': user['token']})).status_code, 200)
|
||||
|
||||
|
||||
class ApiTestCase(unittest.IsolatedAsyncioTestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
|
||||
Reference in New Issue
Block a user