-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathsession-auth-status.test.ts
More file actions
117 lines (99 loc) · 3.47 KB
/
Copy pathsession-auth-status.test.ts
File metadata and controls
117 lines (99 loc) · 3.47 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
import {getAuthStatus} from './session.js'
import {identityFqdn} from './context/fqdn.js'
import {getCurrentSessionId} from '../../private/node/conf-store.js'
import * as sessionStore from '../../private/node/session/store.js'
import {validateSession} from '../../private/node/session/validate.js'
import {Session} from '../../private/node/session/schema.js'
import {beforeEach, describe, expect, test, vi} from 'vitest'
vi.mock('./context/fqdn.js')
vi.mock('../../private/node/conf-store.js')
vi.mock('../../private/node/session/store.js')
vi.mock('../../private/node/session/validate.js')
const expiresAt = new Date('2030-01-01T00:00:00.000Z')
const session: Session = {
identity: {
accessToken: 'identity-token',
refreshToken: 'refresh-token',
expiresAt,
scopes: ['scope'],
userId: 'user-id',
alias: 'user@example.com',
},
applications: {},
}
describe('getAuthStatus', () => {
beforeEach(() => {
vi.mocked(identityFqdn).mockResolvedValue('accounts.shopify.com')
vi.mocked(getCurrentSessionId).mockReturnValue('user-id')
vi.mocked(sessionStore.fetch).mockResolvedValue({
'accounts.shopify.com': {
'user-id': session,
},
})
vi.mocked(validateSession).mockResolvedValue('ok')
})
test('returns authenticated for a valid current session', async () => {
// When
const got = await getAuthStatus()
// Then
expect(got).toEqual({
status: 'authenticated',
authenticated: true,
account: {
userId: 'user-id',
alias: 'user@example.com',
},
identityFqdn: 'accounts.shopify.com',
expiresAt: '2030-01-01T00:00:00.000Z',
agentGuidance: {
instruction: 'A Shopify CLI session is available. Continue with the requested Shopify CLI command.',
},
})
})
test('returns needs_refresh for a refreshable current session', async () => {
// Given
vi.mocked(validateSession).mockResolvedValue('needs_refresh')
// When
const got = await getAuthStatus()
// Then
expect(got.status).toBe('needs_refresh')
expect(got.authenticated).toBe(true)
expect(got.account?.userId).toBe('user-id')
})
test('falls back to the first stored session when no current session is configured', async () => {
// Given
vi.mocked(getCurrentSessionId).mockReturnValue(undefined)
// When
const got = await getAuthStatus()
// Then
expect(got.status).toBe('authenticated')
expect(got.account?.userId).toBe('user-id')
})
test('returns not_authenticated when no session exists', async () => {
// Given
vi.mocked(getCurrentSessionId).mockReturnValue(undefined)
vi.mocked(sessionStore.fetch).mockResolvedValue(undefined)
// When
const got = await getAuthStatus()
// Then
expect(got).toEqual({
status: 'not_authenticated',
authenticated: false,
identityFqdn: 'accounts.shopify.com',
agentGuidance: {
instruction:
'No usable Shopify CLI session is available. Run `shopify auth login`, show the verification URL and user code to the user, and keep the command running until authentication completes.',
nextCommand: 'shopify auth login',
},
})
})
test('returns invalid when the current session id is missing from storage', async () => {
// Given
vi.mocked(getCurrentSessionId).mockReturnValue('missing-user-id')
// When
const got = await getAuthStatus()
// Then
expect(got.status).toBe('invalid')
expect(got.authenticated).toBe(false)
})
})