Skip to content

Commit

Permalink
feat: strip protocol from host if provided during upload [EXT-00] (#2151
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jjolton-contentful authored Oct 1, 2024
1 parent 526977c commit 6bbe36d
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import assert from 'assert';
import sinon from 'sinon';
import inquirer from 'inquirer';

import { buildAppUploadSettings, hostProtocolFilter } from './build-upload-settings';

import * as getAppInfoModule from '../get-app-info';
import * as utilsModule from '../utils';

describe('buildAppUploadSettings', () => {
let promptStub;
let getAppInfoStub;
let getEntityFromManifestStub;

beforeEach(() => {
promptStub = sinon.stub(inquirer, 'prompt');
getAppInfoStub = sinon.stub(getAppInfoModule, 'getAppInfo');
getEntityFromManifestStub = sinon.stub(utilsModule, 'getEntityFromManifest');
});

afterEach(() => {
sinon.restore();
});

for (const protocol of ['http://', 'https://']) {
it(`should strip ${protocol} protocol from host input when provided via prompt`, async () => {
const options = {};
const prompts = {
bundleDirectory: './custom-build',
comment: 'Test comment',
activateBundle: true,
host: `${protocol}api.contentful.com`,
};

// Manually apply the filter function to simulate inquirer's behavior
prompts.host = hostProtocolFilter(prompts.host);

promptStub.resolves(prompts);

getAppInfoStub.resolves({ appId: '123', appName: 'Test App' });

getEntityFromManifestStub.withArgs('actions').returns('actionsManifest');
getEntityFromManifestStub.withArgs('functions').returns('functionsManifest');

const result = await buildAppUploadSettings(options);

assert.strictEqual(result.host, 'api.contentful.com', 'Protocol should be stripped from host');
assert.strictEqual(result.bundleDirectory, './custom-build');
assert.strictEqual(result.comment, 'Test comment');
assert.strictEqual(result.skipActivation, false); // activateBundle is true, so skipActivation should be false
assert.strictEqual(result.actions, 'actionsManifest');
assert.strictEqual(result.functions, 'functionsManifest');
});
}

it('should prompt for missing options and use defaults', async () => {
const options = {};
const prompts = {
bundleDirectory: './build',
comment: '',
activateBundle: true,
host: 'api.contentful.com',
};

promptStub.resolves(prompts);
getAppInfoStub.resolves({ appId: '123', appName: 'Test App' });
getEntityFromManifestStub.withArgs('actions').returns('actionsManifest');
getEntityFromManifestStub.withArgs('functions').returns('functionsManifest');

const result = await buildAppUploadSettings(options);

assert.strictEqual(result.bundleDirectory, './build');
assert.strictEqual(result.comment, '');
assert.strictEqual(result.skipActivation, false);
assert.strictEqual(result.host, 'api.contentful.com');
});

it('should handle skipActivation correctly when option is provided', async () => {
const options = {
skipActivation: true,
};

promptStub.resolves({});
getAppInfoStub.resolves({ appId: '123', appName: 'Test App' });
getEntityFromManifestStub.withArgs('actions').returns('actionsManifest');
getEntityFromManifestStub.withArgs('functions').returns('functionsManifest');

const result = await buildAppUploadSettings(options);

assert.strictEqual(result.skipActivation, true);
});

it('should correctly handle activateBundle prompt when skipActivation is undefined', async () => {
const options = {};
const prompts = {
activateBundle: false,
host: 'api.contentful.com',
};

promptStub.resolves(prompts);
getAppInfoStub.resolves({ appId: '123', appName: 'Test App' });
getEntityFromManifestStub.withArgs('actions').returns('actionsManifest');
getEntityFromManifestStub.withArgs('functions').returns('functionsManifest');

const result = await buildAppUploadSettings(options);

assert.strictEqual(result.skipActivation, true);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export async function buildAppUploadSettings(options: UploadOptions): Promise<Up
name: 'host',
message: `Contentful CMA endpoint URL:`,
default: DEFAULT_CONTENTFUL_API_HOST,
filter: hostProtocolFilter,
});
}

Expand All @@ -55,3 +56,7 @@ export async function buildAppUploadSettings(options: UploadOptions): Promise<Up
...appInfo,
};
}

export function hostProtocolFilter (input: string) {
return input.replace(/^https?:\/\//, '');
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ describe('createAppUpload', () => {
const appUpload = await createAppUpload(mockedSettings);
assert.strictEqual(appUpload, uploadMock);
});

it('shows creation error when createAppUpload throws', async () => {
clientMock = {
getOrganization: () => ({
Expand Down

0 comments on commit 6bbe36d

Please sign in to comment.