Skip to content

Commit

Permalink
Merge pull request #348 from CollActionteam/tests/gh-225/infra-tests
Browse files Browse the repository at this point in the history
Infrastructure Tests
  • Loading branch information
eStarKee06 authored Feb 6, 2023
2 parents fa94247 + 1a0acbf commit 13a834c
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 0 deletions.
190 changes: 190 additions & 0 deletions test/infrastructure/auth/firebase_auth_repository_test.dart
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));
});
});
}
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 test/infrastructure/contact_form/contact_form_repository_test.dart
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());
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:collaction_app/application/crowdaction/crowdaction_details/crowd
import 'package:collaction_app/application/crowdaction/spotlight/spotlight_bloc.dart';
import 'package:collaction_app/application/participation/top_participants/top_participants_bloc.dart';
import 'package:collaction_app/domain/crowdaction/crowdaction_failures.dart';
import 'package:collaction_app/presentation/crowdaction/crowdaction_home/widgets/spotlight_crowdactions/spotlight_crowdactions.dart';
import 'package:collaction_app/presentation/crowdaction/crowdaction_home/widgets/in_spotlight_header.dart';
import 'package:collaction_app/presentation/crowdaction/crowdaction_home/widgets/spotlight_crowdactions/spotlight_crowdactions.dart';
import 'package:collaction_app/presentation/shared_widgets/content_placeholder.dart';
Expand Down

0 comments on commit 13a834c

Please sign in to comment.