-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathaudit.test.ts
More file actions
101 lines (90 loc) · 3.05 KB
/
audit.test.ts
File metadata and controls
101 lines (90 loc) · 3.05 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
jest.mock('@contentstack/cli-command', () => ({
Command: class {
log = jest.fn()
error = jest.fn()
warn = jest.fn()
},
}))
jest.mock('@contentstack/cli-utilities', () => ({
managementSDKClient: jest.fn().mockResolvedValue({}),
cliux: {
loaderV2: jest.fn().mockReturnValue({}),
print: jest.fn(),
},
flags: new Proxy(
{},
{
get: (_t, prop) =>
jest.fn((opts: Record<string, unknown>) => ({ ...opts, __f: String(prop) })),
}
),
printFlagDeprecation: jest.fn(() => () => undefined),
}))
jest.mock('../../../src/utils', () => ({
getContentType: jest.fn(),
getStack: jest.fn(),
getUsers: jest.fn(),
}))
import { getContentType, getStack, getUsers } from '../../../src/utils'
import AuditCommand from '../../../src/commands/content-type/audit'
describe('content-type:audit', () => {
it('loads audit logs and prints output', async () => {
;(getContentType as jest.Mock).mockResolvedValueOnce({ _version: 1, uid: 'ct' })
;(getStack as jest.Mock).mockResolvedValue({ name: 'St' })
;(getUsers as jest.Mock).mockResolvedValue([
{ first_name: 'A', last_name: 'B', uid: 'u1' },
])
const cmd = new AuditCommand([], {} as any)
;(cmd as any).cmaHost = 'https://api.contentstack.io'
;(cmd as any).context = { analyticsInfo: {} }
;(cmd as any).parse = jest.fn().mockResolvedValue({
flags: { 'content-type': 'home', 'stack-api-key': 'k' },
})
;(cmd as any).setup = jest.fn().mockResolvedValue(undefined)
;(cmd as any).apiKey = 'k'
;(cmd as any).contentTypeManagementClient = {}
;(cmd as any).client = {
getContentTypeAuditLogs: jest.fn().mockResolvedValue({
logs: [
{
created_at: '2020-01-01T00:00:00.000Z',
created_by: 'u1',
event_type: 'save',
metadata: { version: 1 },
},
],
}),
}
;(cmd as any).printOutput = jest.fn()
await cmd.run()
expect(getContentType).toHaveBeenCalled()
expect((cmd as any).client.getContentTypeAuditLogs).toHaveBeenCalled()
expect(cmd.printOutput).toHaveBeenCalledWith(
expect.objectContaining({ hasResults: true }),
'Audit Logs',
'home',
'St'
)
})
it('calls error when getStack fails', async () => {
;(getContentType as jest.Mock).mockResolvedValueOnce({ uid: 'ct' })
;(getStack as jest.Mock).mockRejectedValue(new Error('network'))
const cmd = new AuditCommand([], {} as any)
;(cmd as any).cmaHost = 'https://api.contentstack.io'
;(cmd as any).context = { analyticsInfo: {} }
;(cmd as any).parse = jest.fn().mockResolvedValue({
flags: { 'content-type': 'home', 'stack-api-key': 'k' },
})
;(cmd as any).setup = jest.fn().mockResolvedValue(undefined)
;(cmd as any).apiKey = 'k'
;(cmd as any).contentTypeManagementClient = {}
;(cmd as any).client = {
getContentTypeAuditLogs: jest.fn().mockResolvedValue({ logs: [] }),
}
await cmd.run()
expect(cmd.error).toHaveBeenCalledWith('network', {
exit: 1,
suggestions: undefined,
})
})
})