Skip to content

Commit

Permalink
feat(editor): Don't list managed credentials in HTTP node (no-changel…
Browse files Browse the repository at this point in the history
…og) (#12409)
  • Loading branch information
RicardoE105 authored Dec 31, 2024
1 parent 8dc691d commit 4e9f2b3
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 1 deletion.
135 changes: 135 additions & 0 deletions packages/editor-ui/src/components/NodeCredentials.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import { describe, it } from 'vitest';
import { fireEvent, screen } from '@testing-library/vue';
import { createTestingPinia } from '@pinia/testing';
import NodeCredentials from './NodeCredentials.vue';
import type { RenderOptions } from '@/__tests__/render';
import { createComponentRenderer } from '@/__tests__/render';
import { useCredentialsStore } from '@/stores/credentials.store';
import { mockedStore } from '@/__tests__/utils';
import type { INodeUi } from '@/Interface';

const httpNode: INodeUi = {
parameters: {
curlImport: '',
method: 'GET',
url: '',
authentication: 'predefinedCredentialType',
nodeCredentialType: 'openAiApi',
provideSslCertificates: false,
sendQuery: false,
sendHeaders: false,
sendBody: false,
options: {},
infoMessage: '',
},
type: 'n8n-nodes-base.httpRequest',
typeVersion: 4.2,
position: [-200, -160],
id: '416988b5-e994-42c7-8576-6ef28a7619b2',
name: 'HTTP Request',
credentials: { openAiApi: { id: 'c8vqdPpPClh4TgIO', name: 'OpenAi account 2' } },
issues: { parameters: { url: ['Parameter "URL" is required.'] } },
};

describe('NodeCredentials', () => {
const defaultRenderOptions: RenderOptions = {
pinia: createTestingPinia(),
props: {
overrideCredType: 'openAiApi',
node: httpNode,
readonly: false,
showAll: false,
hideIssues: false,
},
};

const renderComponent = createComponentRenderer(NodeCredentials, defaultRenderOptions);

beforeAll(() => {
credentialsStore.state.credentialTypes = {
openAiApi: {
name: 'openAiApi',
displayName: 'OpenAi',
documentationUrl: 'openAi',
properties: [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string',
typeOptions: { password: true },
required: true,
default: '',
},
],
authenticate: {
type: 'generic',
properties: {
headers: {
Authorization: '=Bearer {{$credentials.apiKey}}',
'OpenAI-Organization': '={{$credentials.organizationId}}',
},
},
},
test: { request: { baseURL: '={{$credentials?.url}}', url: '/models' } },
supportedNodes: ['openAi'],
iconUrl: {
light: 'icons/n8n-nodes-base/dist/nodes/OpenAi/openAi.svg',
dark: 'icons/n8n-nodes-base/dist/nodes/OpenAi/openAi.dark.svg',
},
},
};
});

const credentialsStore = mockedStore(useCredentialsStore);

it('should display available credentials in the dropdown', async () => {
credentialsStore.state.credentials = {
c8vqdPpPClh4TgIO: {
id: 'c8vqdPpPClh4TgIO',
name: 'OpenAi account',
type: 'openAiApi',
isManaged: false,
createdAt: '',
updatedAt: '',
},
};

renderComponent();

const credentialsSelect = screen.getByTestId('node-credentials-select');

await fireEvent.click(credentialsSelect);

expect(screen.queryByText('OpenAi account')).toBeInTheDocument();
});

it('should ignore managed credentials in the dropdown', async () => {
credentialsStore.state.credentials = {
c8vqdPpPClh4TgIO: {
id: 'c8vqdPpPClh4TgIO',
name: 'OpenAi account',
type: 'openAiApi',
isManaged: false,
createdAt: '',
updatedAt: '',
},
SkXM3oUkQvvYS31c: {
id: 'c8vqdPpPClh4TgIO',
name: 'OpenAi account 2',
type: 'openAiApi',
isManaged: true,
createdAt: '',
updatedAt: '',
},
};

renderComponent();

const credentialsSelect = screen.getByTestId('node-credentials-select');

await fireEvent.click(credentialsSelect);

expect(screen.queryByText('OpenAi account')).toBeInTheDocument();
expect(screen.queryByText('OpenAi account 2')).not.toBeInTheDocument();
});
});
3 changes: 2 additions & 1 deletion packages/editor-ui/src/components/NodeCredentials.vue
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ function getCredentialOptions(types: string[]): CredentialDropdownOption[] {
),
);
});
return options;
return options.filter((option) => !option.isManaged);
}
function getSelectedId(type: string) {
Expand Down

0 comments on commit 4e9f2b3

Please sign in to comment.