-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.test.ts
More file actions
205 lines (176 loc) · 5.26 KB
/
main.test.ts
File metadata and controls
205 lines (176 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import nock from 'nock'
import {
beforeEach,
afterEach,
expect,
jest,
test,
describe
} from '@jest/globals'
import {readFile} from 'fs/promises'
import mockFS from 'mock-fs'
import * as os from 'os'
import YAML from 'yaml'
import * as core from '@actions/core'
import {configureApiToken} from '../src/configure-api-token'
import {assumeRole} from '../src/oidc/assumeRole'
import {exchangeToken} from '../src/oidc/trustedPublisher'
jest.mock('os', () => {
const originalModule = jest.requireActual('os') as any
return {
__esModule: true, // Use it when dealing with esModules
default: originalModule,
...originalModule,
homedir: jest.fn()
}
})
describe('configureApiToken', () => {
test('throws no token', async () => {
await expect(configureApiToken(undefined as any, '')).rejects.toThrow(
'Empty apiToken is not supported'
)
})
test('throws empty token', async () => {
await expect(configureApiToken(undefined as any, '')).rejects.toThrow(
'Empty apiToken is not supported'
)
})
test('adding default token', async () => {
mockHomedir('/home/runner')
mockFS({})
await configureApiToken('token', 'https://rubygems.org')
await expect(
readFile('/home/runner/.gem/credentials', 'utf8').then(YAML.parse)
).resolves.toEqual({':rubygems_api_key': 'token'})
expect(exportedVariables).toEqual({
BUNDLE_GEM__PUSH_KEY: 'token',
GEM_HOST_API_KEY: 'token',
RUBYGEMS_API_KEY: 'token'
})
})
test('adding custom token', async () => {
mockHomedir('/home/runner')
mockFS({})
await configureApiToken('token', 'https://oidc-api-token.rubygems.org')
await expect(
readFile('/home/runner/.gem/credentials', 'utf8').then(YAML.parse)
).resolves.toEqual({'https://oidc-api-token.rubygems.org': 'token'})
expect(exportedVariables).toEqual({
BUNDLE_GEM__PUSH_KEY: 'token',
GEM_HOST_API_KEY: 'token',
RUBYGEMS_API_KEY: 'token'
})
})
})
describe('assumeRole', () => {
test('works', async () => {
jest.spyOn(core, 'getIDToken').mockReturnValue(Promise.resolve('ID_TOKEN'))
nock('https://rubygems.org')
.post('/api/v1/oidc/api_key_roles/1/assume_role', {
jwt: 'ID_TOKEN'
})
.reply(201, {
name: 'role name',
rubygems_api_key: 'API_KEY',
expires_at: '2021-01-01T00:00:00Z',
scopes: ['push_rubygem']
})
await expect(
assumeRole('1', 'rubygems.org', 'https://rubygems.org')
).resolves.toEqual({
expiresAt: '2021-01-01T00:00:00Z',
gem: undefined,
name: 'role name',
rubygemsApiKey: 'API_KEY',
scopes: ['push_rubygem']
})
})
test('works when scoped to a gem', async () => {
jest.spyOn(core, 'getIDToken').mockReturnValue(Promise.resolve('ID_TOKEN'))
nock('https://rubygems.org')
.post('/api/v1/oidc/api_key_roles/1/assume_role', {
jwt: 'ID_TOKEN'
})
.reply(201, {
name: 'role name',
rubygems_api_key: 'API_KEY',
expires_at: '2021-01-01T00:00:00Z',
gem: {name: 'rubygem0', version: '1.0.0'},
scopes: ['push_rubygem']
})
await expect(
assumeRole('1', 'rubygems.org', 'https://rubygems.org')
).resolves.toEqual({
expiresAt: '2021-01-01T00:00:00Z',
gem: {name: 'rubygem0'},
name: 'role name',
rubygemsApiKey: 'API_KEY',
scopes: ['push_rubygem']
})
})
})
describe('exchangeToken', () => {
test('works', async () => {
jest.spyOn(core, 'getIDToken').mockReturnValue(Promise.resolve('ID_TOKEN'))
nock('https://rubygems.org')
.post('/api/v1/oidc/trusted_publisher/exchange_token', {
jwt: 'ID_TOKEN'
})
.reply(201, {
name: 'role name',
rubygems_api_key: 'API_KEY',
expires_at: '2021-01-01T00:00:00Z',
scopes: ['push_rubygem']
})
await expect(
exchangeToken('rubygems.org', 'https://rubygems.org')
).resolves.toEqual({
expiresAt: '2021-01-01T00:00:00Z',
gem: undefined,
name: 'role name',
rubygemsApiKey: 'API_KEY',
scopes: ['push_rubygem']
})
})
test('handles a 404', async () => {
jest.spyOn(core, 'getIDToken').mockReturnValue(Promise.resolve('ID_TOKEN'))
nock('https://rubygems.org')
.post('/api/v1/oidc/trusted_publisher/exchange_token', {
jwt: 'ID_TOKEN'
})
.reply(404, '')
await expect(
exchangeToken('rubygems.org', 'https://rubygems.org')
).rejects.toEqual(
new Error(
'No trusted publisher configured for this workflow found on https://rubygems.org for audience rubygems.org'
)
)
})
})
function mockHomedir(homedir: string) {
mockOf(os.homedir).mockReturnValue(homedir)
}
function mockOf<T extends (...args: any) => any>(dep: T): jest.Mock<T> {
return <jest.Mock<T>>(<unknown>dep)
}
let exportedVariables: Record<string, string>
let secrets: string[]
beforeEach(() => {
if (!nock.isActive()) {
nock.activate()
}
nock.disableNetConnect()
exportedVariables = {}
secrets = []
jest.spyOn(core, 'exportVariable').mockImplementation((name, value) => {
exportedVariables[name] = value
})
jest.spyOn(core, 'setSecret').mockImplementation(value => {
secrets.push(value)
})
})
afterEach(() => {
mockFS.restore()
nock.restore()
})