Skip to content

Commit

Permalink
feat: update Auth.setMarket to accept string values and modify relate…
Browse files Browse the repository at this point in the history
…d scope formatting
  • Loading branch information
gianluca-sestito committed Nov 6, 2024
1 parent e22db36 commit a9a4577
Show file tree
Hide file tree
Showing 19 changed files with 131 additions and 119 deletions.
4 changes: 2 additions & 2 deletions __tests__/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ describe('api', () => {
access_token: 'asdasd',
expires_in: 7200,
})
await setMarket(9528)
await setMarket('9528')

const config = await createRequest('/test', 'get')
expect(config).toHaveProperty('headers', {
Expand All @@ -250,7 +250,7 @@ describe('api', () => {
access_token: 'qwerty',
refresh_token: 'asdasd',
expires_in: 7200,
scope: 'market:1',
scope: 'market:code:1',
created_at: Date.now() - 60 * 1000,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
Expand Down
36 changes: 18 additions & 18 deletions __tests__/auth/customer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:1',
scope: 'market:code:1',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})
await setMarket(1)
await setMarket('1')

const response = await loginAsCustomer('asd', 'asd')
expect(response).toHaveProperty('id', 'zxcVBnMASd')
Expand All @@ -87,13 +87,13 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})

await setMarket([7777])
await setMarket(['7777'])

await loginAsCustomer('asdasd', 'asdasd')
expect(currentCustomerData).toEqual({
Expand All @@ -116,12 +116,12 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})
setMarket([7777])
setMarket(['7777'])

await loginAsCustomer('asdasd', 'asdasd')
expect(currentCustomerData).toEqual({
Expand Down Expand Up @@ -155,13 +155,13 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})

await setMarket([7777])
await setMarket(['7777'])

await loginAsCustomer('asdasd', 'asdasd')
expect(isCustomerLoggedIn()).toBe(true)
Expand All @@ -185,13 +185,13 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})

await setMarket([7777])
await setMarket(['7777'])

await loginAsCustomer('asdasd', 'asdasd')
expect(getCustomerToken()).toEqual({
Expand Down Expand Up @@ -223,13 +223,13 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7778',
scope: 'market:code:7778',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})

await setMarket([7778])
await setMarket(['7778'])

const badResult = await refreshCustomer()
expect(badResult).toEqual({
Expand Down Expand Up @@ -279,26 +279,26 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:7778',
scope: 'market:code:7778',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})

await setMarket([77738])
await setMarket(['77738'])

expect(async () => {
await useCustomerSession(
'your-access-token',
'your-refresh-token',
'market:123',
'market:code:123',
)
}).rejects.toThrow('Invalid session data')

const result = await useCustomerSession(
'your-access-token',
'your-refresh-token',
'market:77738',
'market:code:77738',
)
expect(result).toEqual({
id: 'zxcVBnMASd',
Expand Down Expand Up @@ -337,12 +337,12 @@ describe('auth:customer', () => {
token_type: 'bearer',
expires_in: 7200,
refresh_token: 'your-refresh-token',
scope: 'market:1',
scope: 'market:code:1',
created_at: createdAt,
owner_id: 'zxcVBnMASd',
owner_type: 'customer',
})
await setMarket(1)
await setMarket('1')

await loginAsCustomer('asd', 'asd')

Expand Down
18 changes: 9 additions & 9 deletions __tests__/auth/guest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('auth:guest', () => {
access_token: 'your-592-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:592',
scope: 'market:code:592',
created_at: Date.now(),
})
})
Expand Down Expand Up @@ -58,10 +58,10 @@ describe('auth:guest', () => {
access_token: 'your-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:1234',
scope: 'market:code:1234',
created_at: createdAt,
})
await setMarket(1)
await setMarket('1')

const response = await loginAsGuest()
expect(response).toHaveProperty('token', 'your-access-token')
Expand All @@ -82,11 +82,11 @@ describe('auth:guest', () => {
access_token: 'your-7777-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
})

await setMarket([7777])
await setMarket(['7777'])

