Skip to content

Commit 8a1dc61

Browse files
authored
fix: add SCA check and warning with link to activate it (#136)
Signed-off-by: Denis Golovin <[email protected]>
1 parent 1855ab0 commit 8a1dc61

File tree

2 files changed

+97
-4
lines changed

2 files changed

+97
-4
lines changed

src/extension.spec.ts

+73-4
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
***********************************************************************/
1818

19-
import { beforeEach, expect, suite, test, vi } from 'vitest';
20-
import { activate } from './extension';
19+
import { beforeEach, afterEach, expect, suite, test, vi } from 'vitest';
20+
import * as extension from './extension';
2121
import {
2222
AuthenticationGetSessionOptions,
2323
TelemetryLogger,
@@ -28,6 +28,7 @@ import {
2828
import { authentication, commands } from '@podman-desktop/api';
2929
import * as podmanCli from './podman-cli';
3030
import { ExtensionTelemetryLogger } from './telemetry';
31+
import { OrganizationService } from '@redhat-developer/rhsm-client';
3132

3233
vi.mock('@podman-desktop/api', async () => {
3334
return {
@@ -104,11 +105,15 @@ function createExtContext(): ExtensionContext {
104105

105106
beforeEach(() => {
106107
vi.resetAllMocks();
107-
activate(createExtContext());
108+
});
109+
110+
afterEach(() => {
111+
extension.deactivate();
108112
});
109113

110114
suite('extension activation', () => {
111115
test('register commands declared in package.json', async () => {
116+
await extension.activate(createExtContext());
112117
expect(commands.registerCommand).toBeCalledTimes(4);
113118
expect(commands.registerCommand).toBeCalledWith('redhat.authentication.signin', expect.anything());
114119
expect(commands.registerCommand).toBeCalledWith('redhat.authentication.navigate.settings', expect.anything());
@@ -171,7 +176,7 @@ suite('signin command telemetry reports', () => {
171176
},
172177
);
173178
vi.spyOn(podmanCli, 'isPodmanMachineRunning').mockReturnValue(false);
174-
await activate(createExtContext());
179+
await extension.activate(createExtContext());
175180
expect(commandFunctionCopy!).toBeDefined();
176181
await commandFunctionCopy!();
177182
expect(authentication.onDidChangeSessions).toBeCalled();
@@ -181,4 +186,68 @@ suite('signin command telemetry reports', () => {
181186
errorIn: 'subscription-activation',
182187
});
183188
});
189+
190+
test('unsuccessful login when simple content access is not enabled for account', async () => {
191+
const logSpy = vi.spyOn(ExtensionTelemetryLogger, 'logUsage').mockImplementation(() => {
192+
return;
193+
});
194+
let notificationCallback: (event: any) => Promise<any>;
195+
let session: AuthenticationSession;
196+
vi.mocked(authentication.onDidChangeSessions).mockImplementation((callback: (event: any) => Promise<any>) => {
197+
notificationCallback = callback;
198+
return {
199+
dispose: vi.fn(),
200+
};
201+
});
202+
vi.mocked(authentication.getSession).mockImplementation(
203+
async (
204+
_p1: string,
205+
_p2: string[],
206+
options: AuthenticationGetSessionOptions | undefined,
207+
): Promise<AuthenticationSession | undefined> => {
208+
if (session) {
209+
return Promise.resolve(session);
210+
}
211+
if (options?.createIfNone) {
212+
session = {
213+
id: '1',
214+
accessToken: 'token',
215+
scopes: ['scope1', 'scope2'],
216+
account: {
217+
id: 'accountId',
218+
label: 'label',
219+
},
220+
};
221+
await notificationCallback({
222+
provider: {
223+
id: 'redhat.authentication-provider',
224+
},
225+
});
226+
}
227+
return;
228+
},
229+
);
230+
let commandFunctionCopy: () => Promise<void>;
231+
vi.mocked(commands.registerCommand).mockImplementation(
232+
(commandId: string, commandFunction: () => Promise<void>) => {
233+
if (commandId === 'redhat.authentication.signin') {
234+
commandFunctionCopy = commandFunction;
235+
}
236+
return {
237+
dispose: vi.fn(),
238+
};
239+
},
240+
);
241+
vi.spyOn(podmanCli, 'isPodmanMachineRunning').mockReturnValue(true);
242+
vi.spyOn(OrganizationService.prototype, 'checkOrgScaCapability').mockResolvedValue({ body: {} });
243+
await extension.activate(createExtContext());
244+
expect(commandFunctionCopy!).toBeDefined();
245+
await commandFunctionCopy!();
246+
expect(authentication.onDidChangeSessions).toBeCalled();
247+
expect(logSpy).toBeCalledWith('signin', {
248+
successful: false,
249+
error: 'Error: SCA is not enabled and message closed',
250+
errorIn: 'subscription-activation',
251+
});
252+
});
184253
});

src/extension.ts

+24
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ async function createOrReuseActivationKey() {
187187
await runSubscriptionManagerRegister('podman-desktop', accessTokenJson.organization.id);
188188
}
189189

190+
async function isSimpleContentAccessEnabled(): Promise<boolean> {
191+
const currentSession = await signIntoRedHatDeveloperAccount();
192+
const client = new SubscriptionManagerClient({
193+
BASE: 'https://console.redhat.com/api/rhsm/v2',
194+
TOKEN: currentSession!.accessToken,
195+
});
196+
const response = await client.organization.checkOrgScaCapability();
197+
return response.body && response.body.simpleContentAccess === 'enabled';
198+
}
199+
190200
async function isSubscriptionManagerInstalled(): Promise<boolean> {
191201
const exitCode = await runSubscriptionManager();
192202
return exitCode === 0;
@@ -279,6 +289,19 @@ async function configureRegistryAndActivateSubscription() {
279289
}
280290
return;
281291
} else {
292+
if (!(await isSimpleContentAccessEnabled())) {
293+
const choice = await extensionApi.window.showInformationMessage(
294+
'Simple Content Access (SCA) is not enabled for your Red Hat account. Please enable it and try again.',
295+
'Close',
296+
'Enable SCA',
297+
);
298+
if (choice === 'Enable SCA') {
299+
extensionApi.env.openExternal(extensionApi.Uri.parse('https://access.redhat.com/management'));
300+
throw new Error('SCA is not enabled and subscription management page requested');
301+
}
302+
throw new Error('SCA is not enabled and message closed');
303+
}
304+
282305
if (!(await isSubscriptionManagerInstalled())) {
283306
await installSubscriptionManger();
284307
await runStopPodmanMachine();
@@ -415,4 +438,5 @@ export async function activate(context: extensionApi.ExtensionContext): Promise<
415438

416439
export function deactivate(): void {
417440
console.log('stopping redhat-authentication extension');
441+
currentSession = undefined;
418442
}

0 commit comments

Comments
 (0)