Skip to content

Commit ee24f16

Browse files
committed
add test coverage for google
1 parent 6c7ab9a commit ee24f16

4 files changed

Lines changed: 93 additions & 25 deletions

File tree

src/google.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { google } from 'googleapis'
2-
3-
const privatekey = JSON.parse(Buffer.from(process.env.GOOGLE_CREDENTIALS, 'base64').toString('utf-8'))
4-
2+
import * as mod from './google'
53
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
64
export async function googleAuth() {
5+
const privatekey = JSON.parse(Buffer.from(process.env.GOOGLE_CREDENTIALS, 'base64').toString('utf-8'))
76
const jwtClient = new google.auth.JWT(
87
privatekey.client_email,
98
null,
@@ -24,7 +23,7 @@ export async function getAdminService() {
2423
}
2524

2625
export async function getGithubUsersFromGoogle(): Promise<Set<string>> {
27-
const service = await getAdminService()
26+
const service = await mod.getAdminService()
2827

2928
const userList = await service.users.list({
3029
customer: 'my_customer',
@@ -34,15 +33,15 @@ export async function getGithubUsersFromGoogle(): Promise<Set<string>> {
3433
customFieldMask: 'Accounts',
3534
})
3635

37-
const githubAccounts = formatUserList(userList.data.users)
36+
const githubAccounts = mod.formatUserList(userList.data.users)
3837
return githubAccounts
3938
}
4039

4140
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
4241
export function formatUserList(users): Set<string> {
4342
return new Set(
4443
users
45-
.map((user) => user?.customSchemas?.Accounts?.github.map((account) => account.value.toLowerCase()))
44+
.map((user) => user.customSchemas?.Accounts?.github.map((account) => account?.value?.toLowerCase()))
4645
.flat()
4746
.filter(Boolean),
4847
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`google integration getGithubUsersFromGoogle 1`] = `
4+
Set {
5+
"chrisns",
6+
"foo",
7+
"tar",
8+
"bar",
9+
}
10+
`;
11+
12+
exports[`google integration googleAuth 1`] = `
13+
Array [
14+
Array [
15+
"foo",
16+
null,
17+
"bar",
18+
Array [
19+
"https://www.googleapis.com/auth/admin.directory.user.readonly",
20+
],
21+
"hello@example.com",
22+
],
23+
]
24+
`;

tests/google.spec.ts

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
1+
jest.mock('googleapis')
2+
import { google } from 'googleapis'
13
import * as mod from '../src/google'
24

5+
const fakeUsersResponse = [
6+
{ customSchemas: { Accounts: { github: [{ value: 'chrisns' }] } } },
7+
{
8+
customSchemas: {
9+
Accounts: { github: [{ value: 'Foo' }, , { value: 'tar' }] },
10+
},
11+
},
12+
{
13+
customSchemas: {
14+
Accounts: { github: [{ value: 'foo' }, { value: 'bar' }] },
15+
},
16+
},
17+
]
18+
319
describe('google integration', () => {
4-
it.todo('googleAuth')
5-
it.skip('getAdminService', () => {
20+
beforeEach(() => {
21+
process.env.GOOGLE_EMAIL_ADDRESS = 'hello@example.com'
22+
process.env.GOOGLE_CREDENTIALS = Buffer.from(JSON.stringify({ client_email: 'foo', private_key: 'bar' })).toString(
23+
'base64',
24+
)
25+
jest.resetAllMocks()
26+
})
27+
it('googleAuth', () => {
28+
mod.googleAuth()
29+
// @ts-expect-error .mocks isn't on the original object so will fail
30+
return expect(google.auth.JWT.mock.calls).toMatchSnapshot()
31+
})
32+
it('getAdminService', () => {
33+
// @ts-expect-error mock service isn't a complete implementation, so being lazy and just doing the bare minimum
34+
google.admin.mockReturnValue('adminservice')
635
const result = mod.getAdminService()
7-
return expect(result).resolves.toBe({})
36+
return expect(result).resolves.toBe('adminservice')
837
})
938

10-
it.todo('getGithubUsersFromGoogle')
39+
it('getGithubUsersFromGoogle', () => {
40+
const service = { users: { list: jest.fn().mockResolvedValue({ data: { users: fakeUsersResponse } }) } }
41+
// @ts-expect-error mock service isn't a complete implementation, so being lazy and just doing the bare minimum
42+
jest.spyOn(mod, 'getAdminService').mockResolvedValue(service)
1143

12-
it('formatUserList', () => {
13-
const response = [
14-
{ customSchemas: { Accounts: { github: [{ value: 'chrisns' }] } } },
15-
{
16-
customSchemas: {
17-
Accounts: { github: [{ value: 'Foo' }, , { value: 'tar' }] },
18-
},
19-
},
20-
{
21-
customSchemas: {
22-
Accounts: { github: [{ value: 'foo' }, { value: 'bar' }] },
23-
},
24-
},
25-
]
26-
return expect(mod.formatUserList(response)).toEqual(new Set(['chrisns', 'foo', 'bar', 'tar']))
44+
const result = mod.getGithubUsersFromGoogle()
45+
expect(result).resolves.toMatchSnapshot()
2746
})
47+
48+
it('formatUserList', () =>
49+
expect(mod.formatUserList(fakeUsersResponse)).toEqual(new Set(['chrisns', 'foo', 'bar', 'tar'])))
2850
})

wallaby.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2+
module.exports = function (w) {
3+
return {
4+
files: [
5+
'*.ts',
6+
'tests/**/*.json',
7+
'src/**/*.ts',
8+
{ pattern: '.env', instrument: false },
9+
// '__mocks__/**/*.ts',
10+
],
11+
tests: ['tests/**/*.spec.ts'],
12+
env: {
13+
type: 'node',
14+
},
15+
testFramework: 'jest',
16+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
17+
// setup: function (w) {
18+
// // eslint-disable-next-line @typescript-eslint/no-var-requires
19+
// require('dotenv').config()
20+
21+
// },
22+
}
23+
}

0 commit comments

Comments
 (0)