|
|
@@ -0,0 +1,150 @@
|
|
|
+/**
|
|
|
+ * guardWorker 单身份守卫单元测试(train-frontend)
|
|
|
+ *
|
|
|
+ * 覆盖三组(对应派单任务 A / B / C):
|
|
|
+ * A 网络异常不误判:网络抖动放行、已开通放行;仅「明确无档案/无身份」才拒绝。
|
|
|
+ * B 非 worker 身份拒绝:普通用户 / org 用户 / 404 / 无身份 → 拒绝且不抛未捕获异常。
|
|
|
+ * C 已建档标记快路径:本地 role|healthServiceProfile 命中 → 放行且 0 次网络请求。
|
|
|
+ *
|
|
|
+ * 双身份隔离(页面层退化断言 —— 见文末 describe):
|
|
|
+ * 页面接入形如:onLoad/onShow 首行 guardPage() 后接
|
|
|
+ * guardWorker().then(ok => { if (!ok) return; ...loadData() })
|
|
|
+ * 因 train-frontend 未安装 @vue/test-utils / vue-jest(.vue 无法在 jest 直接编译),
|
|
|
+ * 页面级隔离逻辑完全由 guardWorker 分支决定,故直接断言 guardWorker 的两个身份分支,
|
|
|
+ * 等价覆盖页面隔离契约(CERT-TC-005 worker 可见管理动作 / CERT-TC-006 非 worker 被拦截)。
|
|
|
+ *
|
|
|
+ * 约束:uni-app Vue2,禁 TypeScript / 可选链 `?.` / CSS Grid。
|
|
|
+ */
|
|
|
+
|
|
|
+// 拦截 guard.js 依赖的档案接口,避免真实网络(模块名 @/api/practitioner.js 经 jest.config 别名解析)
|
|
|
+jest.mock('@/api/practitioner.js', () => ({
|
|
|
+ getMyPractitioner: jest.fn()
|
|
|
+}))
|
|
|
+
|
|
|
+import { getMyPractitioner } from '@/api/practitioner.js'
|
|
|
+import { guardWorker } from '@/utils/guard.js'
|
|
|
+
|
|
|
+// ---- 全局环境模拟:uni / getApp(guard.js 运行期读取的全局)----
|
|
|
+var currentRole
|
|
|
+var currentStorage
|
|
|
+
|
|
|
+function buildUni() {
|
|
|
+ return {
|
|
|
+ getStorageSync: jest.fn(function (key) {
|
|
|
+ return Object.prototype.hasOwnProperty.call(currentStorage, key) ? currentStorage[key] : ''
|
|
|
+ }),
|
|
|
+ setStorageSync: jest.fn(),
|
|
|
+ showToast: jest.fn(),
|
|
|
+ switchTab: jest.fn(),
|
|
|
+ $emit: jest.fn()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+beforeEach(() => {
|
|
|
+ jest.useFakeTimers()
|
|
|
+ currentRole = 'student'
|
|
|
+ currentStorage = {}
|
|
|
+ global.uni = buildUni()
|
|
|
+ global.getApp = jest.fn(function () {
|
|
|
+ return { $store: { state: { role: currentRole }, getters: {} } }
|
|
|
+ })
|
|
|
+ getMyPractitioner.mockReset()
|
|
|
+})
|
|
|
+
|
|
|
+afterEach(() => {
|
|
|
+ jest.clearAllTimers()
|
|
|
+ delete global.uni
|
|
|
+ delete global.getApp
|
|
|
+})
|
|
|
+
|
|
|
+/* --------------------------- A 网络异常不误判 --------------------------- */
|
|
|
+describe('A 网络异常不误判:失败放行 + 重试成功放行', () => {
|
|
|
+ it('首次网络抖动(5xx)放行;重试返回已开通资料仍放行', async () => {
|
|
|
+ // 首次:非 404、无 档案/身份 语义的网络错误 → 放行,不误判为拒绝
|
|
|
+ getMyPractitioner.mockRejectedValueOnce({ code: 502, message: 'Bad Gateway' })
|
|
|
+ await expect(guardWorker()).resolves.toBe(true)
|
|
|
+
|
|
|
+ // 重试:返回已开通资料(status=approved)→ 放行
|
|
|
+ getMyPractitioner.mockResolvedValueOnce({ data: { status: 'approved' } })
|
|
|
+ await expect(guardWorker()).resolves.toBe(true)
|
|
|
+
|
|
|
+ expect(getMyPractitioner).toHaveBeenCalledTimes(2)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('网络超时放行,且不弹「暂未开通健康服务身份」拦截 toast', async () => {
|
|
|
+ getMyPractitioner.mockRejectedValueOnce({ code: 'ECONNABORTED', message: 'timeout of 5000ms exceeded' })
|
|
|
+ const ok = await guardWorker()
|
|
|
+ expect(ok).toBe(true)
|
|
|
+ expect(global.uni.showToast).not.toHaveBeenCalledWith(
|
|
|
+ expect.objectContaining({ title: '暂未开通健康服务身份' }))
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+/* --------------------- B 非 worker 身份拒绝 / 双身份隔离 --------------------- */
|
|
|
+describe('B 非 worker 身份拒绝:明确无档案/无身份才拦截,不抛未捕获异常', () => {
|
|
|
+ it('普通用户(pending 档案)被拒绝:返回拒绝态 + toast + 跳转「我的」', async () => {
|
|
|
+ getMyPractitioner.mockResolvedValueOnce({
|
|
|
+ data: { status: 'pending', serviceStatus: 'off', lifecycleStage: 'training', authLevel: 'none' }
|
|
|
+ })
|
|
|
+ await expect(guardWorker()).resolves.toBe(false)
|
|
|
+ jest.runAllTimers()
|
|
|
+ expect(global.uni.showToast).toHaveBeenCalledWith(
|
|
|
+ expect.objectContaining({ title: '暂未开通健康服务身份' }))
|
|
|
+ expect(global.uni.switchTab).toHaveBeenCalledWith({ url: '/pages/mine/index' })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('接口明确返回无档案(404)被拒绝', async () => {
|
|
|
+ getMyPractitioner.mockRejectedValueOnce({ code: 404, message: '无档案' })
|
|
|
+ await expect(guardWorker()).resolves.toBe(false)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('接口明确返回无身份被拒绝(message 含「身份」)', async () => {
|
|
|
+ getMyPractitioner.mockRejectedValueOnce({ code: 200, message: '未开通健康服务身份' })
|
|
|
+ await expect(guardWorker()).resolves.toBe(false)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+/* ------------------------ C 已建档标记快路径 ------------------------ */
|
|
|
+describe('C 已建档标记快路径:本地命中直接放行,不发多余网络请求', () => {
|
|
|
+ it('healthServiceProfile=true 走快路径放行,getMyPractitioner 0 次', async () => {
|
|
|
+ currentStorage = { healthServiceProfile: true }
|
|
|
+ await expect(guardWorker()).resolves.toBe(true)
|
|
|
+ expect(getMyPractitioner).toHaveBeenCalledTimes(0)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('role=worker 走快路径放行,0 次网络请求', async () => {
|
|
|
+ currentRole = 'worker'
|
|
|
+ await expect(guardWorker()).resolves.toBe(true)
|
|
|
+ expect(getMyPractitioner).toHaveBeenCalledTimes(0)
|
|
|
+ })
|
|
|
+})
|
|
|
+
|
|
|
+/* ------------- 双身份隔离(页面层退化断言,等同 CERT-TC-005 / TC-006) -------------
|
|
|
+ * 代表页:practitioner/index(档案工作台)、attendance/scan(现场签到)。
|
|
|
+ * 二者接入形态一致:guardPage() 后接 guardWorker().then(ok => { if (!ok) return; 加载页面 })。
|
|
|
+ * 因 .vue 无法在 jest 直接编译(未装 @vue/test-utils/vue-jest),此处退化为
|
|
|
+ * 「直接调 guardWorker 的两个身份分支并断言放行/拦截结果」,断言即页面隔离契约。 */
|
|
|
+describe('双身份隔离(页面层退化断言)', () => {
|
|
|
+ async function enterHealthDomainPage() {
|
|
|
+ const ok = await guardWorker()
|
|
|
+ if (!ok) return { loaded: false, blocked: true }
|
|
|
+ return { loaded: true, blocked: false }
|
|
|
+ }
|
|
|
+
|
|
|
+ it('worker 身份可进入健康服务域页面(工作台/管理动作可见)', async () => {
|
|
|
+ currentRole = 'worker'
|
|
|
+ await expect(enterHealthDomainPage()).resolves.toEqual({ loaded: true, blocked: false })
|
|
|
+ expect(getMyPractitioner).toHaveBeenCalledTimes(0)
|
|
|
+ })
|
|
|
+
|
|
|
+ it('普通用户身份被拦截(不加载页面数据)', async () => {
|
|
|
+ getMyPractitioner.mockResolvedValueOnce({ data: { status: 'pending', lifecycleStage: 'training' } })
|
|
|
+ await expect(enterHealthDomainPage()).resolves.toEqual({ loaded: false, blocked: true })
|
|
|
+ })
|
|
|
+
|
|
|
+ it('org/机构用户身份同样被拦截(非 housekeeper/worker 一律拒绝)', async () => {
|
|
|
+ currentRole = 'org'
|
|
|
+ getMyPractitioner.mockResolvedValueOnce({ data: { authLevel: 'none' } })
|
|
|
+ await expect(enterHealthDomainPage()).resolves.toEqual({ loaded: false, blocked: true })
|
|
|
+ })
|
|
|
+})
|