@faststore/core 4.10.1 → 4.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-generate.log +3 -3
- package/.turbo/turbo-test.log +278 -235
- package/@generated/graphql.ts +1 -0
- package/@generated/schema.graphql +1 -1
- package/CHANGELOG.md +86 -0
- package/package.json +6 -6
- package/src/components/account/Drawer/OrganizationDrawer/OrganizationDrawer.tsx +3 -3
- package/src/components/account/Drawer/OrganizationDrawer/OrganizationDrawerBody.tsx +5 -7
- package/src/components/account/Drawer/OrganizationSignInButton/OrganizationSignInButton.tsx +4 -1
- package/src/components/account/Layout/Layout.tsx +5 -7
- package/src/components/account/Menu/Menu.tsx +1 -1
- package/src/components/navigation/Navbar/Navbar.tsx +3 -3
- package/src/components/navigation/Navbar/NavbarAccountArea.tsx +5 -5
- package/src/components/navigation/NavbarSlider/NavbarSlider.tsx +3 -3
- package/src/experimental/myAccountCmsServerSideProps.ts +3 -3
- package/src/experimental/myAccountServerSideProps.ts +4 -4
- package/src/pages/[slug]/p.tsx +18 -4
- package/src/pages/api/fs/logout.ts +18 -10
- package/src/pages/api/fs/switch-contract.ts +170 -21
- package/src/pages/api/graphql.ts +101 -60
- package/src/pages/pvt/account/403.tsx +5 -4
- package/src/pages/pvt/account/cards/index.tsx +5 -5
- package/src/pages/pvt/account/orders/[id].tsx +5 -5
- package/src/pages/pvt/account/orders/index.tsx +5 -5
- package/src/pages/pvt/account/profile.tsx +5 -5
- package/src/pages/pvt/account/quotes/index.tsx +5 -5
- package/src/pages/pvt/account/security.tsx +5 -5
- package/src/pages/pvt/account/user-details.tsx +6 -6
- package/src/sdk/account/getB2BSessionClaims.ts +1 -1
- package/src/sdk/account/getIsB2BBuyer.ts +28 -0
- package/src/sdk/account/refreshToken.ts +89 -11
- package/src/sdk/account/refreshTokenPaths.ts +25 -0
- package/src/sdk/account/useRefreshToken.ts +15 -11
- package/src/sdk/product/isProductNotFound.ts +13 -0
- package/src/sdk/product/usePageProductsQuery.ts +80 -21
- package/src/sdk/product/useProductNotFoundRedirect.ts +43 -0
- package/src/sdk/product/useProductQuery.ts +29 -0
- package/src/sdk/session/index.ts +46 -34
- package/src/server/cms/index.ts +59 -7
- package/src/server/cms/pdp.ts +10 -5
- package/src/server/cms/plp.ts +10 -5
- package/src/server/content/promiseCache.ts +68 -0
- package/src/server/content/service.ts +71 -51
- package/src/utils/getCheckoutParentDomain.ts +53 -0
- package/src/utils/isLocalHost.ts +2 -16
- package/src/utils/shouldBypassRefreshToken.ts +63 -0
- package/src/utils/trustedForwardedHost.ts +18 -0
- package/test/components/account/Layout.test.tsx +1 -1
- package/test/components/account/Menu.browser.test.tsx +7 -5
- package/test/components/account/OrganizationDrawer.browser.test.tsx +13 -13
- package/test/components/account/OrganizationDrawerBody.test.tsx +3 -3
- package/test/components/account/OrganizationSignInButton.browser.test.tsx +19 -4
- package/test/components/navigation/Navbar.browser.test.tsx +3 -3
- package/test/components/navigation/NavbarAccountArea.browser.test.tsx +5 -5
- package/test/components/navigation/NavbarSlider.browser.test.tsx +3 -3
- package/test/experimental/myAccountCmsServerSideProps.test.ts +5 -5
- package/test/pages/api/graphql.test.ts +175 -0
- package/test/pages/api/logout.test.ts +106 -0
- package/test/pages/api/switch-contract.test.ts +443 -15
- package/test/pages/pvt/account/403.test.ts +67 -0
- package/test/pages/pvt/account/cards/index.test.ts +3 -3
- package/test/sdk/account/getIsB2BBuyer.test.ts +52 -0
- package/test/sdk/account/refreshToken.browser.test.ts +10 -1
- package/test/sdk/account/refreshToken.test.ts +150 -18
- package/test/sdk/account/useRefreshToken.browser.test.ts +72 -0
- package/test/sdk/product/usePageProductsQuery.browser.test.ts +260 -0
- package/test/sdk/product/usePageProductsQuery.deliveryPromise.browser.test.ts +130 -0
- package/test/sdk/product/useProductNotFoundRedirect.browser.test.ts +83 -0
- package/test/sdk/product/useProductQuery.browser.test.ts +154 -0
- package/test/sdk/session/refreshTokenRoute.browser.test.ts +106 -0
- package/test/sdk/session/validateSession.browser.test.ts +111 -5
- package/test/server/cms/index.test.ts +102 -9
- package/test/server/content/promiseCache.test.ts +150 -0
- package/test/server/content/service.test.ts +277 -11
- package/test/utils/shouldBypassRefreshToken.browser.test.ts +38 -0
- package/test/utils/shouldBypassRefreshToken.test.ts +98 -0
- package/test/utils/trustedForwardedHost.test.ts +21 -0
- package/src/sdk/account/getIsRepresentative.ts +0 -20
- package/test/utils/isLocalHost.browser.test.ts +0 -37
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { getRequestHostname } from './getRequestHostname'
|
|
2
|
+
import { isLocalHost } from './isLocalHost'
|
|
3
|
+
import { isHostAllowed, singleForwardedHost } from './trustedForwardedHost'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* WebOps preview deployments are served under `<preview>.preview.vtex.app`.
|
|
7
|
+
*
|
|
8
|
+
* Only this subdomain is matched: `{account}.vtex.app` is a production default
|
|
9
|
+
* domain and MUST keep the refresh-token flow.
|
|
10
|
+
*/
|
|
11
|
+
const PREVIEW_HOST_SUFFIXES = ['.preview.vtex.app']
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Returns `true` when the refresh-token flow must be skipped for the given
|
|
15
|
+
* hostname: local development (`localhost` / `127.0.0.1`) and WebOps previews.
|
|
16
|
+
*
|
|
17
|
+
* Neither environment proxies VTEX ID, so `/api/vtexid/refreshtoken/webstore`
|
|
18
|
+
* always fails there and the resulting logout wipes a manually injected
|
|
19
|
+
* `VtexIdclientAutCookie_<account>` cookie. Skipping the flow lets developers
|
|
20
|
+
* test logged-in scenarios with that cookie alone.
|
|
21
|
+
*
|
|
22
|
+
* The input MUST be a bare hostname (no port). Use {@link getRequestHostname}
|
|
23
|
+
* on server-side request headers before calling this helper. A trailing dot
|
|
24
|
+
* (fully qualified name) is ignored.
|
|
25
|
+
*/
|
|
26
|
+
export function shouldBypassRefreshToken(
|
|
27
|
+
hostname: string | null | undefined
|
|
28
|
+
): boolean {
|
|
29
|
+
if (!hostname) return false
|
|
30
|
+
const normalizedHostname = hostname.toLowerCase().replace(/\.$/, '')
|
|
31
|
+
return (
|
|
32
|
+
isLocalHost(normalizedHostname) ||
|
|
33
|
+
isHostAllowed(normalizedHostname, PREVIEW_HOST_SUFFIXES)
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Server-side check from the request headers. Behind the WebOps preview
|
|
39
|
+
* ingress the browser host arrives in `x-forwarded-host` rather than `Host`
|
|
40
|
+
* (see `normalizeSetCookieDomain` in `pages/api/graphql.ts`), so both are
|
|
41
|
+
* checked. Both are client-controlled: a forged value only skips the forced
|
|
42
|
+
* refresh, which grants nothing since the JWT is still validated upstream.
|
|
43
|
+
*/
|
|
44
|
+
export function shouldBypassRefreshTokenForRequest(headers: {
|
|
45
|
+
host?: string
|
|
46
|
+
'x-forwarded-host'?: string | string[]
|
|
47
|
+
}): boolean {
|
|
48
|
+
return (
|
|
49
|
+
shouldBypassRefreshToken(getRequestHostname(headers.host)) ||
|
|
50
|
+
shouldBypassRefreshToken(
|
|
51
|
+
getRequestHostname(singleForwardedHost(headers['x-forwarded-host']))
|
|
52
|
+
)
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Browser-side wrapper that reads `window.location.hostname`. Returns `false`
|
|
58
|
+
* when `window` is not available (SSR/Node).
|
|
59
|
+
*/
|
|
60
|
+
export function shouldBypassRefreshTokenBrowser(): boolean {
|
|
61
|
+
if (globalThis.window === undefined) return false
|
|
62
|
+
return shouldBypassRefreshToken(globalThis.window.location.hostname)
|
|
63
|
+
}
|
|
@@ -2,6 +2,24 @@
|
|
|
2
2
|
// characters such as ";" or spaces would inject cookie attributes.
|
|
3
3
|
const HOSTNAME_PATTERN = /^[a-z0-9.-]+$/
|
|
4
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Hosts whose upstream cookie domain does not match the browser host (previews
|
|
7
|
+
* and local development). Cookies served there are written host-only.
|
|
8
|
+
*/
|
|
9
|
+
export const HOST_ONLY_COOKIE_HOST_SUFFIXES = [
|
|
10
|
+
'localhost',
|
|
11
|
+
'.vtex.app',
|
|
12
|
+
'.localhost',
|
|
13
|
+
]
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Whether `host` can be written as a Set-Cookie Domain attribute without
|
|
17
|
+
* injecting extra attributes.
|
|
18
|
+
*/
|
|
19
|
+
export function isCookieSafeHostname(host: string): boolean {
|
|
20
|
+
return HOSTNAME_PATTERN.test(host.toLowerCase())
|
|
21
|
+
}
|
|
22
|
+
|
|
5
23
|
/**
|
|
6
24
|
* An entry starting with "." matches subdomains only; any other entry matches
|
|
7
25
|
* the host itself and its subdomains. Matching is anchored on a label boundary,
|
|
@@ -65,7 +65,7 @@ describe('Menu', () => {
|
|
|
65
65
|
|
|
66
66
|
it('mounts the OrganizationDrawer with initialView "switch" when Switch is clicked', () => {
|
|
67
67
|
mockUseSession.mockReturnValue({
|
|
68
|
-
b2b: { unitId: 'unit-1',
|
|
68
|
+
b2b: { unitId: 'unit-1', customerId: 'contract-1' },
|
|
69
69
|
})
|
|
70
70
|
|
|
71
71
|
render(<Menu accountName="Jane" items={items} />)
|
|
@@ -79,19 +79,21 @@ describe('Menu', () => {
|
|
|
79
79
|
expect.objectContaining({
|
|
80
80
|
isOpen: true,
|
|
81
81
|
initialView: 'switch',
|
|
82
|
-
|
|
82
|
+
isB2BBuyer: true,
|
|
83
83
|
})
|
|
84
84
|
)
|
|
85
85
|
})
|
|
86
86
|
|
|
87
|
-
it('passes
|
|
88
|
-
mockUseSession.mockReturnValue({
|
|
87
|
+
it('passes isB2BBuyer for a B2B session without the isRepresentative flag', () => {
|
|
88
|
+
mockUseSession.mockReturnValue({
|
|
89
|
+
b2b: { unitId: 'unit-1', isRepresentative: false },
|
|
90
|
+
})
|
|
89
91
|
|
|
90
92
|
render(<Menu accountName="Jane" items={items} />)
|
|
91
93
|
fireEvent.click(screen.getByRole('button', { name: 'Switch' }))
|
|
92
94
|
|
|
93
95
|
expect(mockOrganizationDrawer).toHaveBeenCalledWith(
|
|
94
|
-
expect.objectContaining({
|
|
96
|
+
expect.objectContaining({ isB2BBuyer: true })
|
|
95
97
|
)
|
|
96
98
|
})
|
|
97
99
|
|
|
@@ -108,7 +108,7 @@ describe('OrganizationDrawer', () => {
|
|
|
108
108
|
<OrganizationDrawer
|
|
109
109
|
isOpen
|
|
110
110
|
closeDrawer={mockCloseDrawer}
|
|
111
|
-
|
|
111
|
+
isB2BBuyer={false}
|
|
112
112
|
/>
|
|
113
113
|
)
|
|
114
114
|
|
|
@@ -124,7 +124,7 @@ describe('OrganizationDrawer', () => {
|
|
|
124
124
|
<OrganizationDrawer
|
|
125
125
|
isOpen
|
|
126
126
|
closeDrawer={mockCloseDrawer}
|
|
127
|
-
|
|
127
|
+
isB2BBuyer={false}
|
|
128
128
|
/>
|
|
129
129
|
)
|
|
130
130
|
|
|
@@ -141,7 +141,7 @@ describe('OrganizationDrawer', () => {
|
|
|
141
141
|
<OrganizationDrawer
|
|
142
142
|
isOpen
|
|
143
143
|
closeDrawer={mockCloseDrawer}
|
|
144
|
-
|
|
144
|
+
isB2BBuyer={false}
|
|
145
145
|
/>
|
|
146
146
|
)
|
|
147
147
|
|
|
@@ -152,7 +152,7 @@ describe('OrganizationDrawer', () => {
|
|
|
152
152
|
<OrganizationDrawer
|
|
153
153
|
isOpen={false}
|
|
154
154
|
closeDrawer={mockCloseDrawer}
|
|
155
|
-
|
|
155
|
+
isB2BBuyer={false}
|
|
156
156
|
/>
|
|
157
157
|
)
|
|
158
158
|
|
|
@@ -160,7 +160,7 @@ describe('OrganizationDrawer', () => {
|
|
|
160
160
|
<OrganizationDrawer
|
|
161
161
|
isOpen
|
|
162
162
|
closeDrawer={mockCloseDrawer}
|
|
163
|
-
|
|
163
|
+
isB2BBuyer={false}
|
|
164
164
|
/>
|
|
165
165
|
)
|
|
166
166
|
|
|
@@ -181,7 +181,7 @@ describe('OrganizationDrawer', () => {
|
|
|
181
181
|
<OrganizationDrawer
|
|
182
182
|
isOpen
|
|
183
183
|
closeDrawer={mockCloseDrawer}
|
|
184
|
-
|
|
184
|
+
isB2BBuyer={false}
|
|
185
185
|
/>
|
|
186
186
|
)
|
|
187
187
|
|
|
@@ -200,7 +200,7 @@ describe('OrganizationDrawer', () => {
|
|
|
200
200
|
<OrganizationDrawer
|
|
201
201
|
isOpen
|
|
202
202
|
closeDrawer={mockCloseDrawer}
|
|
203
|
-
|
|
203
|
+
isB2BBuyer={false}
|
|
204
204
|
/>
|
|
205
205
|
)
|
|
206
206
|
|
|
@@ -215,7 +215,7 @@ describe('OrganizationDrawer', () => {
|
|
|
215
215
|
<OrganizationDrawer
|
|
216
216
|
isOpen
|
|
217
217
|
closeDrawer={vi.fn()}
|
|
218
|
-
|
|
218
|
+
isB2BBuyer
|
|
219
219
|
initialView="switch"
|
|
220
220
|
/>
|
|
221
221
|
)
|
|
@@ -258,7 +258,7 @@ describe('OrganizationDrawer', () => {
|
|
|
258
258
|
<OrganizationDrawer
|
|
259
259
|
isOpen
|
|
260
260
|
closeDrawer={mockCloseDrawer}
|
|
261
|
-
|
|
261
|
+
isB2BBuyer={false}
|
|
262
262
|
/>
|
|
263
263
|
)
|
|
264
264
|
|
|
@@ -301,7 +301,7 @@ describe('OrganizationDrawer', () => {
|
|
|
301
301
|
<OrganizationDrawer
|
|
302
302
|
isOpen
|
|
303
303
|
closeDrawer={mockCloseDrawer}
|
|
304
|
-
|
|
304
|
+
isB2BBuyer={false}
|
|
305
305
|
/>
|
|
306
306
|
)
|
|
307
307
|
|
|
@@ -313,7 +313,7 @@ describe('OrganizationDrawer', () => {
|
|
|
313
313
|
<OrganizationDrawer
|
|
314
314
|
isOpen={false}
|
|
315
315
|
closeDrawer={mockCloseDrawer}
|
|
316
|
-
|
|
316
|
+
isB2BBuyer={false}
|
|
317
317
|
/>
|
|
318
318
|
)
|
|
319
319
|
|
|
@@ -323,7 +323,7 @@ describe('OrganizationDrawer', () => {
|
|
|
323
323
|
<OrganizationDrawer
|
|
324
324
|
isOpen
|
|
325
325
|
closeDrawer={mockCloseDrawer}
|
|
326
|
-
|
|
326
|
+
isB2BBuyer={false}
|
|
327
327
|
/>
|
|
328
328
|
)
|
|
329
329
|
|
|
@@ -340,7 +340,7 @@ describe('OrganizationDrawer', () => {
|
|
|
340
340
|
<OrganizationDrawer
|
|
341
341
|
isOpen
|
|
342
342
|
closeDrawer={mockCloseDrawer}
|
|
343
|
-
|
|
343
|
+
isB2BBuyer={false}
|
|
344
344
|
/>
|
|
345
345
|
)
|
|
346
346
|
|
|
@@ -27,7 +27,7 @@ describe('OrganizationDrawerBody', () => {
|
|
|
27
27
|
it('hides the Quotes route when the user is not an org member', () => {
|
|
28
28
|
mockUseAccountNavigationLabels.mockReturnValue(undefined)
|
|
29
29
|
|
|
30
|
-
render(<OrganizationDrawerBody
|
|
30
|
+
render(<OrganizationDrawerBody isB2BBuyer isOrgMember={false} />)
|
|
31
31
|
|
|
32
32
|
expect(screen.queryByText('Quotes')).toBeNull()
|
|
33
33
|
expect(screen.getByText('Profile')).toBeTruthy()
|
|
@@ -36,7 +36,7 @@ describe('OrganizationDrawerBody', () => {
|
|
|
36
36
|
it('shows the Quotes route when the user belongs to an org unit', () => {
|
|
37
37
|
mockUseAccountNavigationLabels.mockReturnValue(undefined)
|
|
38
38
|
|
|
39
|
-
render(<OrganizationDrawerBody
|
|
39
|
+
render(<OrganizationDrawerBody isB2BBuyer isOrgMember />)
|
|
40
40
|
|
|
41
41
|
expect(screen.getByText('Quotes')).toBeTruthy()
|
|
42
42
|
})
|
|
@@ -44,7 +44,7 @@ describe('OrganizationDrawerBody', () => {
|
|
|
44
44
|
it('hides User Details when not a representative, regardless of org membership', () => {
|
|
45
45
|
mockUseAccountNavigationLabels.mockReturnValue(undefined)
|
|
46
46
|
|
|
47
|
-
render(<OrganizationDrawerBody
|
|
47
|
+
render(<OrganizationDrawerBody isB2BBuyer={false} isOrgMember />)
|
|
48
48
|
|
|
49
49
|
expect(screen.queryByText('User Details')).toBeNull()
|
|
50
50
|
expect(screen.getByText('Quotes')).toBeTruthy()
|
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
* @vitest-environment jsdom
|
|
3
3
|
*/
|
|
4
4
|
import '@testing-library/jest-dom/vitest'
|
|
5
|
-
import { render, screen } from '@testing-library/react'
|
|
5
|
+
import { fireEvent, render, screen } from '@testing-library/react'
|
|
6
6
|
import { describe, expect, it, vi } from 'vitest'
|
|
7
7
|
|
|
8
8
|
const mockUseSession = vi.hoisted(() => vi.fn())
|
|
9
|
+
const mockOrganizationDrawer = vi.hoisted(() =>
|
|
10
|
+
vi.fn((_props: Record<string, unknown>) => null)
|
|
11
|
+
)
|
|
9
12
|
vi.mock('src/sdk/session', () => ({ useSession: mockUseSession }))
|
|
10
13
|
vi.mock('src/sdk/account/accountPageContext', () => ({
|
|
11
14
|
useAccountNavigationLabels: () => ({
|
|
@@ -16,7 +19,7 @@ vi.mock('src/sdk/account/accountPageContext', () => ({
|
|
|
16
19
|
vi.mock(
|
|
17
20
|
'src/components/account/Drawer/OrganizationDrawer/OrganizationDrawer',
|
|
18
21
|
() => ({
|
|
19
|
-
OrganizationDrawer:
|
|
22
|
+
OrganizationDrawer: mockOrganizationDrawer,
|
|
20
23
|
})
|
|
21
24
|
)
|
|
22
25
|
|
|
@@ -30,7 +33,7 @@ describe('OrganizationSignInButton', () => {
|
|
|
30
33
|
person: { id: 'p1' },
|
|
31
34
|
b2b: { contractName: ' Caetano Corp ' },
|
|
32
35
|
})
|
|
33
|
-
render(<OrganizationSignInButton icon={icon}
|
|
36
|
+
render(<OrganizationSignInButton icon={icon} isB2BBuyer />)
|
|
34
37
|
expect(screen.getByRole('button', { name: 'account' })).toHaveTextContent(
|
|
35
38
|
'Caetano Corp'
|
|
36
39
|
)
|
|
@@ -41,9 +44,21 @@ describe('OrganizationSignInButton', () => {
|
|
|
41
44
|
person: { id: 'p1' },
|
|
42
45
|
b2b: { contractName: '' },
|
|
43
46
|
})
|
|
44
|
-
render(<OrganizationSignInButton icon={icon}
|
|
47
|
+
render(<OrganizationSignInButton icon={icon} isB2BBuyer />)
|
|
45
48
|
expect(screen.getByRole('button', { name: 'account' })).toHaveTextContent(
|
|
46
49
|
'Company'
|
|
47
50
|
)
|
|
48
51
|
})
|
|
52
|
+
|
|
53
|
+
it.each([
|
|
54
|
+
['isB2BBuyer', { isB2BBuyer: true }],
|
|
55
|
+
['the deprecated isRepresentative', { isRepresentative: true }],
|
|
56
|
+
])('opens the drawer as a B2B buyer from %s', (_label, props) => {
|
|
57
|
+
mockUseSession.mockReturnValue({ person: { id: 'p1' }, b2b: {} })
|
|
58
|
+
render(<OrganizationSignInButton icon={icon} {...props} />)
|
|
59
|
+
fireEvent.click(screen.getByRole('button', { name: 'account' }))
|
|
60
|
+
expect(mockOrganizationDrawer.mock.lastCall?.[0]).toMatchObject({
|
|
61
|
+
isB2BBuyer: true,
|
|
62
|
+
})
|
|
63
|
+
})
|
|
49
64
|
})
|
|
@@ -146,11 +146,11 @@ describe('Navbar', () => {
|
|
|
146
146
|
)
|
|
147
147
|
})
|
|
148
148
|
|
|
149
|
-
it('forwards isSignInResolved=true and isOrganizationEnabled for a resolved B2B
|
|
149
|
+
it('forwards isSignInResolved=true and isOrganizationEnabled for a resolved B2B session', () => {
|
|
150
150
|
mockStoreConfig.experimental.enableFaststoreMyAccount = true
|
|
151
151
|
mockUseScreenResize.mockReturnValue({ isDesktop: true })
|
|
152
152
|
mockUseSession.mockReturnValue({
|
|
153
|
-
b2b: {
|
|
153
|
+
b2b: { unitId: 'unit-1', customerId: 'contract-1' },
|
|
154
154
|
isSessionReady: true,
|
|
155
155
|
hasValidated: true,
|
|
156
156
|
})
|
|
@@ -161,7 +161,7 @@ describe('Navbar', () => {
|
|
|
161
161
|
expect.objectContaining({
|
|
162
162
|
isSignInResolved: true,
|
|
163
163
|
isOrganizationEnabled: true,
|
|
164
|
-
|
|
164
|
+
isB2BBuyer: true,
|
|
165
165
|
})
|
|
166
166
|
)
|
|
167
167
|
|
|
@@ -11,17 +11,17 @@ vi.mock(
|
|
|
11
11
|
() => ({
|
|
12
12
|
OrganizationSignInButton: ({
|
|
13
13
|
icon,
|
|
14
|
-
|
|
14
|
+
isB2BBuyer,
|
|
15
15
|
}: {
|
|
16
16
|
icon: { alt: string; icon: string }
|
|
17
|
-
|
|
17
|
+
isB2BBuyer?: boolean
|
|
18
18
|
}) => (
|
|
19
19
|
<button
|
|
20
20
|
type="button"
|
|
21
21
|
data-testid="organization-signin"
|
|
22
22
|
data-icon={icon.alt}
|
|
23
23
|
>
|
|
24
|
-
{
|
|
24
|
+
{isB2BBuyer ? 'B2B buyer' : 'Not a B2B buyer'}
|
|
25
25
|
</button>
|
|
26
26
|
),
|
|
27
27
|
})
|
|
@@ -85,7 +85,7 @@ describe('NavbarAccountArea', () => {
|
|
|
85
85
|
<NavbarAccountArea
|
|
86
86
|
isSignInResolved
|
|
87
87
|
isOrganizationEnabled
|
|
88
|
-
|
|
88
|
+
isB2BBuyer
|
|
89
89
|
signInButton={signInButton}
|
|
90
90
|
ButtonSignIn={ButtonSignInStub}
|
|
91
91
|
skeletonAttr="data-fs-navbar-signin-skeleton"
|
|
@@ -93,7 +93,7 @@ describe('NavbarAccountArea', () => {
|
|
|
93
93
|
)
|
|
94
94
|
|
|
95
95
|
expect(screen.getByTestId('organization-signin')).toHaveTextContent(
|
|
96
|
-
'
|
|
96
|
+
'B2B buyer'
|
|
97
97
|
)
|
|
98
98
|
expect(screen.queryByTestId('storefront-signin')).toBeNull()
|
|
99
99
|
})
|
|
@@ -120,10 +120,10 @@ describe('NavbarSlider', () => {
|
|
|
120
120
|
)
|
|
121
121
|
})
|
|
122
122
|
|
|
123
|
-
it('forwards isSignInResolved=true and isOrganizationEnabled once the B2B
|
|
123
|
+
it('forwards isSignInResolved=true and isOrganizationEnabled once the B2B session resolves', () => {
|
|
124
124
|
mockStoreConfig.experimental.enableFaststoreMyAccount = true
|
|
125
125
|
mockUseSession.mockReturnValue({
|
|
126
|
-
b2b: {
|
|
126
|
+
b2b: { unitId: 'unit-1', customerId: 'contract-1' },
|
|
127
127
|
isSessionReady: true,
|
|
128
128
|
hasValidated: true,
|
|
129
129
|
})
|
|
@@ -134,7 +134,7 @@ describe('NavbarSlider', () => {
|
|
|
134
134
|
expect.objectContaining({
|
|
135
135
|
isSignInResolved: true,
|
|
136
136
|
isOrganizationEnabled: true,
|
|
137
|
-
|
|
137
|
+
isB2BBuyer: true,
|
|
138
138
|
})
|
|
139
139
|
)
|
|
140
140
|
|
|
@@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
2
2
|
|
|
3
3
|
const mockValidateUser = vi.fn()
|
|
4
4
|
const mockGetMyAccountRedirect = vi.fn()
|
|
5
|
-
const
|
|
5
|
+
const mockGetIsB2BBuyer = vi.fn()
|
|
6
6
|
const mockFetchMyAccountPageContent = vi.fn()
|
|
7
7
|
const mockGetGlobalSectionsData = vi.fn()
|
|
8
8
|
const mockExecute = vi.fn()
|
|
@@ -20,8 +20,8 @@ vi.mock('src/utils/myAccountRedirect', () => ({
|
|
|
20
20
|
mockGetMyAccountRedirect(...args),
|
|
21
21
|
}))
|
|
22
22
|
|
|
23
|
-
vi.mock('src/sdk/account/
|
|
24
|
-
|
|
23
|
+
vi.mock('src/sdk/account/getIsB2BBuyer', () => ({
|
|
24
|
+
getIsB2BBuyer: (...args: unknown[]) => mockGetIsB2BBuyer(...args),
|
|
25
25
|
}))
|
|
26
26
|
|
|
27
27
|
vi.mock('src/server/cms/fetchMyAccountPageContent', () => ({
|
|
@@ -78,7 +78,7 @@ function buildContext(overrides: Record<string, unknown> = {}) {
|
|
|
78
78
|
describe('myAccountCmsServerSideProps', () => {
|
|
79
79
|
beforeEach(() => {
|
|
80
80
|
vi.clearAllMocks()
|
|
81
|
-
|
|
81
|
+
mockGetIsB2BBuyer.mockReturnValue(false)
|
|
82
82
|
mockGetGlobalSectionsData.mockReturnValue([
|
|
83
83
|
Promise.resolve({ sections: [], settings: {} }),
|
|
84
84
|
Promise.resolve({ sections: [] }),
|
|
@@ -176,7 +176,7 @@ describe('myAccountCmsServerSideProps', () => {
|
|
|
176
176
|
expect(result).toMatchObject({
|
|
177
177
|
props: {
|
|
178
178
|
accountName: 'Ada',
|
|
179
|
-
|
|
179
|
+
isB2BBuyer: false,
|
|
180
180
|
navigationLabels: { profileLabel: 'Profile' },
|
|
181
181
|
pageSections: [
|
|
182
182
|
{
|
|
@@ -9,14 +9,23 @@ import { GraphQLError } from 'graphql'
|
|
|
9
9
|
import type { NextApiRequest, NextApiResponse } from 'next'
|
|
10
10
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
11
11
|
|
|
12
|
+
import discoveryConfig from '../../../discovery.config'
|
|
12
13
|
import handler from '../../../src/pages/api/graphql'
|
|
13
14
|
import { execute } from '../../../src/server'
|
|
15
|
+
import { shouldForceRefreshTokenForValidateSession } from '../../../src/utils/validateSessionRefreshToken'
|
|
14
16
|
|
|
15
17
|
vi.mock('../../../src/server', () => ({
|
|
16
18
|
execute: vi.fn(),
|
|
17
19
|
}))
|
|
18
20
|
|
|
21
|
+
vi.mock('../../../src/utils/validateSessionRefreshToken', () => ({
|
|
22
|
+
shouldForceRefreshTokenForValidateSession: vi.fn(),
|
|
23
|
+
}))
|
|
24
|
+
|
|
19
25
|
const mockedExecute = vi.mocked(execute)
|
|
26
|
+
const mockedShouldForceRefreshToken = vi.mocked(
|
|
27
|
+
shouldForceRefreshTokenForValidateSession
|
|
28
|
+
)
|
|
20
29
|
|
|
21
30
|
const createResponse = ({ headersSent = false } = {}) => {
|
|
22
31
|
const res = {
|
|
@@ -229,3 +238,169 @@ describe('/api/graphql request handling', () => {
|
|
|
229
238
|
expect(res.end).toHaveBeenCalled()
|
|
230
239
|
})
|
|
231
240
|
})
|
|
241
|
+
|
|
242
|
+
describe('/api/graphql ValidateSession refresh-token bypass', () => {
|
|
243
|
+
const originalRefreshToken = discoveryConfig.experimental.refreshToken
|
|
244
|
+
|
|
245
|
+
const createValidateSessionRequest = (
|
|
246
|
+
host: string,
|
|
247
|
+
extraHeaders: Record<string, string> = {}
|
|
248
|
+
) =>
|
|
249
|
+
({
|
|
250
|
+
method: 'GET',
|
|
251
|
+
url: '/api/graphql',
|
|
252
|
+
headers: { host, ...extraHeaders },
|
|
253
|
+
query: {
|
|
254
|
+
operationName: 'ValidateSession',
|
|
255
|
+
operationHash: 'hash',
|
|
256
|
+
variables: JSON.stringify({ session: {} }),
|
|
257
|
+
},
|
|
258
|
+
}) as unknown as NextApiRequest
|
|
259
|
+
|
|
260
|
+
beforeEach(() => {
|
|
261
|
+
vi.clearAllMocks()
|
|
262
|
+
discoveryConfig.experimental.refreshToken = true
|
|
263
|
+
mockedShouldForceRefreshToken.mockReturnValue(true)
|
|
264
|
+
mockedExecute.mockResolvedValue({
|
|
265
|
+
data: { validateSession: null },
|
|
266
|
+
errors: undefined,
|
|
267
|
+
extensions: { cookies: new Map(), cacheControl: undefined },
|
|
268
|
+
} as never)
|
|
269
|
+
})
|
|
270
|
+
|
|
271
|
+
afterEach(() => {
|
|
272
|
+
discoveryConfig.experimental.refreshToken = originalRefreshToken
|
|
273
|
+
})
|
|
274
|
+
|
|
275
|
+
it.each([
|
|
276
|
+
'localhost:3000',
|
|
277
|
+
'127.0.0.1:3000',
|
|
278
|
+
'brandless-abc123.preview.vtex.app',
|
|
279
|
+
])('does not force a 401 on %s', async (host) => {
|
|
280
|
+
const res = createResponse()
|
|
281
|
+
await handler(createValidateSessionRequest(host), res)
|
|
282
|
+
|
|
283
|
+
expect(mockedShouldForceRefreshToken).not.toHaveBeenCalled()
|
|
284
|
+
expect(res.status).not.toHaveBeenCalledWith(401)
|
|
285
|
+
expect(mockedExecute).toHaveBeenCalled()
|
|
286
|
+
})
|
|
287
|
+
|
|
288
|
+
it('does not force a 401 on a preview behind the ingress', async () => {
|
|
289
|
+
const res = createResponse()
|
|
290
|
+
await handler(
|
|
291
|
+
createValidateSessionRequest('faststore-internal:3000', {
|
|
292
|
+
'x-forwarded-host': 'brandless-abc123.preview.vtex.app',
|
|
293
|
+
}),
|
|
294
|
+
res
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
expect(mockedShouldForceRefreshToken).not.toHaveBeenCalled()
|
|
298
|
+
expect(res.status).not.toHaveBeenCalledWith(401)
|
|
299
|
+
expect(mockedExecute).toHaveBeenCalled()
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
it.each(['brandless.vtex.app', 'brandless.fast.store'])(
|
|
303
|
+
'forces a 401 on the production host %s',
|
|
304
|
+
async (host) => {
|
|
305
|
+
const res = createResponse()
|
|
306
|
+
await handler(createValidateSessionRequest(host), res)
|
|
307
|
+
|
|
308
|
+
expect(res.status).toHaveBeenCalledWith(401)
|
|
309
|
+
expect(mockedExecute).not.toHaveBeenCalled()
|
|
310
|
+
}
|
|
311
|
+
)
|
|
312
|
+
})
|
|
313
|
+
|
|
314
|
+
describe('/api/graphql success cache-control', () => {
|
|
315
|
+
const originalGraphqlCacheControl =
|
|
316
|
+
discoveryConfig.experimental.graphqlCacheControl
|
|
317
|
+
|
|
318
|
+
const mockExecuteWithCacheControl = (cacheControl?: unknown) => {
|
|
319
|
+
mockedExecute.mockResolvedValue({
|
|
320
|
+
data: {},
|
|
321
|
+
errors: undefined,
|
|
322
|
+
extensions: { cookies: new Map(), cacheControl },
|
|
323
|
+
} as never)
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
beforeEach(() => {
|
|
327
|
+
vi.clearAllMocks()
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
afterEach(() => {
|
|
331
|
+
discoveryConfig.experimental.graphqlCacheControl =
|
|
332
|
+
originalGraphqlCacheControl
|
|
333
|
+
})
|
|
334
|
+
|
|
335
|
+
it('uses the cache-control collected while executing the operation', async () => {
|
|
336
|
+
mockExecuteWithCacheControl({ scope: 'public', sMaxAge: 120 })
|
|
337
|
+
|
|
338
|
+
const res = createResponse()
|
|
339
|
+
await handler(createRequest(), res)
|
|
340
|
+
|
|
341
|
+
expect(res.setHeader).toHaveBeenCalledWith(
|
|
342
|
+
'cache-control',
|
|
343
|
+
'public, s-maxage=120, stale-while-revalidate=0'
|
|
344
|
+
)
|
|
345
|
+
})
|
|
346
|
+
|
|
347
|
+
it('makes collected cache-control private when there is an auth cookie', async () => {
|
|
348
|
+
mockExecuteWithCacheControl({ scope: 'public', sMaxAge: 120 })
|
|
349
|
+
|
|
350
|
+
const req = createRequest()
|
|
351
|
+
req.headers.cookie = 'VtexIdclientAutCookie_store=jwt'
|
|
352
|
+
const res = createResponse()
|
|
353
|
+
await handler(req, res)
|
|
354
|
+
|
|
355
|
+
expect(res.setHeader).toHaveBeenCalledWith(
|
|
356
|
+
'cache-control',
|
|
357
|
+
'private, s-maxage=120, stale-while-revalidate=0'
|
|
358
|
+
)
|
|
359
|
+
})
|
|
360
|
+
|
|
361
|
+
it('falls back to the default max-age for GET queries without collected cache-control', async () => {
|
|
362
|
+
discoveryConfig.experimental.graphqlCacheControl = {
|
|
363
|
+
maxAge: 0,
|
|
364
|
+
staleWhileRevalidate: 0,
|
|
365
|
+
}
|
|
366
|
+
mockExecuteWithCacheControl()
|
|
367
|
+
|
|
368
|
+
const res = createResponse()
|
|
369
|
+
await handler(createRequest(), res)
|
|
370
|
+
|
|
371
|
+
expect(res.setHeader).toHaveBeenCalledWith(
|
|
372
|
+
'cache-control',
|
|
373
|
+
'public, s-maxage=300, stale-while-revalidate=3600'
|
|
374
|
+
)
|
|
375
|
+
})
|
|
376
|
+
|
|
377
|
+
it('uses the configured max-age and stale-while-revalidate for GET queries', async () => {
|
|
378
|
+
discoveryConfig.experimental.graphqlCacheControl = {
|
|
379
|
+
maxAge: 60,
|
|
380
|
+
staleWhileRevalidate: 30,
|
|
381
|
+
}
|
|
382
|
+
mockExecuteWithCacheControl()
|
|
383
|
+
|
|
384
|
+
const req = createRequest()
|
|
385
|
+
req.headers.cookie = 'VtexIdclientAutCookie_store=jwt'
|
|
386
|
+
const res = createResponse()
|
|
387
|
+
await handler(req, res)
|
|
388
|
+
|
|
389
|
+
expect(res.setHeader).toHaveBeenCalledWith(
|
|
390
|
+
'cache-control',
|
|
391
|
+
'private, s-maxage=60, stale-while-revalidate=30'
|
|
392
|
+
)
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
it('does not cache operations that are not GET queries', async () => {
|
|
396
|
+
mockExecuteWithCacheControl()
|
|
397
|
+
|
|
398
|
+
const res = createResponse()
|
|
399
|
+
await handler(createRequest('SubscribeToNewsletter'), res)
|
|
400
|
+
|
|
401
|
+
expect(res.setHeader).toHaveBeenCalledWith(
|
|
402
|
+
'cache-control',
|
|
403
|
+
'no-cache, no-store'
|
|
404
|
+
)
|
|
405
|
+
})
|
|
406
|
+
})
|