Skip to content

Commit bdccdcf

Browse files
committedApr 22, 2024··
basic tests for expected cf api org responses
instructs jest to ignore mocks directory adds interface for api org response
1 parent f899d21 commit bdccdcf

File tree

9 files changed

+100
-13
lines changed

9 files changed

+100
-13
lines changed
 

‎.env.test

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ UAA_AUTH_PATH=/oauth/authorize
77
UAA_TOKEN_PATH=/oauth/token
88
UAA_LOGOUT_PATH=/logout.do
99

10+
CF_API_URL=https://example.com
11+
CF_API_TOKEN=placeholder

‎__tests__/api/cloudfoundry.test.js

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
1-
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
2-
import { getToken } from '../../api/cloudfoundry';
1+
import nock from 'nock';
32
import { cookies } from 'next/headers';
3+
import { describe, it, expect, beforeEach, afterEach } from '@jest/globals';
4+
import {
5+
getToken,
6+
getCFOrg,
7+
getCFOrgUsers,
8+
getCFOrgs,
9+
} from '../../api/cloudfoundry';
10+
import {
11+
mockOrg,
12+
mockOrgNotFound,
13+
mockOrgUsers,
14+
mockOrgs,
15+
} from './mocks/organizations';
416

517
/* global jest */
618
/* eslint no-undef: "off" */
@@ -10,6 +22,54 @@ jest.mock('next/headers', () => ({
1022
/* eslint no-undef: "error" */
1123

1224
describe('cloudfoundry tests', () => {
25+
describe('getCFOrg', () => {
26+
it('when given a valid org guid, returns a single org', async () => {
27+
nock(process.env.CF_API_URL)
28+
.get('/organizations/validGUID')
29+
.reply(200, mockOrg);
30+
const res = await getCFOrg('validGUID');
31+
expect(res).toEqual(mockOrg);
32+
});
33+
34+
it('when given an invalid or unauthorized org guid, throws an error', async () => {
35+
nock(process.env.CF_API_URL)
36+
.get('/organizations/invalidGUID')
37+
.reply(404, mockOrgNotFound);
38+
39+
expect(async () => {
40+
await getCFOrg('invalidGUID');
41+
}).rejects.toThrow(new Error('an error occurred with response code 404'));
42+
});
43+
});
44+
45+
describe('getCFOrgUsers', () => {
46+
it('when given a valid org guid, returns associated users', async () => {
47+
nock(process.env.CF_API_URL)
48+
.get('/organizations/validGUID/users')
49+
.reply(200, mockOrgUsers);
50+
const res = await getCFOrgUsers('validGUID');
51+
expect(res).toEqual(mockOrgUsers.resources);
52+
});
53+
54+
it('when given an invalid or unauthorized org guid, throws an error', async () => {
55+
nock(process.env.CF_API_URL)
56+
.get('/organizations/invalidGUID/users')
57+
.reply(404, mockOrgNotFound);
58+
59+
expect(async () => {
60+
await getCFOrgUsers('invalidGUID');
61+
}).rejects.toThrow(new Error('an error occurred with response code 404'));
62+
});
63+
});
64+
65+
describe('get CFOrgs', () => {
66+
it('returns orgs available to the user', async () => {
67+
nock(process.env.CF_API_URL).get('/organizations').reply(200, mockOrgs);
68+
const res = await getCFOrgs();
69+
expect(res).toEqual(mockOrgs.resources);
70+
});
71+
});
72+
1373
describe('getToken', () => {
1474
describe('when token environment variable is set', () => {
1575
beforeEach(() => {

‎__tests__/api/mocks/organizations.js

+11
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ export const mockOrg = {
3232
},
3333
};
3434

35+
// /v3/organizations/[bad-guid]
36+
export const mockOrgNotFound = {
37+
errors: [
38+
{
39+
detail: 'Organization not found',
40+
title: 'CF-ResourceNotFound',
41+
code: 10010,
42+
},
43+
],
44+
};
45+
3546
// /v3/organizations/[guid]/users
3647
export const mockOrgUsers = {
3748
pagination: {

‎api/cloudfoundry.ts

+18-5
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,28 @@ export async function getCFResources(resourcePath: string) {
1818
} else {
1919
throw new Error('resources not found');
2020
}
21-
} catch (error) {
21+
} catch (error: any) {
2222
throw new Error(error.message);
2323
}
2424
}
2525
export async function getCFApps() {
2626
return getCFResources('/apps');
2727
}
2828

29-
export async function getCFOrg(guid: string) {
29+
interface CfOrg {
30+
guid: string;
31+
created_at: string;
32+
updated_at: string;
33+
name: string;
34+
suspended: boolean;
35+
// relationships, metadata, and links are all objects, but as we
36+
// do not rely on them existing yet, they are not defined in the interface
37+
relationships: any;
38+
metadata: any;
39+
links: any;
40+
}
41+
42+
export async function getCFOrg(guid: string): Promise<CfOrg> {
3043
try {
3144
const body = await getData(CF_API_URL + '/organizations/' + guid, {
3245
headers: {
@@ -36,9 +49,9 @@ export async function getCFOrg(guid: string) {
3649
if (body) {
3750
return body;
3851
} else {
39-
throw new Error('resources not found');
52+
throw new Error('resource not found');
4053
}
41-
} catch (error) {
54+
} catch (error: any) {
4255
throw new Error(error.message);
4356
}
4457
}
@@ -62,7 +75,7 @@ export function getCFToken(): string {
6275
if (authSession === undefined) throw new Error();
6376
try {
6477
return JSON.parse(authSession.value).accessToken;
65-
} catch (error) {
78+
} catch (error: any) {
6679
throw new Error('accessToken not found, please confirm you are logged in');
6780
}
6881
}

‎app/cloudfoundry/orgs/[guid]/page.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ export default async function OrgPage({
2020
<li>Suspended: {org.suspended}</li>
2121
<li>Created: {org.created_at}</li>
2222
</ul>
23-
{/* <div>{JSON.stringify(org)}</div> */}
2423

2524
<h2>Org members</h2>
2625
<ul>
@@ -32,7 +31,7 @@ export default async function OrgPage({
3231
</ul>
3332
</>
3433
);
35-
} catch (error) {
34+
} catch (error: any) {
3635
return <div>{error.message}</div>;
3736
}
3837
}

‎app/cloudfoundry/orgs/page.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default async function CloudFoundryOrgsPage() {
1717
</ul>
1818
</>
1919
);
20-
} catch (error) {
20+
} catch (error: any) {
2121
return <div role="alert">{error.message}</div>;
2222
}
2323
}

‎jest.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const config = {
1313
testEnvironment: 'node',
1414
// Add more setup options before each test is run
1515
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
16+
testPathIgnorePatterns: ['<rootDir>/__tests__/api/mocks'],
1617
};
1718

1819
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async

‎public/js/uswds/uswds.min.js

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222
{
2323
"name": "next"
2424
}
25-
],
26-
"useUnknownInCatchVariables": false
25+
]
2726
},
2827
"include": [
2928
"next-env.d.ts",

0 commit comments

Comments
 (0)
Please sign in to comment.