-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #188 from bcgov/dev
chore: release
- Loading branch information
Showing
53 changed files
with
4,735 additions
and
3,835 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
nodejs 18.18.2 | ||
yarn 1.22.4 | ||
nodejs 20.17.0 | ||
yarn 1.22.22 | ||
python 3.11.0 | ||
postgres 14.1 | ||
helm 3.10.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "next/core-web-vitals" | ||
"extends": ["next/core-web-vitals"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,29 @@ | ||
import { createMocks } from 'node-mocks-http'; | ||
import deleteHandler from '../../pages/api/realms/[id]'; | ||
import githubResponseHandler from '../../pages/api/realms/pending'; | ||
import prisma from 'utils/prisma'; | ||
import { CustomRealmProfiles } from '../fixtures'; | ||
import { getServerSession } from 'next-auth'; | ||
import { createCustomRealmPullRequest, mergePullRequest } from 'utils/github'; | ||
import { EventEnum, StatusEnum } from 'validators/create-realm'; | ||
import { removeUserAsRealmAdmin } from 'controllers/keycloak'; | ||
import { sendDeletionCompleteEmail } from 'utils/mailer'; | ||
|
||
jest.mock('../../controllers/keycloak', () => { | ||
import { CustomRealmProfiles, MockHttpRequest } from '../fixtures'; | ||
import { manageCustomRealm, removeUserAsRealmAdmin } from 'controllers/keycloak'; | ||
import { ssoTeamEmail } from 'utils/mailer'; | ||
import { createMockSendEmail, mockAdminSession, mockSession } from './utils/mocks'; | ||
import { createEvent } from 'utils/helpers'; | ||
import { EventEnum } from 'validators/create-realm'; | ||
|
||
jest.mock('../../utils/helpers', () => { | ||
return { | ||
removeUserAsRealmAdmin: jest.fn(), | ||
...jest.requireActual('../../utils/helpers'), | ||
createEvent: jest.fn(), | ||
}; | ||
}); | ||
|
||
jest.mock('../../utils/mailer', () => { | ||
return { | ||
sendUpdateEmail: jest.fn(), | ||
sendDeleteEmail: jest.fn(), | ||
sendDeletionCompleteEmail: jest.fn(() => Promise.resolve(true)), | ||
}; | ||
}); | ||
jest.mock('utils/ches'); | ||
|
||
jest.mock('../../utils/github', () => { | ||
jest.mock('../../controllers/keycloak', () => { | ||
return { | ||
createCustomRealmPullRequest: jest.fn(() => Promise.resolve({ data: { number: 1 } })), | ||
mergePullRequest: jest.fn(() => Promise.resolve({ data: { merged: true } })), | ||
deleteBranch: jest.fn(), | ||
removeUserAsRealmAdmin: jest.fn(() => true), | ||
createCustomRealm: jest.fn(() => true), | ||
disableCustomRealm: jest.fn(() => true), | ||
deleteCustomRealm: jest.fn(() => true), | ||
manageCustomRealm: jest.fn(() => true), | ||
}; | ||
}); | ||
|
||
|
@@ -37,16 +33,6 @@ jest.mock('next/config', () => () => ({ | |
}, | ||
})); | ||
|
||
const mockSession = { | ||
expires: new Date(Date.now() + 2 * 86400).toISOString(), | ||
user: { | ||
username: 'test', | ||
family_name: 'test', | ||
idir_username: 'test', | ||
}, | ||
status: 'authenticated', | ||
}; | ||
|
||
jest.mock('next-auth/next', () => { | ||
return { | ||
__esModule: true, | ||
|
@@ -61,19 +47,7 @@ jest.mock('../../pages/api/auth/[...nextauth]', () => { | |
}; | ||
}); | ||
|
||
const mockAdminSession = () => { | ||
(getServerSession as jest.Mock).mockImplementation(() => { | ||
return { | ||
...mockSession, | ||
user: { | ||
...mockSession.user, | ||
client_roles: ['sso-admin'], | ||
}, | ||
}; | ||
}); | ||
}; | ||
|
||
describe('Realm Delete Request', () => { | ||
describe('Delete Realm Permissions', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
// Mock prisma find/update functions | ||
|
@@ -89,123 +63,79 @@ describe('Realm Delete Request', () => { | |
}); | ||
|
||
it('Only allows admins to delete realms', async () => { | ||
const { req, res } = createMocks({ | ||
const { req, res }: MockHttpRequest = createMocks({ | ||
method: 'DELETE', | ||
query: { id: 1 }, | ||
}); | ||
await deleteHandler(req, res); | ||
expect(res.statusCode).toBe(401); | ||
expect(createCustomRealmPullRequest).not.toHaveBeenCalled(); | ||
expect(mergePullRequest).not.toHaveBeenCalled(); | ||
|
||
mockAdminSession(); | ||
await deleteHandler(req, res); | ||
expect(res.statusCode).toBe(200); | ||
expect(createCustomRealmPullRequest).toHaveBeenCalled(); | ||
expect(mergePullRequest).toHaveBeenCalled(); | ||
}); | ||
|
||
it('Updates the status, archived, and prNumber when deleted successfully', async () => { | ||
const { req, res } = createMocks({ | ||
method: 'DELETE', | ||
query: { id: 1 }, | ||
}); | ||
mockAdminSession(); | ||
await deleteHandler(req, res); | ||
expect(res.statusCode).toBe(200); | ||
|
||
const rosterUpdateArgs = (prisma.roster.update as jest.Mock).mock.calls[0][0]; | ||
expect(rosterUpdateArgs.data.archived).toBe(true); | ||
expect(rosterUpdateArgs.data.prNumber).toBe(1); | ||
expect(rosterUpdateArgs.data.status).toBe(StatusEnum.PRSUCCESS); | ||
}); | ||
|
||
it('Updates the status to failed if the pr fails or merge fails and logs an event', async () => { | ||
// PR Creation failure | ||
const failureEvent = { | ||
data: { | ||
realmId: 1, | ||
eventCode: EventEnum.REQUEST_DELETE_FAILED, | ||
idirUserId: 'test', | ||
}, | ||
}; | ||
const { req, res } = createMocks({ | ||
method: 'DELETE', | ||
query: { id: 1 }, | ||
}); | ||
(createCustomRealmPullRequest as jest.Mock).mockImplementationOnce(() => Promise.reject(new Error('Failed'))); | ||
mockAdminSession(); | ||
await deleteHandler(req, res); | ||
expect(res.statusCode).toBe(500); | ||
|
||
expect(manageCustomRealm).toHaveBeenCalledTimes(1); | ||
let rosterUpdateArgs = (prisma.roster.update as jest.Mock).mock.calls[0][0]; | ||
expect(rosterUpdateArgs.data.status).toBe(StatusEnum.PRFAILED); | ||
expect(prisma.event.create).toHaveBeenCalledWith(failureEvent); | ||
|
||
// PR merge failure | ||
(mergePullRequest as jest.Mock).mockImplementationOnce(() => Promise.reject(new Error('Failed'))); | ||
await deleteHandler(req, res); | ||
|
||
expect(res.statusCode).toBe(500); | ||
rosterUpdateArgs = (prisma.roster.update as jest.Mock).mock.calls[0][0]; | ||
expect(rosterUpdateArgs.data.status).toBe(StatusEnum.PRFAILED); | ||
expect(prisma.event.create).toHaveBeenCalledWith(failureEvent); | ||
expect(rosterUpdateArgs.data.archived).toBe(true); | ||
expect(rosterUpdateArgs.data.status).toBe('applied'); | ||
}); | ||
}); | ||
|
||
describe('Github Actions Delete', () => { | ||
const mockToken = 'secret'; | ||
describe('Delete Realms', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(prisma.roster.findUnique as jest.Mock).mockImplementation(() => { | ||
return Promise.resolve({ ...CustomRealmProfiles[0], id: 1, archived: true }); | ||
}); | ||
}); | ||
const requestData = { | ||
method: 'PUT' as 'PUT', | ||
body: { | ||
ids: [1], | ||
action: 'tf_apply', | ||
success: 'true', | ||
}, | ||
headers: { | ||
Authorization: mockToken, | ||
}, | ||
}; | ||
|
||
it('requires api token', async () => { | ||
let { req, res } = createMocks(requestData); | ||
await githubResponseHandler(req, res); | ||
expect(res.statusCode).toBe(200); | ||
|
||
// Remove auth header | ||
({ req, res } = createMocks({ ...requestData, headers: { Authorization: 'empty' } })); | ||
await githubResponseHandler(req, res); | ||
expect(res.statusCode).toBe(401); | ||
}); | ||
it('successfully deletes realm in all environments and sends email', async () => { | ||
mockAdminSession(); | ||
const { req, res }: MockHttpRequest = createMocks({ | ||
method: 'DELETE', | ||
query: { id: 1 }, | ||
}); | ||
|
||
it('Removes technical contact and product owner from all envirionments', async () => { | ||
const { req, res } = createMocks(requestData); | ||
await githubResponseHandler(req, res); | ||
expect(res.statusCode).toBe(200); | ||
const emailList = createMockSendEmail(); | ||
|
||
// Email only sent once | ||
expect(sendDeletionCompleteEmail).toHaveBeenCalledTimes(1); | ||
await deleteHandler(req, res); | ||
|
||
expect(res.statusCode).toBe(200); | ||
expect(manageCustomRealm).toHaveBeenCalledTimes(1); | ||
// PO email and technical contact email removed in each realm | ||
['dev', 'test', 'prod'].forEach((env) => { | ||
expect(removeUserAsRealmAdmin).toHaveBeenCalledWith(['[email protected]', '[email protected]'], env, 'realm 1'); | ||
CustomRealmProfiles[0].environments.forEach((env) => { | ||
expect(removeUserAsRealmAdmin).toHaveBeenCalledWith( | ||
[CustomRealmProfiles[0].productOwnerEmail, CustomRealmProfiles[0].technicalContactEmail], | ||
env, | ||
CustomRealmProfiles[0].realm, | ||
); | ||
}); | ||
// No extra calls | ||
expect(removeUserAsRealmAdmin).toHaveBeenCalledTimes(3); | ||
expect(emailList.length).toBe(1); | ||
expect(emailList[0].subject).toContain( | ||
`Notification: Custom Realm ${CustomRealmProfiles[0].realm} has now been Deleted.`, | ||
); | ||
expect(emailList[0].to).toEqual( | ||
expect.arrayContaining([CustomRealmProfiles[0].productOwnerEmail, CustomRealmProfiles[0].technicalContactEmail]), | ||
); | ||
expect(emailList[0].cc).toEqual(expect.arrayContaining([ssoTeamEmail])); | ||
}); | ||
|
||
it('Only sends deletion complete email if all users removed successfully', async () => { | ||
const { req, res } = createMocks(requestData); | ||
(removeUserAsRealmAdmin as jest.Mock).mockImplementationOnce(() => Promise.reject(new Error('failure'))); | ||
await githubResponseHandler(req, res); | ||
it('does not send email if deleting realm in all environments fails', async () => { | ||
mockAdminSession(); | ||
|
||
(manageCustomRealm as jest.Mock).mockImplementationOnce(() => Promise.reject('some error')); | ||
|
||
expect(res.statusCode).toBe(500); | ||
expect(sendDeletionCompleteEmail).not.toHaveBeenCalled(); | ||
const { req, res }: MockHttpRequest = createMocks({ | ||
method: 'DELETE', | ||
query: { id: 1 }, | ||
}); | ||
|
||
const emailList = createMockSendEmail(); | ||
|
||
await deleteHandler(req, res); | ||
const createEventArgs = (createEvent as jest.Mock).mock.calls[0][0]; | ||
expect(createEventArgs.eventCode).toBe(EventEnum.REQUEST_DELETE_FAILED); | ||
expect(res.statusCode).toBe(422); | ||
expect(manageCustomRealm).toHaveBeenCalledTimes(1); | ||
expect(emailList.length).toBe(0); | ||
}); | ||
}); |
Oops, something went wrong.