Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

3.2 KB
Raw Blame
import { apId } from '@activepieces/core-utils'
import { FastifyInstance } from 'fastify'
import { StatusCodes } from 'http-status-codes'
import { createServiceContext, createTestContext } from '../../../helpers/test-context'
import { setupTestEnvironment, teardownTestEnvironment } from '../../../helpers/test-setup'

let app: FastifyInstance | null = null

beforeAll(async () => {
    app = await setupTestEnvironment()
})

afterAll(async () => {
    await teardownTestEnvironment()
})

describe('User Profile API', () => {
    describe('GET /v1/users/:id', () => {
        it('should get user by id', async () => {
            const ctx = await createTestContext(app!)

            const response = await ctx.get(`/v1/users/${ctx.user.id}`)

            expect(response?.statusCode).toBe(StatusCodes.OK)
            const body = response?.json()
            expect(body.id).toBe(ctx.user.id)
            expect(body.platformId).toBe(ctx.platform.id)
            expect(body.email).toBe(ctx.userIdentity.email)
            expect(body.firstName).toBe(ctx.userIdentity.firstName)
            expect(body.lastName).toBe(ctx.userIdentity.lastName)
        })

        it('should return 404 for non-existent user', async () => {
            const ctx = await createTestContext(app!)

            const response = await ctx.get(`/v1/users/${apId()}`)

            expect(response?.statusCode).toBe(StatusCodes.NOT_FOUND)
        })

        it('should return 404 for user on different platform', async () => {
            const ctx1 = await createTestContext(app!)
            const ctx2 = await createTestContext(app!)

            const response = await ctx2.get(`/v1/users/${ctx1.user.id}`)

            expect(response?.statusCode).toBe(StatusCodes.NOT_FOUND)
        })

        it('should allow API-key (SERVICE) callers to fetch a user on their platform', async () => {
            const ctx = await createTestContext(app!)
            const serviceCtx = await createServiceContext(app!, ctx)

            const response = await serviceCtx.get(`/v1/users/${ctx.user.id}`)

            expect(response?.statusCode).toBe(StatusCodes.OK)
            const body = response?.json()
            expect(body.id).toBe(ctx.user.id)
            expect(body.platformId).toBe(ctx.platform.id)
            expect(body.email).toBe(ctx.userIdentity.email)
        })

        it('should return 404 when an API-key tries to fetch a user on a different platform', async () => {
            const ctx1 = await createTestContext(app!)
            const ctx2 = await createTestContext(app!)
            const serviceCtx2 = await createServiceContext(app!, ctx2)

            const response = await serviceCtx2.get(`/v1/users/${ctx1.user.id}`)

            expect(response?.statusCode).toBe(StatusCodes.NOT_FOUND)
        })
    })

    describe('DELETE /v1/users/me/profile-picture', () => {
        it('should delete profile picture', async () => {
            const ctx = await createTestContext(app!)

            const response = await ctx.delete('/v1/users/me/profile-picture')

            expect(response?.statusCode).toBe(StatusCodes.OK)
            const body = response?.json()
            expect(body.success).toBe(true)
        })
    })
})