Skip to content

Commit c5e5864

Browse files
Sagar Miglanisagarmiglani
authored andcommitted
fix: Use env variable for aemy endpoint, minor fix and added more tests
1 parent 3e51573 commit c5e5864

File tree

2 files changed

+92
-17
lines changed

2 files changed

+92
-17
lines changed

src/support/slack/commands/add-repo.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,16 @@ function AddRepoCommand(context) {
6969
}
7070

7171
async function isOnboardedWithAemy(owner, repo, branch) {
72-
const AEMY_ENDPOINT = `https://ec-xp-fapp-coordinator.azurewebsites.net/api/fn-ghapp/functions/get_installation_token/${owner}/${repo}/${branch}`;
72+
const { AEMY_BASE_URL } = process.env;
73+
if (!AEMY_BASE_URL) {
74+
throw new Error('AEMY_BASE_URL is not set');
75+
}
76+
const AEMY_ENDPOINT = `${AEMY_BASE_URL}/api/fn-ghapp/functions/get_installation_token/${owner}/${repo}/${branch}`;
7377
try {
7478
const response = await fetch(AEMY_ENDPOINT, { headers: { 'x-api-key': process.env.AEMY_API_KEY } });
7579
if (response.ok) {
7680
const data = await response.json();
77-
return data.token !== null;
81+
return !!data.token;
7882
} else {
7983
throw new Error('Failed to check if repository is onboarded with Aemy');
8084
}

test/support/slack/commands/add-repo.test.js

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ describe('AddRepoCommand', () => {
2424
let dataAccessStub;
2525
let sqsStub;
2626
let siteStub;
27+
const AEMY_BASE_URL = 'https://test-aemy-base-url.net';
2728

2829
beforeEach(() => {
2930
process.env.AEMY_API_KEY = 'test-api-key';
31+
process.env.AEMY_BASE_URL = AEMY_BASE_URL;
3032

3133
sqsStub = {
3234
sendMessage: sinon.stub().resolves(),
@@ -68,10 +70,7 @@ describe('AddRepoCommand', () => {
6870
context = {
6971
dataAccess: dataAccessStub,
7072
sqs: sqsStub,
71-
env: {
72-
AUDIT_JOBS_QUEUE_URL: 'testQueueUrl',
73-
AEMY_API_KEY: 'test-api-key',
74-
},
73+
env: { AUDIT_JOBS_QUEUE_URL: 'testQueueUrl' },
7574
log: console,
7675
};
7776
});
@@ -99,7 +98,7 @@ describe('AddRepoCommand', () => {
9998
default_branch: 'main',
10099
});
101100

102-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
101+
const aemyScope = nock(AEMY_BASE_URL)
103102
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
104103
.reply(200, { token: 'some-token' });
105104

@@ -144,7 +143,7 @@ describe('AddRepoCommand', () => {
144143
default_branch: 'main',
145144
});
146145

147-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
146+
const aemyScope = nock(AEMY_BASE_URL)
148147
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
149148
.reply(200, { token: 'some-token' });
150149

@@ -237,7 +236,7 @@ describe('AddRepoCommand', () => {
237236
default_branch: 'main',
238237
});
239238

240-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
239+
const aemyScope = nock(AEMY_BASE_URL)
241240
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
242241
.reply(200, { token: 'some-token' });
243242

@@ -260,7 +259,7 @@ describe('AddRepoCommand', () => {
260259
.get('/repos/private/repo')
261260
.reply(404);
262261

263-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
262+
const aemyScope = nock(AEMY_BASE_URL)
264263
.get('/api/fn-ghapp/functions/get_installation_token/private/repo/main')
265264
.reply(200, { token: 'some-token' });
266265

@@ -323,7 +322,7 @@ describe('AddRepoCommand', () => {
323322
default_branch: 'main',
324323
});
325324

326-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
325+
const aemyScope = nock(AEMY_BASE_URL)
327326
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/develop')
328327
.reply(200, { token: 'some-token' });
329328

@@ -348,7 +347,7 @@ describe('AddRepoCommand', () => {
348347
.get('/repos/private/repo')
349348
.reply(404);
350349

351-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
350+
const aemyScope = nock(AEMY_BASE_URL)
352351
.get('/api/fn-ghapp/functions/get_installation_token/private/repo/feature-branch')
353352
.reply(200, { token: 'some-token' });
354353

@@ -379,7 +378,7 @@ describe('AddRepoCommand', () => {
379378
.get('/repos/private/repo')
380379
.reply(404);
381380

382-
const aemyScope = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
381+
const aemyScope = nock(AEMY_BASE_URL)
383382
.get('/api/fn-ghapp/functions/get_installation_token/private/repo/main')
384383
.reply(200, { token: 'some-token' });
385384

@@ -401,7 +400,9 @@ describe('AddRepoCommand', () => {
401400
});
402401

403402
describe('Aemy Integration', () => {
404-
it('blocks repository not onboarded with Aemy', async () => {
403+
it('throws error when AEMY_BASE_URL is not set', async () => {
404+
delete process.env.AEMY_BASE_URL;
405+
405406
nock('https://api.github.com')
406407
.get('/repos/valid/repo')
407408
.reply(200, {
@@ -411,7 +412,29 @@ describe('AddRepoCommand', () => {
411412
default_branch: 'main',
412413
});
413414

414-
const aemyNock = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
415+
const args = ['validSite.com', 'https://github.com/valid/repo'];
416+
const command = AddRepoCommand(context);
417+
418+
await command.handleExecution(args, slackContext);
419+
420+
expect(slackContext.say.calledWithMatch(/AEMY_BASE_URL is not set/)).to.be.true;
421+
expect(siteStub.save).to.not.have.been.called;
422+
423+
// Restore for subsequent tests
424+
process.env.AEMY_BASE_URL = AEMY_BASE_URL;
425+
});
426+
427+
it('blocks repository not onboarded with Aemy (token is null)', async () => {
428+
nock('https://api.github.com')
429+
.get('/repos/valid/repo')
430+
.reply(200, {
431+
archived: false,
432+
name: 'repo',
433+
owner: { login: 'valid' },
434+
default_branch: 'main',
435+
});
436+
437+
const aemyNock = nock(AEMY_BASE_URL)
415438
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
416439
.reply(200, { token: null });
417440

@@ -425,6 +448,54 @@ describe('AddRepoCommand', () => {
425448
expect(siteStub.save).to.not.have.been.called;
426449
});
427450

451+
it('blocks repository not onboarded with Aemy (token is empty string)', async () => {
452+
nock('https://api.github.com')
453+
.get('/repos/valid/repo')
454+
.reply(200, {
455+
archived: false,
456+
name: 'repo',
457+
owner: { login: 'valid' },
458+
default_branch: 'main',
459+
});
460+
461+
const aemyNock = nock(AEMY_BASE_URL)
462+
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
463+
.reply(200, { token: '' });
464+
465+
const args = ['validSite.com', 'https://github.com/valid/repo'];
466+
const command = AddRepoCommand(context);
467+
468+
await command.handleExecution(args, slackContext);
469+
470+
expect(aemyNock.isDone()).to.be.true;
471+
expect(slackContext.say.calledWith(':warning: The repository \'https://github.com/valid/repo\' is not onboarded with Aemy. Please onboard it with Aemy before adding it to a site.')).to.be.true;
472+
expect(siteStub.save).to.not.have.been.called;
473+
});
474+
475+
it('blocks repository not onboarded with Aemy (token is undefined)', async () => {
476+
nock('https://api.github.com')
477+
.get('/repos/valid/repo')
478+
.reply(200, {
479+
archived: false,
480+
name: 'repo',
481+
owner: { login: 'valid' },
482+
default_branch: 'main',
483+
});
484+
485+
const aemyNock = nock(AEMY_BASE_URL)
486+
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
487+
.reply(200, {});
488+
489+
const args = ['validSite.com', 'https://github.com/valid/repo'];
490+
const command = AddRepoCommand(context);
491+
492+
await command.handleExecution(args, slackContext);
493+
494+
expect(aemyNock.isDone()).to.be.true;
495+
expect(slackContext.say.calledWith(':warning: The repository \'https://github.com/valid/repo\' is not onboarded with Aemy. Please onboard it with Aemy before adding it to a site.')).to.be.true;
496+
expect(siteStub.save).to.not.have.been.called;
497+
});
498+
428499
it('handles Aemy API error responses', async () => {
429500
nock('https://api.github.com')
430501
.get('/repos/valid/repo')
@@ -435,7 +506,7 @@ describe('AddRepoCommand', () => {
435506
default_branch: 'main',
436507
});
437508

438-
const aemyNock = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
509+
const aemyNock = nock(AEMY_BASE_URL)
439510
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
440511
.reply(500, { error: 'Internal Server Error' });
441512

@@ -459,7 +530,7 @@ describe('AddRepoCommand', () => {
459530
default_branch: 'main',
460531
});
461532

462-
const aemyNock = nock('https://ec-xp-fapp-coordinator.azurewebsites.net')
533+
const aemyNock = nock(AEMY_BASE_URL)
463534
.get('/api/fn-ghapp/functions/get_installation_token/valid/repo/main')
464535
.replyWithError('Network error');
465536

0 commit comments

Comments
 (0)