halkyon_app: instances, sign-in and sync plumbing for Halkyon's apps
What every Halkyon app's shell needs, taken from Halkyon Learn's and set on halkyon_base's client: the instances a device keeps an account on, the first start's tiles and where an instance keeps people's data, the account switcher; signing in anonymously, with GitHub or an email code, switching person and the start screen; the gates that build an app's dependencies per instance and per person; and the sync service with its status, Settings card and banner. Texts of its own in English and German take the app's name; what only the app can say, it passes in. Fakes for apps' tests are in testing.dart. An instance's basics now name the app it serves (`app`, set by the host's configuration), and an app accepts only an instance of its own. SerialQueue and CoalescingRunner move into halkyon_core. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
af1de799f4
commit
e23cc77f7c
8 changed files with 10761 additions and 10 deletions
242
packages/halkyon_app/test/auth/bloc/email_sign_in_bloc_test.dart
Normal file
242
packages/halkyon_app/test/auth/bloc/email_sign_in_bloc_test.dart
Normal file
|
|
@ -0,0 +1,242 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
|
||||
void main() {
|
||||
const anonymous = SignedIn(authUserId: 'anonymous-1', isIdentified: false);
|
||||
|
||||
late FakeAuthGateway gateway;
|
||||
late MemorySignInAddresses addresses;
|
||||
|
||||
/// Whether this device holds content a switch would leave behind.
|
||||
late bool holdsContent;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeAuthGateway(status: anonymous);
|
||||
addresses = MemorySignInAddresses();
|
||||
holdsContent = false;
|
||||
});
|
||||
|
||||
EmailSignInBloc newBloc({String language = 'en'}) {
|
||||
final bloc = EmailSignInBloc(
|
||||
AuthRepository(gateway, addresses),
|
||||
deviceHoldsContent: () async => holdsContent,
|
||||
language: language,
|
||||
);
|
||||
addTearDown(bloc.close);
|
||||
return bloc;
|
||||
}
|
||||
|
||||
/// A bloc waiting for the code mailed to [address].
|
||||
Future<EmailSignInBloc> codeMailed(String address) async {
|
||||
final bloc = newBloc()..add(EmailSignInEventCodeRequested(address));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(isA<EmailSignInStateCode>()),
|
||||
);
|
||||
return bloc;
|
||||
}
|
||||
|
||||
test('starts at the address', () {
|
||||
expect(newBloc().state, const EmailSignInStateAddress());
|
||||
});
|
||||
|
||||
test("mails a code in the app's language, and the code makes the address "
|
||||
"the person's: they keep everything and become identified", () async {
|
||||
final bloc = newBloc(language: 'de')
|
||||
..add(const EmailSignInEventCodeRequested(' Anna@Example.org '));
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateAddress(sending: true),
|
||||
const EmailSignInStateCode(
|
||||
address: 'anna@example.org',
|
||||
request: 'request-1',
|
||||
),
|
||||
]),
|
||||
);
|
||||
expect(gateway.mailed.single.language, 'de');
|
||||
|
||||
bloc.add(const EmailSignInEventCodeEntered('code-1'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateCode(
|
||||
address: 'anna@example.org',
|
||||
request: 'request-1',
|
||||
checking: true,
|
||||
),
|
||||
const EmailSignInStateLinked(address: 'anna@example.org'),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'anonymous-1', isIdentified: true),
|
||||
);
|
||||
expect(addresses.addresses, {'anonymous-1': 'anna@example.org'});
|
||||
});
|
||||
|
||||
test('says what is wrong with an address, and with a code, and takes the '
|
||||
'next try', () async {
|
||||
final bloc = newBloc()..add(const EmailSignInEventCodeRequested('anna'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateAddress(sending: true),
|
||||
const EmailSignInStateAddress(
|
||||
problem: EmailSignInProblem.notAnAddress,
|
||||
),
|
||||
]),
|
||||
);
|
||||
|
||||
bloc.add(const EmailSignInEventCodeRequested('anna@example.org'));
|
||||
await expectLater(bloc.stream, emitsThrough(isA<EmailSignInStateCode>()));
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-9'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(
|
||||
const EmailSignInStateCode(
|
||||
address: 'anna@example.org',
|
||||
request: 'request-1',
|
||||
problem: EmailSignInProblem.wrongCode,
|
||||
),
|
||||
),
|
||||
);
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(const EmailSignInStateLinked(address: 'anna@example.org')),
|
||||
);
|
||||
});
|
||||
|
||||
test('mails a new code on request, and goes back to the address', () async {
|
||||
final bloc = await codeMailed('anna@example.org');
|
||||
|
||||
bloc.add(const EmailSignInEventCodeResent());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateCode(
|
||||
address: 'anna@example.org',
|
||||
request: 'request-1',
|
||||
checking: true,
|
||||
),
|
||||
const EmailSignInStateCode(
|
||||
address: 'anna@example.org',
|
||||
request: 'request-2',
|
||||
resent: true,
|
||||
),
|
||||
]),
|
||||
);
|
||||
|
||||
bloc.add(const EmailSignInEventAddressReopened());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emits(const EmailSignInStateAddress()),
|
||||
);
|
||||
});
|
||||
|
||||
test("another person's address: a device holding nothing switches to "
|
||||
'them straight away', () async {
|
||||
gateway.addressOwners['ben@example.org'] = 'ben';
|
||||
final bloc = await codeMailed('ben@example.org');
|
||||
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(const EmailSignInStateSwitching()),
|
||||
);
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(gateway.emailSwitches, 1);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'ben', isIdentified: true),
|
||||
);
|
||||
expect(addresses.addresses, {'ben': 'ben@example.org'});
|
||||
});
|
||||
|
||||
test('a device holding content asks first: declining changes nothing, '
|
||||
'confirming switches', () async {
|
||||
gateway.addressOwners['ben@example.org'] = 'ben';
|
||||
holdsContent = true;
|
||||
final bloc = await codeMailed('ben@example.org');
|
||||
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(
|
||||
const EmailSignInStateConfirmSwitch(
|
||||
address: 'ben@example.org',
|
||||
switchToken: 'switch:ben@example.org',
|
||||
),
|
||||
),
|
||||
);
|
||||
bloc.add(const EmailSignInEventSwitchDeclined());
|
||||
await expectLater(bloc.stream, emits(const EmailSignInStateAddress()));
|
||||
expect(gateway.emailSwitches, 0);
|
||||
expect(gateway.status, anonymous);
|
||||
|
||||
bloc.add(const EmailSignInEventCodeRequested('ben@example.org'));
|
||||
await expectLater(bloc.stream, emitsThrough(isA<EmailSignInStateCode>()));
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-2'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(isA<EmailSignInStateConfirmSwitch>()),
|
||||
);
|
||||
bloc.add(const EmailSignInEventSwitchConfirmed());
|
||||
await expectLater(bloc.stream, emits(const EmailSignInStateSwitching()));
|
||||
await pumpEventQueue();
|
||||
expect(gateway.emailSwitches, 1);
|
||||
});
|
||||
|
||||
test("someone identified whose address is another person's is told so, "
|
||||
'never switched: their own account stays theirs', () async {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'carla', isIdentified: true),
|
||||
)..addressOwners['ben@example.org'] = 'ben';
|
||||
final bloc = await codeMailed('ben@example.org');
|
||||
|
||||
bloc.add(const EmailSignInEventCodeEntered('CODE-1'));
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(const EmailSignInStateTaken(address: 'ben@example.org')),
|
||||
);
|
||||
expect(gateway.emailSwitches, 0);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'carla', isIdentified: true),
|
||||
);
|
||||
});
|
||||
|
||||
test("says when the instance couldn't send the mail", () async {
|
||||
gateway.emailError = const EmailSignInFailed(
|
||||
EmailSignInProblem.mailFailed,
|
||||
);
|
||||
final bloc = newBloc()
|
||||
..add(const EmailSignInEventCodeRequested('anna@example.org'));
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateAddress(sending: true),
|
||||
const EmailSignInStateAddress(problem: EmailSignInProblem.mailFailed),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
test("says when the instance can't be reached", () async {
|
||||
gateway.emailError = Exception('offline');
|
||||
final bloc = newBloc()
|
||||
..add(const EmailSignInEventCodeRequested('anna@example.org'));
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const EmailSignInStateAddress(sending: true),
|
||||
const EmailSignInStateAddress(problem: EmailSignInProblem.failed),
|
||||
]),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,246 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
|
||||
void main() {
|
||||
const anonymous = SignedIn(authUserId: 'anonymous-1', isIdentified: false);
|
||||
|
||||
late FakeAuthGateway gateway;
|
||||
|
||||
/// Whether this device holds content a switch would leave behind.
|
||||
late bool holdsContent;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeAuthGateway(status: anonymous);
|
||||
holdsContent = false;
|
||||
});
|
||||
|
||||
/// A bloc as the account dialog starts it, once it knows whether this
|
||||
/// instance offers GitHub sign-in.
|
||||
Future<GitHubSignInBloc> ready() async {
|
||||
final bloc = GitHubSignInBloc(
|
||||
AuthRepository(gateway, MemorySignInAddresses()),
|
||||
deviceHoldsContent: () async => holdsContent,
|
||||
)..add(const GitHubSignInEventStarted());
|
||||
addTearDown(bloc.close);
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
|
||||
return bloc;
|
||||
}
|
||||
|
||||
test('offers nothing where the instance has no GitHub sign-in', () async {
|
||||
gateway.offersGitHub = false;
|
||||
final bloc = GitHubSignInBloc(
|
||||
AuthRepository(gateway, MemorySignInAddresses()),
|
||||
deviceHoldsContent: () async => holdsContent,
|
||||
)..add(const GitHubSignInEventStarted());
|
||||
addTearDown(bloc.close);
|
||||
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateUnavailable()));
|
||||
});
|
||||
|
||||
test('links the GitHub account to the person', () async {
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const GitHubSignInStateBusy(),
|
||||
const GitHubSignInStateLinked(),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'anonymous-1', isIdentified: true),
|
||||
);
|
||||
});
|
||||
|
||||
test('changes nothing when the person cancels on GitHub', () async {
|
||||
gateway.gitHubLinkOutcome = GitHubLinkOutcome.cancelled;
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const GitHubSignInStateBusy(),
|
||||
const GitHubSignInStateReady(),
|
||||
]),
|
||||
);
|
||||
expect(gateway.status, anonymous);
|
||||
});
|
||||
|
||||
test(
|
||||
"the person can cancel while GitHub's page is open; nothing changes",
|
||||
() async {
|
||||
gateway
|
||||
..canCancelGitHub = true
|
||||
..gitHubPageOpens = true;
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emits(const GitHubSignInStateBusy(cancellable: true)),
|
||||
);
|
||||
bloc.add(const GitHubSignInEventCancelled());
|
||||
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
|
||||
expect(gateway.gitHubCancels, 1);
|
||||
expect(gateway.status, anonymous);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
"offers no cancel where the system's sign-in screen has its own",
|
||||
() async {
|
||||
gateway.gitHubPageOpens = true;
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emits(
|
||||
isA<GitHubSignInStateBusy>().having(
|
||||
(state) => state.cancellable,
|
||||
'cancellable',
|
||||
isFalse,
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
"closing the dialog while GitHub's page is open cancels the sign-in",
|
||||
() async {
|
||||
gateway
|
||||
..canCancelGitHub = true
|
||||
..gitHubPageOpens = true;
|
||||
final bloc = await ready();
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emits(const GitHubSignInStateBusy(cancellable: true)),
|
||||
);
|
||||
|
||||
await bloc.close();
|
||||
|
||||
expect(gateway.gitHubCancels, 1);
|
||||
expect(gateway.status, anonymous);
|
||||
},
|
||||
);
|
||||
|
||||
test('closing the dialog otherwise cancels nothing', () async {
|
||||
final bloc = await ready();
|
||||
|
||||
await bloc.close();
|
||||
|
||||
expect(gateway.gitHubCancels, 0);
|
||||
});
|
||||
|
||||
test("switches right away when the GitHub account is someone else's and "
|
||||
'this device holds nothing', () async {
|
||||
gateway.gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateBusy()));
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(gateway.gitHubSwitches, 1);
|
||||
});
|
||||
|
||||
test("someone identified whose GitHub account is another person's is told "
|
||||
'so, never switched: their own account stays theirs', () async {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'carla', isIdentified: true),
|
||||
)..gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
|
||||
final bloc = await ready();
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const GitHubSignInStateBusy(),
|
||||
const GitHubSignInStateTaken(),
|
||||
]),
|
||||
);
|
||||
expect(gateway.gitHubSwitches, 0);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'carla', isIdentified: true),
|
||||
);
|
||||
});
|
||||
|
||||
group("when the GitHub account is someone else's and this device holds "
|
||||
'content', () {
|
||||
late GitHubSignInBloc bloc;
|
||||
|
||||
setUp(() async {
|
||||
gateway.gitHubLinkOutcome = GitHubLinkOutcome.takenByAnotherPerson;
|
||||
holdsContent = true;
|
||||
bloc = (await ready())..add(const GitHubSignInEventRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const GitHubSignInStateBusy(),
|
||||
const GitHubSignInStateConfirmSwitch(),
|
||||
]),
|
||||
);
|
||||
});
|
||||
|
||||
test('asks first; staying anonymous changes nothing', () async {
|
||||
bloc.add(const GitHubSignInEventSwitchDeclined());
|
||||
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
|
||||
expect(gateway.gitHubSwitches, 0);
|
||||
expect(gateway.status, anonymous);
|
||||
});
|
||||
|
||||
test('switches once confirmed', () async {
|
||||
bloc.add(const GitHubSignInEventSwitchConfirmed());
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateBusy()));
|
||||
await pumpEventQueue();
|
||||
|
||||
expect(gateway.gitHubSwitches, 1);
|
||||
});
|
||||
|
||||
test("cancelling GitHub's page for the switch stays anonymous", () async {
|
||||
gateway
|
||||
..canCancelGitHub = true
|
||||
..gitHubPageOpens = true;
|
||||
|
||||
bloc.add(const GitHubSignInEventSwitchConfirmed());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emits(const GitHubSignInStateBusy(cancellable: true)),
|
||||
);
|
||||
bloc.add(const GitHubSignInEventCancelled());
|
||||
|
||||
await expectLater(bloc.stream, emits(const GitHubSignInStateReady()));
|
||||
expect(gateway.gitHubSwitches, 0);
|
||||
expect(gateway.status, anonymous);
|
||||
});
|
||||
});
|
||||
|
||||
test('says so when signing in fails', () async {
|
||||
final bloc = await ready();
|
||||
gateway.gitHubError = Exception('No connection');
|
||||
|
||||
bloc.add(const GitHubSignInEventRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const GitHubSignInStateBusy(),
|
||||
const GitHubSignInStateReady(failed: true),
|
||||
]),
|
||||
);
|
||||
expect(gateway.status, anonymous);
|
||||
});
|
||||
}
|
||||
206
packages/halkyon_app/test/auth/bloc/session_bloc_test.dart
Normal file
206
packages/halkyon_app/test/auth/bloc/session_bloc_test.dart
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
|
||||
void main() {
|
||||
late FakeAuthGateway gateway;
|
||||
late MemorySignedOutMark mark;
|
||||
late SessionBloc bloc;
|
||||
|
||||
SessionStateActive anonymous(int number) => SessionStateActive(
|
||||
SignedIn(authUserId: 'anonymous-$number', isIdentified: false),
|
||||
);
|
||||
|
||||
void start({AuthStatus status = const SignedOut(), bool signedOut = false}) {
|
||||
gateway = FakeAuthGateway(status: status);
|
||||
mark = MemorySignedOutMark(signedOut);
|
||||
bloc = SessionBloc(AuthRepository(gateway, MemorySignInAddresses()), mark);
|
||||
}
|
||||
|
||||
tearDown(() => bloc.close());
|
||||
|
||||
test('signs in anonymously at a first start, nobody having signed out '
|
||||
'here', () async {
|
||||
start();
|
||||
|
||||
bloc.add(const SessionEventStarted());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([const SessionStateLoading(), anonymous(1)]),
|
||||
);
|
||||
expect(gateway.signIns, 1);
|
||||
});
|
||||
|
||||
test('keeps a stored session instead of creating a second account', () async {
|
||||
const stored = SignedIn(authUserId: 'stored', isIdentified: false);
|
||||
start(status: stored);
|
||||
|
||||
bloc.add(const SessionEventStarted());
|
||||
|
||||
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
|
||||
expect(gateway.signIns, 0);
|
||||
});
|
||||
|
||||
test('reports a failed sign-in and signs in when asked again', () async {
|
||||
start();
|
||||
gateway.signInError = Exception('No connection');
|
||||
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([const SessionStateLoading(), const SessionStateFailed()]),
|
||||
);
|
||||
|
||||
gateway.signInError = null;
|
||||
bloc.add(const SessionEventSignInRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([const SessionStateLoading(), anonymous(2)]),
|
||||
);
|
||||
});
|
||||
|
||||
test('after signing out, asks how to start, making no account; the next '
|
||||
'start of the app asks again', () async {
|
||||
const stored = SignedIn(authUserId: 'stored', isIdentified: false);
|
||||
start(status: stored);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
|
||||
|
||||
bloc.add(const SessionEventSignOutRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const SessionStateLoading(),
|
||||
const SessionStateSignedOut(),
|
||||
]),
|
||||
);
|
||||
// Signing out of an anonymous account deletes it.
|
||||
expect(gateway.accountDeletions, 1);
|
||||
expect(gateway.signIns, 0);
|
||||
expect(mark.signedOut, isTrue);
|
||||
|
||||
await bloc.close();
|
||||
bloc = SessionBloc(AuthRepository(gateway, MemorySignInAddresses()), mark)
|
||||
..add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
expect(gateway.signIns, 0);
|
||||
});
|
||||
|
||||
test('starting without an account from there signs in anonymously, and '
|
||||
'the device is no longer signed out', () async {
|
||||
start(signedOut: true);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
|
||||
bloc.add(const SessionEventStartedAnonymously());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([const SessionStateLoading(), anonymous(1)]),
|
||||
);
|
||||
expect(mark.signedOut, isFalse);
|
||||
});
|
||||
|
||||
test('signing in with GitHub from there switches to its person; '
|
||||
'cancelled, it asks again', () async {
|
||||
start(signedOut: true);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
|
||||
bloc.add(const SessionEventGitHubRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(
|
||||
const SessionStateActive(
|
||||
SignedIn(authUserId: 'github-person', isIdentified: true),
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(gateway.signIns, 0);
|
||||
expect(mark.signedOut, isFalse);
|
||||
});
|
||||
|
||||
test('a GitHub account nobody here signs in with yet is said so; any '
|
||||
'other refusal fails, and nothing is made either way', () async {
|
||||
start(signedOut: true);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
|
||||
gateway.gitHubError = const GitHubSignInFailed(
|
||||
GitHubSignInProblem.unknownAccount,
|
||||
);
|
||||
bloc.add(const SessionEventGitHubRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const SessionStateSignedOut(busy: true),
|
||||
const SessionStateSignedOut(problem: SignedOutProblem.noGitHubAccount),
|
||||
]),
|
||||
);
|
||||
|
||||
gateway.gitHubError = const GitHubSignInFailed(GitHubSignInProblem.refused);
|
||||
bloc.add(const SessionEventGitHubRequested());
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const SessionStateSignedOut(busy: true),
|
||||
const SessionStateSignedOut(problem: SignedOutProblem.failed),
|
||||
]),
|
||||
);
|
||||
expect(gateway.signIns, 0);
|
||||
expect(gateway.gitHubSwitches, 0);
|
||||
});
|
||||
|
||||
test('with an email address from there, it starts without an account and '
|
||||
'asks for the address', () async {
|
||||
start(signedOut: true);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
|
||||
bloc.add(const SessionEventEmailRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsThrough(
|
||||
const SessionStateActive(
|
||||
SignedIn(authUserId: 'anonymous-1', isIdentified: false),
|
||||
signsInWithEmail: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test('a session the server ended asks how to start too', () async {
|
||||
const stored = SignedIn(authUserId: 'github-1', isIdentified: true);
|
||||
start(status: stored);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
|
||||
|
||||
gateway.revokeSession();
|
||||
|
||||
await expectLater(bloc.stream, emits(const SessionStateSignedOut()));
|
||||
expect(gateway.signIns, 0);
|
||||
});
|
||||
|
||||
test('deleting the account asks how to start', () async {
|
||||
const stored = SignedIn(authUserId: 'github-1', isIdentified: true);
|
||||
start(status: stored);
|
||||
bloc.add(const SessionEventStarted());
|
||||
await expectLater(bloc.stream, emits(const SessionStateActive(stored)));
|
||||
|
||||
bloc.add(const SessionEventDeleteRequested());
|
||||
|
||||
await expectLater(
|
||||
bloc.stream,
|
||||
emitsInOrder([
|
||||
const SessionStateLoading(),
|
||||
const SessionStateSignedOut(),
|
||||
]),
|
||||
);
|
||||
expect(gateway.accountDeletions, 1);
|
||||
expect(gateway.signIns, 0);
|
||||
});
|
||||
}
|
||||
153
packages/halkyon_app/test/auth/data/auth_repository_test.dart
Normal file
153
packages/halkyon_app/test/auth/data/auth_repository_test.dart
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
|
||||
void main() {
|
||||
group('AuthRepository.ensureSignedIn', () {
|
||||
test('signs in anonymously when nobody is signed in', () async {
|
||||
final gateway = FakeAuthGateway();
|
||||
|
||||
await AuthRepository(gateway, MemorySignInAddresses()).ensureSignedIn();
|
||||
|
||||
expect(gateway.signIns, 1);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'anonymous-1', isIdentified: false),
|
||||
);
|
||||
});
|
||||
|
||||
test('keeps the person already signed in', () async {
|
||||
const stored = SignedIn(authUserId: 'stored', isIdentified: false);
|
||||
final gateway = FakeAuthGateway(status: stored);
|
||||
|
||||
await AuthRepository(gateway, MemorySignInAddresses()).ensureSignedIn();
|
||||
|
||||
expect(gateway.signIns, 0);
|
||||
expect(gateway.status, stored);
|
||||
});
|
||||
});
|
||||
|
||||
group('AuthRepository.signOut', () {
|
||||
test(
|
||||
'deletes an anonymous account, which nobody could sign into again',
|
||||
() async {
|
||||
final gateway = FakeAuthGateway(
|
||||
status: const SignedIn(
|
||||
authUserId: 'anonymous-1',
|
||||
isIdentified: false,
|
||||
),
|
||||
);
|
||||
|
||||
await AuthRepository(gateway, MemorySignInAddresses()).signOut();
|
||||
|
||||
expect(gateway.accountDeletions, 1);
|
||||
expect(gateway.signOuts, 0);
|
||||
expect(gateway.status, const SignedOut());
|
||||
},
|
||||
);
|
||||
|
||||
test('only signs an identified person out on this device', () async {
|
||||
final gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'github-1', isIdentified: true),
|
||||
);
|
||||
|
||||
await AuthRepository(gateway, MemorySignInAddresses()).signOut();
|
||||
|
||||
expect(gateway.signOuts, 1);
|
||||
expect(gateway.accountDeletions, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('deletes the account of whoever is signed in', () async {
|
||||
final gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'github-1', isIdentified: true),
|
||||
);
|
||||
|
||||
await AuthRepository(gateway, MemorySignInAddresses()).deleteAccount();
|
||||
|
||||
expect(gateway.accountDeletions, 1);
|
||||
expect(gateway.status, const SignedOut());
|
||||
});
|
||||
|
||||
group('signing in with an email address', () {
|
||||
late FakeAuthGateway gateway;
|
||||
late MemorySignInAddresses addresses;
|
||||
late AuthRepository auth;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'anonymous-1', isIdentified: false),
|
||||
);
|
||||
addresses = MemorySignInAddresses();
|
||||
auth = AuthRepository(gateway, addresses);
|
||||
});
|
||||
|
||||
test("a code for a new address makes it the person's, and this device "
|
||||
'remembers it for them, to show', () async {
|
||||
final request = await auth.sendEmailCode(
|
||||
'anna@example.org',
|
||||
language: 'en',
|
||||
);
|
||||
|
||||
final outcome = await auth.verifyEmailCode(
|
||||
request,
|
||||
'CODE-1',
|
||||
address: 'anna@example.org',
|
||||
);
|
||||
|
||||
expect(outcome, const EmailLinked());
|
||||
expect(auth.isIdentified, isTrue);
|
||||
expect(await auth.emailAddress(), 'anna@example.org');
|
||||
});
|
||||
|
||||
test("a code for another person's address changes nothing here; "
|
||||
'switching remembers the address for the person switched to, not '
|
||||
'the one left', () async {
|
||||
gateway.addressOwners['ben@example.org'] = 'ben';
|
||||
final request = await auth.sendEmailCode(
|
||||
'ben@example.org',
|
||||
language: 'en',
|
||||
);
|
||||
|
||||
final outcome = await auth.verifyEmailCode(
|
||||
request,
|
||||
'CODE-1',
|
||||
address: 'ben@example.org',
|
||||
);
|
||||
expect(outcome, isA<EmailTakenByAnotherPerson>());
|
||||
expect(auth.isIdentified, isFalse);
|
||||
expect(addresses.addresses, isEmpty);
|
||||
|
||||
await auth.switchToEmail(
|
||||
(outcome as EmailTakenByAnotherPerson).switchToken,
|
||||
address: 'ben@example.org',
|
||||
);
|
||||
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'ben', isIdentified: true),
|
||||
);
|
||||
expect(addresses.addresses, {'ben': 'ben@example.org'});
|
||||
expect(await auth.emailAddress(), 'ben@example.org');
|
||||
});
|
||||
|
||||
test('a wrong code remembers nothing', () async {
|
||||
final request = await auth.sendEmailCode(
|
||||
'anna@example.org',
|
||||
language: 'en',
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
auth.verifyEmailCode(request, 'CODE-9', address: 'anna@example.org'),
|
||||
throwsA(
|
||||
isA<EmailSignInFailed>().having(
|
||||
(failure) => failure.problem,
|
||||
'problem',
|
||||
EmailSignInProblem.wrongCode,
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(addresses.addresses, isEmpty);
|
||||
});
|
||||
});
|
||||
}
|
||||
259
packages/halkyon_app/test/auth/data/loopback_web_auth_test.dart
Normal file
259
packages/halkyon_app/test/auth/data/loopback_web_auth_test.dart
Normal file
|
|
@ -0,0 +1,259 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'dart:ui' show AppLifecycleState;
|
||||
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:flutter_web_auth_2/flutter_web_auth_2.dart';
|
||||
import 'package:flutter_web_auth_2_platform_interface/flutter_web_auth_2_platform_interface.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
|
||||
void main() {
|
||||
const gitHubPage = 'https://github.com/login/oauth/authorize?client_id=app';
|
||||
|
||||
late int port;
|
||||
late String callback;
|
||||
late Completer<Uri> pageOpened;
|
||||
late int broughtToFront;
|
||||
late LoopbackWebAuth webAuth;
|
||||
|
||||
/// The loopback part with a fake browser that only records the page it
|
||||
/// opens; [opens] is whether opening it works.
|
||||
LoopbackWebAuth loopback({
|
||||
bool opens = true,
|
||||
Future<void> Function()? bringToFront,
|
||||
Duration timeout = const Duration(minutes: 1),
|
||||
}) => LoopbackWebAuth(
|
||||
appName: 'Halkyon Learn',
|
||||
launch: (url) {
|
||||
pageOpened.complete(url);
|
||||
return Future.value(opens);
|
||||
},
|
||||
bringToFront:
|
||||
bringToFront ??
|
||||
() {
|
||||
broughtToFront++;
|
||||
return Future.value();
|
||||
},
|
||||
timeout: timeout,
|
||||
);
|
||||
|
||||
setUp(() async {
|
||||
// A port that is free right now, so the tests never meet a running app.
|
||||
final probe = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
port = probe.port;
|
||||
await probe.close();
|
||||
callback = 'http://127.0.0.1:$port/github-callback';
|
||||
pageOpened = Completer();
|
||||
broughtToFront = 0;
|
||||
webAuth = loopback();
|
||||
});
|
||||
tearDown(() => webAuth.cancel());
|
||||
|
||||
/// Starts a sign-in as flutter_web_auth_2 hands it over.
|
||||
Future<String> signIn() => webAuth.authenticate(
|
||||
url: gitHubPage,
|
||||
callbackUrlScheme: callback,
|
||||
options: const {},
|
||||
);
|
||||
|
||||
/// The browser asking this machine's [address] for [pathAndQuery], as when
|
||||
/// GitHub sends it back. Over a raw socket, so that the fake HTTP of
|
||||
/// Flutter's test binding can't get in between.
|
||||
Future<({int status, String body})> browserAsks(
|
||||
String pathAndQuery, {
|
||||
InternetAddress? address,
|
||||
}) async {
|
||||
final socket = await Socket.connect(
|
||||
address ?? InternetAddress.loopbackIPv4,
|
||||
port,
|
||||
timeout: const Duration(seconds: 2),
|
||||
);
|
||||
socket.write(
|
||||
'GET $pathAndQuery HTTP/1.1\r\n'
|
||||
'Host: 127.0.0.1:$port\r\n'
|
||||
'Connection: close\r\n\r\n',
|
||||
);
|
||||
final response = await utf8.decoder.bind(socket).join();
|
||||
socket.destroy();
|
||||
return (
|
||||
status: int.parse(response.split(' ')[1]),
|
||||
body: response.substring(response.indexOf('\r\n\r\n') + 4),
|
||||
);
|
||||
}
|
||||
|
||||
test(
|
||||
"opens GitHub's page and returns where the browser comes back to",
|
||||
() async {
|
||||
final result = signIn();
|
||||
|
||||
expect(await pageOpened.future, Uri.parse(gitHubPage));
|
||||
final response = await browserAsks('/github-callback?code=c&state=s');
|
||||
|
||||
expect(await result, '$callback?code=c&state=s');
|
||||
expect(response.status, HttpStatus.ok);
|
||||
expect(response.body, contains('return to Halkyon Learn'));
|
||||
expect(broughtToFront, 1);
|
||||
},
|
||||
);
|
||||
|
||||
test(
|
||||
"keeps waiting when the app's window regains focus",
|
||||
() async {
|
||||
// The way the bug went: flutter_web_auth_2's front ends every pending
|
||||
// sign-in when the app is resumed, and on the desktop the window
|
||||
// regaining focus resumes it.
|
||||
TestWidgetsFlutterBinding.ensureInitialized();
|
||||
FlutterWebAuth2Platform.instance = webAuth;
|
||||
final result = FlutterWebAuth2.authenticate(
|
||||
url: gitHubPage,
|
||||
callbackUrlScheme: callback,
|
||||
);
|
||||
await pageOpened.future;
|
||||
|
||||
// The browser takes the focus; then the person looks back at the app.
|
||||
await _embedderReports(AppLifecycleState.inactive);
|
||||
await _embedderReports(AppLifecycleState.resumed);
|
||||
final response = await browserAsks('/github-callback?code=c&state=s');
|
||||
|
||||
expect(response.status, HttpStatus.ok);
|
||||
expect(await result, '$callback?code=c&state=s');
|
||||
},
|
||||
// Elsewhere, flutter_web_auth_2 insists on a URL scheme as the callback.
|
||||
skip: !(Platform.isLinux || Platform.isWindows),
|
||||
);
|
||||
|
||||
test('a cancel ends the wait as cancelled and stops listening', () async {
|
||||
final result = signIn();
|
||||
await pageOpened.future;
|
||||
|
||||
await webAuth.cancel();
|
||||
|
||||
await expectLater(result, throwsA(_cancelled));
|
||||
await expectLater(
|
||||
browserAsks('/github-callback?code=c&state=s'),
|
||||
throwsA(isA<SocketException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('gives up after the timeout and stops listening', () async {
|
||||
webAuth = loopback(timeout: const Duration(milliseconds: 50));
|
||||
|
||||
await expectLater(signIn(), throwsA(isA<TimeoutException>()));
|
||||
await expectLater(
|
||||
browserAsks('/github-callback?code=c&state=s'),
|
||||
throwsA(isA<SocketException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test('a second sign-in ends the first as cancelled', () async {
|
||||
final first = signIn();
|
||||
await pageOpened.future;
|
||||
pageOpened = Completer();
|
||||
|
||||
final second = signIn();
|
||||
|
||||
await expectLater(first, throwsA(_cancelled));
|
||||
await pageOpened.future;
|
||||
await browserAsks('/github-callback?code=c&state=s');
|
||||
expect(await second, '$callback?code=c&state=s');
|
||||
});
|
||||
|
||||
test('answers other paths with "not found" and keeps waiting', () async {
|
||||
final result = signIn();
|
||||
await pageOpened.future;
|
||||
|
||||
final stray = await browserAsks('/favicon.ico');
|
||||
final response = await browserAsks('/github-callback?code=c&state=s');
|
||||
|
||||
expect(stray.status, HttpStatus.notFound);
|
||||
expect(response.status, HttpStatus.ok);
|
||||
expect(await result, '$callback?code=c&state=s');
|
||||
});
|
||||
|
||||
test('listens on the loopback address only', () async {
|
||||
final otherAddresses = [
|
||||
for (final interface in await NetworkInterface.list())
|
||||
for (final address in interface.addresses)
|
||||
if (!address.isLoopback) address,
|
||||
];
|
||||
final result = signIn();
|
||||
await pageOpened.future;
|
||||
|
||||
for (final address in otherAddresses) {
|
||||
await expectLater(
|
||||
browserAsks('/github-callback?code=c&state=s', address: address),
|
||||
throwsA(isA<SocketException>()),
|
||||
reason: '$address',
|
||||
);
|
||||
}
|
||||
await browserAsks('/github-callback?code=c&state=s');
|
||||
expect(await result, '$callback?code=c&state=s');
|
||||
});
|
||||
|
||||
test("stops listening when the browser can't be opened", () async {
|
||||
webAuth = loopback(opens: false);
|
||||
|
||||
await expectLater(signIn(), throwsA(isA<PlatformException>()));
|
||||
await expectLater(
|
||||
browserAsks('/github-callback?code=c&state=s'),
|
||||
throwsA(isA<SocketException>()),
|
||||
);
|
||||
});
|
||||
|
||||
test("signs in even when the window can't come to the front", () async {
|
||||
webAuth = loopback(
|
||||
bringToFront: () => Future.error(MissingPluginException()),
|
||||
);
|
||||
final result = signIn();
|
||||
await pageOpened.future;
|
||||
|
||||
await browserAsks('/github-callback?code=c&state=s');
|
||||
|
||||
expect(await result, '$callback?code=c&state=s');
|
||||
});
|
||||
|
||||
test(
|
||||
'refuses a callback other than a path on 127.0.0.1 with a port',
|
||||
() async {
|
||||
for (final address in [
|
||||
'http://0.0.0.0:$port/github-callback',
|
||||
'http://localhost:$port/github-callback',
|
||||
'http://example.com:$port/github-callback',
|
||||
'https://127.0.0.1:$port/github-callback',
|
||||
'http://127.0.0.1/github-callback',
|
||||
'http://127.0.0.1:$port',
|
||||
]) {
|
||||
await expectLater(
|
||||
webAuth.authenticate(
|
||||
url: gitHubPage,
|
||||
callbackUrlScheme: address,
|
||||
options: const {},
|
||||
),
|
||||
throwsArgumentError,
|
||||
reason: address,
|
||||
);
|
||||
}
|
||||
expect(pageOpened.isCompleted, isFalse);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// How flutter_web_auth_2 reports a cancelled sign-in, which is how the
|
||||
/// gateway's callers learn of it.
|
||||
final _cancelled = isA<PlatformException>().having(
|
||||
(error) => error.code,
|
||||
'code',
|
||||
'CANCELED',
|
||||
);
|
||||
|
||||
/// The embedder reporting the app's lifecycle, as it does when the window
|
||||
/// gains or loses focus.
|
||||
Future<void> _embedderReports(AppLifecycleState state) =>
|
||||
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
|
||||
.handlePlatformMessage(
|
||||
SystemChannels.lifecycle.name,
|
||||
SystemChannels.lifecycle.codec.encodeMessage(state.toString()),
|
||||
(_) {},
|
||||
);
|
||||
|
|
@ -0,0 +1,186 @@
|
|||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_base_client/halkyon_base_client.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:http/testing.dart';
|
||||
import 'package:serverpod_auth_idp_flutter/serverpod_auth_idp_flutter.dart';
|
||||
|
||||
/// A session kept in memory, as no test reaches the system's storage.
|
||||
final class _MemoryStorage() implements ClientAuthSuccessStorage {
|
||||
AuthSuccess? _session;
|
||||
|
||||
@override
|
||||
Future<AuthSuccess?> get() async => _session;
|
||||
|
||||
@override
|
||||
Future<void> set(AuthSuccess? data) async => _session = data;
|
||||
}
|
||||
|
||||
/// An instance's halkyon_base as the network shows it: anonymous sign-ins
|
||||
/// get `anonymous-token`, its basics offer GitHub and email, and an address
|
||||
/// belongs to Ben, whom a switch signs in as with `ben-token`.
|
||||
final class _Instance() {
|
||||
static final anonymous = UuidValue.withValidation(
|
||||
'0191f0c4-1111-7000-8000-000000000001',
|
||||
);
|
||||
static final ben = UuidValue.withValidation(
|
||||
'0191f0c4-2222-7000-8000-000000000002',
|
||||
);
|
||||
|
||||
/// Each call, by its endpoint and method, with the session it carried.
|
||||
final calls = <({String call, String? session})>[];
|
||||
|
||||
/// What mailing a code is refused for, while set.
|
||||
EmailSignInRefusal? sendRefused;
|
||||
|
||||
http.Client client() => MockClient((request) async {
|
||||
final call = request.url.path.substring(1);
|
||||
calls.add((call: call, session: request.headers['authorization']));
|
||||
String sent(Object? body) => jsonEncode(body);
|
||||
return switch (call) {
|
||||
'halkyon_base.anonymousIdp/login' => http.Response(
|
||||
sent(_session(anonymous, 'anonymous-token', identified: false)),
|
||||
200,
|
||||
),
|
||||
'halkyon_base.instance/basics' => http.Response(
|
||||
sent(
|
||||
InstanceBasics(
|
||||
app: 'learn',
|
||||
gitHubClientId: 'github-client',
|
||||
emailSignIn: true,
|
||||
sourceCodeUrl: 'https://code.example.org/learn',
|
||||
minimumAppBuild: 1,
|
||||
).toJson(),
|
||||
),
|
||||
200,
|
||||
),
|
||||
'halkyon_base.emailCode/sendCode' => switch (sendRefused) {
|
||||
final reason? => http.Response(
|
||||
sent({
|
||||
'className': 'halkyon_base.EmailSignInRefusedException',
|
||||
'data': EmailSignInRefusedException(reason: reason).toJson(),
|
||||
}),
|
||||
400,
|
||||
),
|
||||
null => http.Response(
|
||||
sent('0191f0c4-3333-7000-8000-000000000003'),
|
||||
200,
|
||||
),
|
||||
},
|
||||
'halkyon_base.emailCode/verifyCode' => http.Response(
|
||||
sent(EmailCodeAccepted(switchToken: 'switch-to-ben').toJson()),
|
||||
200,
|
||||
),
|
||||
'halkyon_base.emailSignIn/signIn' => http.Response(
|
||||
sent(_session(ben, 'ben-token', identified: true)),
|
||||
200,
|
||||
),
|
||||
'halkyon_base.account/delete' => http.Response('null', 200),
|
||||
_ => http.Response('', 404),
|
||||
};
|
||||
});
|
||||
|
||||
static Map<String, dynamic> _session(
|
||||
UuidValue person,
|
||||
String token, {
|
||||
required bool identified,
|
||||
}) => AuthSuccess(
|
||||
authStrategy: 'session',
|
||||
token: token,
|
||||
authUserId: person,
|
||||
scopeNames: {if (identified) 'identified'},
|
||||
).toJson();
|
||||
}
|
||||
|
||||
void main() {
|
||||
late _Instance instance;
|
||||
late BaseClient app;
|
||||
late ServerpodAuthGateway gateway;
|
||||
|
||||
setUp(() async {
|
||||
instance = _Instance();
|
||||
app =
|
||||
BaseClient(
|
||||
'https://learn.example.org/',
|
||||
httpClientOverride: instance.client(),
|
||||
)
|
||||
..authSessionManager = FlutterAuthSessionManager(
|
||||
storage: _MemoryStorage(),
|
||||
);
|
||||
await app.auth.initialize();
|
||||
gateway = ServerpodAuthGateway(
|
||||
app,
|
||||
base: app.modules,
|
||||
callbackScheme: 'app.halkyon.test',
|
||||
appName: 'Halkyon Test',
|
||||
httpClient: instance.client,
|
||||
);
|
||||
addTearDown(() async {
|
||||
await gateway.dispose();
|
||||
app.close();
|
||||
});
|
||||
});
|
||||
|
||||
test(
|
||||
'signs in anonymously through halkyon_base, nobody identified',
|
||||
() async {
|
||||
await gateway.signInAnonymously();
|
||||
|
||||
expect(
|
||||
gateway.status,
|
||||
SignedIn(authUserId: '${_Instance.anonymous}', isIdentified: false),
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('tells what the instance offers from its basics', () async {
|
||||
expect(await gateway.gitHubClientId(), 'github-client');
|
||||
expect(await gateway.offersEmailSignIn(), isTrue);
|
||||
});
|
||||
|
||||
test("the instance's refusals to mail a code arrive in the package's own "
|
||||
'words', () async {
|
||||
await gateway.signInAnonymously();
|
||||
instance.sendRefused = EmailSignInRefusal.tooManyCodes;
|
||||
|
||||
await expectLater(
|
||||
gateway.sendEmailCode('ben@example.org', language: 'en'),
|
||||
throwsA(
|
||||
isA<EmailSignInFailed>().having(
|
||||
(failure) => failure.problem,
|
||||
'problem',
|
||||
EmailSignInProblem.tooManyCodes,
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test("switching to an address's person signs in without this device's "
|
||||
'session, then deletes the anonymous account left behind as '
|
||||
'itself', () async {
|
||||
await gateway.signInAnonymously();
|
||||
final request = await gateway.sendEmailCode(
|
||||
'ben@example.org',
|
||||
language: 'en',
|
||||
);
|
||||
final outcome = await gateway.verifyEmailCode(request, 'K7QXM2PD');
|
||||
expect(outcome, const EmailTakenByAnotherPerson('switch-to-ben'));
|
||||
|
||||
expect(await gateway.switchToEmail('switch-to-ben'), '${_Instance.ben}');
|
||||
|
||||
expect(
|
||||
gateway.status,
|
||||
SignedIn(authUserId: '${_Instance.ben}', isIdentified: true),
|
||||
);
|
||||
final signIn = instance.calls.singleWhere(
|
||||
(call) => call.call == 'halkyon_base.emailSignIn/signIn',
|
||||
);
|
||||
expect(signIn.session, isNull, reason: 'no session goes with it');
|
||||
final deletion = instance.calls.singleWhere(
|
||||
(call) => call.call == 'halkyon_base.account/delete',
|
||||
);
|
||||
expect(deletion.session, contains('anonymous-token'));
|
||||
});
|
||||
}
|
||||
|
|
@ -0,0 +1,204 @@
|
|||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
import 'package:halkyon_ui/halkyon_ui.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
|
||||
void main() {
|
||||
late FakeAuthGateway gateway;
|
||||
late TestAppDependencies dependencies;
|
||||
|
||||
/// Whether this device holds content a switch would leave behind.
|
||||
late bool holdsContent;
|
||||
|
||||
/// What the sheet returned once it closed.
|
||||
late List<String?> returned;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'anonymous-1', isIdentified: false),
|
||||
);
|
||||
dependencies = TestAppDependencies(gateway: gateway);
|
||||
holdsContent = false;
|
||||
returned = [];
|
||||
});
|
||||
|
||||
/// Opens the sheet, in [locale].
|
||||
Future<void> open(
|
||||
WidgetTester tester, {
|
||||
Locale locale = const Locale('en'),
|
||||
}) async {
|
||||
tester.view.physicalSize = const Size(412, 900);
|
||||
tester.view.devicePixelRatio = 1;
|
||||
addTearDown(tester.view.reset);
|
||||
await tester.pumpWidget(
|
||||
testApp(
|
||||
Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => TextButton(
|
||||
onPressed: () async => returned.add(
|
||||
await showEmailSignInSheet(
|
||||
context,
|
||||
deviceHoldsContent: () async => holdsContent,
|
||||
),
|
||||
),
|
||||
child: const Text('Open'),
|
||||
),
|
||||
),
|
||||
),
|
||||
dependencies: dependencies,
|
||||
locale: locale,
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('Open'));
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
/// The sheet's text field.
|
||||
final inSheet = find.descendant(
|
||||
of: find.byType(SheetScaffold),
|
||||
matching: find.byType(TextField),
|
||||
);
|
||||
|
||||
/// Types [address] and sends a code to it, then types [code]. A switch to
|
||||
/// another person shows a spinner until their app replaces the whole
|
||||
/// subtree, which a test never settles: [switches] pumps frames instead.
|
||||
Future<void> signInWith(
|
||||
WidgetTester tester,
|
||||
String address, {
|
||||
String code = 'CODE-1',
|
||||
bool switches = false,
|
||||
}) async {
|
||||
await tester.enterText(inSheet, address);
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Send code'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text("We mailed a code to $address. It's valid for 15 minutes."),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.enterText(inSheet, code);
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Continue'));
|
||||
if (switches) {
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
} else {
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
}
|
||||
|
||||
testWidgets('an anonymous person continues with email: the address, then '
|
||||
'the code mailed to it, after which the address is theirs', (
|
||||
tester,
|
||||
) async {
|
||||
await open(tester);
|
||||
expect(
|
||||
find.textContaining('keeps only a fingerprint of your address'),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
await signInWith(tester, 'anna@example.org');
|
||||
|
||||
expect(returned, ['anna@example.org']);
|
||||
expect(gateway.mailed.single.language, 'en');
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'anonymous-1', isIdentified: true),
|
||||
);
|
||||
expect(dependencies.addresses.addresses, {
|
||||
'anonymous-1': 'anna@example.org',
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('the mail is worded in German for someone who reads the app in '
|
||||
'German', (tester) async {
|
||||
await open(tester, locale: const Locale('de'));
|
||||
await tester.enterText(inSheet, 'anna@example.org');
|
||||
await tester.tap(find.widgetWithText(FilledButton, 'Code senden'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(gateway.mailed.single.language, 'de');
|
||||
});
|
||||
|
||||
testWidgets('a wrong code says so, and a new one can be asked for', (
|
||||
tester,
|
||||
) async {
|
||||
await open(tester);
|
||||
|
||||
await signInWith(tester, 'anna@example.org', code: 'CODE-9');
|
||||
|
||||
expect(find.text("That code isn't right."), findsOneWidget);
|
||||
await tester.tap(find.text('Send a new code'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('A new code is on its way.'), findsOneWidget);
|
||||
expect(gateway.mailed, hasLength(2));
|
||||
});
|
||||
|
||||
testWidgets("another person's address switches at once where the device "
|
||||
'holds nothing', (tester) async {
|
||||
gateway.addressOwners['ben@example.org'] = 'ben';
|
||||
await open(tester);
|
||||
|
||||
await signInWith(tester, 'ben@example.org', switches: true);
|
||||
|
||||
expect(gateway.emailSwitches, 1);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'ben', isIdentified: true),
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets("another person's address asks first where the device holds "
|
||||
"content, in the app's own words: staying changes nothing, switching "
|
||||
'switches', (tester) async {
|
||||
gateway.addressOwners['ben@example.org'] = 'ben';
|
||||
holdsContent = true;
|
||||
await open(tester);
|
||||
|
||||
await signInWith(tester, 'ben@example.org');
|
||||
|
||||
expect(
|
||||
find.text('Switch to the account of ben@example.org?'),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.text(
|
||||
"The notes on this device can't be combined with it yet: if you "
|
||||
'switch, they are deleted.',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
await tester.tap(find.text('Stay anonymous'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(gateway.emailSwitches, 0);
|
||||
|
||||
await signInWith(tester, 'ben@example.org', code: 'CODE-2');
|
||||
await tester.tap(find.text('Switch'));
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
expect(gateway.emailSwitches, 1);
|
||||
});
|
||||
|
||||
testWidgets("someone identified whose address is another account's is "
|
||||
'told so, and stays who they are', (tester) async {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'github-person', isIdentified: true),
|
||||
)..addressOwners['ben@example.org'] = 'ben';
|
||||
dependencies = TestAppDependencies(gateway: gateway);
|
||||
await open(tester);
|
||||
|
||||
await signInWith(tester, 'ben@example.org');
|
||||
|
||||
expect(
|
||||
find.text(
|
||||
'ben@example.org already signs in to another account. To switch to '
|
||||
'it, sign out first.',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(gateway.emailSwitches, 0);
|
||||
expect(
|
||||
gateway.status,
|
||||
const SignedIn(authUserId: 'github-person', isIdentified: true),
|
||||
);
|
||||
});
|
||||
}
|
||||
173
packages/halkyon_app/test/auth/widget/session_gate_test.dart
Normal file
173
packages/halkyon_app/test/auth/widget/session_gate_test.dart
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:halkyon_app/halkyon_app.dart';
|
||||
import 'package:halkyon_app/testing.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
|
||||
/// A signed-in person's app as a test makes one: whose it is, whether
|
||||
/// they asked to continue with email, and a way to sign out.
|
||||
class const _PersonApp(final SignedIn person) extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) => MaterialApp(
|
||||
localizationsDelegates: testLocalizationsDelegates,
|
||||
supportedLocales: HalkyonAppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: Builder(
|
||||
builder: (context) => Column(
|
||||
children: [
|
||||
Text('App of ${person.authUserId}'),
|
||||
if (context.watch<SessionBloc>().state case SessionStateActive(
|
||||
signsInWithEmail: true,
|
||||
))
|
||||
const Text('Asks for the address'),
|
||||
TextButton(
|
||||
onPressed: () => context.read<SessionBloc>().add(
|
||||
const SessionEventSignOutRequested(),
|
||||
),
|
||||
child: const Text('Sign out'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
late FakeAuthGateway gateway;
|
||||
late TestAppDependencies dependencies;
|
||||
|
||||
setUp(() {
|
||||
gateway = FakeAuthGateway();
|
||||
dependencies = TestAppDependencies(gateway: gateway);
|
||||
});
|
||||
|
||||
Future<void> start(WidgetTester tester) async {
|
||||
await tester.pumpWidget(
|
||||
AppFrameScope(
|
||||
frame: testFrame(),
|
||||
child: AppScope(
|
||||
dependencies: dependencies,
|
||||
child: SessionGate(
|
||||
look: (context) => (themeMode: ThemeMode.light, locale: null),
|
||||
app: _PersonApp.new,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
testWidgets('shows the app once signed in anonymously at a first start', (
|
||||
tester,
|
||||
) async {
|
||||
await start(tester);
|
||||
|
||||
expect(find.text('App of anonymous-1'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('offers to try again when signing in fails', (tester) async {
|
||||
gateway.signInError = Exception('No connection');
|
||||
await start(tester);
|
||||
|
||||
expect(find.text("Couldn't reach the server."), findsOneWidget);
|
||||
|
||||
gateway.signInError = null;
|
||||
await tester.tap(find.text('Try again'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('App of anonymous-2'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('signed out, it asks how to start on the instance, making no '
|
||||
'account; starting without one makes one', (tester) async {
|
||||
await dependencies.signedOut.write(signedOut: true);
|
||||
await start(tester);
|
||||
|
||||
expect(find.text('Signed out'), findsOneWidget);
|
||||
expect(find.text('Start on test.example.org:'), findsOneWidget);
|
||||
expect(find.text('Sign in with GitHub'), findsOneWidget);
|
||||
expect(find.text('Continue with email'), findsOneWidget);
|
||||
expect(
|
||||
find.text('Use another instance'),
|
||||
findsNothing,
|
||||
reason: 'no instances to choose among here',
|
||||
);
|
||||
expect(gateway.signIns, 0);
|
||||
|
||||
await tester.tap(find.text('Start without an account'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('App of anonymous-1'), findsOneWidget);
|
||||
expect(dependencies.signedOut.signedOut, isFalse);
|
||||
});
|
||||
|
||||
testWidgets('from there, GitHub signs in its person; a GitHub account '
|
||||
'nobody here has yet is said so', (tester) async {
|
||||
await dependencies.signedOut.write(signedOut: true);
|
||||
await start(tester);
|
||||
|
||||
gateway.gitHubError = const GitHubSignInFailed(
|
||||
GitHubSignInProblem.unknownAccount,
|
||||
);
|
||||
await tester.tap(find.text('Sign in with GitHub'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
find.text(
|
||||
'No account here signs in with this GitHub account yet. Start '
|
||||
'without an account, then add GitHub in Profile.',
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
|
||||
gateway.gitHubError = null;
|
||||
await tester.tap(find.text('Sign in with GitHub'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('App of github-person'), findsOneWidget);
|
||||
expect(gateway.signIns, 0);
|
||||
});
|
||||
|
||||
testWidgets('from there, an email address starts without an account and '
|
||||
'asks for the address in the app', (tester) async {
|
||||
await dependencies.signedOut.write(signedOut: true);
|
||||
await start(tester);
|
||||
|
||||
await tester.tap(find.text('Continue with email'));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text('App of anonymous-1'), findsOneWidget);
|
||||
expect(find.text('Asks for the address'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('offers only the ways the instance has', (tester) async {
|
||||
gateway
|
||||
..offersGitHub = false
|
||||
..offersEmail = false;
|
||||
await dependencies.signedOut.write(signedOut: true);
|
||||
await start(tester);
|
||||
|
||||
expect(find.text('Sign in with GitHub'), findsNothing);
|
||||
expect(find.text('Continue with email'), findsNothing);
|
||||
expect(find.text('Start without an account'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('signing out, or the instance ending the session, asks how to '
|
||||
'start', (tester) async {
|
||||
gateway = FakeAuthGateway(
|
||||
status: const SignedIn(authUserId: 'ada', isIdentified: true),
|
||||
);
|
||||
dependencies = TestAppDependencies(gateway: gateway);
|
||||
await start(tester);
|
||||
expect(find.text('App of ada'), findsOneWidget);
|
||||
|
||||
await tester.tap(find.text('Sign out'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Signed out'), findsOneWidget);
|
||||
expect(dependencies.signedOut.signedOut, isTrue);
|
||||
|
||||
await tester.tap(find.text('Start without an account'));
|
||||
await tester.pumpAndSettle();
|
||||
gateway.revokeSession();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Signed out'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue