1
- import { describe , it , expect , beforeEach , afterEach } from '@jest/globals' ;
2
- import { getToken } from '../../api/cloudfoundry' ;
1
+ import nock from 'nock' ;
3
2
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' ;
4
16
5
17
/* global jest */
6
18
/* eslint no-undef: "off" */
@@ -10,6 +22,54 @@ jest.mock('next/headers', () => ({
10
22
/* eslint no-undef: "error" */
11
23
12
24
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
+
13
73
describe ( 'getToken' , ( ) => {
14
74
describe ( 'when token environment variable is set' , ( ) => {
15
75
beforeEach ( ( ) => {
0 commit comments