mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-17 05:29:44 +02:00
fix: replace type casting with String() conversion and add tests
This commit is contained in:
parent
73e7843aa1
commit
ea2763337e
|
|
@ -54,14 +54,14 @@ export class WooCommerceApi implements ICredentialType {
|
|||
requestOptions.qs = {};
|
||||
}
|
||||
Object.assign(requestOptions.qs, {
|
||||
consumer_key: credentials.consumerKey,
|
||||
consumer_secret: credentials.consumerSecret,
|
||||
consumer_key: String(credentials.consumerKey || ''),
|
||||
consumer_secret: String(credentials.consumerSecret || ''),
|
||||
});
|
||||
} else {
|
||||
// Use HTTP Basic Auth
|
||||
requestOptions.auth = {
|
||||
user: credentials.consumerKey as string,
|
||||
password: credentials.consumerSecret as string,
|
||||
user: String(credentials.consumerKey || ''),
|
||||
password: String(credentials.consumerSecret || ''),
|
||||
};
|
||||
}
|
||||
return requestOptions;
|
||||
|
|
|
|||
92
packages/nodes-base/credentials/WooCommerceApi.test.ts
Normal file
92
packages/nodes-base/credentials/WooCommerceApi.test.ts
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
import { WooCommerceApi } from './WooCommerceApi.credentials';
|
||||
import type { ICredentialDataDecryptedObject, IHttpRequestOptions } from 'n8n-workflow';
|
||||
|
||||
describe('WooCommerceApi', () => {
|
||||
let wooCommerceApi: WooCommerceApi;
|
||||
|
||||
beforeEach(() => {
|
||||
wooCommerceApi = new WooCommerceApi();
|
||||
});
|
||||
|
||||
describe('authenticate', () => {
|
||||
it('should use Basic Auth by default', async () => {
|
||||
const credentials: ICredentialDataDecryptedObject = {
|
||||
consumerKey: 'test_key',
|
||||
consumerSecret: 'test_secret',
|
||||
includeCredentialsInQuery: false,
|
||||
};
|
||||
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
url: 'https://example.com/api',
|
||||
};
|
||||
|
||||
const result = await wooCommerceApi.authenticate(credentials, requestOptions);
|
||||
|
||||
expect(result.auth).toEqual({
|
||||
user: 'test_key',
|
||||
password: 'test_secret',
|
||||
});
|
||||
expect(result.qs).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should use query parameters when includeCredentialsInQuery is true', async () => {
|
||||
const credentials: ICredentialDataDecryptedObject = {
|
||||
consumerKey: 'test_key',
|
||||
consumerSecret: 'test_secret',
|
||||
includeCredentialsInQuery: true,
|
||||
};
|
||||
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
url: 'https://example.com/api',
|
||||
};
|
||||
|
||||
const result = await wooCommerceApi.authenticate(credentials, requestOptions);
|
||||
|
||||
expect(result.qs).toEqual({
|
||||
consumer_key: 'test_key',
|
||||
consumer_secret: 'test_secret',
|
||||
});
|
||||
expect(result.auth).toBeUndefined();
|
||||
});
|
||||
|
||||
it('should handle existing query parameters', async () => {
|
||||
const credentials: ICredentialDataDecryptedObject = {
|
||||
consumerKey: 'test_key',
|
||||
consumerSecret: 'test_secret',
|
||||
includeCredentialsInQuery: true,
|
||||
};
|
||||
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
url: 'https://example.com/api',
|
||||
qs: { existing: 'param' },
|
||||
};
|
||||
|
||||
const result = await wooCommerceApi.authenticate(credentials, requestOptions);
|
||||
|
||||
expect(result.qs).toEqual({
|
||||
existing: 'param',
|
||||
consumer_key: 'test_key',
|
||||
consumer_secret: 'test_secret',
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle non-string credentials gracefully', async () => {
|
||||
const credentials: ICredentialDataDecryptedObject = {
|
||||
consumerKey: null,
|
||||
consumerSecret: undefined,
|
||||
includeCredentialsInQuery: false,
|
||||
};
|
||||
|
||||
const requestOptions: IHttpRequestOptions = {
|
||||
url: 'https://example.com/api',
|
||||
};
|
||||
|
||||
const result = await wooCommerceApi.authenticate(credentials, requestOptions);
|
||||
|
||||
expect(result.auth).toEqual({
|
||||
user: '',
|
||||
password: '',
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user