Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { authMockFns } from '@sim/testing'
import { NextRequest } from 'next/server'
import { describe, expect, it } from 'vitest'
import { GET } from '@/app/api/credential-groups/slack-managed-users/callback/route'

describe('GET /api/credential-groups/slack-managed-users/callback', () => {
it('sends a signed-out browser to the sign-in recovery screen', async () => {
authMockFns.mockGetSession.mockResolvedValueOnce(null)
const response = await GET(
new NextRequest(
'http://localhost/api/credential-groups/slack-managed-users/callback?state=s1&code=c1'
),
{}
)
expect(response.status).toBe(303)
const location = new URL(response.headers.get('location') ?? '')
expect(location.pathname).toBe('/credential-groups/slack-complete')
expect(location.searchParams.get('reason')).toBe('signin_required')
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const GET = withRouteHandler(async (request: NextRequest) => {
ok: false,
message: 'Sign in to Sim to complete this Slack setup.',
state: rawState,
reason: 'unauthenticated',
reason: 'signin_required',
})
}
const parsed = await parseRequest(slackCredentialGroupConfigurationCallbackContract, request, {})
Expand Down
27 changes: 27 additions & 0 deletions apps/sim/app/api/desktop/source-connect/route.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { authMockFns } from '@sim/testing'
import { NextRequest } from 'next/server'
import { beforeEach, describe, expect, it } from 'vitest'
import { POST } from '@/app/api/desktop/source-connect/route'

describe('POST /api/desktop/source-connect', () => {
beforeEach(() => {
authMockFns.mockGetSession.mockResolvedValue({ user: { id: 'u1' }, session: { id: 's1' } })
})

it('rejects a body far beyond the ticket limit at the parser', async () => {
const body = JSON.stringify({
requestId: 'a'.repeat(32),
request: { kind: 'reconnect-account', credentialId: 'c'.repeat(128) },
padding: 'x'.repeat(256 * 1024),
})
const response = await POST(
new NextRequest('http://localhost/api/desktop/source-connect', {
method: 'POST',
headers: { 'content-type': 'application/json' },
body,
}),
{}
)
expect(response.status).toBe(413)
})
})
1 change: 1 addition & 0 deletions apps/sim/app/api/desktop/source-connect/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const POST = defineInternalJsonRoute({
operation: createDesktopSourceRequest.operation,
rateLimit: internalRateLimits.user({ bucketName: 'desktop-source-connect' }),
errorPolicy: internalOrchestrationErrorPolicy,
parseOptions: { maxBodyBytes: 64 * 1024 },
mapInput: ({ body }) => ({ requestId: body.requestId, payload: JSON.stringify(body.request) }),
useCase: createDesktopSourceRequest,
staticResponseHeaders: { 'Cache-Control': 'no-store' },
Expand Down
72 changes: 72 additions & 0 deletions apps/sim/hooks/queries/slack-search.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/** @vitest-environment jsdom */

import { act } from 'react'
import {
apiClientRequestMock,
apiClientRequestMockFns,
} from '@sim/testing/mocks/api-client-request.mock'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

vi.mock('@/lib/api/client/request', () => apiClientRequestMock)

import { useStartSlackSearchOAuth } from '@/hooks/queries/slack-search'

const mockRequestJson = apiClientRequestMockFns.mockRequestJson

describe('useStartSlackSearchOAuth', () => {
let root: Root
let client: QueryClient
let result: ReturnType<typeof useStartSlackSearchOAuth>

function Probe() {
Comment thread
waleedlatif1 marked this conversation as resolved.
result = useStartSlackSearchOAuth()
return null
}

beforeEach(async () => {
vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true)
mockRequestJson.mockReset()
client = new QueryClient()
root = createRoot(document.createElement('div'))
await act(async () =>
root.render(
<QueryClientProvider client={client}>
<Probe />
</QueryClientProvider>
)
)
})

afterEach(async () => {
await act(async () => root.unmount())
client.clear()
})

it('cancels an in-flight browser OAuth start when its signal aborts', async () => {
mockRequestJson.mockImplementation(
(_contract: unknown, input: { signal?: AbortSignal }) =>
new Promise((_resolve, reject) =>
input.signal?.addEventListener('abort', () => reject(input.signal?.reason))
)
)
const controller = new AbortController()
let outcome: 'pending' | 'rejected' = 'pending'
await act(async () => {
void result
.mutateAsync({
organizationId: 'org-1',
name: 'Sim Search',
description: 'Search Slack',
mode: 'shared',
signal: controller.signal,
})
.catch(() => {
outcome = 'rejected'
})
})
await act(async () => controller.abort())
expect(outcome).toBe('rejected')
})
})
2 changes: 1 addition & 1 deletion apps/sim/hooks/queries/slack-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function useStartSlackSearchOAuth() {
await connectDesktopSource({ kind: 'slack-search', body }, signal)
return null
}
return requestJson(startSlackSearchOAuthContract, { body })
return requestJson(startSlackSearchOAuthContract, { body, signal })
},
onSettled: (_data, _error, input) =>
Promise.all([
Expand Down
Loading