Skip to content

Commit

Permalink
Merge pull request #130 from Flajt/129-detect-mock-locations
Browse files Browse the repository at this point in the history
129 detect mock locations
  • Loading branch information
Flajt authored Mar 19, 2024
2 parents ec094b9 + bc3fd96 commit da5e090
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/features/metadata/logic/LocationServiceWrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class LocationServiceWrapper implements ILocationService {
Future<LocationModel> requestLocation() async {
Position locationData = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best);
if (locationData.isMocked) {
throw Exception(
'Invalid Location: It seems like you are using a mock location service. Please disable it and try again.');
}
return LocationModel(
latitude: locationData.latitude, longitude: locationData.longitude);
}
Expand Down
53 changes: 53 additions & 0 deletions test/hashing/bloc/PreparationBloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import 'package:test/test.dart';

import '../../mocks.mocks.dart';

/// The whole suit is kinda a mess. Since it's hard to get a good stream of events, the worst thing is that I've got to simulate the stream of events.
/// Idealy this wouldn't be the case and we could just hook into a real stream, and do the testing that way. Any ideas are appreciated.
void main() {
setUp(() async {
await GetIt.I.reset();
Expand Down Expand Up @@ -588,6 +590,57 @@ void main() {
PrepareationIsAddingMetaData(),
PreparationHasError("Location Service is not enabled!")
]);
blocTest("w. error due to mock location",
setUp: () {
register(
videoSavingService,
imageSavingService,
imageHashingService,
videoHashingService,
videoWaterMarkSerivce,
imageWaterMarkService,
audioMetaDataService,
videoMetaDataService,
imageMetaDataService,
audioHashingService,
locationService,
metaDataPermissionService,
foregroundServiceWrapper);
final testPort = ReceivePort.fromRawReceivePort(RawReceivePort());
final sendPort = testPort.sendPort;
when(foregroundServiceWrapper.getReceivePort())
.thenAnswer((_) => Future.value(testPort));
sendPort.send({
"status": "AddingWaterMark"
}); // Since stuff is returned from a stream it is in the currect order
sendPort.send({"status": "AddingMetaData"});
sendPort.send({"status": "Hashing", "progress": 0});
sendPort.send({
"status": "Fail",
"description":
"Invalid Location: It seems like you are using a mock location service. Please disable it and try again.",
});
when(imageSavingService.saveFile())
.thenAnswer((_) => Future.value("some/path/to/image.png"));
when(imageWaterMarkService.addWaterMark(any))
.thenAnswer((_) => Future.value("sample/path/to/image.png"));
when(metaDataPermissionService.shouldEmbedLocation())
.thenReturn(true);
when(locationService.requestLocation()).thenThrow((_) =>
"Invalid Location: It seems like you are using a mock location service. Please disable it and try again.");
when(locationService.serviceEnabled())
.thenAnswer((realInvocation) => Future.value(true));
},
act: (bloc) => bloc.add(PrepareImage()),
build: () => PreparationBloc(),
wait: const Duration(milliseconds: 100),
expect: () => [
PrepareationIsAplyingWaterMark(),
PrepareationIsAddingMetaData(),
PrepareationIsHashing(),
PreparationHasError(
"Invalid Location: It seems like you are using a mock location service. Please disable it and try again.")
]);
});
});
}
Expand Down

0 comments on commit da5e090

Please sign in to comment.