mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-12-04 06:54:28 +00:00
test(backend): add tests for resolving creator avatar URL in share and collection APIs
This commit is contained in:
@@ -3,6 +3,13 @@ import { collectionService } from '@shumai/core/src/collection/collection'
|
|||||||
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
import { authzService, Permission, ResourceType } from '@shumai/core/src/authz/authz'
|
||||||
import { Hono, Context, Next } from 'hono'
|
import { Hono, Context, Next } from 'hono'
|
||||||
import collectionRoute from './collection'
|
import collectionRoute from './collection'
|
||||||
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
|
|
||||||
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||||
|
s3Service: {
|
||||||
|
presign: vi.fn(),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
vi.mock('./middleware/auth', () => ({
|
vi.mock('./middleware/auth', () => ({
|
||||||
authMiddleware: async (c: Context, next: Next) => {
|
authMiddleware: async (c: Context, next: Next) => {
|
||||||
@@ -38,6 +45,7 @@ describe('Collection API', () => {
|
|||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.restoreAllMocks()
|
vi.restoreAllMocks()
|
||||||
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
vi.mocked(authzService.hasPermission).mockResolvedValue(undefined)
|
||||||
|
vi.mocked(s3Service.presign).mockResolvedValue('http://s3/presigned-avatar')
|
||||||
})
|
})
|
||||||
|
|
||||||
it('POST /projects/:projectId/collections', async () => {
|
it('POST /projects/:projectId/collections', async () => {
|
||||||
@@ -52,8 +60,12 @@ describe('Collection API', () => {
|
|||||||
projectId,
|
projectId,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
updatedAt: new Date(),
|
updatedAt: new Date(),
|
||||||
creator: null,
|
creator: {
|
||||||
creatorId: null,
|
id: 'user-1',
|
||||||
|
name: 'Test User',
|
||||||
|
image: 'avatars/test.png',
|
||||||
|
} as any,
|
||||||
|
creatorId: 'user-1',
|
||||||
})
|
})
|
||||||
|
|
||||||
const res = await app.request(`/projects/${projectId}/collections`, {
|
const res = await app.request(`/projects/${projectId}/collections`, {
|
||||||
@@ -69,6 +81,8 @@ describe('Collection API', () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
expect(res.status).toBe(200)
|
expect(res.status).toBe(200)
|
||||||
|
const json = await res.json()
|
||||||
|
expect(json.creator?.image).toBe('http://s3/presigned-avatar')
|
||||||
expect(mockCreate).toHaveBeenCalledWith(projectId, expect.any(Object), 'user-1')
|
expect(mockCreate).toHaveBeenCalledWith(projectId, expect.any(Object), 'user-1')
|
||||||
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
expect(authzService.hasPermission).toHaveBeenCalledWith({
|
||||||
user: expect.any(Object),
|
user: expect.any(Object),
|
||||||
|
|||||||
@@ -1,14 +1,25 @@
|
|||||||
import { describe, it, expect, beforeEach } from 'vitest'
|
import { describe, it, expect, beforeEach, vi } from 'vitest'
|
||||||
import { prisma } from '@shumai/db'
|
import { prisma } from '@shumai/db'
|
||||||
import { setupTestDbHooks } from '@shumai/db/test'
|
import { setupTestDbHooks } from '@shumai/db/test'
|
||||||
import { shareService } from './share'
|
import { shareService } from './share'
|
||||||
import { assetService } from '@shumai/core/src/asset/asset'
|
import { assetService } from '@shumai/core/src/asset/asset'
|
||||||
import { projectService } from '@shumai/core/src/project/project'
|
import { projectService } from '@shumai/core/src/project/project'
|
||||||
import { AssetType } from '@shumai/db'
|
import { AssetType } from '@shumai/db'
|
||||||
|
import { s3Service } from '@shumai/core/src/s3/s3'
|
||||||
|
|
||||||
|
vi.mock('@shumai/core/src/s3/s3', () => ({
|
||||||
|
s3Service: {
|
||||||
|
presign: vi.fn(),
|
||||||
|
},
|
||||||
|
}))
|
||||||
|
|
||||||
describe('ShareService', () => {
|
describe('ShareService', () => {
|
||||||
setupTestDbHooks()
|
setupTestDbHooks()
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
vi.mocked(s3Service.presign).mockResolvedValue('http://s3/mock-avatar')
|
||||||
|
})
|
||||||
|
|
||||||
let teamId: string
|
let teamId: string
|
||||||
let projectId: string
|
let projectId: string
|
||||||
let userId: string
|
let userId: string
|
||||||
@@ -268,4 +279,32 @@ describe('ShareService', () => {
|
|||||||
const symlinkAfter = await prisma.asset.findUnique({ where: { id: symlink!.id } })
|
const symlinkAfter = await prisma.asset.findUnique({ where: { id: symlink!.id } })
|
||||||
expect(symlinkAfter).toBeNull()
|
expect(symlinkAfter).toBeNull()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('resolves the creator avatar image to a presigned URL', async () => {
|
||||||
|
const userWithAvatar = await prisma.user.create({
|
||||||
|
data: {
|
||||||
|
name: 'User With Avatar',
|
||||||
|
email: `avatar-test-${Date.now()}@example.com`,
|
||||||
|
image: 'avatars/user123.png',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
vi.mocked(s3Service.presign).mockResolvedValue('http://s3/avatars/user123-presigned.png')
|
||||||
|
|
||||||
|
const shareLink = await shareService.createShareLink(
|
||||||
|
projectId,
|
||||||
|
{ name: 'Avatar Share' },
|
||||||
|
userWithAvatar.id,
|
||||||
|
)
|
||||||
|
|
||||||
|
expect(shareLink.creator).toBeDefined()
|
||||||
|
expect(shareLink.creator?.image).toBe('http://s3/avatars/user123-presigned.png')
|
||||||
|
|
||||||
|
const fetched = await shareService.getShareLink(shareLink.id)
|
||||||
|
expect(fetched.creator?.image).toBe('http://s3/avatars/user123-presigned.png')
|
||||||
|
|
||||||
|
const list = await shareService.listProjectShareLinks({ projectId })
|
||||||
|
const item = list.data.find((l) => l.id === shareLink.id)
|
||||||
|
expect(item?.creator?.image).toBe('http://s3/avatars/user123-presigned.png')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user