Skip to content

Commit

Permalink
Merge pull request #53 from snyk-tech-services/feat/validate-org-id
Browse files Browse the repository at this point in the history
feat: validate orgPublic ID and throw if org not found
  • Loading branch information
lili2311 authored Dec 14, 2020
2 parents 6e6f076 + 8c703ad commit 61dc29d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/cmds/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export async function handler(argv: {
}),
);
getApiToken();
const orgData = await getOrgData(orgPublicId);
const options = {
filters: {
projects: _.castArray(project),
Expand All @@ -85,7 +86,6 @@ export async function handler(argv: {
orgPublicId,
options,
);
const orgData = await getOrgData(orgPublicId);
const reportData = await generateHtmlReport(
orgPublicId,
licenseData,
Expand All @@ -103,6 +103,6 @@ export async function handler(argv: {
`${outputFormat.toUpperCase()} license report saved at: ${fileName}`,
);
} catch (e) {
console.error(e);
console.error(e.message || e);
}
}
7 changes: 6 additions & 1 deletion src/lib/get-org-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ export async function getOrgData(orgPublicId: string): Promise<OrgData> {
try {
const snykApiClient = await new snykApiSdk.Orgs();
const allOrgs: OrgData[] = _.get(await snykApiClient.get(), 'orgs', []);
const orgData = allOrgs.filter(org => org.id === orgPublicId)[0];
const orgData = allOrgs.filter((org) => org.id === orgPublicId)[0];
if (_.isEmpty(orgData)) {
throw new Error(
`No organization data found for provided ID: ${orgPublicId}.\nPlease check that the --orgPublicId is the correct public ID found in Organization Settings page on https://app.snyk.io/org/<ORG_NAME>/manage/settings`,
);
}
return orgData;
} catch (e) {
debug('❌ Failed to get org data for org ID:' + orgPublicId);
Expand Down
5 changes: 5 additions & 0 deletions test/system/__snapshots__/generate.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`\`snyk-licenses-report generate <...>\` Shows error when invalid --orgPublicId 1`] = `
"No organization data found for provided ID: my-org.
Please check that the --orgPublicId is the correct public ID found in Organization Settings page on https://app.snyk.io/org/<ORG_NAME>/manage/settings"
`;
exports[`\`snyk-licenses-report generate <...>\` Shows error when missing --orgPublicId 1`] = `
"Command failed: node ./dist/index.js generate
index.js generate
Expand Down
24 changes: 21 additions & 3 deletions test/system/generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ describe('`snyk-licenses-report generate <...>`', () => {
);
});

it('Shows error when invalid --orgPublicId', async (done) => {
exec(
`node ${main} generate --orgPublicId=my-org`,
{
env: {
PATH: process.env.PATH,
SNYK_TOKEN: process.env.SNYK_TEST_TOKEN,
},
},
(err, stdout, stderr) => {
expect(stdout).toBe('');
expect(err).toBeNull();
expect(stderr.trim()).toMatchSnapshot();
done();
},
);
});

it('generated the report successfully with default params', (done) => {
exec(
`node ${main} generate --orgPublicId=${ORG_ID}`,
Expand All @@ -37,7 +55,7 @@ describe('`snyk-licenses-report generate <...>`', () => {
done();
},
);
}, 50000);
}, 80000);

it.skip('generated the report successfully as PDF', (done) => {
exec(
Expand All @@ -54,7 +72,7 @@ describe('`snyk-licenses-report generate <...>`', () => {
done();
},
);
}, 50000);
}, 80000);
it('generated the report successfully with custom template', (done) => {
exec(
`node ${main} generate --orgPublicId=${ORG_ID} --template=${__dirname +
Expand All @@ -71,7 +89,7 @@ describe('`snyk-licenses-report generate <...>`', () => {
done();
},
);
}, 50000);
}, 80000);
it.todo('API is down');
it.todo('Requested org has no licenses policy');
});

0 comments on commit 61dc29d

Please sign in to comment.