Skip to content

Commit 72bc3c2

Browse files
committed
fix(portal-next): only show api key mode selection if shared api key is enabled
1 parent 3e4337c commit 72bc3c2

File tree

2 files changed

+118
-37
lines changed

2 files changed

+118
-37
lines changed

gravitee-apim-portal-webui-next/src/app/api/subscribe-to-api/subscribe-to-api.component.spec.ts

Lines changed: 111 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import { fakePlan } from '../../../entities/plan/plan.fixture';
3737
import { CreateSubscription, Subscription } from '../../../entities/subscription/subscription';
3838
import { fakeSubscription, fakeSubscriptionResponse } from '../../../entities/subscription/subscription.fixture';
3939
import { SubscriptionsResponse } from '../../../entities/subscription/subscriptions-response';
40+
import { ConfigService } from '../../../services/config.service';
4041
import { AppTestingModule, TESTING_BASE_URL } from '../../../testing/app-testing.module';
4142

4243
describe('SubscribeToApiComponent', () => {
@@ -59,9 +60,26 @@ describe('SubscribeToApiComponent', () => {
5960
const APP_ID_NO_SUBSCRIPTIONS = 'app-id-no-subscriptions';
6061
const APP_ID_ONE_API_KEY_SUBSCRIPTION = 'app-id-one-api-key-subscription';
6162

62-
beforeEach(async () => {
63+
const init = async (sharedApiKeyModeEnabled: boolean) => {
6364
await TestBed.configureTestingModule({
6465
imports: [SubscribeToApiComponent, AppTestingModule],
66+
providers: [
67+
{
68+
provide: ConfigService,
69+
useValue: {
70+
baseURL: TESTING_BASE_URL,
71+
configuration: {
72+
plan: {
73+
security: {
74+
sharedApiKey: {
75+
enabled: sharedApiKeyModeEnabled,
76+
},
77+
},
78+
},
79+
},
80+
},
81+
},
82+
],
6583
}).compileComponents();
6684

6785
fixture = TestBed.createComponent(SubscribeToApiComponent);
@@ -84,13 +102,16 @@ describe('SubscribeToApiComponent', () => {
84102
],
85103
});
86104
fixture.detectChanges();
87-
});
105+
};
88106

89107
afterEach(() => {
90108
httpTestingController.verify();
91109
});
92110

93111
describe('User subscribes to Keyless plan', () => {
112+
beforeEach(async () => {
113+
await init(true);
114+
});
94115
describe('Step 1 -- Choose a plan', () => {
95116
it('should be able to go to step 3 once plan chosen', async () => {
96117
const step1 = await harnessLoader.getHarness(SubscribeToApiChoosePlanHarness);
@@ -132,6 +153,10 @@ describe('SubscribeToApiComponent', () => {
132153

133154
describe('User subscribes to API Key plan', () => {
134155
describe('Step 1 -- Choose a plan', () => {
156+
beforeEach(async () => {
157+
await init(true);
158+
});
159+
135160
it('should choose API Key plan and go to step 2', async () => {
136161
const step1 = await harnessLoader.getHarness(SubscribeToApiChoosePlanHarness);
137162
expect(step1).toBeTruthy();
@@ -153,6 +178,8 @@ describe('SubscribeToApiComponent', () => {
153178
});
154179
describe('Step 2 -- Choose an application', () => {
155180
beforeEach(async () => {
181+
await init(true);
182+
156183
const step1 = await harnessLoader.getHarness(SubscribeToApiChoosePlanHarness);
157184
await step1.selectPlanByPlanId(API_KEY_PLAN_ID);
158185
await goToNextStep();
@@ -313,6 +340,7 @@ describe('SubscribeToApiComponent', () => {
313340
describe('Step 3 -- Checkout', () => {
314341
describe('When comment is required', () => {
315342
beforeEach(async () => {
343+
await init(true);
316344
await selectPlan(API_KEY_PLAN_ID_COMMENT_REQUIRED);
317345
await selectApplication();
318346

@@ -346,6 +374,7 @@ describe('SubscribeToApiComponent', () => {
346374
contentRevisionId: { revision: 2, pageId: GENERAL_CONDITIONS_ID },
347375
});
348376
beforeEach(async () => {
377+
await init(true);
349378
await selectPlan(API_KEY_PLAN_ID_GENERAL_CONDITIONS);
350379
await selectApplication();
351380

@@ -388,6 +417,7 @@ describe('SubscribeToApiComponent', () => {
388417
describe('API Key Management', () => {
389418
describe('When a chosen application has no existing subscriptions', () => {
390419
beforeEach(async () => {
420+
await init(true);
391421
await selectPlan(API_KEY_PLAN_ID);
392422
await selectApplication(APP_ID_NO_SUBSCRIPTIONS);
393423

@@ -411,49 +441,86 @@ describe('SubscribeToApiComponent', () => {
411441
});
412442
describe('When a chosen application has one existing API Key subscription', () => {
413443
describe('When the existing API Key subscription is with current API', () => {
414-
beforeEach(async () => {
415-
await selectPlan(API_KEY_PLAN_ID);
416-
await selectApplication(APP_ID_ONE_API_KEY_SUBSCRIPTION);
417-
418-
expectGetApi();
419-
expectGetSubscriptionsForApplication(
420-
APP_ID_ONE_API_KEY_SUBSCRIPTION,
421-
fakeSubscriptionResponse({
422-
data: [fakeSubscription({ plan: 'plan-id', api: API_ID })],
423-
metadata: {
424-
'plan-id': { securityType: 'API_KEY' },
425-
},
426-
}),
427-
);
428-
fixture.detectChanges();
429-
});
430-
it('should show api key mode choice + only allow exclusive', async () => {
431-
const step3 = await harnessLoader.getHarness(SubscribeToApiCheckoutHarness);
432-
expect(await step3.isChooseApiKeyModeVisible()).toBeTruthy();
444+
describe('When Shared API Key mode is enabled', () => {
445+
beforeEach(async () => {
446+
await init(true);
447+
await selectPlan(API_KEY_PLAN_ID);
448+
await selectApplication(APP_ID_ONE_API_KEY_SUBSCRIPTION);
449+
450+
expectGetApi();
451+
expectGetSubscriptionsForApplication(
452+
APP_ID_ONE_API_KEY_SUBSCRIPTION,
453+
fakeSubscriptionResponse({
454+
data: [fakeSubscription({ plan: 'plan-id', api: API_ID })],
455+
metadata: {
456+
'plan-id': { securityType: 'API_KEY' },
457+
},
458+
}),
459+
);
460+
fixture.detectChanges();
461+
});
462+
it('should show api key mode choice + only allow exclusive', async () => {
463+
const step3 = await harnessLoader.getHarness(SubscribeToApiCheckoutHarness);
464+
expect(await step3.isChooseApiKeyModeVisible()).toBeTruthy();
433465

434-
const sharedApiKeyOption = await step3.getSharedApiKeyRadio();
435-
expect(await sharedApiKeyOption.isDisabled()).toEqual(true);
466+
const sharedApiKeyOption = await step3.getSharedApiKeyRadio();
467+
expect(await sharedApiKeyOption.isDisabled()).toEqual(true);
436468

437-
const generatedApiKeyOption = await step3.getGeneratedApiKeyRadio();
438-
expect(await generatedApiKeyOption.isDisabled()).toEqual(false);
469+
const generatedApiKeyOption = await step3.getGeneratedApiKeyRadio();
470+
expect(await generatedApiKeyOption.isDisabled()).toEqual(false);
471+
});
472+
it('should create subscription', async () => {
473+
const step3 = await harnessLoader.getHarness(SubscribeToApiCheckoutHarness);
474+
const generatedApiKeyOption = await step3.getGeneratedApiKeyRadio();
475+
await generatedApiKeyOption.select();
476+
477+
const subscribe = await getSubscribeButton();
478+
await subscribe?.click();
479+
480+
expectPostCreateSubscription({
481+
plan: API_KEY_PLAN_ID,
482+
application: APP_ID_ONE_API_KEY_SUBSCRIPTION,
483+
api_key_mode: 'EXCLUSIVE',
484+
});
485+
});
439486
});
440-
it('should create subscription', async () => {
441-
const step3 = await harnessLoader.getHarness(SubscribeToApiCheckoutHarness);
442-
const generatedApiKeyOption = await step3.getGeneratedApiKeyRadio();
443-
await generatedApiKeyOption.select();
444-
445-
const subscribe = await getSubscribeButton();
446-
await subscribe?.click();
487+
describe('When Shared API Key mode is disabled', () => {
488+
beforeEach(async () => {
489+
await init(false);
490+
await selectPlan(API_KEY_PLAN_ID);
491+
await selectApplication(APP_ID_ONE_API_KEY_SUBSCRIPTION);
492+
493+
expectGetApi();
494+
expectGetSubscriptionsForApplication(
495+
APP_ID_ONE_API_KEY_SUBSCRIPTION,
496+
fakeSubscriptionResponse({
497+
data: [fakeSubscription({ plan: 'plan-id', api: API_ID })],
498+
metadata: {
499+
'plan-id': { securityType: 'API_KEY' },
500+
},
501+
}),
502+
);
503+
fixture.detectChanges();
504+
});
447505

448-
expectPostCreateSubscription({
449-
plan: API_KEY_PLAN_ID,
450-
application: APP_ID_ONE_API_KEY_SUBSCRIPTION,
451-
api_key_mode: 'EXCLUSIVE',
506+
it('should not show api key mode selection', async () => {
507+
const step3 = await harnessLoader.getHarness(SubscribeToApiCheckoutHarness);
508+
expect(await step3.isChooseApiKeyModeVisible()).toBeFalsy();
509+
});
510+
it('should create subscription', async () => {
511+
const subscribe = await getSubscribeButton();
512+
await subscribe?.click();
513+
514+
expectPostCreateSubscription({
515+
plan: API_KEY_PLAN_ID,
516+
application: APP_ID_ONE_API_KEY_SUBSCRIPTION,
517+
});
452518
});
453519
});
454520
});
455521
describe('When the existing API Key subscription is for a different API', () => {
456522
beforeEach(async () => {
523+
await init(true);
457524
await selectPlan(API_KEY_PLAN_ID);
458525
await selectApplication(APP_ID_ONE_API_KEY_SUBSCRIPTION);
459526

@@ -499,6 +566,7 @@ describe('SubscribeToApiComponent', () => {
499566
});
500567
describe('When the API is Federated', () => {
501568
beforeEach(async () => {
569+
await init(true);
502570
await selectPlan(API_KEY_PLAN_ID);
503571
await selectApplication(APP_ID_ONE_API_KEY_SUBSCRIPTION);
504572

@@ -523,6 +591,7 @@ describe('SubscribeToApiComponent', () => {
523591

524592
describe('When comment is NOT required + Terms and conditions NOT required', () => {
525593
beforeEach(async () => {
594+
await init(true);
526595
await selectPlan(API_KEY_PLAN_ID);
527596
await selectApplication();
528597

@@ -552,6 +621,9 @@ describe('SubscribeToApiComponent', () => {
552621
});
553622

554623
describe('User subscribes to OAuth2 plan', () => {
624+
beforeEach(async () => {
625+
await init(true);
626+
});
555627
describe('Step 1 -- Choose a plan', () => {
556628
it('should be disabled', async () => {
557629
const step1 = await harnessLoader.getHarness(SubscribeToApiChoosePlanHarness);
@@ -565,6 +637,9 @@ describe('SubscribeToApiComponent', () => {
565637
});
566638

567639
describe('User subscribes to JWT plan', () => {
640+
beforeEach(async () => {
641+
await init(true);
642+
});
568643
describe('Step 1 -- Choose a plan', () => {
569644
it('should be disabled', async () => {
570645
const step1 = await harnessLoader.getHarness(SubscribeToApiChoosePlanHarness);

gravitee-apim-portal-webui-next/src/app/api/subscribe-to-api/subscribe-to-api.component.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import { CreateSubscription, Subscription } from '../../../entities/subscription
4242
import { SubscriptionsResponse } from '../../../entities/subscription/subscriptions-response';
4343
import { ApiService } from '../../../services/api.service';
4444
import { ApplicationService } from '../../../services/application.service';
45+
import { ConfigService } from '../../../services/config.service';
4546
import { PageService } from '../../../services/page.service';
4647
import { PlanService } from '../../../services/plan.service';
4748
import { SubscriptionService } from '../../../services/subscription.service';
@@ -116,6 +117,7 @@ export class SubscribeToApiComponent implements OnInit {
116117

117118
private currentApplicationsPage: BehaviorSubject<number> = new BehaviorSubject(1);
118119
private destroyRef = inject(DestroyRef);
120+
private configuration = inject(ConfigService).configuration;
119121

120122
constructor(
121123
private planService: PlanService,
@@ -144,7 +146,11 @@ export class SubscribeToApiComponent implements OnInit {
144146
this.checkoutData$ = this.api$.pipe(
145147
switchMap(api => this.handleCheckoutData$(api)),
146148
tap(({ api, applicationApiKeySubscriptions }) => {
147-
this.showApiKeyModeSelection.set(api.definitionVersion !== 'FEDERATED' && applicationApiKeySubscriptions.length === 1);
149+
this.showApiKeyModeSelection.set(
150+
this.configuration.plan?.security?.sharedApiKey?.enabled === true &&
151+
api.definitionVersion !== 'FEDERATED' &&
152+
applicationApiKeySubscriptions.length === 1,
153+
);
148154
}),
149155
);
150156
}

0 commit comments

Comments
 (0)