await loginAsGuest()
expect(tokenCache.has('7777')).toBe(true)
Expand All @@ -103,11 +103,11 @@ describe('auth:guest', () => {
access_token: 'your-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:1234',
scope: 'market:code:1234',
created_at: Date.now(),
})

await setMarket([19])
await setMarket(['19'])
await loginAsGuest()
expect(axios.post).toHaveBeenCalledTimes(1)

Expand All @@ -125,10 +125,10 @@ describe('auth:guest', () => {
access_token: 'your-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:1234',
scope: 'market:code:1234',
created_at: createdAt,
})
await setMarket(1)
await setMarket('1')

const response = await refreshGuest()
expect(response).toHaveProperty('token', 'your-access-token')
Expand Down
14 changes: 7 additions & 7 deletions __tests__/auth/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('auth:integration', () => {
access_token: 'your-592-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:592',
scope: 'market:code:592',
created_at: Date.now(),
})
})
Expand Down Expand Up @@ -67,10 +67,10 @@ describe('auth:integration', () => {
access_token: 'your-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:1234',
scope: 'market:code:1234',
created_at: createdAt,
})
await setMarket(1)
await setMarket('1')

const response = await loginAsIntegration()
expect(response).toHaveProperty('token', 'your-access-token')
Expand Down Expand Up @@ -114,11 +114,11 @@ describe('auth:integration', () => {
access_token: 'your-7777-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:7777',
scope: 'market:code:7777',
created_at: createdAt,
})

await setMarket([7777])
await setMarket(['7777'])

await loginAsIntegration()
const key = `${INTEGRATION_PREFIX}7777`
Expand All @@ -137,11 +137,11 @@ describe('auth:integration', () => {
access_token: 'your-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:1234',
scope: 'market:code:1234',
created_at: Date.now(),
})

await setMarket([19])
await setMarket(['19'])
await loginAsIntegration()
expect(axios.post).toHaveBeenCalledTimes(1)

Expand Down
34 changes: 17 additions & 17 deletions __tests__/auth/market.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('auth:market', () => {
access_token: 'your-7777-access-token',
token_type: 'bearer',
expires_in: 7200,
scope: 'market:7777',
scope: 'market:code:7777',
created_at: Date.now(),
})
})
Expand All @@ -30,40 +30,40 @@ describe('auth:market', () => {
})

it('sets the current market', async () => {
await setMarket(123)
expect(getMarket()).toEqual([123])
await setMarket('123')
expect(getMarket()).toEqual(['123'])
})

it('handles both single and array inputs', async () => {
await setMarket(123)
expect(getMarket()).toEqual([123])
await setMarket('123')
expect(getMarket()).toEqual(['123'])

await setMarket([123, 456])
expect(getMarket()).toEqual([123, 456])
await setMarket(['123', '456'])
expect(getMarket()).toEqual(['123', '456'])
})

it('returns the scope based on current market', async () => {
await setMarket(777)
expect(getScope()).toEqual('market:777')
await setMarket('777')
expect(getScope()).toEqual('market:code:777')

await setMarket([987, 333])
expect(getScope()).toEqual('market:987 market:333')
await setMarket(['987', '333'])
expect(getScope()).toEqual('market:code:987 market:code:333')
})

it('sets current market by value', async () => {
const market = [567]
const market = ['567']
await setMarket(market)
expect(getMarket()).toEqual([567])
expect(getMarket()).toEqual(['567'])

market.push(432)
expect(getMarket()).toEqual([567])
market.push('432')
expect(getMarket()).toEqual(['567'])
})

it('logs out current customer when changing market', async () => {
const originalFn = resetSession
;(resetSession as any) = jest.fn()

await setMarket(918)
await setMarket('918')
expect(resetSession).toHaveBeenCalledTimes(1)
;(resetSession as any) = originalFn
})
Expand All @@ -72,7 +72,7 @@ describe('auth:market', () => {
const originalFn = loginAsGuest
;(loginAsGuest as any) = jest.fn()

await setMarket(918)
await setMarket('918')
expect(loginAsGuest).toHaveBeenCalledTimes(1)
;(loginAsGuest as any) = originalFn
})
Expand Down
Loading

0 comments on commit a9a4577

Please sign in to comment.