Skip to content

Commit ebb2546

Browse files
fix(apikeys): pinned api key to track API key a workflow is deployed with (#924)
* fix(apikeys): pinned api key to track API key a workflow is deployed with * remove deprecated behaviour tests
1 parent a204032 commit ebb2546

File tree

9 files changed

+6055
-252
lines changed

9 files changed

+6055
-252
lines changed

apps/sim/app/api/workflows/[id]/deploy/route.test.ts

Lines changed: 41 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -118,44 +118,49 @@ describe('Workflow Deployment API Route', () => {
118118
db: {
119119
select: vi.fn().mockImplementation(() => {
120120
selectCallCount++
121+
const buildLimitResponse = () => ({
122+
limit: vi.fn().mockImplementation(() => {
123+
// First call: workflow lookup (should return workflow)
124+
if (selectCallCount === 1) {
125+
return Promise.resolve([{ userId: 'user-id', id: 'workflow-id' }])
126+
}
127+
// Second call: blocks lookup
128+
if (selectCallCount === 2) {
129+
return Promise.resolve([
130+
{
131+
id: 'block-1',
132+
type: 'starter',
133+
name: 'Start',
134+
positionX: '100',
135+
positionY: '100',
136+
enabled: true,
137+
subBlocks: {},
138+
data: {},
139+
},
140+
])
141+
}
142+
// Third call: edges lookup
143+
if (selectCallCount === 3) {
144+
return Promise.resolve([])
145+
}
146+
// Fourth call: subflows lookup
147+
if (selectCallCount === 4) {
148+
return Promise.resolve([])
149+
}
150+
// Fifth call: API key lookup (should return empty for new key test)
151+
if (selectCallCount === 5) {
152+
return Promise.resolve([])
153+
}
154+
// Default: empty array
155+
return Promise.resolve([])
156+
}),
157+
})
158+
121159
return {
122160
from: vi.fn().mockImplementation(() => ({
123161
where: vi.fn().mockImplementation(() => ({
124-
limit: vi.fn().mockImplementation(() => {
125-
// First call: workflow lookup (should return workflow)
126-
if (selectCallCount === 1) {
127-
return Promise.resolve([{ userId: 'user-id', id: 'workflow-id' }])
128-
}
129-
// Second call: blocks lookup
130-
if (selectCallCount === 2) {
131-
return Promise.resolve([
132-
{
133-
id: 'block-1',
134-
type: 'starter',
135-
name: 'Start',
136-
positionX: '100',
137-
positionY: '100',
138-
enabled: true,
139-
subBlocks: {},
140-
data: {},
141-
},
142-
])
143-
}
144-
// Third call: edges lookup
145-
if (selectCallCount === 3) {
146-
return Promise.resolve([])
147-
}
148-
// Fourth call: subflows lookup
149-
if (selectCallCount === 4) {
150-
return Promise.resolve([])
151-
}
152-
// Fifth call: API key lookup (should return empty for new key test)
153-
if (selectCallCount === 5) {
154-
return Promise.resolve([])
155-
}
156-
// Default: empty array
157-
return Promise.resolve([])
158-
}),
162+
...buildLimitResponse(),
163+
orderBy: vi.fn().mockReturnValue(buildLimitResponse()),
159164
})),
160165
})),
161166
}
@@ -216,160 +221,7 @@ describe('Workflow Deployment API Route', () => {
216221
expect(data).toHaveProperty('deployedAt', null)
217222
})
218223

219-
/**
220-
* Test POST deployment with no existing API key
221-
* This should generate a new API key
222-
*/
223-
it('should create new API key when deploying workflow for user with no API key', async () => {
224-
// Override the global mock for this specific test
225-
vi.doMock('@/db', () => ({
226-
db: {
227-
select: vi
228-
.fn()
229-
.mockReturnValueOnce({
230-
from: vi.fn().mockReturnValue({
231-
where: vi.fn().mockReturnValue({
232-
limit: vi.fn().mockResolvedValue([{ userId: 'user-id', id: 'workflow-id' }]),
233-
}),
234-
}),
235-
})
236-
.mockReturnValueOnce({
237-
from: vi.fn().mockReturnValue({
238-
where: vi.fn().mockResolvedValue([
239-
{
240-
id: 'block-1',
241-
type: 'starter',
242-
name: 'Start',
243-
positionX: '100',
244-
positionY: '100',
245-
enabled: true,
246-
subBlocks: {},
247-
data: {},
248-
},
249-
]),
250-
}),
251-
})
252-
.mockReturnValueOnce({
253-
from: vi.fn().mockReturnValue({
254-
where: vi.fn().mockResolvedValue([]), // No edges
255-
}),
256-
})
257-
.mockReturnValueOnce({
258-
from: vi.fn().mockReturnValue({
259-
where: vi.fn().mockResolvedValue([]), // No subflows
260-
}),
261-
})
262-
.mockReturnValueOnce({
263-
from: vi.fn().mockReturnValue({
264-
where: vi.fn().mockReturnValue({
265-
limit: vi.fn().mockResolvedValue([]), // No existing API key
266-
}),
267-
}),
268-
}),
269-
insert: vi.fn().mockImplementation(() => ({
270-
values: vi.fn().mockResolvedValue([{ id: 'mock-api-key-id' }]),
271-
})),
272-
update: vi.fn().mockImplementation(() => ({
273-
set: vi.fn().mockImplementation(() => ({
274-
where: vi.fn().mockResolvedValue([]),
275-
})),
276-
})),
277-
},
278-
}))
279-
280-
const req = createMockRequest('POST')
281-
282-
const params = Promise.resolve({ id: 'workflow-id' })
283-
284-
const { POST } = await import('@/app/api/workflows/[id]/deploy/route')
285-
286-
const response = await POST(req, { params })
287-
288-
expect(response.status).toBe(200)
289-
290-
const data = await response.json()
291-
292-
expect(data).toHaveProperty('apiKey', 'sim_testkeygenerated12345')
293-
expect(data).toHaveProperty('isDeployed', true)
294-
expect(data).toHaveProperty('deployedAt')
295-
})
296-
297-
/**
298-
* Test POST deployment with existing API key
299-
* This should use the existing API key
300-
*/
301-
it('should use existing API key when deploying workflow', async () => {
302-
// Override the global mock for this specific test
303-
vi.doMock('@/db', () => ({
304-
db: {
305-
select: vi
306-
.fn()
307-
.mockReturnValueOnce({
308-
from: vi.fn().mockReturnValue({
309-
where: vi.fn().mockReturnValue({
310-
limit: vi.fn().mockResolvedValue([{ userId: 'user-id', id: 'workflow-id' }]),
311-
}),
312-
}),
313-
})
314-
.mockReturnValueOnce({
315-
from: vi.fn().mockReturnValue({
316-
where: vi.fn().mockResolvedValue([
317-
{
318-
id: 'block-1',
319-
type: 'starter',
320-
name: 'Start',
321-
positionX: '100',
322-
positionY: '100',
323-
enabled: true,
324-
subBlocks: {},
325-
data: {},
326-
},
327-
]),
328-
}),
329-
})
330-
.mockReturnValueOnce({
331-
from: vi.fn().mockReturnValue({
332-
where: vi.fn().mockResolvedValue([]), // No edges
333-
}),
334-
})
335-
.mockReturnValueOnce({
336-
from: vi.fn().mockReturnValue({
337-
where: vi.fn().mockResolvedValue([]), // No subflows
338-
}),
339-
})
340-
.mockReturnValueOnce({
341-
from: vi.fn().mockReturnValue({
342-
where: vi.fn().mockReturnValue({
343-
limit: vi.fn().mockResolvedValue([{ key: 'sim_existingtestapikey12345' }]), // Existing API key
344-
}),
345-
}),
346-
}),
347-
insert: vi.fn().mockImplementation(() => ({
348-
values: vi.fn().mockResolvedValue([{ id: 'mock-api-key-id' }]),
349-
})),
350-
update: vi.fn().mockImplementation(() => ({
351-
set: vi.fn().mockImplementation(() => ({
352-
where: vi.fn().mockResolvedValue([]),
353-
})),
354-
})),
355-
},
356-
}))
357-
358-
const req = createMockRequest('POST')
359-
360-
const params = Promise.resolve({ id: 'workflow-id' })
361-
362-
const { POST } = await import('@/app/api/workflows/[id]/deploy/route')
363-
364-
const response = await POST(req, { params })
365-
366-
expect(response.status).toBe(200)
367-
368-
const data = await response.json()
369-
370-
expect(data).toHaveProperty('apiKey', 'sim_existingtestapikey12345')
371-
expect(data).toHaveProperty('isDeployed', true)
372-
})
224+
// Removed two POST deployment tests by request
373225

374226
/**
375227
* Test DELETE undeployment

0 commit comments

Comments
 (0)