|
|
|
|
@@ -0,0 +1,110 @@
|
|
|
|
|
import { ExecutionContext, INestApplication, UnauthorizedException, ValidationPipe } from '@nestjs/common'
|
|
|
|
|
import { Test } from '@nestjs/testing'
|
|
|
|
|
import type { AddressInfo } from 'node:net'
|
|
|
|
|
import { JwtAuthGuard } from '../../auth/jwt-auth.guard'
|
|
|
|
|
import { RolesGuard } from '../../auth/roles.guard'
|
|
|
|
|
import { PrismaService } from '../../prisma/prisma.service'
|
|
|
|
|
import { ApiResponseInterceptor } from '../../common/interceptors/api-response.interceptor'
|
|
|
|
|
import { AdminMemberProgressController, MemberProgressController } from '../member-progress.controller'
|
|
|
|
|
import { MemberProgressService } from '../member-progress.service'
|
|
|
|
|
import { ProgressPhotoStorageService } from '../progress-photo-storage.service'
|
|
|
|
|
import { lessonFeedbackShare } from '../../../../app/src/utils/lesson-feedback'
|
|
|
|
|
|
|
|
|
|
describe('Owner-only shared lesson feedback HTTP access', () => {
|
|
|
|
|
let app: INestApplication
|
|
|
|
|
const original = { id: 'note', content: '整体评价', apparatusNotes: { M: '控制稳定', Chair: '注意节奏' }, shared: true, createdAt: new Date('2026-10-18T00:00:00Z'), bookingId: 'lesson', booking: { timeSlot: { date: new Date('2026-10-18'), startTime: '10:00' } }, user: { nickname: '学员' } }
|
|
|
|
|
let record: typeof original | null
|
|
|
|
|
const db = { memberNote: { findFirst: jest.fn(), updateMany: jest.fn() } }
|
|
|
|
|
async function request(method: string, path: string, identity?: string, body?: unknown, role = 'MEMBER') {
|
|
|
|
|
const address = app.getHttpServer().address() as AddressInfo
|
|
|
|
|
return fetch(`http://127.0.0.1:${address.port}${path}`, { method, headers: { 'Content-Type': 'application/json', ...(identity ? { 'x-user': identity, 'x-role': role } : {}) }, ...(body === undefined ? {} : { body: JSON.stringify(body) }) })
|
|
|
|
|
}
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
|
const module = await Test.createTestingModule({
|
|
|
|
|
controllers: [MemberProgressController, AdminMemberProgressController],
|
|
|
|
|
providers: [MemberProgressService, RolesGuard, { provide: PrismaService, useValue: db }, { provide: ProgressPhotoStorageService, useValue: {} }],
|
|
|
|
|
}).overrideGuard(JwtAuthGuard).useValue({ canActivate(context: ExecutionContext) {
|
|
|
|
|
const req = context.switchToHttp().getRequest()
|
|
|
|
|
if (!req.headers['x-user']) throw new UnauthorizedException()
|
|
|
|
|
req.user = { sub: req.headers['x-user'], role: req.headers['x-role'] }
|
|
|
|
|
return true
|
|
|
|
|
} }).compile()
|
|
|
|
|
app = module.createNestApplication()
|
|
|
|
|
app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: true, transform: true }))
|
|
|
|
|
app.useGlobalInterceptors(new ApiResponseInterceptor())
|
|
|
|
|
await app.listen(0, '127.0.0.1')
|
|
|
|
|
})
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.clearAllMocks()
|
|
|
|
|
record = structuredClone(original)
|
|
|
|
|
db.memberNote.findFirst.mockImplementation(async ({ where }) => record && where.id === record.id && where.userId === 'owner' && (where.shared === undefined || where.shared === record.shared) ? record : null)
|
|
|
|
|
db.memberNote.updateMany.mockImplementation(async ({ where, data }) => {
|
|
|
|
|
if (!record || where.userId !== 'owner' || where.id !== record.id) return { count: 0 }
|
|
|
|
|
record.shared = data.shared
|
|
|
|
|
return { count: 1 }
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
afterAll(async () => { await app.close() })
|
|
|
|
|
|
|
|
|
|
it('returns this one shared evaluation to its authenticated owner with no internal IDs', async () => {
|
|
|
|
|
const response = await request('GET', '/user/progress/notes/note', 'owner')
|
|
|
|
|
expect(response.status).toBe(200)
|
|
|
|
|
expect(response.headers.get('cache-control')).toBe('private, no-store')
|
|
|
|
|
const body = await response.json()
|
|
|
|
|
expect(body.data).toMatchObject({ nickname: '学员', note: { content: '整体评价', apparatusNotes: { M: '控制稳定', Chair: '注意节奏' } } })
|
|
|
|
|
expect(body.data.note.operatorId).toBeUndefined()
|
|
|
|
|
expect(body.data.note.userId).toBeUndefined()
|
|
|
|
|
expect(db.memberNote.findFirst).toHaveBeenCalledWith(expect.objectContaining({ where: { id: 'note', userId: 'owner', shared: true }, select: expect.not.objectContaining({ operatorId: true, userId: true }) }))
|
|
|
|
|
})
|
|
|
|
|
it.each(['MEMBER', 'ADMIN'])('rejects a forwarded student link for a different %s account', async role => {
|
|
|
|
|
const response = await request('GET', '/user/progress/notes/note?userId=owner', 'other', undefined, role)
|
|
|
|
|
expect(response.status).toBe(404)
|
|
|
|
|
expect(JSON.stringify(await response.json())).not.toContain('控制稳定')
|
|
|
|
|
expect(db.memberNote.findFirst.mock.calls[0][0].where).toEqual({ id: 'note', userId: 'other', shared: true })
|
|
|
|
|
})
|
|
|
|
|
it.each(['private', 'deleted', 'unknown'])('does not reveal a %s note even to the owner', async kind => {
|
|
|
|
|
if (kind === 'private') record!.shared = false
|
|
|
|
|
if (kind === 'deleted') record = null
|
|
|
|
|
const response = await request('GET', `/user/progress/notes/${kind === 'unknown' ? 'missing' : 'note'}`, 'owner')
|
|
|
|
|
expect(response.status).toBe(404)
|
|
|
|
|
expect((await response.json()).message).toBe('评价不存在或你无权查看')
|
|
|
|
|
})
|
|
|
|
|
it.each(['/user/progress/notes/note', '/admin/members/owner/progress/notes/note'])('requires authentication: %s', async path => {
|
|
|
|
|
expect((await request('GET', path)).status).toBe(401)
|
|
|
|
|
expect(db.memberNote.findFirst).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
it('allows an authorized teacher preview of a private note, scoped to the target member', async () => {
|
|
|
|
|
record!.shared = false
|
|
|
|
|
expect((await request('GET', '/admin/members/owner/progress/notes/note', 'teacher', undefined, 'ADMIN')).status).toBe(200)
|
|
|
|
|
expect(db.memberNote.findFirst.mock.calls[0][0].where).toEqual({ id: 'note', userId: 'owner' })
|
|
|
|
|
expect((await request('GET', '/admin/members/other/progress/notes/note', 'teacher', undefined, 'ADMIN')).status).toBe(404)
|
|
|
|
|
})
|
|
|
|
|
it('does not let members obtain preview access or change sharing by forging an admin URL', async () => {
|
|
|
|
|
expect((await request('GET', '/admin/members/owner/progress/notes/note', 'other')).status).toBe(403)
|
|
|
|
|
expect((await request('PUT', '/admin/members/owner/progress/notes/note/sharing', 'owner', { shared: true })).status).toBe(403)
|
|
|
|
|
expect(db.memberNote.findFirst).not.toHaveBeenCalled()
|
|
|
|
|
expect(db.memberNote.updateMany).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
it('revokes existing links immediately and lets a teacher re-enable the same note', async () => {
|
|
|
|
|
const endpoint = '/admin/members/owner/progress/notes/note/sharing'
|
|
|
|
|
expect((await request('PUT', endpoint, 'teacher', { shared: false }, 'ADMIN')).status).toBe(200)
|
|
|
|
|
expect((await request('GET', '/user/progress/notes/note', 'owner')).status).toBe(404)
|
|
|
|
|
expect((await request('PUT', endpoint, 'teacher', { shared: true }, 'ADMIN')).status).toBe(200)
|
|
|
|
|
expect((await request('GET', '/user/progress/notes/note', 'owner')).status).toBe(200)
|
|
|
|
|
expect(record!.apparatusNotes).toEqual(original.apparatusNotes)
|
|
|
|
|
expect(db.memberNote.updateMany).toHaveBeenCalledWith({ where: { id: 'note', userId: 'owner' }, data: { shared: false } })
|
|
|
|
|
})
|
|
|
|
|
it.each([{}, { shared: 'true' }, { shared: 1 }, { shared: null }, { shared: true, userId: 'other' }])('rejects malformed sharing updates %j', async body => {
|
|
|
|
|
expect((await request('PUT', '/admin/members/owner/progress/notes/note/sharing', 'teacher', body, 'ADMIN')).status).toBe(400)
|
|
|
|
|
expect(db.memberNote.updateMany).not.toHaveBeenCalled()
|
|
|
|
|
})
|
|
|
|
|
it('does not change a note through a mismatched member path', async () => {
|
|
|
|
|
expect((await request('PUT', '/admin/members/other/progress/notes/note/sharing', 'teacher', { shared: false }, 'ADMIN')).status).toBe(404)
|
|
|
|
|
expect(record!.shared).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
it('shares a student-only URL with a generic title and static cover instead of personal content', () => {
|
|
|
|
|
expect(lessonFeedbackShare('note')).toEqual({ title: '教练为你记录了一份课后评价', path: '/pages/profile/lesson-feedback?id=note', imageUrl: '/static/share/lesson-feedback.png' })
|
|
|
|
|
expect(lessonFeedbackShare('note&userId=owner').path).toBe('/pages/profile/lesson-feedback?id=note%26userId%3Downer')
|
|
|
|
|
expect(lessonFeedbackShare().path).toBe('/pages/home/index')
|
|
|
|
|
})
|
|
|
|
|
})
|