Skip to content

Commit

Permalink
feat: Adding image repository tests. #275
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Oct 9, 2023
1 parent 46ec091 commit 6439db5
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 6 deletions.
14 changes: 12 additions & 2 deletions lib/data/providers/image_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import 'package:http/http.dart' as http;
import 'package:http_parser/http_parser.dart';
import 'package:mime/mime.dart';

const imageEndpoint = 'http://localhost:4000/api/images';
const imageEndpoint = 'https://imgup.fly.dev/api/images';

/// Image data provider.
/// Meant to be invoked by the domain layer (repositories).
///
///
/// It will call the appropriate URL (in this case, it's `imgup`) to mainly upload images.
class ImageProvider {
/// HTTP client used to make requests.
Expand All @@ -20,8 +20,18 @@ class ImageProvider {

/// Uploads an image (as an array of [bytes]) to https://imgup.fly.dev/ and returns the appropriate response.
Future<Either<RequestError, String>> uploadImage(Uint8List bytes, String filename) async {
// Check if byte array is empty
if (bytes.isEmpty) {
return Left(RequestError(code: 404, description: 'Empty image. Make sure to select an image with content.'));
}

final request = http.MultipartRequest('POST', Uri.parse(imageEndpoint));

final mimeType = lookupMimeType('', headerBytes: bytes);
if(mimeType == null) {
return Left(RequestError(code: 404, description: 'Invalid mimetype.'));
}

final httpImage =
http.MultipartFile.fromBytes('image', bytes, contentType: MediaType.parse(lookupMimeType('', headerBytes: bytes)!), filename: filename);
request.files.add(httpImage);
Expand Down
9 changes: 5 additions & 4 deletions lib/presentation/widgets/editor/image_callbacks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import 'dart:async';
import 'dart:io';

import 'package:dwyl_app/data/repositories/repositories.dart';
import 'package:dwyl_app/logging/logging.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter_quill_extensions/flutter_quill_extensions.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';

/// The URL endpoint in which the images will be uploaded and hosted.
const apiEndpointURL = 'http://localhost:4000/api/images';

/// Receives a file [file], copies it to the app's documents directory and returns the path of the copied file.
Future<String> onImagePickCallback(File file) async {
final appDocDir = await getApplicationDocumentsDirectory();
Expand Down Expand Up @@ -40,7 +38,10 @@ Future<String?> webImagePickImpl(ImageRepository imageRepository, ImageFilePicke

final uploadRet = await imageRepository.uploadImage(bytes, platformFile.name);
return uploadRet.fold(
(error) => null,
(error) {
logError('Error uploading image: \n${error.toString()}');
return null;
},
(r) => r,
);
}
Expand Down
40 changes: 40 additions & 0 deletions test/unit/data/image_repository_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:typed_data';

import 'package:dwyl_app/core/data_layer.dart';
import 'package:dwyl_app/data/repositories/image/image_repository.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart' as http;

void main() {
test('ImageRepository should yield results on a successful request (these make a REAL request)', () async {
final imageRepository = ImgupRepository(client: http.Client());

// Byte array from number byte array https://gist.github.com/leommoore/f9e57ba2aa4bf197ebc5.
final bytes = Uint8List.fromList([0xff, 0xd8, 0xff, 0xe0]);

// Should return an URL, not an error.
final ret = await imageRepository.uploadImage(bytes, "cool_birthday");
expect(ret.isRight(), true);
});

test('ImageRepository should yield an error because bytes array is empty.', () async {
final imageRepository = ImgupRepository(client: http.Client());

final bytes = Uint8List.fromList([]);

// Should error out
final ret = await imageRepository.uploadImage(bytes, "cool_birthday");
expect(ret.isLeft(), true);
});

test('ImageRepository should yield an error because bytes array has invalid mimetype', () async {
final imageRepository = ImgupRepository(client: http.Client());

// Invalid byte array (a simple [0] does not have any mime type)
final bytes = Uint8List.fromList([0]);

// Should error out
final ret = await imageRepository.uploadImage(bytes, "cool_birthday");
expect(ret.isLeft(), true);
});
}

0 comments on commit 6439db5

Please sign in to comment.