Skip to content

Commit

Permalink
Allow setting mock user to null (#112)
Browse files Browse the repository at this point in the history
* Clear mock user on sign out

* Don't clear mock user on sign out

* ... but still clear current user on sign out

* Revert invalid test change

* Revert a few more redundant test changes
  • Loading branch information
michaelowolf authored Sep 5, 2024
1 parent f919ba1 commit cf953b0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 26 deletions.
43 changes: 25 additions & 18 deletions lib/src/firebase_auth_mocks_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ class MockFirebaseAuth implements FirebaseAuth {
}
} else {
// Notify of null on startup.
signOut();
_notifyCredential(null);
}
}

set mockUser(MockUser user) {
set mockUser(MockUser? user) {
_mockUser = user;
// Update _currentUser if already sign in
if (_currentUser != null) {
Expand Down Expand Up @@ -168,9 +168,7 @@ class MockFirebaseAuth implements FirebaseAuth {
maybeThrowException(this, Invocation.method(#signOut, [null]));

_currentUser = null;
stateChangedStreamController.add(null);
userChangedStreamController.add(null);
authForFakeFirestoreStreamController.add(null);
_notifyCredential(null);
}

@override
Expand All @@ -184,22 +182,31 @@ class MockFirebaseAuth implements FirebaseAuth {
Future<UserCredential> _fakeSignIn({bool isAnonymous = false}) async {
final userCredential = MockUserCredential(isAnonymous, mockUser: _mockUser);
_currentUser = userCredential.user;
stateChangedStreamController.add(_currentUser);
userChangedStreamController.add(_currentUser);
final u = userCredential.mockUser;
authForFakeFirestoreStreamController.add({
'uid': u.uid,
'token': {
'name': u.displayName,
'email': u.email,
'email_verified': u.emailVerified,
'firebase.sign_in_provider': u.getIdTokenResultSync().signInProvider,
...u.getIdTokenResultSync().claims ?? {}
}
});
_notifyCredential(userCredential);
return Future.value(userCredential);
}

void _notifyCredential(MockUserCredential? credential) {
final user = credential?.user;
stateChangedStreamController.add(user);
userChangedStreamController.add(user);

final mockUser = credential?.mockUser;
authForFakeFirestoreStreamController.add(mockUser == null
? null
: {
'uid': mockUser.uid,
'token': {
'name': mockUser.displayName,
'email': mockUser.email,
'email_verified': mockUser.emailVerified,
'firebase.sign_in_provider':
mockUser.getIdTokenResultSync().signInProvider,
...mockUser.getIdTokenResultSync().claims ?? {}
}
});
}

Future<UserCredential> _fakeSignUp({bool isAnonymous = false}) {
return _fakeSignIn(isAnonymous: isAnonymous);
}
Expand Down
47 changes: 39 additions & 8 deletions test/firebase_auth_mocks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,46 @@ void main() {
expect(idTokenResult.claims, {'weight': 70});
});

test('Returns null after sign out', () async {
final auth = MockFirebaseAuth(signedIn: true, mockUser: tUser);
final user = auth.currentUser;
group('Sign out', () {
test('Returns null after sign out', () async {
final auth = MockFirebaseAuth(signedIn: true, mockUser: tUser);
final user = auth.currentUser;

await auth.signOut();
await auth.signOut();

expect(auth.currentUser, isNull);
expect(auth.authStateChanges(), emitsInOrder([user, null]));
expect(auth.userChanges(), emitsInOrder([user, null]));
});

test('Can sign in again after sign out', () async {
final auth = MockFirebaseAuth(signedIn: true, mockUser: tUser);
final user = auth.currentUser;

await auth.signOut();

expect(auth.currentUser, isNull);
expect(auth.authStateChanges(), emitsInOrder([user, null]));
expect(auth.userChanges(), emitsInOrder([user, null]));
auth.mockUser = tUser;
await auth.signInWithProvider(AppleAuthProvider());

expect(auth.authStateChanges(),
emitsInOrder([user, null, auth.currentUser]));
expect(auth.userChanges(), emitsInOrder([user, null, auth.currentUser]));
});

test('Can sign in anonymously after sign out', () async {
final auth = MockFirebaseAuth(signedIn: true, mockUser: tUser);
final user = auth.currentUser;

await auth.signOut();

auth.mockUser = null;
await auth.signInAnonymously();

expect(auth.currentUser!.isAnonymous, isTrue);
expect(auth.authStateChanges(),
emitsInOrder([user, null, auth.currentUser]));
expect(auth.userChanges(), emitsInOrder([user, null, auth.currentUser]));
});
});

test('sendPasswordResetEmail works', () async {
Expand Down Expand Up @@ -774,7 +805,7 @@ void main() {
expect(decodedToken['bodyHeight'], 169);
});

test('The customClain should exist after sign-out and sign-in', () async {
test('The customClaim should exist after sign-out and sign-in', () async {
final auth = MockFirebaseAuth(
mockUser: MockUser(customClaim: {'role': 'admin', 'bodyHeight': 169}),
signedIn: true,
Expand Down

0 comments on commit cf953b0

Please sign in to comment.