Skip to content

Commit 33c88b9

Browse files
committed
increase test coverage and addres bug with incomplete response coming back from google
1 parent 6b505c4 commit 33c88b9

8 files changed

Lines changed: 269 additions & 11 deletions

File tree

index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@ export async function run(): Promise<void> {
1313
const usersNotInGoogle = new Set(Array.from(gitHubUsers).filter((x) => !googleUsers.has(x)))
1414
if (usersNotInGithub.size > 0) {
1515
console.log(`Users not in github: ${Array.from(usersNotInGithub).join(', ')}`)
16-
if (process.env.ADD_USERS.toLowerCase() === 'true') await addUsersToGitHubOrg(usersNotInGithub)
16+
if (process.env.ADD_USERS?.toLowerCase() === 'true') await addUsersToGitHubOrg(usersNotInGithub)
1717
}
1818

1919
if (usersNotInGoogle.size > 0) {
2020
console.log(`Users not in google: ${Array.from(usersNotInGoogle).join(', ')}`)
21-
if (process.env.REMOVE_USERS.toLowerCase() === 'true') await removeUsersToGitHubOrg(usersNotInGoogle)
21+
if (process.env.REMOVE_USERS?.toLowerCase() === 'true') await removeUsersToGitHubOrg(usersNotInGoogle)
2222
}
2323

24+
let exitCode
2425
if (usersNotInGoogle.size > 0 || usersNotInGithub.size > 0)
25-
process.exit(parseInt(process.env.EXIT_CODE_ON_MISMATCH) ?? 0)
26+
exitCode = parseInt(process.env.EXIT_CODE_ON_MISMATCH ?? '0')
27+
28+
process.exit(exitCode ?? 0)
2629
}
2730

