Skip to content

Commit a74682c

Browse files
authored
chore(auth): Add celest_test helper for email OTP events (#272)
Adds the ability to monitor email OTP events in `package:celest_test` so that full E2E tests can be performed.
1 parent ff6cb4d commit a74682c

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

packages/celest_test/lib/celest_test.dart

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,35 @@ final class CelestTester {
3535

3636
final VmService _vmService;
3737

38+
/// Auth-related test helpers.
39+
late final CelestAuthTester auth = CelestAuthTester._(_vmService);
40+
3841
/// Closes the connection to the VM service.
3942
Future<void> close() async {
4043
await _vmService.dispose();
4144
}
4245
}
46+
47+
/// An OTP code sent by the server.
48+
typedef Otp = ({String to, String code});
49+
50+
/// A helper class for running integration tests with Celest Auth.
51+
final class CelestAuthTester {
52+
CelestAuthTester._(this._vmService);
53+
54+
final VmService _vmService;
55+
56+
/// OTP codes sent to users during sign up/sign in.
57+
Stream<Otp> get onSentOtp async* {
58+
await for (final event in _vmService.onExtensionEvent) {
59+
if (event.extensionKind != 'celest.cloud_auth.emailOtpSent') {
60+
continue;
61+
}
62+
final data = event.extensionData!.data;
63+
yield (
64+
to: data['to'] as String,
65+
code: data['code'] as String,
66+
);
67+
}
68+
}
69+
}

services/celest_cloud_auth/lib/src/email/email_provider.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:developer' as developer;
2+
13
import 'package:celest_cloud_auth/src/context.dart';
24
import 'package:celest_cloud_auth/src/otp/otp_provider.dart';
35

@@ -7,6 +9,7 @@ extension type const EmailOtpProvider._(OtpSender _send) implements Object {
79
const EmailOtpProvider([OtpSender send = _defaultSend]) : this._(send);
810

911
static Future<void> _defaultSend(Otp message) async {
12+
developer.postEvent('celest.cloud_auth.emailOtpSent', message.toJson());
1013
context.logger.info('Verification code for ${message.to}: ${message.code}');
1114
}
1215

services/celest_cloud_auth/lib/src/otp/otp_provider.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ typedef Otp = ({
33
String code,
44
});
55

6+
extension OtpX on Otp {
7+
Map<String, Object?> toJson() => {'to': to, 'code': code};
8+
}
9+
610
typedef OtpSender = Future<void> Function(Otp);

0 commit comments

Comments
 (0)