-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(editor): Add free AI credits CTA #12365
base: master
Are you sure you want to change the base?
Changes from all commits
93d693b
6500400
09a5768
940d88c
794fa79
7809e1c
81c8a28
dd33520
e38df6f
f812117
d716416
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import CredentialConfig from './CredentialEdit/CredentialConfig.vue'; | ||
import { screen } from '@testing-library/vue'; | ||
import type { ICredentialDataDecryptedObject, ICredentialType } from 'n8n-workflow'; | ||
import { createTestingPinia } from '@pinia/testing'; | ||
import type { RenderOptions } from '@/__tests__/render'; | ||
import { createComponentRenderer } from '@/__tests__/render'; | ||
import { STORES } from '@/constants'; | ||
|
||
const defaultRenderOptions: RenderOptions = { | ||
pinia: createTestingPinia({ | ||
initialState: { | ||
[STORES.SETTINGS]: { | ||
settings: { | ||
enterprise: { | ||
sharing: false, | ||
externalSecrets: false, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}), | ||
props: { | ||
isManaged: true, | ||
mode: 'edit', | ||
credentialType: {} as ICredentialType, | ||
credentialProperties: [], | ||
credentialData: {} as ICredentialDataDecryptedObject, | ||
credentialPermissions: { | ||
share: false, | ||
move: false, | ||
create: false, | ||
read: false, | ||
update: false, | ||
delete: false, | ||
list: false, | ||
}, | ||
}, | ||
}; | ||
|
||
const renderComponent = createComponentRenderer(CredentialConfig, defaultRenderOptions); | ||
|
||
describe('CredentialConfig', () => { | ||
it('should display a warning when isManaged is true', async () => { | ||
renderComponent(); | ||
expect( | ||
screen.queryByText('This is a managed credential and cannot be edited.'), | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('should not display a warning when isManaged is false', async () => { | ||
renderComponent({ props: { isManaged: false } }, { merge: true }); | ||
expect( | ||
screen.queryByText('This is a managed credential and cannot be edited.'), | ||
).not.toBeInTheDocument(); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,6 +160,11 @@ const credentialTypeName = computed(() => { | |
return `${props.activeId}`; | ||
}); | ||
|
||
const isEditingManagedCredential = computed(() => { | ||
if (!props.activeId) return false; | ||
return credentialsStore.getCredentialById(props.activeId)?.isManaged ?? false; | ||
}); | ||
|
||
const isCredentialTestable = computed(() => { | ||
if (isOAuthType.value || !requiredPropertiesFilled.value) { | ||
return false; | ||
|
@@ -597,6 +602,10 @@ function scrollToBottom() { | |
} | ||
|
||
async function retestCredential() { | ||
if (isEditingManagedCredential.value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not send the request as there is nothing to test on managed credentials |
||
return; | ||
} | ||
|
||
if (!isCredentialTestable.value || !credentialTypeName.value) { | ||
authError.value = ''; | ||
testedSuccessfully.value = false; | ||
|
@@ -1061,7 +1070,9 @@ function resetCredentialData(): void { | |
<InlineNameEdit | ||
:model-value="credentialName" | ||
:subtitle="credentialType ? credentialType.displayName : ''" | ||
:readonly="!credentialPermissions.update || !credentialType" | ||
:readonly=" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prevents users from editing the credential name |
||
!credentialPermissions.update || !credentialType || isEditingManagedCredential | ||
" | ||
type="Credential" | ||
data-test-id="credential-name" | ||
@update:model-value="onNameEdit" | ||
|
@@ -1113,6 +1124,7 @@ function resetCredentialData(): void { | |
:credential-properties="credentialProperties" | ||
:credential-data="credentialData" | ||
:credential-id="credentialId" | ||
:is-managed="isEditingManagedCredential" | ||
:show-validation-warning="showValidationWarning" | ||
:auth-error="authError" | ||
:tested-successfully="testedSuccessfully" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { fireEvent, screen } from '@testing-library/vue'; | ||
import FreeAiCreditsCallout from '@/components/FreeAiCreditsCallout.vue'; | ||
import { useCredentialsStore } from '@/stores/credentials.store'; | ||
import { useSettingsStore } from '@/stores/settings.store'; | ||
import { useUsersStore } from '@/stores/users.store'; | ||
import { useNDVStore } from '@/stores/ndv.store'; | ||
import { usePostHog } from '@/stores/posthog.store'; | ||
import { useProjectsStore } from '@/stores/projects.store'; | ||
import { useRootStore } from '@/stores/root.store'; | ||
import { useToast } from '@/composables/useToast'; | ||
import { renderComponent } from '@/__tests__/render'; | ||
import { mockedStore } from '@/__tests__/utils'; | ||
|
||
vi.mock('@/composables/useToast', () => ({ | ||
useToast: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/settings.store', () => ({ | ||
useSettingsStore: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/credentials.store', () => ({ | ||
useCredentialsStore: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/users.store', () => ({ | ||
useUsersStore: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/ndv.store', () => ({ | ||
useNDVStore: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/posthog.store', () => ({ | ||
usePostHog: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/projects.store', () => ({ | ||
useProjectsStore: vi.fn(), | ||
})); | ||
|
||
vi.mock('@/stores/root.store', () => ({ | ||
useRootStore: vi.fn(), | ||
})); | ||
|
||
const assertUserCannotClaimCredits = () => { | ||
expect(screen.queryByText('Get 100 free OpenAI API credits')).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'Claim credits' })).not.toBeInTheDocument(); | ||
}; | ||
|
||
const assertUserCanClaimCredits = () => { | ||
expect(screen.getByText('Get 100 free OpenAI API credits')).toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'Claim credits' })).toBeInTheDocument(); | ||
}; | ||
|
||
const assertUserClaimedCredits = () => { | ||
expect(screen.getByText('Claimed 100 free OpenAI API credits')).toBeInTheDocument(); | ||
}; | ||
|
||
describe('FreeAiCreditsCallout', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
|
||
(useSettingsStore as any).mockReturnValue({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be type-safer to avoid using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will address in a follow up PR |
||
isAiCreditsEnabled: true, | ||
aiCreditsQuota: 100, | ||
}); | ||
|
||
(useCredentialsStore as any).mockReturnValue({ | ||
allCredentials: [], | ||
upsertCredential: vi.fn(), | ||
claimFreeAiCredits: vi.fn(), | ||
}); | ||
|
||
(useUsersStore as any).mockReturnValue({ | ||
currentUser: { | ||
settings: { | ||
userClaimedAiCredits: false, | ||
}, | ||
}, | ||
}); | ||
|
||
(useNDVStore as any).mockReturnValue({ | ||
activeNode: { type: '@n8n/n8n-nodes-langchain.openAi' }, | ||
}); | ||
|
||
(usePostHog as any).mockReturnValue({ | ||
isFeatureEnabled: vi.fn().mockReturnValue(true), | ||
}); | ||
|
||
(useProjectsStore as any).mockReturnValue({ | ||
currentProject: { id: 'test-project-id' }, | ||
}); | ||
|
||
(useRootStore as any).mockReturnValue({ | ||
restApiContext: {}, | ||
}); | ||
|
||
(useToast as any).mockReturnValue({ | ||
showError: vi.fn(), | ||
}); | ||
}); | ||
|
||
it('should shows the claim callout when the user can claim credits', () => { | ||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCanClaimCredits(); | ||
}); | ||
|
||
it('should show success callout when credit are claimed', async () => { | ||
const credentialsStore = mockedStore(useCredentialsStore); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
const claimButton = screen.getByRole('button', { | ||
name: 'Claim credits', | ||
}); | ||
|
||
await fireEvent.click(claimButton); | ||
|
||
expect(credentialsStore.claimFreeAiCredits).toHaveBeenCalledWith('test-project-id'); | ||
assertUserClaimedCredits(); | ||
}); | ||
|
||
it('should not be able to claim credits is user already claimed credits', async () => { | ||
(useUsersStore as any).mockReturnValue({ | ||
currentUser: { | ||
settings: { | ||
userClaimedAiCredits: true, | ||
}, | ||
}, | ||
}); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCannotClaimCredits(); | ||
}); | ||
|
||
it('should not be able to claim credits is user does not have ai credits enabled', async () => { | ||
(useSettingsStore as any).mockReturnValue({ | ||
isAiCreditsEnabled: false, | ||
aiCreditsQuota: 0, | ||
}); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCannotClaimCredits(); | ||
}); | ||
|
||
it('should not be able to claim credits if user it is not in experiment', async () => { | ||
(usePostHog as any).mockReturnValue({ | ||
isFeatureEnabled: vi.fn().mockReturnValue(false), | ||
}); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCannotClaimCredits(); | ||
}); | ||
|
||
it('should not be able to claim credits if user already has OpenAiApi credential', async () => { | ||
(useCredentialsStore as any).mockReturnValue({ | ||
allCredentials: [ | ||
{ | ||
type: 'openAiApi', | ||
}, | ||
], | ||
upsertCredential: vi.fn(), | ||
}); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCannotClaimCredits(); | ||
}); | ||
|
||
it('should not be able to claim credits if active node it is not a valid node', async () => { | ||
(useNDVStore as any).mockReturnValue({ | ||
activeNode: { type: '@n8n/n8n-nodes.jira' }, | ||
}); | ||
|
||
renderComponent(FreeAiCreditsCallout); | ||
|
||
assertUserCannotClaimCredits(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Prevent user from seeing the credential values