feat(authentication)!: rename package

This commit is contained in:
2022-04-19 19:34:04 +02:00
parent 22e0aeeeed
commit c70446e2c4
148 changed files with 7788 additions and 0 deletions
@@ -0,0 +1,99 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:bloc_test/bloc_test.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockAuthenticationRepository extends Mock
implements AuthenticationRepositoryInterface {}
class MockUser extends Mock implements UserInterface {}
void main() {
group('AuthenticationCubit', () {
final MockUser user = MockUser();
late AuthenticationRepositoryInterface authenticationRepository;
setUp(() {
authenticationRepository = MockAuthenticationRepository();
when(() => authenticationRepository.user).thenAnswer(
(_) => const Stream.empty(),
);
when(
() => authenticationRepository.currentUser,
).thenReturn(const UserFirebase.empty());
});
test('initial state is unknown', () {
expect(
AuthenticationCubit(authenticationRepository: authenticationRepository)
.state,
const AuthenticationState.unknown(),
);
});
group('UserChanged', () {
blocTest<AuthenticationCubit, AuthenticationState>(
'emits authenticated when user is not empty',
setUp: () {
when(() => user.isNotEmpty).thenReturn(true);
when(() => authenticationRepository.user).thenAnswer(
(_) => Stream.value(user),
);
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
)..init(),
seed: () => const AuthenticationState.unknown(),
expect: () => [AuthenticationState.authenticated(user, null)],
);
blocTest<AuthenticationCubit, AuthenticationState>(
'emits unauthenticated when user is empty',
setUp: () {
when(() => authenticationRepository.user).thenAnswer(
(_) => Stream.value(const UserFirebase.empty()),
);
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
)..init(),
seed: () => const AuthenticationState.unknown(),
expect: () => [const AuthenticationState.unauthenticated()],
);
});
group('LogoutRequested', () {
blocTest<AuthenticationCubit, AuthenticationState>(
'invokes signOut',
setUp: () {
when(
() => authenticationRepository.signOut(),
).thenAnswer((_) async {});
},
build: () => AuthenticationCubit(
authenticationRepository: authenticationRepository,
),
act: (AuthenticationCubit cubit) => cubit.logOut(),
verify: (_) {
verify(() => authenticationRepository.signOut()).called(1);
},
);
});
});
}
@@ -0,0 +1,43 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockUser extends Mock implements UserInterface {}
void main() {
group('AuthenticationState', () {
group('unauthenticated', () {
test('has correct status', () {
const AuthenticationState state = AuthenticationState.unauthenticated();
expect(state.status, AuthenticationStatus.unauthenticated);
expect(state.user, null);
});
});
group('authenticated', () {
test('has correct status', () {
final MockUser user = MockUser();
final AuthenticationState state =
AuthenticationState.authenticated(user, null);
expect(state.status, AuthenticationStatus.authenticated);
expect(state.user, user);
});
});
});
}
@@ -0,0 +1,232 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:bloc_test/bloc_test.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockAuthenticationRepository extends Mock
implements AuthenticationRepositoryInterface {}
class MockAuthenticationCubit extends Mock implements AuthenticationCubit {}
void main() {
const String invalidEmailString = 'invalid';
const Email invalidEmail = Email.dirty(invalidEmailString);
const String validEmailString = 'test@gmail.com';
const Email validEmail = Email.dirty(validEmailString);
const String invalidPasswordString = 'invalid';
const Password invalidPassword = Password.dirty(invalidPasswordString);
const String validPasswordString = 't0pS3cret1234';
const Password validPassword = Password.dirty(validPasswordString);
group('SignInCubit', () {
late AuthenticationRepositoryInterface authenticationRepository;
late AuthenticationCubit authenticationCubit;
setUp(() {
authenticationRepository = MockAuthenticationRepository();
authenticationCubit = MockAuthenticationCubit();
when(
() => authenticationRepository.signInWithEmailAndPassword(
email: any(named: 'email'),
password: any(named: 'password'),
),
).thenAnswer((_) async {});
when(
() => authenticationCubit.start(),
).thenReturn(true);
});
test('initial state is SignInState', () {
expect(
SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
).state,
const SignInState(),
);
});
group('emailChanged', () {
blocTest<SignInCubit, SignInState>(
'emits [invalid] when email/password are invalid',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
act: (SignInCubit cubit) => cubit.emailChanged(invalidEmailString),
expect: () => const <SignInState>[
SignInState(email: invalidEmail, status: FormStatus.invalid),
],
);
blocTest<SignInCubit, SignInState>(
'emits [valid] when email/password are valid',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
seed: () => const SignInState(password: validPassword),
act: (SignInCubit cubit) => cubit.emailChanged(validEmailString),
expect: () => const <SignInState>[
SignInState(
email: validEmail,
password: validPassword,
status: FormStatus.valid,
),
],
);
});
group('passwordChanged', () {
blocTest<SignInCubit, SignInState>(
'emits [invalid] when email/password are invalid',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
act: (SignInCubit cubit) =>
cubit.passwordChanged(invalidPasswordString),
expect: () => const <SignInState>[
SignInState(
password: invalidPassword,
status: FormStatus.invalid,
),
],
);
blocTest<SignInCubit, SignInState>(
'emits [valid] when email/password are valid',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
seed: () => const SignInState(email: validEmail),
act: (SignInCubit cubit) => cubit.passwordChanged(validPasswordString),
expect: () => const <SignInState>[
SignInState(
email: validEmail,
password: validPassword,
status: FormStatus.valid,
),
],
);
});
group('logInWithCredentials', () {
blocTest<SignInCubit, SignInState>(
'does nothing when status is not validated',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
expect: () => const <SignInState>[],
);
blocTest<SignInCubit, SignInState>(
'calls signInWithEmailAndPassword with correct email/password',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
seed: () => const SignInState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
),
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
verify: (_) {
verify(
() => authenticationRepository.signInWithEmailAndPassword(
email: validEmailString,
password: validPasswordString,
),
).called(1);
},
);
blocTest<SignInCubit, SignInState>(
'emits [submissionInProgress, submissionSuccess] '
'when signInWithEmailAndPassword succeeds',
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
seed: () => const SignInState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
),
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
expect: () => const <SignInState>[
SignInState(
status: FormStatus.submissionInProgress,
email: validEmail,
password: validPassword,
),
SignInState(
status: FormStatus.submissionSuccess,
email: validEmail,
password: validPassword,
)
],
);
blocTest<SignInCubit, SignInState>(
'emits [submissionInProgress, submissionFailure] '
'when signInWithEmailAndPassword fails',
setUp: () {
when(
() => authenticationRepository.signInWithEmailAndPassword(
email: any(named: 'email'),
password: any(named: 'password'),
),
).thenThrow(Exception('oops'));
},
build: () => SignInCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
),
seed: () => const SignInState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
),
act: (SignInCubit cubit) => cubit.signInWithEmailAndPassword(),
expect: () => const <SignInState>[
SignInState(
status: FormStatus.submissionInProgress,
email: validEmail,
password: validPassword,
),
SignInState(
status: FormStatus.submissionFailure,
email: validEmail,
password: validPassword,
)
],
);
});
});
}
@@ -0,0 +1,54 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
const Email email = Email.dirty('email');
const Password password = Password.dirty('password');
group('SignInState', () {
test('supports value comparisons', () {
expect(const SignInState(), const SignInState());
});
test('returns same object when no properties are passed', () {
expect(const SignInState().copyWith(), const SignInState());
});
test('returns object with updated status when status is passed', () {
expect(
const SignInState().copyWith(status: FormStatus.pure),
const SignInState(),
);
});
test('returns object with updated email when email is passed', () {
expect(
const SignInState().copyWith(email: email),
const SignInState(email: email),
);
});
test('returns object with updated password when password is passed', () {
expect(
const SignInState().copyWith(password: password),
const SignInState(password: password),
);
});
});
}
@@ -0,0 +1,266 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:bloc_test/bloc_test.dart';
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
class MockAuthenticationRepository extends Mock
implements AuthenticationRepositoryInterface {}
class MockAuthenticationCubit extends Mock implements AuthenticationCubit {}
void main() {
const String invalidEmailString = 'invalid';
const Email invalidEmail = Email.dirty(invalidEmailString);
const String validEmailString = 'test@gmail.com';
const Email validEmail = Email.dirty(validEmailString);
const String invalidPasswordString = 'invalid';
const Password invalidPassword = Password.dirty(invalidPasswordString);
const String validPasswordString = 't0pS3cret1234';
const Password validPassword = Password.dirty(validPasswordString);
group('SignUpCubit', () {
late AuthenticationRepositoryInterface authenticationRepository;
late AuthenticationCubit authenticationCubit;
setUp(() {
authenticationRepository = MockAuthenticationRepository();
authenticationCubit = MockAuthenticationCubit();
when(
() => authenticationRepository.signUp(
email: any(named: 'email'),
password: any(named: 'password'),
),
).thenAnswer((_) async {
return 'uid';
});
when(
() => authenticationCubit.start(),
).thenReturn(true);
when(
() => authenticationCubit.stop(),
).thenReturn(true);
});
test('initial state is SignUpState', () {
expect(
SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
).state,
SignUpState(data: FormData.empty()),
);
});
group('emailChanged', () {
blocTest<SignUpCubit, SignUpState>(
'emits [invalid] when email/password are invalid',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.emailChanged(invalidEmailString),
expect: () => <SignUpState>[
SignUpState(
email: invalidEmail,
status: FormStatus.invalid,
data: FormData.empty(),
),
],
);
blocTest<SignUpCubit, SignUpState>(
'emits [valid] when email/password are valid',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
seed: () => SignUpState(
password: validPassword,
data: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.emailChanged(validEmailString),
expect: () => <SignUpState>[
SignUpState(
email: validEmail,
password: validPassword,
status: FormStatus.valid,
data: FormData.empty(),
),
],
);
});
group('passwordChanged', () {
blocTest<SignUpCubit, SignUpState>(
'emits [invalid] when email/password are invalid',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
act: (SignUpCubit cubit) =>
cubit.passwordChanged(invalidPasswordString),
expect: () => <SignUpState>[
SignUpState(
password: invalidPassword,
status: FormStatus.invalid,
data: FormData.empty(),
),
],
);
blocTest<SignUpCubit, SignUpState>(
'emits [valid] when email/password are valid',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
seed: () => SignUpState(
email: validEmail,
data: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.passwordChanged(validPasswordString),
expect: () => <SignUpState>[
SignUpState(
email: validEmail,
password: validPassword,
status: FormStatus.valid,
data: FormData.empty(),
),
],
);
});
group('signUpFormSubmitted', () {
blocTest<SignUpCubit, SignUpState>(
'does nothing when status is not validated',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
expect: () => const <SignUpState>[],
);
blocTest<SignUpCubit, SignUpState>(
'calls signUp with correct email/password/confirmedPassword',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
seed: () => SignUpState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
data: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
verify: (_) {
verify(
() => authenticationRepository.signUp(
email: validEmailString,
password: validPasswordString,
),
).called(1);
},
);
blocTest<SignUpCubit, SignUpState>(
'emits [submissionInProgress, submissionSuccess] '
'when signUp succeeds',
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
seed: () => SignUpState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
data: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
expect: () => <SignUpState>[
SignUpState(
status: FormStatus.submissionInProgress,
email: validEmail,
password: validPassword,
data: FormData.empty(),
),
SignUpState(
status: FormStatus.submissionSuccess,
email: validEmail,
password: validPassword,
data: FormData.empty(),
)
],
);
blocTest<SignUpCubit, SignUpState>(
'emits [submissionInProgress, submissionFailure] '
'when signUp fails',
setUp: () {
when(
() => authenticationRepository.signUp(
email: any(named: 'email'),
password: any(named: 'password'),
),
).thenThrow(Exception('oops'));
},
build: () => SignUpCubit(
authenticationRepository: authenticationRepository,
authenticationCubit: authenticationCubit,
entries: FormData.empty(),
),
seed: () => SignUpState(
status: FormStatus.valid,
email: validEmail,
password: validPassword,
data: FormData.empty(),
),
act: (SignUpCubit cubit) => cubit.signUpFormSubmitted(),
expect: () => <SignUpState>[
SignUpState(
status: FormStatus.submissionInProgress,
email: validEmail,
password: validPassword,
data: FormData.empty(),
),
SignUpState(
status: FormStatus.submissionFailure,
email: validEmail,
password: validPassword,
data: FormData.empty(),
)
],
);
});
});
}
@@ -0,0 +1,112 @@
// Copyright (C) 2022 WYATT GROUP
// Please see the AUTHORS file for details.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
import 'package:wyatt_authentication_bloc/wyatt_authentication_bloc.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
const Email email = Email.dirty('email');
const String passwordString = 'password';
const Password password = Password.dirty(passwordString);
group('SignUpState', () {
test('supports value comparisons', () {
expect(
SignUpState(
data: FormData.empty(),
),
SignUpState(
data: FormData.empty(),
),
);
});
test('returns same object when no properties are passed', () {
expect(
SignUpState(
data: FormData.empty(),
).copyWith(),
SignUpState(
data: FormData.empty(),
),
);
});
test('returns object with updated status when status is passed', () {
expect(
SignUpState(
data: FormData.empty(),
).copyWith(status: FormStatus.pure),
SignUpState(
data: FormData.empty(),
),
);
});
test('returns object with updated email when email is passed', () {
expect(
SignUpState(
data: FormData.empty(),
).copyWith(email: email),
SignUpState(
email: email,
data: FormData.empty(),
),
);
});
test('returns object with updated password when password is passed', () {
expect(
SignUpState(
data: FormData.empty(),
).copyWith(password: password),
SignUpState(
password: password,
data: FormData.empty(),
),
);
});
test(
'returns object with updated data'
' when data is passed', () {
expect(
SignUpState(
data: FormData.empty(),
).copyWith(
data: const FormData(
[
FormEntry(
'field',
Name.pure(),
),
],
),
),
const SignUpState(
data: FormData(
[
FormEntry(
'field',
Name.pure(),
),
],
),
),
);
});
});
}