31+
// istanbul ignore next
2832
if (require.main === module) run()

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module.exports = {
99
collectCoverage: true,
1010
coverageDirectory: 'coverage',
1111
restoreMocks: true,
12+
resetMocks: true,
1213

1314
collectCoverageFrom: [
1415
'**/*.ts',

src/google.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export async function getGithubUsersFromGoogle(): Promise<Set<string>> {
4141
export function formatUserList(users): Set<string> {
4242
return new Set(
4343
users
44-
.map((user) => user.customSchemas?.Accounts?.github.map((account) => account?.value?.toLowerCase()))
44+
.map((user) => user.customSchemas?.Accounts?.github?.map((account) => account.value?.toLowerCase()))
4545
.flat()
4646
.filter(Boolean),
4747
)

tests/__snapshots__/google.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`google integration formatUserList 1`] = `
4+
Set {
5+
"chrisns",
6+
"foo",
7+
"tar",
8+
"bar",
9+
}
10+
`;
11+
12+
exports[`google integration formatUserList bad 1`] = `
13+
Set {
14+
"chrisns",
15+
}
16+
`;
17+
318
exports[`google integration getGithubUsersFromGoogle 1`] = `
419
Set {
520
"chrisns",
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`match should have consistent console output 1`] = `
4+
[MockFunction] {
5+
"calls": Array [
6+
Array [
7+
"Users from google: a, b, c, d",
8+
],
9+
Array [
10+
"Users from github: a, b, c, d",
11+
],
12+
],
13+
"results": Array [
14+
Object {
15+
"type": "return",
16+
"value": undefined,
17+
},
18+
Object {
19+
"type": "return",
20+
"value": undefined,
21+
},
22+
],
23+
}
24+
`;
25+
26+
exports[`missmatch should add users if set to 1`] = `
27+
[MockFunction] {
28+
"calls": Array [
29+
Array [
30+
Set {
31+
"d",
32+
},
33+
],
34+
],
35+
"results": Array [
36+
Object {
37+
"type": "return",
38+
"value": undefined,
39+
},
40+
],
41+
}
42+
`;
43+
44+
exports[`missmatch should have consistent console output 1`] = `
45+
[MockFunction] {
46+
"calls": Array [
47+
Array [
48+
"Users from google: a, d",
49+
],
50+
Array [
51+
"Users from github: b, c, a",
52+
],
53+
Array [
54+
"Users not in github: d",
55+
],
56+
Array [
57+
"Users not in google: b, c",
58+
],
59+
],
60+
"results": Array [
61+
Object {
62+
"type": "return",
63+
"value": undefined,
64+
},
65+
Object {
66+
"type": "return",
67+
"value": undefined,
68+
},
69+
Object {
70+
"type": "return",
71+
"value": undefined,
72+
},
73+
Object {
74+
"type": "return",
75+
"value": undefined,
76+
},
77+
],
78+
}
79+
`;
80+
81+
exports[`missmatch should have consistent console output with full destructive mode on 1`] = `
82+
[MockFunction] {
83+
"calls": Array [
84+
Array [
85+
"Users from google: a, d",
86+
],
87+
Array [
88+
"Users from github: b, c, a",
89+
],
90+
Array [
91+
"Users not in github: d",
92+
],
93+
Array [
94+
"Users not in google: b, c",
95+
],
96+
],
97+
"results": Array [
98+
Object {
99+
"type": "return",
100+
"value": undefined,
101+
},
102+
Object {
103+
"type": "return",
104+
"value": undefined,
105+
},
106+
Object {
107+
"type": "return",
108+
"value": undefined,
109+
},
110+
Object {
111+
"type": "return",
112+
"value": undefined,
113+
},
114+
],
115+
}
116+
`;
117+
118+
exports[`missmatch should remove users if set to 1`] = `
119+
[MockFunction] {
120+
"calls": Array [
121+
Array [
122+
Set {
123+
"b",
124+
"c",
125+
},
126+
],
127+
],
128+
"results": Array [
129+
Object {
130+
"type": "return",
131+
"value": undefined,
132+
},
133+
],
134+
}
135+
`;

tests/github.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
jest.mock('@octokit/rest')
22
import { Octokit } from '@octokit/rest'
3-
43
import * as mod from '../src/github'
4+
55
describe('github integration', () => {
66
beforeEach(() => {
77
process.env.GITHUB_PRIVATE_KEY = Buffer.from('helloworld').toString('base64')
88
process.env.GITHUB_APP_ID = '123'
99
process.env.GITHUB_INSTALLATION_ID = '123'
10-
jest.restoreAllMocks()
10+
jest.spyOn(global.console, 'log').mockImplementation()
1111
})
1212

1313
it('getAuthenticatedOctokit', () => {

tests/google.spec.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('google integration', () => {
2222
process.env.GOOGLE_CREDENTIALS = Buffer.from(JSON.stringify({ client_email: 'foo', private_key: 'bar' })).toString(
2323
'base64',
2424
)
25-
jest.restoreAllMocks()
25+
jest.spyOn(global.console, 'log').mockImplementation()
2626
})
2727
it('googleAuth', () => {
2828
mod.googleAuth()
@@ -44,6 +44,17 @@ describe('google integration', () => {
4444
expect(result).resolves.toMatchSnapshot()
4545
})
4646

47-
it('formatUserList', () =>
48-
expect(mod.formatUserList(fakeUsersResponse)).toEqual(new Set(['chrisns', 'foo', 'bar', 'tar'])))
47+
it('formatUserList', () => expect(mod.formatUserList(fakeUsersResponse)).toMatchSnapshot())
48+
49+
it('formatUserList bad', () =>
50+
expect(
51+
mod.formatUserList([
52+
{},
53+
{ customSchemas: {} },
54+
{ customSchemas: { Accounts: {} } },
55+
{ customSchemas: { Accounts: { github: [] } } },
56+
{ customSchemas: { Accounts: { github: [{}] } } },
57+
{ customSchemas: { Accounts: { github: [{ value: 'chrisns' }] } } },
58+
]),
59+
).toMatchSnapshot())
4960
})

tests/index.spec.ts

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,95 @@
1-
describe('index', () => {
2-
it.todo('run')
1+
jest.mock('../src/google')
2+
jest.mock('../src/github')
3+
import * as google from '../src/google'
4+
import * as github from '../src/github'
5+
import * as mod from '../index'
6+
7+
let processExitSpy
8+
let consoleSpy
9+
10+
beforeEach(() => {
11+
processExitSpy = jest.spyOn(global.process, 'exit').mockImplementation(() => {
12+
return undefined as never
13+
})
14+
consoleSpy = jest.spyOn(global.console, 'log').mockImplementation()
15+
})
16+
17+
describe('missmatch', () => {
18+
beforeEach(() => {
19+
// @ts-expect-error mockResolved unexpected
20+
google.getGithubUsersFromGoogle.mockResolvedValue(new Set(['a', 'd']))
21+
// @ts-expect-error mockResolved unexpected
22+
github.getGithubUsersFromGithub.mockResolvedValue(new Set(['b', 'c', 'a']))
23+
// github.addUsersToGitHubOrg
24+
})
25+
it('should have consistent console output', async () => {
26+
await mod.run()
27+
return expect(consoleSpy).toMatchSnapshot()
28+
})
29+
30+
it('should exit with 0 by default as there is a missmatch', async () => {
31+
await mod.run()
32+
return expect(processExitSpy).toBeCalledWith(0)
33+
})
34+
it('should exit with 122 if defined when there is a missmatch', async () => {
35+
process.env.EXIT_CODE_ON_MISMATCH = '122'
36+
await mod.run()
37+
return expect(processExitSpy).toBeCalledWith(122)
38+
})
39+
it('should not add users if not set to', async () => {
40+
await mod.run()
41+
return expect(github.addUsersToGitHubOrg).not.toBeCalled()
42+
})
43+
it('should not remove users if not set to', async () => {
44+
await mod.run()
45+
return expect(github.removeUsersToGitHubOrg).not.toBeCalled()
46+
})
47+
it('should add users if set to', async () => {
48+
process.env.ADD_USERS = 'true'
49+
await mod.run()
50+
return expect(github.addUsersToGitHubOrg).toMatchSnapshot()
51+
})
52+
it('should remove users if set to', async () => {
53+
process.env.REMOVE_USERS = 'true'
54+
await mod.run()
55+
return expect(github.removeUsersToGitHubOrg).toMatchSnapshot()
56+
})
57+
it('should have consistent console output with full destructive mode on', async () => {
58+
process.env.REMOVE_USERS = 'true'
59+
process.env.ADD_USERS = 'true'
60+
await mod.run()
61+
return expect(consoleSpy).toMatchSnapshot()
62+
})
63+
})
64+
65+
describe('match', () => {
66+
beforeEach(() => {
67+
// @ts-expect-error mockResolved unexpected
68+
google.getGithubUsersFromGoogle.mockResolvedValue(new Set(['a', 'b', 'c', 'd']))
69+
// @ts-expect-error mockResolved unexpected
70+
github.getGithubUsersFromGithub.mockResolvedValue(new Set(['a', 'b', 'c', 'd']))
71+
})
72+
it('should have consistent console output', async () => {
73+
await mod.run()
74+
return expect(consoleSpy).toMatchSnapshot()
75+
})
76+
it('should exit with 0 by default', async () => {
77+
await mod.run()
78+
return expect(processExitSpy).toBeCalledWith(0)
79+
})
80+
it('should not exit with 122 if defined', async () => {
81+
process.env.EXIT_CODE_ON_MISMATCH = '122'
82+
await mod.run()
83+
return expect(processExitSpy).not.toBeCalledWith(122)
84+
})
85+
it('should not add users', async () => {
86+
process.env.ADD_USERS = 'true'
87+
await mod.run()
88+
return expect(github.addUsersToGitHubOrg).not.toBeCalled()
89+
})
90+
it('should not remove users', async () => {
91+
process.env.REMOVE_USERS = 'true'
92+
await mod.run()
93+
return expect(github.removeUsersToGitHubOrg).not.toBeCalled()
94+
})
395
})

0 commit comments

Comments
 (0)