Skip to content

Commit 5ee6288

Browse files
committed
fix(@aws-amplify-react/auth): handle required attributes in NEW_PASSWORD_REQUIRED challenge
1 parent c68017b commit 5ee6288

File tree

3 files changed

+2081
-12
lines changed

3 files changed

+2081
-12
lines changed

packages/aws-amplify-react/__tests__/Auth/RequireNewPassword-test.js

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,27 @@ describe('RequireNewPassword test', () => {
1010

1111
wrapper.setProps({
1212
authState: 'requireNewPassword',
13+
authData: {
14+
challengeParam: {
15+
requiredAttributes: []
16+
}
17+
},
18+
hide: false
19+
});
20+
21+
expect(wrapper).toMatchSnapshot();
22+
});
23+
24+
test('render correctly with required attributes', () => {
25+
const wrapper = shallow(<RequireNewPassword/>);
26+
27+
wrapper.setProps({
28+
authState: 'requireNewPassword',
29+
authData: {
30+
challengeParam: {
31+
requiredAttributes: ['given_name', 'family_name']
32+
}
33+
},
1334
hide: false
1435
});
1536

@@ -47,6 +68,11 @@ describe('RequireNewPassword test', () => {
4768

4869
wrapper.setProps({
4970
authState: 'requireNewPassword',
71+
authData: {
72+
challengeParam: {
73+
requiredAttributes: []
74+
}
75+
},
5076
hide: false
5177
});
5278

@@ -64,6 +90,11 @@ describe('RequireNewPassword test', () => {
6490

6591
wrapper.setProps({
6692
authState: 'requireNewPassword',
93+
authData: {
94+
challengeParam: {
95+
requiredAttributes: []
96+
}
97+
},
6798
hide: false
6899
});
69100

@@ -80,7 +111,7 @@ describe('RequireNewPassword test', () => {
80111
const props = {
81112
authData: {
82113
challengeParam: {
83-
requiredAttributes: 'requiredAttributes'
114+
requiredAttributes: []
84115
}
85116
}
86117
}
@@ -102,9 +133,9 @@ describe('RequireNewPassword test', () => {
102133

103134
await requireNewPassword.change();
104135

105-
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": "requiredAttributes"}},
136+
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": []}},
106137
'password',
107-
'requiredAttributes');
138+
{});
108139

109140
expect(spyon2).toBeCalledWith('user');
110141
spyon.mockClear();
@@ -114,7 +145,7 @@ describe('RequireNewPassword test', () => {
114145
const props = {
115146
authData: {
116147
challengeParam: {
117-
requiredAttributes: 'requiredAttributes'
148+
requiredAttributes: []
118149
}
119150
}
120151
}
@@ -133,14 +164,14 @@ describe('RequireNewPassword test', () => {
133164

134165
wrapper.setProps(props);
135166
requireNewPassword.inputs = {
136-
password: 'password'
167+
password: 'password',
137168
}
138169

139170
await requireNewPassword.change();
140171

141-
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": "requiredAttributes"}},
172+
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": []}},
142173
'password',
143-
'requiredAttributes');
174+
{});
144175

145176
expect(spyon2).toBeCalledWith('confirmSignIn', { challengeName: 'SMS_MFA' });
146177
spyon.mockClear();
@@ -150,7 +181,7 @@ describe('RequireNewPassword test', () => {
150181
const props = {
151182
authData: {
152183
challengeParam: {
153-
requiredAttributes: 'requiredAttributes'
184+
requiredAttributes: []
154185
}
155186
}
156187
}
@@ -174,9 +205,9 @@ describe('RequireNewPassword test', () => {
174205

175206
await requireNewPassword.change();
176207

177-
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": "requiredAttributes"}},
208+
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": []}},
178209
'password',
179-
'requiredAttributes');
210+
{});
180211

181212
expect(spyon2).toBeCalledWith('TOTPSetup', { challengeName: 'MFA_SETUP' });
182213
spyon.mockClear();
@@ -211,6 +242,42 @@ describe('RequireNewPassword test', () => {
211242
spyon.mockClear();
212243
spyon2.mockClear();
213244
});
245+
246+
test('requiredAttributes test', async () => {
247+
const props = {
248+
authData: {
249+
challengeParam: {
250+
requiredAttributes: ['given_name', 'family_name']
251+
}
252+
}
253+
}
254+
255+
const spyon = jest.spyOn(Auth, 'completeNewPassword').mockImplementationOnce(() => {
256+
return new Promise((res, rej) => {
257+
res('user');
258+
});
259+
});
260+
const spyon2 = jest.spyOn(RequireNewPassword.prototype, 'checkContact').mockImplementationOnce(() => { return; });
261+
262+
const wrapper = shallow(<RequireNewPassword/>);
263+
const requireNewPassword = wrapper.instance();
264+
265+
wrapper.setProps(props);
266+
requireNewPassword.inputs = {
267+
password: 'password',
268+
given_name: 'Max',
269+
family_name: 'Power'
270+
}
271+
272+
await requireNewPassword.change();
273+
274+
expect(spyon).toBeCalledWith({"challengeParam": {"requiredAttributes": ['given_name', 'family_name']}},
275+
'password',
276+
{given_name: 'Max', family_name: 'Power'});
277+
278+
expect(spyon2).toBeCalledWith('user');
279+
spyon.mockClear();
280+
});
214281
});
215282

216283
describe('checkContact test', () => {

0 commit comments

Comments
 (0)