-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Infrastructure Tests
- Loading branch information
Showing
4 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
190 changes: 190 additions & 0 deletions
190
test/infrastructure/auth/firebase_auth_repository_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,190 @@ | ||
import 'package:collaction_app/domain/auth/auth_failures.dart'; | ||
import 'package:collaction_app/domain/auth/auth_success.dart'; | ||
import 'package:collaction_app/domain/user/i_user_repository.dart'; | ||
import 'package:collaction_app/domain/auth/i_auth_repository.dart'; | ||
import 'package:collaction_app/domain/user/user.dart'; | ||
|
||
import 'package:collaction_app/infrastructure/auth/firebase_auth_repository.dart'; | ||
|
||
import 'package:firebase_auth/firebase_auth.dart' as firebase_auth; | ||
import 'package:firebase_auth_mocks/firebase_auth_mocks.dart'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
|
||
class CustomMockFirebaseAuth extends Mock | ||
implements firebase_auth.FirebaseAuth {} | ||
|
||
class FirebaseAuthSetup { | ||
late MockUser mockFirebaseUser; | ||
late firebase_auth.FirebaseAuth mockFirebaseAuth; | ||
FirebaseAuthSetup({bool signedIn = true}) { | ||
mockFirebaseUser = MockUser(); | ||
mockFirebaseAuth = signedIn | ||
? MockFirebaseAuth(signedIn: true, mockUser: mockFirebaseUser) | ||
: MockFirebaseAuth(); | ||
} | ||
} | ||
|
||
class CustomFirebaseAuthSetup { | ||
late firebase_auth.FirebaseAuth mockFirebaseAuth; | ||
late When mockVerifyPhoneNumber; | ||
CustomFirebaseAuthSetup() { | ||
mockFirebaseAuth = CustomMockFirebaseAuth(); | ||
|
||
when(() => mockFirebaseAuth.authStateChanges()) | ||
.thenAnswer((_) => const Stream.empty()); | ||
|
||
mockVerifyPhoneNumber = when(() => mockFirebaseAuth.verifyPhoneNumber( | ||
phoneNumber: any(named: 'phoneNumber'), | ||
verificationCompleted: any(named: 'verificationCompleted'), | ||
verificationFailed: any(named: 'verificationFailed'), | ||
codeSent: any(named: 'codeSent'), | ||
codeAutoRetrievalTimeout: any(named: 'codeAutoRetrievalTimeout'))); | ||
} | ||
} | ||
|
||
void main() { | ||
group('testing getSignedInUser: ', () { | ||
test("signed in user exists", () async { | ||
// mock | ||
FirebaseAuthSetup mocks = FirebaseAuthSetup(signedIn: true); | ||
|
||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
var result = await firebaseAuthRepository.getSignedInUser(); | ||
var user = result.getOrElse(() => User.anonymous); | ||
|
||
// verify | ||
expect(user.id, equals(mocks.mockFirebaseUser.uid)); | ||
}); | ||
|
||
test("no signed in user", () async { | ||
// mock | ||
FirebaseAuthSetup mocks = FirebaseAuthSetup(signedIn: false); | ||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
var result = await firebaseAuthRepository.getSignedInUser(); | ||
var user = result.getOrElse(() => User.anonymous); | ||
|
||
// verify | ||
expect(user, equals(User.anonymous)); | ||
}); | ||
}); | ||
|
||
test("testing signOut -- remove user reference", () async { | ||
// mock | ||
FirebaseAuthSetup mocks = FirebaseAuthSetup(signedIn: true); | ||
firebase_auth.FirebaseAuth mockFirebaseAuth = mocks.mockFirebaseAuth; | ||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mockFirebaseAuth); | ||
|
||
// perform test | ||
expect(mockFirebaseAuth.currentUser, equals(mocks.mockFirebaseUser)); | ||
await firebaseAuthRepository.signOut(); | ||
|
||
// verify | ||
expect(mockFirebaseAuth.currentUser, isNull); | ||
}); | ||
|
||
group('testing verifyPhone function: ', () { | ||
test('codeSent callback', () async { | ||
// mock | ||
CustomFirebaseAuthSetup mocks = CustomFirebaseAuthSetup(); | ||
mocks.mockVerifyPhoneNumber.thenAnswer((invocation) async { | ||
Function codeSent = invocation.namedArguments[Symbol('codeSent')]; | ||
await codeSent("verify id", 123); | ||
}); | ||
|
||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
Stream result = firebaseAuthRepository.verifyPhone(phoneNumber: ''); | ||
|
||
// verify | ||
result.listen(expectAsync1((value) { | ||
Credential cred = (value.value as AuthSuccess).credential; | ||
Credential expected = | ||
Credential(verificationId: "verify id", forceResendToken: 123); | ||
expect(cred == expected, true); | ||
}, count: 1)); | ||
}); | ||
|
||
test('verificationFailed callback', () async { | ||
// mock | ||
CustomFirebaseAuthSetup mocks = CustomFirebaseAuthSetup(); | ||
mocks.mockVerifyPhoneNumber.thenAnswer((invocation) async { | ||
Function verificationFailed = | ||
invocation.namedArguments[Symbol('verificationFailed')]; | ||
await verificationFailed( | ||
firebase_auth.FirebaseAuthException(code: 'unknown-server-error')); | ||
}); | ||
|
||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
Stream result = firebaseAuthRepository.verifyPhone(phoneNumber: ''); | ||
|
||
// verify | ||
result.listen(expectAsync1((value) { | ||
AuthFailure failure = value.value; | ||
expect(failure == ServerError(), true); | ||
}, count: 1)); | ||
}); | ||
|
||
test('codeAutoRetrievalTimeout callback', () async { | ||
// mock | ||
CustomFirebaseAuthSetup mocks = CustomFirebaseAuthSetup(); | ||
mocks.mockVerifyPhoneNumber.thenAnswer((invocation) async { | ||
Function codeAutoRetrievalTimeout = | ||
invocation.namedArguments[Symbol('codeAutoRetrievalTimeout')]; | ||
await codeAutoRetrievalTimeout("verify id"); | ||
}); | ||
|
||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
Stream result = firebaseAuthRepository.verifyPhone(phoneNumber: ''); | ||
|
||
// verify | ||
result.listen(expectAsync1((value) { | ||
Credential cred = (value.value as AuthSuccess).credential; | ||
Credential expected = Credential(verificationId: "verify id"); | ||
expect(cred == expected, true); | ||
}, count: 1)); | ||
}); | ||
|
||
test('verificationCompleted callback', () async { | ||
// mock | ||
CustomFirebaseAuthSetup mocks = CustomFirebaseAuthSetup(); | ||
mocks.mockVerifyPhoneNumber.thenAnswer((invocation) async { | ||
Function verificationCompleted = | ||
invocation.namedArguments[Symbol('verificationCompleted')]; | ||
|
||
await verificationCompleted(firebase_auth.PhoneAuthProvider.credential( | ||
verificationId: 'verify id', smsCode: 'code')); | ||
}); | ||
|
||
IAuthRepository firebaseAuthRepository = | ||
FirebaseAuthRepository(firebaseAuth: mocks.mockFirebaseAuth); | ||
|
||
// perform test | ||
Stream result = firebaseAuthRepository.verifyPhone(phoneNumber: ''); | ||
|
||
// verify | ||
result.listen(expectAsync1((value) { | ||
Credential cred = (value.value as AuthSuccess).credential; | ||
Credential expected = | ||
Credential(verificationId: "verify id", smsCode: 'code'); | ||
expect(cred == expected, true); | ||
}, count: 1)); | ||
}); | ||
}); | ||
} |
4 changes: 4 additions & 0 deletions
4
test/infrastructure/contact_form/contact_form_dto_fixtures.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,4 @@ | ||
import 'package:collaction_app/infrastructure/contact_form/contact_form_dto.dart'; | ||
|
||
final tContactFormDto = ContactFormDto( | ||
email: '[email protected]', message: "message", subject: 'subject'); |
92 changes: 92 additions & 0 deletions
92
test/infrastructure/contact_form/contact_form_repository_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,92 @@ | ||
import 'dart:convert'; | ||
import 'package:collaction_app/domain/contact_form/i_contact_form_repository.dart'; | ||
import 'package:collaction_app/domain/core/i_settings_repository.dart'; | ||
import 'package:collaction_app/domain/contact_form/contact_failures.dart'; | ||
import 'package:http/http.dart' as http; | ||
|
||
import 'package:collaction_app/infrastructure/contact_form/contact_form_repository.dart'; | ||
import './contact_form_dto_fixtures.dart'; | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:mocktail/mocktail.dart'; | ||
import '../../test_utilities.dart'; | ||
|
||
const baseUrl = 'https://example.com'; | ||
void main() { | ||
late http.Client client; | ||
late ISettingsRepository settingsRepository; | ||
late IContactRepository contactRepository; | ||
|
||
setUp(() { | ||
client = MockHttpClient(); | ||
settingsRepository = MockSettingsRepository(); | ||
contactRepository = ContactRepository(client, settingsRepository); | ||
|
||
when(() => settingsRepository.baseApiEndpointUrl) | ||
.thenAnswer((_) async => baseUrl); | ||
}); | ||
|
||
group('create contact: ', () { | ||
final contactPostUrl = Uri.parse('$baseUrl/v1/contact'); | ||
final contactPostHeader = { | ||
'accept': '*/*', | ||
'Content-Type': 'application/json', | ||
}; | ||
String expectedBody = json.encode({ | ||
'email': tContactFormDto.email, | ||
'title': tContactFormDto.subject, | ||
'body': tContactFormDto.message | ||
}); | ||
|
||
test("successful api call", () async { | ||
// mock data | ||
when(() => client.post(contactPostUrl, | ||
body: expectedBody, | ||
headers: contactPostHeader)).thenAnswer((_) async { | ||
return http.Response('', 201); | ||
}); | ||
|
||
// perform test | ||
final result = | ||
await contactRepository.sendContactFormContents(tContactFormDto); | ||
|
||
// verify | ||
verify(() => client.post(contactPostUrl, | ||
body: expectedBody, headers: contactPostHeader)).called(1); | ||
expect(result.isRight(), equals(true)); | ||
}); | ||
test("fail response from call", () async { | ||
// mock data | ||
when(() => client.post(contactPostUrl, | ||
body: expectedBody, | ||
headers: contactPostHeader)).thenAnswer((_) async { | ||
return http.Response('', 500); | ||
}); | ||
|
||
// perform test | ||
final result = | ||
await contactRepository.sendContactFormContents(tContactFormDto); | ||
|
||
// verify | ||
verify(() => client.post(contactPostUrl, | ||
body: expectedBody, headers: contactPostHeader)).called(1); | ||
expect(result.isLeft(), equals(true)); | ||
expect(result.fold((l) => l, (r) => r), ContactFailure.serverError()); | ||
}); | ||
test("anonymous failure", () async { | ||
// mock data | ||
when(() => client.post(contactPostUrl, | ||
body: expectedBody, headers: contactPostHeader)).thenThrow("error"); | ||
|
||
// perform test | ||
final result = | ||
await contactRepository.sendContactFormContents(tContactFormDto); | ||
|
||
// verify | ||
verify(() => client.post(contactPostUrl, | ||
body: expectedBody, headers: contactPostHeader)).called(1); | ||
expect(result.isLeft(), equals(true)); | ||
expect(result.fold((l) => l, (r) => r), ContactFailure.unexpectedError()); | ||
}); | ||
}); | ||
} |
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