-
Notifications
You must be signed in to change notification settings - Fork 63
/
firebase_auth_test.dart
119 lines (100 loc) · 3.25 KB
/
firebase_auth_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import 'package:firebase_admin/firebase_admin.dart';
import 'package:firedart/auth/exceptions.dart';
import 'package:firedart/firedart.dart';
import 'package:test/test.dart';
import 'test_config.dart';
Future main() async {
late TokenStore tokenStore;
late FirebaseAuth auth;
setUp(() {
tokenStore = VolatileStore();
auth = FirebaseAuth(apiKey, tokenStore);
});
test('Sign In', () async {
expect(auth.isSignedIn, false);
await auth.signIn(email, password);
expect(auth.isSignedIn, true);
auth.signOut();
expect(auth.isSignedIn, false);
});
test('Sign In with Custom Token', () async {
final customToken = await createCustomToken();
expect(auth.isSignedIn, false);
await auth.signInWithCustomToken(customToken);
expect(auth.isSignedIn, true);
auth.signOut();
expect(auth.isSignedIn, false);
});
test('Sign In Anonymously', () async {
expect(auth.isSignedIn, false);
await auth.signInAnonymously();
expect(auth.isSignedIn, true);
await auth.deleteAccount();
expect(auth.isSignedIn, false);
});
test('Fail sign-in on invalid email', () async {
await expectLater(
auth.signIn('bademail.com', 'bad_pass'), throwsA(isA<AuthException>()));
});
test('Fail sign-in on email not found', () async {
await expectLater(auth.signIn('[email protected]', 'bad_pass'),
throwsA(isA<AuthException>()));
});
test('Fail sign-in on bad password', () async {
await expectLater(
auth.signIn(email, 'bad_pass'), throwsA(isA<AuthException>()));
});
test('Fail to get user while logged out', () async {
await expectLater(auth.getUser(), throwsA(isA<SignedOutException>()));
});
test('Get user on signin', () async {
var user = await auth.signIn(email, password);
expect(user.email, email);
});
test('Get user id', () async {
await auth.signIn(email, password);
expect(auth.userId, isNotEmpty);
});
test('Get anonymous user id', () async {
await auth.signInAnonymously();
expect(auth.userId, isNotEmpty);
await auth.deleteAccount();
});
test('Get user', () async {
await auth.signIn(email, password);
var user = await auth.getUser();
expect(user.email, email);
});
test('Refresh token when expired', () async {
await auth.signIn(email, password);
tokenStore.expireToken();
var user = await auth.getUser();
expect(user.email, isNotEmpty);
expect(auth.isSignedIn, true);
});
test('Sign out on bad refresh token', () async {
await auth.signIn(email, password);
tokenStore.setToken('user_id', 'bad_token', 'bad_token', 0);
await expectLater(auth.getUser(), throwsA(isA<AuthException>()));
expect(auth.isSignedIn, false);
});
test('Emit signedIn events', () async {
var stream = auth.signInState;
var expect = expectLater(
stream,
emitsInOrder([
(isSignedIn) => isSignedIn == true,
(isSignedIn) => isSignedIn == false,
]));
await auth.signIn(email, password);
auth.signOut();
await expect;
});
}
Future<String> createCustomToken() => FirebaseAdmin.instance
.initializeApp(AppOptions(
credential:
FirebaseAdmin.instance.certFromPath('test/service-account.json'),
))
.auth()
.createCustomToken('test');