Skip to content

Commit c3e0dda

Browse files
authored
Send Phone Code Union Introduction (canada-ca#1796)
* create new send phone code objects * create new send phone code union * update sendPhoneCodeResult object * update mutation to use union * update faker
1 parent e7b4c8c commit c3e0dda

11 files changed

Lines changed: 340 additions & 36 deletions

api-js/src/user/mutations/__tests__/send-phone-code.test.js

Lines changed: 141 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,15 @@ describe('user send password reset email', () => {
9696
`
9797
mutation {
9898
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
99-
status
99+
result {
100+
... on SendPhoneCodeResult {
101+
status
102+
}
103+
... on SendPhoneCodeError {
104+
code
105+
description
106+
}
107+
}
100108
}
101109
}
102110
`,
@@ -125,8 +133,10 @@ describe('user send password reset email', () => {
125133
const expectedResult = {
126134
data: {
127135
sendPhoneCode: {
128-
status:
129-
'Two factor code has been successfully sent, you will receive a text message shortly.',
136+
result: {
137+
status:
138+
'Two factor code has been successfully sent, you will receive a text message shortly.',
139+
},
130140
},
131141
},
132142
}
@@ -163,7 +173,15 @@ describe('user send password reset email', () => {
163173
`
164174
mutation {
165175
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
166-
status
176+
result {
177+
... on SendPhoneCodeResult {
178+
status
179+
}
180+
... on SendPhoneCodeError {
181+
code
182+
description
183+
}
184+
}
167185
}
168186
}
169187
`,
@@ -189,11 +207,18 @@ describe('user send password reset email', () => {
189207
},
190208
)
191209

192-
const error = [
193-
new GraphQLError('Unable to send TFA code, please try again.'),
194-
]
210+
const error = {
211+
data: {
212+
sendPhoneCode: {
213+
result: {
214+
code: 400,
215+
description: 'Unable to send TFA code, please try again.',
216+
},
217+
},
218+
},
219+
}
195220

196-
expect(response.errors).toEqual(error)
221+
expect(response).toEqual(error)
197222
expect(consoleOutput).toEqual([
198223
`User attempted to send TFA text message, however no account is associated with this key: 1.`,
199224
])
@@ -206,7 +231,15 @@ describe('user send password reset email', () => {
206231
`
207232
mutation {
208233
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
209-
status
234+
result {
235+
... on SendPhoneCodeResult {
236+
status
237+
}
238+
... on SendPhoneCodeError {
239+
code
240+
description
241+
}
242+
}
210243
}
211244
}
212245
`,
@@ -232,11 +265,18 @@ describe('user send password reset email', () => {
232265
},
233266
)
234267

235-
const error = [
236-
new GraphQLError('Authentication error, please sign in again.'),
237-
]
268+
const error = {
269+
data: {
270+
sendPhoneCode: {
271+
result: {
272+
code: 400,
273+
description: 'Authentication error, please sign in again.',
274+
},
275+
},
276+
},
277+
}
238278

239-
expect(response.errors).toEqual(error)
279+
expect(response).toEqual(error)
240280
expect(consoleOutput).toEqual([
241281
`User attempted to send TFA text message, however the userKey does not exist.`,
242282
])
@@ -255,7 +295,15 @@ describe('user send password reset email', () => {
255295
`
256296
mutation {
257297
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
258-
status
298+
result {
299+
... on SendPhoneCodeResult {
300+
status
301+
}
302+
... on SendPhoneCodeError {
303+
code
304+
description
305+
}
306+
}
259307
}
260308
}
261309
`,
@@ -305,7 +353,15 @@ describe('user send password reset email', () => {
305353
`
306354
mutation {
307355
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
308-
status
356+
result {
357+
... on SendPhoneCodeResult {
358+
status
359+
}
360+
... on SendPhoneCodeError {
361+
code
362+
description
363+
}
364+
}
309365
}
310366
}
311367
`,
@@ -375,7 +431,15 @@ describe('user send password reset email', () => {
375431
`
376432
mutation {
377433
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
378-
status
434+
result {
435+
... on SendPhoneCodeResult {
436+
status
437+
}
438+
... on SendPhoneCodeError {
439+
code
440+
description
441+
}
442+
}
379443
}
380444
}
381445
`,
@@ -404,7 +468,9 @@ describe('user send password reset email', () => {
404468
const expectedResult = {
405469
data: {
406470
sendPhoneCode: {
407-
status: 'todo',
471+
result: {
472+
status: 'todo',
473+
},
408474
},
409475
},
410476
}
@@ -441,7 +507,15 @@ describe('user send password reset email', () => {
441507
`
442508
mutation {
443509
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
444-
status
510+
result {
511+
... on SendPhoneCodeResult {
512+
status
513+
}
514+
... on SendPhoneCodeError {
515+
code
516+
description
517+
}
518+
}
445519
}
446520
}
447521
`,
@@ -467,9 +541,18 @@ describe('user send password reset email', () => {
467541
},
468542
)
469543

470-
const error = [new GraphQLError('todo')]
544+
const error = {
545+
data: {
546+
sendPhoneCode: {
547+
result: {
548+
code: 400,
549+
description: 'todo',
550+
},
551+
},
552+
},
553+
}
471554

472-
expect(response.errors).toEqual(error)
555+
expect(response).toEqual(error)
473556
expect(consoleOutput).toEqual([
474557
`User attempted to send TFA text message, however no account is associated with this key: 1.`,
475558
])
@@ -482,7 +565,15 @@ describe('user send password reset email', () => {
482565
`
483566
mutation {
484567
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
485-
status
568+
result {
569+
... on SendPhoneCodeResult {
570+
status
571+
}
572+
... on SendPhoneCodeError {
573+
code
574+
description
575+
}
576+
}
486577
}
487578
}
488579
`,
@@ -508,9 +599,18 @@ describe('user send password reset email', () => {
508599
},
509600
)
510601

511-
const error = [new GraphQLError('todo')]
602+
const error = {
603+
data: {
604+
sendPhoneCode: {
605+
result: {
606+
code: 400,
607+
description: 'todo',
608+
},
609+
},
610+
},
611+
}
512612

513-
expect(response.errors).toEqual(error)
613+
expect(response).toEqual(error)
514614
expect(consoleOutput).toEqual([
515615
`User attempted to send TFA text message, however the userKey does not exist.`,
516616
])
@@ -529,7 +629,15 @@ describe('user send password reset email', () => {
529629
`
530630
mutation {
531631
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
532-
status
632+
result {
633+
... on SendPhoneCodeResult {
634+
status
635+
}
636+
... on SendPhoneCodeError {
637+
code
638+
description
639+
}
640+
}
533641
}
534642
}
535643
`,
@@ -576,7 +684,15 @@ describe('user send password reset email', () => {
576684
`
577685
mutation {
578686
sendPhoneCode(input: { phoneNumber: "+12345678901" }) {
579-
status
687+
result {
688+
... on SendPhoneCodeResult {
689+
status
690+
}
691+
... on SendPhoneCodeError {
692+
code
693+
description
694+
}
695+
}
580696
}
581697
}
582698
`,

api-js/src/user/mutations/send-phone-code.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import crypto from 'crypto'
2-
import { GraphQLNonNull, GraphQLString } from 'graphql'
2+
import { GraphQLNonNull } from 'graphql'
33
import { mutationWithClientMutationId } from 'graphql-relay'
44
import { GraphQLPhoneNumber } from 'graphql-scalars'
55
import { t } from '@lingui/macro'
66

7+
import { sendPhoneCodeUnion } from '../unions'
8+
79
const { CIPHER_KEY } = process.env
810

911
export const sendPhoneCode = new mutationWithClientMutationId({
@@ -17,13 +19,11 @@ export const sendPhoneCode = new mutationWithClientMutationId({
1719
},
1820
}),
1921
outputFields: () => ({
20-
status: {
21-
type: GraphQLString,
22+
result: {
23+
type: sendPhoneCodeUnion,
2224
description:
23-
'Informs the user if the text message was successfully sent.',
24-
resolve: async (payload) => {
25-
return payload.status
26-
},
25+
'`SendPhoneCodeUnion` returning either a `SendPhoneCodeResult`, or `SendPhoneCodeError` object.',
26+
resolve: (payload) => payload,
2727
},
2828
}),
2929
mutateAndGetPayload: async (
@@ -45,7 +45,11 @@ export const sendPhoneCode = new mutationWithClientMutationId({
4545
console.warn(
4646
`User attempted to send TFA text message, however the userKey does not exist.`,
4747
)
48-
throw new Error(i18n._(t`Authentication error, please sign in again.`))
48+
return {
49+
_type: 'error',
50+
code: 400,
51+
description: i18n._(t`Authentication error, please sign in again.`),
52+
}
4953
}
5054

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

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

115123
console.info(`User: ${user._key} successfully sent tfa code.`)
116124
return {
125+
_type: 'regular',
117126
status: i18n._(
118127
t`Two factor code has been successfully sent, you will receive a text message shortly.`,
119128
),
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { GraphQLInt, GraphQLString } from 'graphql'
2+
3+
import { sendPhoneCodeErrorType } from '../index'
4+
5+
describe('given the sendPhoneCodeErrorType object', () => {
6+
describe('testing the field definitions', () => {
7+
it('has an code field', () => {
8+
const demoType = sendPhoneCodeErrorType.getFields()
9+
10+
expect(demoType).toHaveProperty('code')
11+
expect(demoType.code.type).toMatchObject(GraphQLInt)
12+
})
13+
it('has a description field', () => {
14+
const demoType = sendPhoneCodeErrorType.getFields()
15+
16+
expect(demoType).toHaveProperty('description')
17+
expect(demoType.description.type).toMatchObject(GraphQLString)
18+
})
19+
})
20+
21+
describe('testing the field resolvers', () => {
22+
describe('testing the code resolver', () => {
23+
it('returns the resolved field', () => {
24+
const demoType = sendPhoneCodeErrorType.getFields()
25+
26+
expect(demoType.code.resolve({ code: 400 })).toEqual(400)
27+
})
28+
})
29+
describe('testing the description field', () => {
30+
it('returns the resolved value', () => {
31+
const demoType = sendPhoneCodeErrorType.getFields()
32+
33+
expect(
34+
demoType.description.resolve({ description: 'description' }),
35+
).toEqual('description')
36+
})
37+
})
38+
})
39+
})

0 commit comments

Comments
 (0)