Skip to content

Commit

Permalink
Fix custom token tests
Browse files Browse the repository at this point in the history
* Add on-demand token creation
* Firestore tests using custom token
  • Loading branch information
cachapa committed Jun 12, 2023
1 parent cdedbab commit c6e735e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ dependencies:
dev_dependencies:
lints: any
test: any

# Create custom auth token for testing
firebase_admin: ^0.2.0
11 changes: 11 additions & 0 deletions test/firebase_auth_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:firebase_admin/firebase_admin.dart';
import 'package:firedart/auth/exceptions.dart';
import 'package:firedart/firedart.dart';
import 'package:test/test.dart';
Expand All @@ -22,6 +23,8 @@ Future main() async {
});

test('Sign In with Custom Token', () async {
final customToken = await createCustomToken();

expect(auth.isSignedIn, false);
await auth.signInWithCustomToken(customToken);
expect(auth.isSignedIn, true);
Expand Down Expand Up @@ -106,3 +109,11 @@ Future main() async {
await expect;
});
}

Future<String> createCustomToken() => FirebaseAdmin.instance
.initializeApp(AppOptions(
credential:
FirebaseAdmin.instance.certFromPath('test/service-account.json'),
))
.auth()
.createCustomToken('test');
15 changes: 15 additions & 0 deletions test/firestore_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:firedart/firestore/application_default_authenticator.dart';
import 'package:firedart/firestore/token_authenticator.dart';
import 'package:test/test.dart';

import 'firebase_auth_test.dart';
import 'test_config.dart';

Future main() async {
Expand Down Expand Up @@ -37,6 +38,20 @@ Future main() async {
});
});

group('Custom token', () {
var tokenStore = VolatileStore();
var auth = FirebaseAuth(apiKey, tokenStore);
final authenticator = TokenAuthenticator.from(auth)?.authenticate;
var firestore = Firestore(projectId, authenticator: authenticator);

setUpAll(() async {
final token = await createCustomToken();
await auth.signInWithCustomToken(token);
});

runTests(firestore);
});

group('ApplicationDefaultAuthenticator', () {
assert(
Platform.environment.containsKey('GOOGLE_APPLICATION_CREDENTIALS'),
Expand Down

0 comments on commit c6e735e

Please sign in to comment.