Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 141 additions & 25 deletions api-js/src/user/mutations/__tests__/send-phone-code.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down Expand Up @@ -125,8 +133,10 @@ describe('user send password reset email', () => {
const expectedResult = {
data: {
sendPhoneCode: {
status:
'Two factor code has been successfully sent, you will receive a text message shortly.',
result: {
status:
'Two factor code has been successfully sent, you will receive a text message shortly.',
},
},
},
}
Expand Down Expand Up @@ -163,7 +173,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand All @@ -189,11 +207,18 @@ describe('user send password reset email', () => {
},
)

const error = [
new GraphQLError('Unable to send TFA code, please try again.'),
]
const error = {
data: {
sendPhoneCode: {
result: {
code: 400,
description: 'Unable to send TFA code, please try again.',
},
},
},
}

expect(response.errors).toEqual(error)
expect(response).toEqual(error)
expect(consoleOutput).toEqual([
`User attempted to send TFA text message, however no account is associated with this key: 1.`,
])
Expand All @@ -206,7 +231,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand All @@ -232,11 +265,18 @@ describe('user send password reset email', () => {
},
)

const error = [
new GraphQLError('Authentication error, please sign in again.'),
]
const error = {
data: {
sendPhoneCode: {
result: {
code: 400,
description: 'Authentication error, please sign in again.',
},
},
},
}

expect(response.errors).toEqual(error)
expect(response).toEqual(error)
expect(consoleOutput).toEqual([
`User attempted to send TFA text message, however the userKey does not exist.`,
])
Expand All @@ -255,7 +295,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down Expand Up @@ -305,7 +353,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down Expand Up @@ -375,7 +431,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down Expand Up @@ -404,7 +468,9 @@ describe('user send password reset email', () => {
const expectedResult = {
data: {
sendPhoneCode: {
status: 'todo',
result: {
status: 'todo',
},
},
},
}
Expand Down Expand Up @@ -441,7 +507,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand All @@ -467,9 +541,18 @@ describe('user send password reset email', () => {
},
)

const error = [new GraphQLError('todo')]
const error = {
data: {
sendPhoneCode: {
result: {
code: 400,
description: 'todo',
},
},
},
}

expect(response.errors).toEqual(error)
expect(response).toEqual(error)
expect(consoleOutput).toEqual([
`User attempted to send TFA text message, however no account is associated with this key: 1.`,
])
Expand All @@ -482,7 +565,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand All @@ -508,9 +599,18 @@ describe('user send password reset email', () => {
},
)

const error = [new GraphQLError('todo')]
const error = {
data: {
sendPhoneCode: {
result: {
code: 400,
description: 'todo',
},
},
},
}

expect(response.errors).toEqual(error)
expect(response).toEqual(error)
expect(consoleOutput).toEqual([
`User attempted to send TFA text message, however the userKey does not exist.`,
])
Expand All @@ -529,7 +629,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down Expand Up @@ -576,7 +684,15 @@ describe('user send password reset email', () => {
`
mutation {
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
status
result {
... on SendPhoneCodeResult {
status
}
... on SendPhoneCodeError {
code
description
}
}
}
}
`,
Expand Down
27 changes: 18 additions & 9 deletions api-js/src/user/mutations/send-phone-code.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import crypto from 'crypto'
import { GraphQLNonNull, GraphQLString } from 'graphql'
import { GraphQLNonNull } from 'graphql'
import { mutationWithClientMutationId } from 'graphql-relay'
import { GraphQLPhoneNumber } from 'graphql-scalars'
import { t } from '@lingui/macro'

import { sendPhoneCodeUnion } from '../unions'

const { CIPHER_KEY } = process.env

export const sendPhoneCode = new mutationWithClientMutationId({
Expand All @@ -17,13 +19,11 @@ export const sendPhoneCode = new mutationWithClientMutationId({
},
}),
outputFields: () => ({
status: {
type: GraphQLString,
result: {
type: sendPhoneCodeUnion,
description:
'Informs the user if the text message was successfully sent.',
resolve: async (payload) => {
return payload.status
},
'`SendPhoneCodeUnion` returning either a `SendPhoneCodeResult`, or `SendPhoneCodeError` object.',
resolve: (payload) => payload,
},
}),
mutateAndGetPayload: async (
Expand All @@ -45,7 +45,11 @@ export const sendPhoneCode = new mutationWithClientMutationId({
console.warn(
`User attempted to send TFA text message, however the userKey does not exist.`,
)
throw new Error(i18n._(t`Authentication error, please sign in again.`))
return {
_type: 'error',
code: 400,
description: i18n._(t`Authentication error, please sign in again.`),
}
}

// Get User From Db
Expand All @@ -55,7 +59,11 @@ export const sendPhoneCode = new mutationWithClientMutationId({
console.warn(
`User attempted to send TFA text message, however no account is associated with this key: ${userKey}.`,
)
throw new Error(i18n._(t`Unable to send TFA code, please try again.`))
return {
_type: 'error',
code: 400,
description: i18n._(t`Unable to send TFA code, please try again.`),
}
}

// Generate TFA code
Expand Down Expand Up @@ -114,6 +122,7 @@ export const sendPhoneCode = new mutationWithClientMutationId({

console.info(`User: ${user._key} successfully sent tfa code.`)
return {
_type: 'regular',
status: i18n._(
t`Two factor code has been successfully sent, you will receive a text message shortly.`,
),
Expand Down
39 changes: 39 additions & 0 deletions api-js/src/user/objects/__tests__/send-phone-code-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { GraphQLInt, GraphQLString } from 'graphql'

import { sendPhoneCodeErrorType } from '../index'

describe('given the sendPhoneCodeErrorType object', () => {
describe('testing the field definitions', () => {
it('has an code field', () => {
const demoType = sendPhoneCodeErrorType.getFields()

expect(demoType).toHaveProperty('code')
expect(demoType.code.type).toMatchObject(GraphQLInt)
})
it('has a description field', () => {
const demoType = sendPhoneCodeErrorType.getFields()

expect(demoType).toHaveProperty('description')
expect(demoType.description.type).toMatchObject(GraphQLString)
})
})

describe('testing the field resolvers', () => {
describe('testing the code resolver', () => {
it('returns the resolved field', () => {
const demoType = sendPhoneCodeErrorType.getFields()

expect(demoType.code.resolve({ code: 400 })).toEqual(400)
})
})
describe('testing the description field', () => {
it('returns the resolved value', () => {
const demoType = sendPhoneCodeErrorType.getFields()

expect(
demoType.description.resolve({ description: 'description' }),
).toEqual('description')
})
})
})
})
Loading