Skip to content

Commit bf1573e

Browse files
committed
Add tests for the keyserver pool
1 parent c8d751c commit bf1573e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

__tests__/gpg.test.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import * as exec from "@actions/exec";
2+
import { setupKeys, verify, refreshKeys } from "../src/gpg";
3+
4+
jest.mock("@actions/exec")
5+
6+
const mockExec = exec.exec as jest.Mock
7+
8+
describe("gpg", () => {
9+
10+
afterEach(() => {
11+
mockExec.mockClear()
12+
})
13+
14+
it('uses the first responding keyserver in the pool', async () => {
15+
mockExec.mockImplementation(() => Promise.resolve(0))
16+
await refreshKeys()
17+
expect(mockExec).toBeCalledTimes(1)
18+
})
19+
20+
it('uses the next keyserver in the pool if the previous fails', async () => {
21+
const failingServers = 3
22+
let testedServers = 0
23+
24+
mockExec.mockImplementation(() => {
25+
testedServers++
26+
if (testedServers >= failingServers) {
27+
return Promise.resolve(0)
28+
} else {
29+
return Promise.resolve(1)
30+
}
31+
})
32+
33+
await refreshKeys()
34+
expect(mockExec).toBeCalledTimes(3)
35+
})
36+
37+
it('throws an error if all servers in the pool fails', async () => {
38+
mockExec.mockImplementation(() => Promise.resolve(1))
39+
40+
try {
41+
await refreshKeys()
42+
} catch (e) {
43+
expect(e).toEqual(new Error("Failed to refresh keys from any server in the pool."));
44+
}
45+
})
46+
47+
})

0 commit comments

Comments
 (0)