-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(cat-voices): user service, keychain, keychain provider (#1056)
* fix: Keychain metadata typo fix + migration tests * fix: secure storage key id lookup * test: vault keychain * test: vault keychain provider * test: KeychainToUnlockTransformer * test: UserService * test: Session cubit * refactor: services as interfaces with factory methods * fix: analyzer * fix: mocktail spelling * refactor: session cubit group
- Loading branch information
1 parent
32ef686
commit 7302840
Showing
18 changed files
with
797 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -317,4 +317,5 @@ libcatalyst | |
staticlib | ||
addrof | ||
Lifetimeable | ||
cbindgen | ||
cbindgen | ||
mocktail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
catalyst_voices/packages/catalyst_voices_blocs/test/session/session_cubit_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
import 'package:catalyst_voices_blocs/src/session/session.dart'; | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:catalyst_voices_services/catalyst_voices_services.dart'; | ||
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
void main() { | ||
late final KeychainProvider keychainProvider; | ||
late final UserStorage userStorage; | ||
|
||
late final UserService userService; | ||
late final RegistrationService registrationService; | ||
late final RegistrationProgressNotifier notifier; | ||
|
||
late SessionCubit sessionCubit; | ||
|
||
setUpAll(() { | ||
keychainProvider = VaultKeychainProvider(); | ||
userStorage = SecureUserStorage(); | ||
|
||
userService = UserService( | ||
keychainProvider: keychainProvider, | ||
userStorage: userStorage, | ||
); | ||
registrationService = _MockRegistrationService(); | ||
notifier = RegistrationProgressNotifier(); | ||
}); | ||
|
||
setUp(() { | ||
FlutterSecureStorage.setMockInitialValues({}); | ||
sessionCubit = SessionCubit(userService, registrationService, notifier); | ||
}); | ||
|
||
tearDown(() async { | ||
await sessionCubit.close(); | ||
|
||
reset(registrationService); | ||
}); | ||
|
||
group(SessionCubit, () { | ||
test('when no keychain is found session is in Visitor state', () async { | ||
// Given | ||
|
||
// When | ||
await userService.removeCurrentAccount(); | ||
|
||
// Then | ||
expect(userService.keychain, isNull); | ||
expect(sessionCubit.state, isA<VisitorSessionState>()); | ||
}); | ||
|
||
test('when no keychain is found session is in Visitor state', () async { | ||
// Given | ||
|
||
// When | ||
await userService.removeCurrentAccount(); | ||
|
||
// Gives time for stream to emit. | ||
await Future<void>.delayed(const Duration(milliseconds: 100)); | ||
|
||
// Then | ||
expect(userService.keychain, isNull); | ||
expect(sessionCubit.state, isA<VisitorSessionState>()); | ||
expect( | ||
sessionCubit.state, | ||
const VisitorSessionState(isRegistrationInProgress: false), | ||
); | ||
}); | ||
|
||
test( | ||
'when no keychain is found but there is a registration progress ' | ||
'session is in Visitor state with correct flag', () async { | ||
// Given | ||
final keychainProgress = KeychainProgress( | ||
seedPhrase: SeedPhrase(), | ||
password: 'Test1234', | ||
); | ||
|
||
// When | ||
notifier.value = RegistrationProgress(keychainProgress: keychainProgress); | ||
|
||
await userService.removeCurrentAccount(); | ||
|
||
// Gives time for stream to emit. | ||
await Future<void>.delayed(const Duration(milliseconds: 100)); | ||
|
||
// Then | ||
expect(userService.keychain, isNull); | ||
expect(sessionCubit.state, isA<VisitorSessionState>()); | ||
expect( | ||
sessionCubit.state, | ||
const VisitorSessionState(isRegistrationInProgress: true), | ||
); | ||
}); | ||
|
||
test('when keychain is locked session is in Guest state', () async { | ||
// Given | ||
const keychainId = 'id'; | ||
const lockFactor = PasswordLockFactor('Test1234'); | ||
|
||
// When | ||
final keychain = await keychainProvider.create(keychainId); | ||
await keychain.setLock(lockFactor); | ||
await keychain.lock(); | ||
|
||
await userService.useKeychain(keychainId); | ||
|
||
// Gives time for stream to emit. | ||
await Future<void>.delayed(const Duration(milliseconds: 100)); | ||
|
||
// Then | ||
expect(userService.keychain, isNotNull); | ||
expect(sessionCubit.state, isNot(isA<VisitorSessionState>())); | ||
expect(sessionCubit.state, isA<GuestSessionState>()); | ||
}); | ||
|
||
test('when keychain is unlocked session is in Active state', () async { | ||
// Given | ||
const keychainId = 'id'; | ||
const lockFactor = PasswordLockFactor('Test1234'); | ||
|
||
// When | ||
final keychain = await keychainProvider.create(keychainId); | ||
await keychain.setLock(lockFactor); | ||
|
||
await userService.useKeychain(keychainId); | ||
await userService.keychain?.unlock(lockFactor); | ||
|
||
// Gives time for stream to emit. | ||
await Future<void>.delayed(const Duration(milliseconds: 100)); | ||
|
||
// Then | ||
expect(userService.keychain, isNotNull); | ||
expect(sessionCubit.state, isNot(isA<VisitorSessionState>())); | ||
expect(sessionCubit.state, isNot(isA<GuestSessionState>())); | ||
expect(sessionCubit.state, isA<ActiveAccountSessionState>()); | ||
}); | ||
}); | ||
} | ||
|
||
class _MockRegistrationService extends Mock implements RegistrationService {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
catalyst_voices/packages/catalyst_voices_models/test/crypto/keychain_metadata_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import 'package:catalyst_voices_models/catalyst_voices_models.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('Serialization', () { | ||
test('json createAt to createdAt', () { | ||
// Given | ||
final createdAt = DateTime.timestamp(); | ||
|
||
// When | ||
final json = { | ||
'createAt': createdAt.toIso8601String(), | ||
'updatedAt': DateTime.timestamp().toIso8601String(), | ||
}; | ||
|
||
// Then | ||
final metadata = KeychainMetadata.fromJson(json); | ||
|
||
expect(metadata.createdAt, createdAt); | ||
}); | ||
}); | ||
|
||
group('Equality', () { | ||
test('same source dates equals', () { | ||
// Given | ||
final now = DateTime.now(); | ||
|
||
// When | ||
final metadataOne = KeychainMetadata(createdAt: now, updatedAt: now); | ||
final metadataTwo = KeychainMetadata(createdAt: now, updatedAt: now); | ||
|
||
// Then | ||
expect(metadataOne, metadataTwo); | ||
}); | ||
|
||
test('different source dates equals', () { | ||
// Given | ||
final now = DateTime.now(); | ||
final isPast = now.subtract(const Duration(days: 1)); | ||
|
||
// When | ||
final metadataOne = KeychainMetadata(createdAt: now, updatedAt: now); | ||
final metadataTwo = KeychainMetadata( | ||
createdAt: isPast, | ||
updatedAt: isPast, | ||
); | ||
|
||
// Then | ||
expect(metadataOne, isNot(metadataTwo)); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.