Skip to content

Commit

Permalink
test(auth): Clean up integration tests
Browse files Browse the repository at this point in the history
- Change `testWidgets` to `test` to improve async handling (`testWidgets` uses FakeAsync: flutter/flutter#5728)
- Fix newly-failing tests for `next`

commit-id:0d79ab75
  • Loading branch information
Dillon Nys committed Oct 12, 2022
1 parent 821bc6c commit efc18e1
Show file tree
Hide file tree
Showing 15 changed files with 187 additions and 143 deletions.
7 changes: 5 additions & 2 deletions packages/amplify_core/lib/src/amplify_class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class AmplifyClass {
/// The Amplify event hub.
final AmplifyHub Hub = AmplifyHub();

final _configCompleter = Completer<AmplifyConfig>();
var _configCompleter = Completer<AmplifyConfig>();

/// Adds one plugin at a time. Note: this method can only
/// be called before Amplify has been configured. Customers are expected
Expand Down Expand Up @@ -128,7 +128,10 @@ abstract class AmplifyClass {
/// Resets the Amplify implementation, removing all traces of Amplify from
/// the device.
@visibleForTesting
Future<void> reset();
@mustCallSuper
Future<void> reset() async {
_configCompleter = Completer();
}
}

// ignore_for_file: non_constant_identifier_names, unnecessary_getters_setters
1 change: 1 addition & 0 deletions packages/amplify_core/lib/src/amplify_class_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ class AmplifyClassImpl extends AmplifyClass {
DataStore.reset(),
Storage.reset(),
]);
await super.reset();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ void main() {
group(
'custom auth passwordless signIn',
() {
setUp(() async {
await signOutUser();
});

setUpAll(() async {
await configureAuth();
// create new user for each test
Expand All @@ -49,9 +45,16 @@ void main() {
);
});

testWidgets(
'signIn should return data from the auth challenge lambda (passwordless)',
(WidgetTester tester) async {
setUp(() async {
await signOutUser();
});

tearDownAll(Amplify.reset);

test(
'signIn should return data from the auth challenge lambda '
'(passwordless)',
() async {
final res = await Amplify.Auth.signIn(
username: username,
options: options,
Expand Down Expand Up @@ -80,9 +83,9 @@ void main() {
},
);

testWidgets(
test(
'a correct challenge reply should sign in the user in',
(WidgetTester tester) async {
() async {
await Amplify.Auth.signIn(
username: username,
options: options,
Expand All @@ -98,9 +101,9 @@ void main() {
},
);

testWidgets(
test(
'an incorrect challenge reply should throw a NotAuthorizedException',
(WidgetTester tester) async {
() async {
await Amplify.Auth.signIn(
username: username,
options: options,
Expand All @@ -115,9 +118,10 @@ void main() {
},
);

testWidgets(
'if a password is provided but is incorrect, throw NotAuthorizedException',
(WidgetTester tester) async {
test(
'if a password is provided but is incorrect, throw '
'NotAuthorizedException',
() async {
// '123' is the arbitrary challenge answer defined in lambda code
expect(
Amplify.Auth.signIn(
Expand All @@ -132,9 +136,10 @@ void main() {
},
);

testWidgets(
'a correct password and correct challenge reply should sign in the user',
(WidgetTester tester) async {
test(
'a correct password and correct challenge reply should sign in '
'the user',
() async {
await Amplify.Auth.signIn(
username: username,
password: password,
Expand All @@ -151,9 +156,10 @@ void main() {
},
);

testWidgets(
'signIn should return data from the auth challenge lambda (with password)',
(WidgetTester tester) async {
test(
'signIn should return data from the auth challenge lambda '
'(with password)',
() async {
final res = await Amplify.Auth.signIn(
username: username,
password: password,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ void main() {
await signOutUser();
});

testWidgets('should delete a confirmed user', (WidgetTester tester) async {
tearDownAll(Amplify.reset);

test('should delete a confirmed user', () async {
final username = generateUsername();
final password = generatePassword();

Expand Down Expand Up @@ -62,9 +64,9 @@ void main() {
);
});

testWidgets(
'fetchAuthSession should throw NotAuthorizedException after user deletion',
(WidgetTester tester) async {
test(
'fetchAuthSession should throw NotAuthorizedException '
'after user deletion', () async {
final username = generateUsername();
final password = generatePassword();

Expand All @@ -86,14 +88,9 @@ void main() {
// Delete the user
await Amplify.Auth.deleteUser();

// Expect fetchAuthSession to throw a NotAuthorizedException
// (the tokens have been cleared and guest users are not allowed).
expect(
Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
),
throwsA(isA<NotAuthorizedException>()),
);
// Expect user to be signed out
final session = await Amplify.Auth.fetchAuthSession();
expect(session.isSignedIn, false);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ void main() {
await signOutUser();
});

tearDownAll(Amplify.reset);

tearDown(signOutUser);

Future<FederateToIdentityPoolResult> federateToIdentityPool() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void main() {
);
});

tearDownAll(Amplify.reset);

// sign in prior to each test
setUp(() async {
await signOutUser();
Expand All @@ -51,9 +53,9 @@ void main() {
);
});

testWidgets('should return user credentials if getAWSCredentials is true',
(WidgetTester tester) async {
var res = await Amplify.Auth.fetchAuthSession(
test('should return user credentials if getAWSCredentials is true',
() async {
final res = await Amplify.Auth.fetchAuthSession(
options: const CognitoSessionOptions(getAWSCredentials: true),
) as CognitoAuthSession;

Expand All @@ -64,9 +66,8 @@ void main() {
expect(isValidAWSCognitoUserPoolTokens(res.userPoolTokens), isTrue);
});

testWidgets('should return user credentials without getAWSCredentials',
(WidgetTester tester) async {
var res = await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;
test('should return user credentials without getAWSCredentials', () async {
final res = await Amplify.Auth.fetchAuthSession() as CognitoAuthSession;

expect(res.isSignedIn, isTrue);
expect(isValidUserSub(res.userSub), isTrue);
Expand All @@ -75,11 +76,13 @@ void main() {
expect(isValidAWSCognitoUserPoolTokens(res.userPoolTokens), isTrue);
});

testWidgets('should return isSignedIn as false if the user is signed out',
(WidgetTester tester) async {
await Amplify.Auth.signOut();
var res = await Amplify.Auth.fetchAuthSession();
expect(res.isSignedIn, isFalse);
});
test(
'should return isSignedIn as false if the user is signed out',
() async {
await Amplify.Auth.signOut();
var res = await Amplify.Auth.fetchAuthSession();
expect(res.isSignedIn, isFalse);
},
);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,47 +36,51 @@ void main() {
return idToken!.claims.customClaims;
}

test('Force refresh', () async {
await configureAuth();
group('Force refresh', () {
setUpAll(configureAuth);

final username = generateUsername();
final password = generatePassword();
tearDownAll(Amplify.reset);

await adminCreateUser(
username,
password,
autoConfirm: true,
verifyAttributes: true,
);
addTearDown(Amplify.Auth.deleteUser);
test('', () async {
final username = generateUsername();
final password = generatePassword();

final res = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(res.nextStep?.signInStep, 'DONE');
await adminCreateUser(
username,
password,
autoConfirm: true,
verifyAttributes: true,
);
addTearDown(Amplify.Auth.deleteUser);

expect(
await getCustomAttributes(),
isNot(contains('address')),
reason: 'No custom attrs exist yet',
);
final res = await Amplify.Auth.signIn(
username: username,
password: password,
);
expect(res.nextStep?.signInStep, 'DONE');

await Amplify.Auth.updateUserAttribute(
userAttributeKey: CognitoUserAttributeKey.address,
value: '1 Main St',
);
expect(
await getCustomAttributes(),
isNot(contains('address')),
reason: 'No custom attrs exist yet',
);

expect(
await getCustomAttributes(),
isNot(contains('address')),
reason: 'Token is not yet updated',
);
await Amplify.Auth.updateUserAttribute(
userAttributeKey: CognitoUserAttributeKey.address,
value: '1 Main St',
);

expect(
await getCustomAttributes(forceRefresh: true),
contains('address'),
reason: 'Token is updated via force refresh',
);
expect(
await getCustomAttributes(),
isNot(contains('address')),
reason: 'Token is not yet updated',
);

expect(
await getCustomAttributes(forceRefresh: true),
contains('address'),
reason: 'Token is updated via force refresh',
);
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ void main() {
);
});

tearDownAll(Amplify.reset);

// sign in prior to each test
setUp(() async {
await signOutUser();
Expand All @@ -50,16 +52,15 @@ void main() {
);
});

testWidgets('should return the current user', (WidgetTester tester) async {
test('should return the current user', () async {
var authUser = await Amplify.Auth.getCurrentUser();
// usernames need to be compared case insensitive due to
// https://github.com/aws-amplify/amplify-flutter/issues/723
expect(authUser.username.toLowerCase(), username.toLowerCase());
expect(isValidUserSub(authUser.userId), isTrue);
});

testWidgets('should throw SignedOutException if the user is signed out',
(WidgetTester tester) async {
test('should throw SignedOutException if the user is signed out', () async {
await Amplify.Auth.signOut();
expect(
Amplify.Auth.getCurrentUser(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ void main() {
await signOutUser();
});

testWidgets(
tearDownAll(Amplify.reset);

test(
'should broadcast events for sign in and sign out',
(WidgetTester tester) async {
() async {
// setup
Future<HubEvent> nextEvent;
HubEvent event;
Expand Down Expand Up @@ -86,9 +88,9 @@ void main() {
},
);

testWidgets(
test(
'should broadcast events for deleteUser',
(WidgetTester tester) async {
() async {
// setup
Future<HubEvent> signinEvent;
Future<HubEvent> deleteEvent;
Expand Down
Loading

0 comments on commit efc18e1

Please sign in to comment.