Skip to content

Commit

Permalink
fix: send correct network error response
Browse files Browse the repository at this point in the history
  • Loading branch information
onehassan committed Jul 5, 2023
1 parent e35c780 commit 742c701
Show file tree
Hide file tree
Showing 17 changed files with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,9 @@ export const createAuthMachine = ({
accessToken: (_, { data }) => {
if (data.session) {
const { accessTokenExpiresIn, accessToken } = data.session

console.info({ accessTokenExpiresIn })

const nextRefresh = new Date(Date.now() + accessTokenExpiresIn * 1_000)
storageSetter(NHOST_JWT_EXPIRES_AT_KEY, nextRefresh.toISOString())
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faker } from '@faker-js/faker'
import { HttpResponse, rest } from 'msw'
import { Mfa, NhostSession } from '../../../src'
import { BASE_URL } from '../config'
import { fakeAnonymousUser } from '../mocks/user'

Expand All @@ -25,7 +26,7 @@ export const anonymousInternalErrorHandler = rest.post(`${BASE_URL}/signin/anony
* Request handler for MSW to mock a successful anonymous sign.
*/
export const correctAnonymousHandler = rest.post(`${BASE_URL}/signin/anonymous`, () =>
HttpResponse.json({
HttpResponse.json<{ mfa: Mfa | null; session: NhostSession | null }>({
session: {
user: fakeAnonymousUser,
accessTokenExpiresIn: 900,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { faker } from '@faker-js/faker'
import { HttpResponse, rest } from 'msw'
import { NhostSession } from '../../../src'
import { BASE_URL } from '../config'
import fakeUser from '../mocks/user'

/**
* Request handler for MSW to mock a successful request for a new access token.
*/
export const authTokenSuccessHandler = rest.post(`${BASE_URL}/token`, () => {
return HttpResponse.json({
return HttpResponse.json<NhostSession>({
accessToken: faker.datatype.string(40),
refreshToken: faker.datatype.uuid(),
accessTokenExpiresIn: 900,
Expand Down Expand Up @@ -42,6 +43,7 @@ export const authTokenInternalErrorHandler = rest.post(`${BASE_URL}/token`, () =
/**
* Request handler for MSW to mock a network error when requesting a new access token.
*/
export const authTokenNetworkErrorHandler = rest.post(`${BASE_URL}/token`, () => {
return new Response('Network error', { status: 500 })
})
export const authTokenNetworkErrorHandler = rest.post(
`${BASE_URL}/token`,
() => new Response(null, { status: 500, statusText: 'Network erro' })
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BASE_URL } from '../config'
*/
export const changeEmailNetworkErrorHandler = rest.post(
`${BASE_URL}/user/email/change`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BASE_URL } from '../config'
*/
export const changePasswordNetworkErrorHandler = rest.post(
`${BASE_URL}/user/password`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { BASE_URL } from '../config'
*/
export const generateMfaTotpNetworkErrorHandler = rest.get(
`${BASE_URL}/mfa/totp/generate`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down Expand Up @@ -59,7 +59,7 @@ export const generateMfaTotpSuccessHandler = rest.get(`${BASE_URL}/mfa/totp/gene
*/
export const activateMfaTotpNetworkErrorHandler = rest.post(
`${BASE_URL}/user/mfa`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fakeUser from '../mocks/user'
*/
export const mfaTotpNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/mfa/totp`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export const unverifiedEmailErrorHandler = rest.post(`${BASE_URL}/signin/email-p
*/
export const emailPasswordNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/email-password`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const correctPasswordlessEmailHandler = rest.post(
*/
export const passwordlessEmailNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/passwordless/email`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const correctPasswordlessSmsHandler = rest.post(
*/
export const passwordlessSmsNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/passwordless/sms`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down Expand Up @@ -100,5 +100,5 @@ export const passwordlessSmsOtpInvalidOtpHandler = rest.post(
*/
export const passwordlessSmsOtpNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/passwordless/sms/otp`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fakeUser from '../mocks/user'
*/
export const patSignInNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/pat`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const resetPasswordInternalErrorHandler = rest.post(`${BASE_URL}/user/pas
*/
export const resetPasswordNetworkErrorHandler = rest.post(
`${BASE_URL}/user/password/reset`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export const incorrectSecurityKeyVerifyHandler = rest.post(
*/
export const emailSecurityKeyNetworkErrorHandler = rest.post(
`${BASE_URL}/signin/webauthn`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import fakeUser from '../mocks/user'
*/
export const signUpEmailSecurityKeyNetworkErrorHandler = rest.post(
`${BASE_URL}/signup/webauthn`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const sendVerificationEmailInternalErrorHandler = rest.post(
*/
export const sendVerificationEmailNetworkErrorHandler = rest.post(
`${BASE_URL}/user/email/send-verification-email`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { BASE_URL } from '../config'
*/
export const signOutNetworkErrorHandler = rest.post(
`${BASE_URL}/signout`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import fakeUser from '../mocks/user'
*/
export const signUpNetworkErrorHandler = rest.post(
`${BASE_URL}/signup/email-password`,
() => new Response('Network error', { status: 500 })
() => new Response(null, { status: 500, statusText: 'Network erro' })
)

/**
Expand Down

0 comments on commit 742c701

Please sign in to comment.