79 lines
4.0 KiB
Python
79 lines
4.0 KiB
Python
from __future__ import annotations
|
|
|
|
import unittest
|
|
from unittest.mock import patch
|
|
from unittest.mock import AsyncMock
|
|
import base64
|
|
|
|
from docx import Document
|
|
from io import BytesIO
|
|
|
|
from test_api import ApiTestCase, auth, register
|
|
from server import recruit
|
|
|
|
|
|
RAW = {
|
|
"gonggaoId": 1,
|
|
"gonggaoTitle": "测试公告",
|
|
"gonggaoContent": '<p>第一段</p><img src="https://example.com/a.png"><div>第二段</div>',
|
|
"companyname": "测试单位",
|
|
"createTime": "2026-11-01 10:00:00",
|
|
"checkTime": "2026-11-02 10:00:00",
|
|
"validTime": "2027-01-01 00:00:00",
|
|
"infoFrom": "测试来源",
|
|
"iswailian": 0,
|
|
"wailian": "",
|
|
"url": "",
|
|
}
|
|
|
|
PNG = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=")
|
|
|
|
|
|
class RecruitTests(ApiTestCase):
|
|
async def test_sync_list_detail_and_exports(self):
|
|
self.assertEqual((await self.client.get('/api/recruit/announcements', headers=auth('x'))).status_code, 401)
|
|
owner = await register(self.client, 'recruiter')
|
|
payload = {"code": 0, "rows": [RAW], "extra": {"pages": 1}}
|
|
fetch = AsyncMock(return_value=payload)
|
|
with patch.object(recruit, 'fetch_page', new=fetch):
|
|
data = (await self.client.post('/api/recruit/sync', headers=auth(owner['token']))).json()
|
|
self.assertEqual(data, {'fetched': 1, 'added': 1, 'updated': 0})
|
|
detail = await self.client.get('/api/recruit/announcements/1', headers=auth(owner['token']))
|
|
self.assertEqual(detail.json()['content_html'], '<p>第一段</p><img src="https://example.com/a.png"><div>第二段</div>')
|
|
rows = (await self.client.get('/api/recruit/announcements', headers=auth(owner['token']))).json()
|
|
self.assertEqual(rows['total'], 1)
|
|
self.assertEqual(rows['items'][0]['gonggao_id'], 1)
|
|
self.assertTrue(rows['items'][0]['detail_url'].endswith('/1'))
|
|
detail = (await self.client.get('/api/recruit/announcements/1', headers=auth(owner['token']))).json()
|
|
self.assertEqual(detail['title'], '测试公告')
|
|
with patch.object(recruit, 'fetch_image', new=AsyncMock(return_value=PNG)):
|
|
response = await self.client.get('/api/recruit/export', params={'ids': '1', 'format': 'docx'}, headers=auth(owner['token']))
|
|
self.assertEqual(response.status_code, 200)
|
|
document = Document(BytesIO(response.content))
|
|
self.assertIn('第一段', '\n'.join(p.text for p in document.paragraphs))
|
|
self.assertEqual(len(document.inline_shapes), 1)
|
|
response = await self.client.get('/api/recruit/export', params={'ids': '1', 'format': 'markdown'}, headers=auth(owner['token']))
|
|
self.assertIn('第一段', response.text)
|
|
self.assertIn('', response.text)
|
|
self.assertIn('原文链接', response.text)
|
|
response = await self.client.get('/api/recruit/export', headers=auth(owner['token']))
|
|
self.assertEqual(response.status_code, 422)
|
|
|
|
async def test_sync_fetches_detail_for_empty_content(self):
|
|
owner = await register(self.client, 'recruit-detail')
|
|
raw = dict(RAW, gonggaoId=2, gonggaoContent='<p><br></p>')
|
|
fetch = AsyncMock(return_value={"code": 0, "rows": [raw], "extra": {"pages": 1}})
|
|
fetch_detail = AsyncMock(return_value='''
|
|
<div class="txt"><p><br></p></div>
|
|
<div class="selected-positions">
|
|
<span>职位列表</span>
|
|
<table><tr><td>外贸业务员</td></tr></table>
|
|
</div>
|
|
''')
|
|
with patch.object(recruit, 'fetch_page', new=fetch), patch.object(recruit, 'fetch_detail_html', new=fetch_detail):
|
|
data = (await self.client.post('/api/recruit/sync', headers=auth(owner['token']))).json()
|
|
self.assertEqual(data, {'fetched': 1, 'added': 1, 'updated': 0})
|
|
fetch_detail.assert_awaited_once()
|
|
detail = (await self.client.get('/api/recruit/announcements/2', headers=auth(owner['token']))).json()
|
|
self.assertIn('外贸业务员', detail['content_html'])
|