Skip to content

Commit

Permalink
test(auth): Fix mock phone numbers (aws-amplify#2562)
Browse files Browse the repository at this point in the history
Using a single mock phone number led to exceptions when multiple users were created with the same phone number. Any number in the range `555-0100` through `555-0199` are reserved for fictional use (https://en.wikipedia.org/wiki/555_(telephone_number)), so generate random numbers with this pattern.
  • Loading branch information
dnys1 authored Jan 10, 2023
1 parent 29c785d commit f7a89fa
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void main() {
options: CognitoSignUpOptions(
userAttributes: {
CognitoUserAttributeKey.email: generateEmail(),
CognitoUserAttributeKey.phoneNumber: mockPhoneNumber
CognitoUserAttributeKey.phoneNumber: generatePhoneNumber(),
},
),
) as CognitoSignUpResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ void main() {
group(
'with alias',
() {
const username = mockPhoneNumber;
final password = generatePassword();
late String username;
late String password;

setUpAll(() async {
await configureAuth(
Expand All @@ -86,16 +86,18 @@ void main() {
tearDownAll(Amplify.reset);

setUp(() async {
username = generatePhoneNumber();
password = generatePassword();
await adminCreateUser(
username,
password,
autoConfirm: true,
verifyAttributes: true,
enableMfa: true,
attributes: const [
attributes: [
AuthUserAttribute(
userAttributeKey: CognitoUserAttributeKey.phoneNumber,
value: mockPhoneNumber,
value: username,
),
],
);
Expand Down Expand Up @@ -129,7 +131,7 @@ void main() {
isA<CognitoSignInDetailsApiBased>().having(
(details) => details.username,
'username',
mockPhoneNumber,
username,
),
reason: 'Should return the phone number alias since it '
'was used to sign in',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void main() {
options: CognitoSignUpOptions(
userAttributes: {
CognitoUserAttributeKey.email: generateEmail(),
CognitoUserAttributeKey.phoneNumber: mockPhoneNumber
CognitoUserAttributeKey.phoneNumber: generatePhoneNumber(),
},
),
) as CognitoSignUpResult;
Expand Down Expand Up @@ -64,7 +64,7 @@ void main() {
final options = CognitoSignUpOptions(
userAttributes: {
CognitoUserAttributeKey.email: generateEmail(),
CognitoUserAttributeKey.phoneNumber: mockPhoneNumber
CognitoUserAttributeKey.phoneNumber: generatePhoneNumber(),
},
);
await expectLater(
Expand All @@ -90,7 +90,7 @@ void main() {
final userOneOptions = CognitoSignUpOptions(
userAttributes: {
CognitoUserAttributeKey.email: generateEmail(),
CognitoUserAttributeKey.phoneNumber: mockPhoneNumber
CognitoUserAttributeKey.phoneNumber: generatePhoneNumber(),
},
);
await Amplify.Auth.signUp(
Expand All @@ -104,7 +104,7 @@ void main() {
final userTwoOptions = CognitoSignUpOptions(
userAttributes: {
CognitoUserAttributeKey.email: generateEmail(),
CognitoUserAttributeKey.phoneNumber: mockPhoneNumber
CognitoUserAttributeKey.phoneNumber: generatePhoneNumber(),
},
);
await expectLater(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void main() {
final username = generateUsername();
final password = generatePassword();
final email = generateEmail();
const phoneNumber = mockPhoneNumber;
final phoneNumber = generatePhoneNumber();
final name = generateNameAttribute();

group('User Attributes', () {
Expand All @@ -44,9 +44,9 @@ void main() {
userAttributeKey: CognitoUserAttributeKey.email,
value: email,
),
const AuthUserAttribute(
AuthUserAttribute(
userAttributeKey: CognitoUserAttributeKey.phoneNumber,
value: mockPhoneNumber,
value: phoneNumber,
)
],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,18 @@ const uppercaseLetters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const lowercaseLetters = 'abcdefghijklmnopqrstuvwxyz';
const digits = '1234567890';
const symbols = '~/`!@#\$%^&\\"\'*(),._?:;{}|<>';
const mockPhoneNumber = '+15555551234';

String generatePhoneNumber() {
final buf = StringBuffer('+1');
for (var i = 0; i < 3; i++) {
buf.write(digits[random.nextInt(digits.length)]);
}
buf.write('55501');
for (var i = 0; i < 2; i++) {
buf.write(digits[random.nextInt(digits.length)]);
}
return buf.toString();
}

String generateEmail() => 'test-amplify-flutter-${uuid()}@test${uuid()}.com';

Expand Down

0 comments on commit f7a89fa

Please sign in to comment.