1
- import { client , startServer } from './setup' ;
1
+ import { client , clientWithAuth , startServer , FakeAdapter } from './setup' ;
2
2
3
3
test ( 'signup - a new user' , async ( ) => {
4
4
const req = client ( await startServer ( ) ) ;
@@ -25,6 +25,7 @@ test('signup - a new user', async () => {
25
25
26
26
test ( 'signup - with existent user' , async ( ) => {
27
27
const req = client ( await startServer ( ) ) ;
28
+ expect . assertions ( 1 ) ;
28
29
29
30
try {
30
31
await req . request ( `mutation {
@@ -39,6 +40,7 @@ test('signup - with existent user', async () => {
39
40
40
41
test ( 'signup - with weak password' , async ( ) => {
41
42
const req = client ( await startServer ( ) ) ;
43
+ expect . assertions ( 1 ) ;
42
44
43
45
try {
44
46
await req . request ( `mutation {
@@ -76,6 +78,7 @@ test('login - correct', async () => {
76
78
77
79
test ( 'login - non-existent user' , async ( ) => {
78
80
const req = client ( await startServer ( ) ) ;
81
+ expect . assertions ( 1 ) ;
79
82
80
83
try {
81
84
await req . request ( `mutation {
@@ -90,6 +93,7 @@ test('login - non-existent user', async () => {
90
93
91
94
test ( 'login - wrong password' , async ( ) => {
92
95
const req = client ( await startServer ( ) ) ;
96
+ expect . assertions ( 1 ) ;
93
97
94
98
try {
95
99
await req . request ( `mutation {
@@ -101,3 +105,118 @@ test('login - wrong password', async () => {
101
105
expect ( String ( e ) ) . toMatch ( / N o u s e r f o u n d / ) ;
102
106
}
103
107
} ) ;
108
+
109
+ test ( 'update current user data - correct' , async ( ) => {
110
+ const req = clientWithAuth ( await startServer ( ) ) ;
111
+
112
+ const result = await req . request ( `mutation {
113
+ updateCurrentUser(data: {name: "Voldemort"}) {
114
+ id
115
+ name
116
+ }
117
+ }` ) ;
118
+
119
+ expect ( ( result as any ) . updateCurrentUser ) . toEqual ( {
120
+ id : '2' ,
121
+ name : 'Voldemort'
122
+ } ) ;
123
+ } ) ;
124
+
125
+ test ( 'update current user data - wrong old passwd' , async ( ) => {
126
+ const req = clientWithAuth ( await startServer ( ) ) ;
127
+ expect . assertions ( 1 ) ;
128
+
129
+ try {
130
+ await req . request ( `mutation {
131
+ changePassword(oldPassword: "testtest3", newPassword: "testtest4") {
132
+ id
133
+ }
134
+ }` ) ;
135
+ } catch ( e ) {
136
+ expect ( String ( e ) ) . toMatch ( / I n v a l i d o l d p a s s w o r d / ) ;
137
+ }
138
+ } ) ;
139
+
140
+ test ( 'update user password' , async ( ) => {
141
+ const req = clientWithAuth ( await startServer ( ) ) ;
142
+
143
+ const result = await req . request ( `mutation {
144
+ changePassword(oldPassword: "testtest2", newPassword: "testtest3") {
145
+ id
146
+ }
147
+ }` ) ;
148
+
149
+ expect ( ( result as any ) . changePassword ) . toEqual ( {
150
+ id : '2'
151
+ } ) ;
152
+
153
+ // Now verify the password has actually been changed correctly.
154
+ const result2 = await req . request ( `mutation {
155
+ login(email: "[email protected] ", password: "testtest3") {
156
+ user {
157
+ id
158
+ }
159
+ }
160
+ }` ) ;
161
+
162
+ expect ( ( result2 as any ) . login . user ) . toEqual ( {
163
+ id : '2'
164
+ } ) ;
165
+ } ) ;
166
+
167
+ test ( 'trigger password reset - correct' , async ( ) => {
168
+ const req = clientWithAuth ( await startServer ( ) ) ;
169
+ expect . assertions ( 6 ) ;
170
+ const spy = jest . spyOn ( FakeAdapter . prototype , 'updateUserResetToken' ) ;
171
+
172
+ const result = await req . request ( `mutation {
173
+ triggerPasswordReset(email: "[email protected] ") {
174
+ ok
175
+ }
176
+ }` ) ;
177
+
178
+ expect ( spy ) . toHaveBeenCalled ( ) ;
179
+
180
+ expect ( ( result as any ) . triggerPasswordReset ) . toEqual ( {
181
+ ok : true
182
+ } ) ;
183
+
184
+ const { resetToken } = await spy . mock . results [ 0 ] . value ;
185
+ // Verify the resetToken is a UUID
186
+ expect ( resetToken . length ) . toBe ( 36 ) ;
187
+
188
+ const result2 = await req . request ( `mutation {
189
+ passwordReset(email: "[email protected] ", password: "testtest4", resetToken: "${ resetToken } ") {
190
+ id
191
+ }
192
+ }` ) ;
193
+
194
+ expect ( ( result2 as any ) . passwordReset ) . toEqual ( {
195
+ id : '2'
196
+ } ) ;
197
+
198
+ const result3 = await req . request ( `mutation {
199
+ login(email: "[email protected] ", password: "testtest4") {
200
+ user {
201
+ id
202
+ }
203
+ }
204
+ }` ) ;
205
+
206
+ expect ( ( result3 as any ) . login . user ) . toEqual ( {
207
+ id : '2'
208
+ } ) ;
209
+
210
+ // Now verify that the resetToken is now invalid
211
+ try {
212
+ await req . request ( `mutation {
213
+ passwordReset(email: "[email protected] ", password: "badbadbad", resetToken: "${ resetToken } ") {
214
+ id
215
+ }
216
+ }` ) ;
217
+ } catch ( e ) {
218
+ expect ( String ( e ) ) . toMatch ( / N o u s e r f o u n d / ) ;
219
+ }
220
+
221
+ spy . mockRestore ( ) ;
222
+ } ) ;
0 commit comments