-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathsupabase_flutter_test.dart
154 lines (137 loc) · 4.7 KB
/
supabase_flutter_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import 'package:app_links/app_links.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:supabase_flutter/supabase_flutter.dart';
import 'widget_test_stubs.dart';
void main() {
const supabaseUrl = '';
const supabaseKey = '';
tearDown(() async => await Supabase.instance.dispose());
group("Valid session", () {
setUp(() async {
mockAppLink();
// Initialize the Supabase singleton
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
debug: false,
authOptions: FlutterAuthClientOptions(
localStorage: MockLocalStorage(),
pkceAsyncStorage: MockAsyncStorage(),
),
);
});
test('can access Supabase singleton', () async {
final supabase = Supabase.instance.client;
expect(supabase, isNotNull);
});
test('can re-initialize client', () async {
final supabase = Supabase.instance.client;
await Supabase.instance.dispose();
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
debug: false,
authOptions: FlutterAuthClientOptions(
localStorage: MockLocalStorage(),
pkceAsyncStorage: MockAsyncStorage(),
),
);
final newClient = Supabase.instance.client;
expect(supabase, isNot(newClient));
});
});
group("Expired session", () {
setUp(() async {
mockAppLink();
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
debug: false,
authOptions: FlutterAuthClientOptions(
localStorage: MockExpiredStorage(),
pkceAsyncStorage: MockAsyncStorage(),
),
);
});
test('initial session contains the error', () async {
// Give it a delay to wait for recoverSession to throw
await Future.delayed(const Duration(milliseconds: 10));
await expectLater(Supabase.instance.client.auth.onAuthStateChange,
emitsError(isA<AuthException>()));
});
});
group("No session", () {
setUp(() async {
mockAppLink();
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
debug: false,
authOptions: FlutterAuthClientOptions(
localStorage: MockEmptyLocalStorage(),
pkceAsyncStorage: MockAsyncStorage(),
),
);
});
test('initial session contains the error', () async {
final event = await Supabase.instance.client.auth.onAuthStateChange.first;
expect(event.event, AuthChangeEvent.initialSession);
expect(event.session, isNull);
});
});
group('Deep Link with PKCE code', () {
late final PkceHttpClient pkceHttpClient;
late final bool mockEventChannel;
/// Check if the current version of AppLinks uses an explicit call to get
/// the initial link. This is only the case before version 6.0.0, where we
/// can find the getInitialAppLink function.
///
/// CI pipeline is set so that it tests both app_links newer and older than v6.0.0
bool appLinksExposesInitialLinkInStream() {
try {
// before app_links 6.0.0
(AppLinks() as dynamic).getInitialAppLink;
return false;
} on NoSuchMethodError catch (_) {
return true;
}
}
setUp(() async {
pkceHttpClient = PkceHttpClient();
// Add initial deep link with a `code` parameter, use method channel if
// we are in a version of AppLinks that use the explcit method for
// getting the initial link. Otherwise we want to mock the event channel
// and put the initial link there.
mockEventChannel = appLinksExposesInitialLinkInStream();
mockAppLink(
mockMethodChannel: !mockEventChannel,
mockEventChannel: mockEventChannel,
initialLink: 'com.supabase://callback/?code=my-code-verifier',
);
await Supabase.initialize(
url: supabaseUrl,
anonKey: supabaseKey,
debug: false,
httpClient: pkceHttpClient,
authOptions: FlutterAuthClientOptions(
localStorage: MockEmptyLocalStorage(),
pkceAsyncStorage: MockAsyncStorage()
..setItem(
key: 'supabase.auth.token-code-verifier',
value: 'raw-code-verifier'),
),
);
});
test(
'Having `code` as the query parameter triggers `getSessionFromUrl` call on initialize',
() async {
// Wait for the initial app link to be handled, as this is an async
// process when mocking the event channel.
if (mockEventChannel) {
await AppLinks().uriLinkStream.first;
}
expect(pkceHttpClient.requestCount, 1);
expect(pkceHttpClient.lastRequestBody['auth_code'], 'my-code-verifier');
});
});
}