Skip to content

Commit

Permalink
PR suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
erickzanardo committed Mar 13, 2024
1 parent 92715df commit 997ff00
Showing 1 changed file with 147 additions and 166 deletions.
313 changes: 147 additions & 166 deletions api/test/routes/board/sections/[sectionId]_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class _MockBoardRenderer extends Mock implements BoardRenderer {}
class _MockFirebaseCloudStorage extends Mock implements FirebaseCloudStorage {}

void main() {
group('POST /board/sections/[sectionId]', () {
group('/board/sections/[sectionId]', () {
late RequestContext requestContext;
late Request request;
late CrosswordRepository crosswordRepository;
Expand All @@ -49,211 +49,192 @@ void main() {
boardRenderer = _MockBoardRenderer();
firebaseCloudStorage = _MockFirebaseCloudStorage();

when(() => request.method).thenReturn(HttpMethod.post);
when(() => requestContext.request).thenReturn(request);
when(() => requestContext.read<BoardRenderer>())
.thenReturn(boardRenderer);
when(() => requestContext.request).thenReturn(request);
when(() => requestContext.read<CrosswordRepository>())
.thenReturn(crosswordRepository);
when(() => requestContext.read<FirebaseCloudStorage>()).thenReturn(
firebaseCloudStorage,
);
});

test('uploads the image and return its url', () async {
final section = BoardSection(
id: '1',
position: const Point(1, 1),
size: 100,
words: [
Word(
position: const Point(1, 1),
axis: Axis.vertical,
answer: 'flutter',
clue: '',
hints: const [],
solvedTimestamp: null,
),
],
borderWords: const [],
);
const allowedMethods = [HttpMethod.get, HttpMethod.post];
final notAllowedMethods = HttpMethod.values.where(
(e) => !allowedMethods.contains(e),
);
for (final method in notAllowedMethods) {
test('returns methods not allowed when method is $method', () async {
when(() => request.method).thenReturn(method);
final response = await route.onRequest(requestContext, '0,0');

when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => section,
);

when(
() => boardRenderer.renderSection(section),
).thenAnswer((_) async {
return Uint8List(0);
expect(response.statusCode, HttpStatus.methodNotAllowed);
});
}

when(
() => firebaseCloudStorage.uploadFile(
any(),
any(),
),
).thenAnswer((_) async {
return 'https://example.com/image.png';
group('POST /board/sections/[sectionId]', () {
setUp(() {
when(() => request.method).thenReturn(HttpMethod.post);
});

when(
() => crosswordRepository.updateSection(any()),
).thenAnswer((_) async {});
test('uploads the image and return its url', () async {
final section = BoardSection(
id: '1',
position: const Point(1, 1),
size: 100,
words: [
Word(
position: const Point(1, 1),
axis: Axis.vertical,
answer: 'flutter',
clue: '',
hints: const [],
solvedTimestamp: null,
),
],
borderWords: const [],
);

when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => section,
);

when(
() => boardRenderer.renderSection(section),
).thenAnswer((_) async {
return Uint8List(0);
});

when(
() => firebaseCloudStorage.uploadFile(
any(),
any(),
),
).thenAnswer((_) async {
return 'https://example.com/image.png';
});

final response = await route.onRequest(requestContext, '0,0');
when(
() => crosswordRepository.updateSection(any()),
).thenAnswer((_) async {});

expect(response.statusCode, HttpStatus.ok);
expect(await response.json(), {'url': 'https://example.com/image.png'});
final response = await route.onRequest(requestContext, '0,0');

verify(
() => crosswordRepository.updateSection(
section.copyWith(snapshotUrl: 'https://example.com/image.png'),
),
).called(1);
});

test('returns 404 when the board is not found', () async {
when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => null,
);
expect(response.statusCode, HttpStatus.ok);
expect(await response.json(), {'url': 'https://example.com/image.png'});

when(
() => boardRenderer.renderSection(any()),
).thenAnswer((_) async {
return Uint8List(0);
verify(
() => crosswordRepository.updateSection(
section.copyWith(snapshotUrl: 'https://example.com/image.png'),
),
).called(1);
});

final response = await route.onRequest(requestContext, '0,0');

expect(response.statusCode, HttpStatus.notFound);
});

test('returns method not allowed when not a get or post method', () async {
when(() => request.method).thenReturn(HttpMethod.put);
final response = await route.onRequest(requestContext, '0,0');

expect(response.statusCode, HttpStatus.methodNotAllowed);
});

test('returns bad request when the id is invalid', () async {
when(() => request.method).thenReturn(HttpMethod.post);
final response = await route.onRequest(requestContext, 'a,0');
test('returns 404 when the board is not found', () async {
when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => null,
);

expect(response.statusCode, HttpStatus.badRequest);
});
when(
() => boardRenderer.renderSection(any()),
).thenAnswer((_) async {
return Uint8List(0);
});

test('returns bad request when the id is incomplete', () async {
when(() => request.method).thenReturn(HttpMethod.post);
final response = await route.onRequest(requestContext, '0');
final response = await route.onRequest(requestContext, '0,0');

expect(response.statusCode, HttpStatus.badRequest);
});
});

group('GET /board/sections/[sectionId]', () {
late RequestContext requestContext;
late Request request;
late CrosswordRepository crosswordRepository;
late BoardRenderer boardRenderer;

setUpAll(() {
registerFallbackValue(
const BoardSection(
id: '',
position: Point(0, 0),
size: 10,
words: [],
borderWords: [],
),
);
registerFallbackValue(Uint8List(0));
});

setUp(() {
requestContext = _MockRequestContext();
request = _MockRequest();
crosswordRepository = _MockCrosswordRepository();
boardRenderer = _MockBoardRenderer();

when(() => request.method).thenReturn(HttpMethod.get);
when(() => requestContext.read<BoardRenderer>())
.thenReturn(boardRenderer);
when(() => requestContext.request).thenReturn(request);
when(() => requestContext.read<CrosswordRepository>())
.thenReturn(crosswordRepository);
});

test('the image', () async {
final section = BoardSection(
id: '1',
position: const Point(1, 1),
size: 100,
words: [
Word(
position: const Point(1, 1),
axis: Axis.vertical,
answer: 'flutter',
clue: '',
hints: const [],
solvedTimestamp: null,
),
],
borderWords: const [],
snapshotUrl: 'https://example.com/image.png',
);
expect(response.statusCode, HttpStatus.notFound);
});

when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => section,
);
test('returns bad request when the id is invalid', () async {
when(() => request.method).thenReturn(HttpMethod.post);
final response = await route.onRequest(requestContext, 'a,0');

when(
() => boardRenderer.renderSection(section),
).thenAnswer((_) async {
return Uint8List(0);
expect(response.statusCode, HttpStatus.badRequest);
});

final response = await route.onRequest(requestContext, '0,0');
test('returns bad request when the id is incomplete', () async {
when(() => request.method).thenReturn(HttpMethod.post);
final response = await route.onRequest(requestContext, '0');

expect(response.statusCode, HttpStatus.movedPermanently);
expect(response.headers['Location'], 'https://example.com/image.png');
expect(response.statusCode, HttpStatus.badRequest);
});
});

test('returns 404 when the board is not found', () async {
when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => null,
);
group('GET', () {
setUp(() {
when(() => request.method).thenReturn(HttpMethod.get);
when(() => requestContext.read<BoardRenderer>())
.thenReturn(boardRenderer);
when(() => requestContext.request).thenReturn(request);
when(() => requestContext.read<CrosswordRepository>())
.thenReturn(crosswordRepository);
});

when(
() => boardRenderer.renderSection(any()),
).thenAnswer((_) async {
return Uint8List(0);
test('the image', () async {
final section = BoardSection(
id: '1',
position: const Point(1, 1),
size: 100,
words: [
Word(
position: const Point(1, 1),
axis: Axis.vertical,
answer: 'flutter',
clue: '',
hints: const [],
solvedTimestamp: null,
),
],
borderWords: const [],
snapshotUrl: 'https://example.com/image.png',
);

when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => section,
);

when(
() => boardRenderer.renderSection(section),
).thenAnswer((_) async {
return Uint8List(0);
});

final response = await route.onRequest(requestContext, '0,0');

expect(response.statusCode, HttpStatus.movedPermanently);
expect(response.headers['Location'], 'https://example.com/image.png');
});

final response = await route.onRequest(requestContext, '0,0');
test('returns 404 when the board is not found', () async {
when(() => crosswordRepository.findSectionByPosition(0, 0)).thenAnswer(
(_) async => null,
);

expect(response.statusCode, HttpStatus.notFound);
});
when(
() => boardRenderer.renderSection(any()),
).thenAnswer((_) async {
return Uint8List(0);
});

test('returns method not allowed when not a get or post method', () async {
when(() => request.method).thenReturn(HttpMethod.put);
final response = await route.onRequest(requestContext, '0,0');
final response = await route.onRequest(requestContext, '0,0');

expect(response.statusCode, HttpStatus.methodNotAllowed);
});
expect(response.statusCode, HttpStatus.notFound);
});

test('returns bad request when the id is invalid', () async {
when(() => request.method).thenReturn(HttpMethod.get);
final response = await route.onRequest(requestContext, 'a,0');
test('returns bad request when the id is invalid', () async {
when(() => request.method).thenReturn(HttpMethod.get);
final response = await route.onRequest(requestContext, 'a,0');

expect(response.statusCode, HttpStatus.badRequest);
});
expect(response.statusCode, HttpStatus.badRequest);
});

test('returns bad request when the id is incomplete', () async {
when(() => request.method).thenReturn(HttpMethod.get);
final response = await route.onRequest(requestContext, '0');
test('returns bad request when the id is incomplete', () async {
when(() => request.method).thenReturn(HttpMethod.get);
final response = await route.onRequest(requestContext, '0');

expect(response.statusCode, HttpStatus.badRequest);
expect(response.statusCode, HttpStatus.badRequest);
});
});
});
}

0 comments on commit 997ff00

Please sign in to comment.