forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser-personal.test.js
More file actions
259 lines (231 loc) · 7.93 KB
/
user-personal.test.js
File metadata and controls
259 lines (231 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import crypto from 'crypto'
import {
GraphQLNonNull,
GraphQLID,
GraphQLString,
GraphQLBoolean,
} from 'graphql'
import { toGlobalId } from 'graphql-relay'
import { GraphQLEmailAddress, GraphQLPhoneNumber } from 'graphql-scalars'
import { affiliationConnection } from '../../../affiliation/objects'
import { userPersonalType } from '../index'
import { LanguageEnums, TfaSendMethodEnum } from '../../../enums'
import { decryptPhoneNumber } from '../../../validators'
const { CIPHER_KEY } = process.env
describe('given the user object', () => {
describe('testing the field definitions', () => {
it('has an id field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('id')
expect(demoType.id.type).toMatchObject(GraphQLNonNull(GraphQLID))
})
it('has a userName field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('userName')
expect(demoType.userName.type).toMatchObject(GraphQLEmailAddress)
})
it('has a displayName field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('displayName')
expect(demoType.displayName.type).toMatchObject(GraphQLString)
})
it('has a phoneNumber field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('phoneNumber')
expect(demoType.phoneNumber.type).toMatchObject(GraphQLPhoneNumber)
})
it('has a preferredLang field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('preferredLang')
expect(demoType.preferredLang.type).toMatchObject(LanguageEnums)
})
it('has a phoneValidated field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('phoneValidated')
expect(demoType.phoneValidated.type).toMatchObject(GraphQLBoolean)
})
it('has a emailValidated field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('emailValidated')
expect(demoType.emailValidated.type).toMatchObject(GraphQLBoolean)
})
it('has a tfaSendMethod field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('tfaSendMethod')
expect(demoType.tfaSendMethod.type).toMatchObject(TfaSendMethodEnum)
})
it('has an affiliations field', () => {
const demoType = userPersonalType.getFields()
expect(demoType).toHaveProperty('affiliations')
expect(demoType.affiliations.type).toMatchObject(
affiliationConnection.connectionType,
)
})
})
describe('testing the field resolvers', () => {
describe('testing the id resolver', () => {
it('returns the resolved field', () => {
const demoType = userPersonalType.getFields()
expect(demoType.id.resolve({ id: '1' })).toEqual(
toGlobalId('user', '1'),
)
})
})
describe('testing the userName field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.userName.resolve({ userName: 'test@email.gc.ca' }),
).toEqual('test@email.gc.ca')
})
})
describe('testing the displayName field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.displayName.resolve({ displayName: 'display name' }),
).toEqual('display name')
})
})
describe('testing the phoneNumber field', () => {
describe('testing undefined phoneDetails', () => {
it('returns null', () => {
const demoType = userPersonalType.getFields()
const phoneDetails = undefined
expect(
demoType.phoneNumber.resolve(
{
phoneDetails,
},
{},
{ validators: { decryptPhoneNumber } },
),
).toEqual(null)
})
})
describe('testing null phoneDetails', () => {
it('returns null', () => {
const demoType = userPersonalType.getFields()
const phoneDetails = null
expect(
demoType.phoneNumber.resolve(
{
phoneDetails,
},
{},
{ validators: { decryptPhoneNumber } },
),
).toEqual(null)
})
})
describe('testing defined phoneDetails', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
const phoneDetails = {
iv: crypto.randomBytes(12).toString('hex'),
phoneNumber: '12345678912',
}
const cipher = crypto.createCipheriv(
'aes-256-ccm',
String(CIPHER_KEY),
Buffer.from(phoneDetails.iv, 'hex'),
{
authTagLength: 16,
},
)
let encrypted = cipher.update(phoneDetails.phoneNumber, 'utf8', 'hex')
encrypted += cipher.final('hex')
expect(
demoType.phoneNumber.resolve(
{
phoneDetails: {
phoneNumber: encrypted,
iv: phoneDetails.iv,
tag: cipher.getAuthTag().toString('hex'),
},
},
{},
{ validators: { decryptPhoneNumber } },
),
).toEqual(phoneDetails.phoneNumber)
})
})
})
describe('testing the preferredLang field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.preferredLang.resolve({ preferredLang: 'english' }),
).toEqual('english')
})
})
describe('testing the phoneValidated field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.phoneValidated.resolve({ phoneValidated: true }),
).toEqual(true)
})
})
describe('testing the emailValidated field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.emailValidated.resolve({ emailValidated: true }),
).toEqual(true)
})
})
describe('testing the tfaSendMethod field', () => {
it('returns the resolved value', () => {
const demoType = userPersonalType.getFields()
expect(
demoType.tfaSendMethod.resolve({ tfaSendMethod: 'phone' }),
).toEqual('phone')
})
})
describe('testing the affiliations field', () => {
it('returns the resolved value', async () => {
const demoType = userPersonalType.getFields()
const expectedResult = {
edges: [
{
cursor: toGlobalId('affiliation', '1'),
node: {
_from: 'organizations/1',
_id: 'affiliations/1',
_key: '1',
_rev: 'rev',
_to: 'users/1',
_type: 'affiliation',
id: '1',
orgKey: '1',
permission: 'user',
userKey: '1',
},
},
],
totalCount: 1,
pageInfo: {
hasNextPage: false,
hasPreviousPage: false,
startCursor: toGlobalId('affiliation', '1'),
endCursor: toGlobalId('affiliation', '1'),
},
}
await expect(
demoType.affiliations.resolve(
{ _id: '1' },
{ first: 1 },
{
loaders: {
loadAffiliationConnectionsByUserId: jest
.fn()
.mockReturnValue(expectedResult),
},
},
),
).resolves.toEqual(expectedResult)
})
})
})
})