1import spawnAsync from '@expo/spawn-async';
2
3import { CommandError } from '../../../../utils/errors';
4import {
5  extractCodeSigningInfo,
6  extractSigningId,
7  findIdentitiesAsync,
8  getSecurityPemAsync,
9} from '../Security';
10
11const asMock = <T extends (...args: any[]) => any>(fn: T): jest.MockedFunction<T> =>
12  fn as jest.MockedFunction<T>;
13
14jest.mock('../../../../start/doctor/SecurityBinPrerequisite', () => ({
15  SecurityBinPrerequisite: {
16    instance: {
17      assertAsync: jest.fn(),
18    },
19  },
20}));
21
22describe(getSecurityPemAsync, () => {
23  it(`asserts that the pem could not be found`, async () => {
24    asMock(spawnAsync).mockResolvedValueOnce({
25      stdout: '',
26    } as any);
27
28    await expect(getSecurityPemAsync('1234')).rejects.toThrow(CommandError);
29  });
30});
31
32describe(findIdentitiesAsync, () => {
33  it(`return identities`, async () => {
34    asMock(spawnAsync).mockResolvedValueOnce({
35      stdout: [
36        'Returns a string like:',
37        '1) 12222234253761286351826735HGKDHAJGF45283 "Apple Development: Evan Bacon (AA00AABB0A)" (CSSMERR_TP_CERT_REVOKED)',
38        '2) 12312234253761286351826735HGKDHAJGF45283 "Apple Development: [email protected] (BB00AABB0A)"',
39        // Duplicate
40        '3) 12312234253761286351826735HGKDHAJGF45283 "Apple Development: [email protected] (BB00AABB0A)"',
41        '4) 12312234253761286351826735HGKDHAJGF452XX "Apple Developer: [email protected] (CD00AABB0A)"',
42        '5) 12442234253761286351826735HGKDHAJGF45283 "iPhone Distribution: Evan Bacon (CC00AABB0B)" (CSSMERR_TP_CERT_REVOKED)',
43        '6) 12442234253761286351826735HGKDHAJGF45283 "iPhone Distribution: Evan Bacon (CC00AABB0B)" (CSSMERR_TP_CERT_REVOKED)',
44        '7) 15672234253761286351826735HGKDHAJGF45283 "Apple Development: Evan Bacon (AA00AABB0A)"',
45        ' 6 valid identities found',
46      ].join('\n'),
47    } as any);
48
49    await expect(findIdentitiesAsync()).resolves.toEqual([
50      'Apple Development: [email protected] (BB00AABB0A)',
51      'Apple Developer: [email protected] (CD00AABB0A)',
52      'Apple Development: Evan Bacon (AA00AABB0A)',
53    ]);
54  });
55});
56
57describe(extractCodeSigningInfo, () => {
58  it(`extracts`, () => {
59    expect(
60      extractCodeSigningInfo(
61        '  2) 12312234253761286351826735HGKDHAJGF45283 "Apple Development: [email protected] (BB00AABB0A)"'
62      )
63    ).toBe('Apple Development: [email protected] (BB00AABB0A)');
64  });
65  it(`does not match lines ending in (CSSMERR_TP_CERT_REVOKED)`, () => {
66    expect(
67      extractCodeSigningInfo(
68        '  3) 12442234253761286351826735HGKDHAJGF45283 "iPhone Distribution: Evan Bacon (CC00AABB0B)" (CSSMERR_TP_CERT_REVOKED)'
69      )
70    ).toBe(null);
71  });
72});
73
74describe(extractSigningId, () => {
75  it(`extracts`, () => {
76    expect(extractSigningId('Apple Development: Evan Bacon (AA00AABB0A)')).toBe('AA00AABB0A');
77  });
78});
79