Skip to content
Open
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
25 changes: 25 additions & 0 deletions apps/sim/lib/credentials/application/save-credential-draft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const credential = {
id: 'credential-1',
workspaceId: 'workspace-1',
type: 'oauth' as const,
providerId: 'google-email',
}

describe('saveCredentialDraft', () => {
Expand Down Expand Up @@ -122,4 +123,28 @@ describe('saveCredentialDraft', () => {
})
expect(mocks.createDraft).not.toHaveBeenCalled()
})
it.each([
{ type: 'oauth' as const, providerId: 'slack' },
{ type: 'service_account' as const, providerId: 'google-email' },
])('rejects a reconnect draft for an incompatible credential', async (target) => {
mocks.getActor.mockResolvedValue({
credential: { ...credential, ...target },
member: { role: 'admin' },
hasWorkspaceAccess: true,
isAdmin: true,
})

await expect(
saveCredentialDraft.execute({
principal,
input: {
workspaceId: 'workspace-1',
providerId: 'google-email',
displayName: 'Work Gmail',
credentialId: 'credential-1',
},
})
).rejects.toMatchObject({ code: 'validation' })
expect(mocks.createDraft).not.toHaveBeenCalled()
})
})
6 changes: 6 additions & 0 deletions apps/sim/lib/credentials/application/save-credential-draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ export const saveCredentialDraft = defineAuthorizedWorkspaceUseCase({
'Admin access required on the target credential'
)
}
if (context.credentialAccess.credential.type !== 'oauth') {
throw new OrchestrationError('validation', 'Only OAuth credentials can be reconnected')
}
if (context.credentialAccess.credential.providerId !== input.providerId) {
throw new OrchestrationError('validation', 'OAuth provider does not match this credential')
}
},
async execute({ principal, input }) {
const draft = await createConnectDraft({
Expand Down