Skip to content

Commit

Permalink
Merge branch 'main' into feat/answer-word
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgalarraga committed Apr 16, 2024
2 parents c986809 + 4bf27b6 commit 6cb33e7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
7 changes: 7 additions & 0 deletions api/routes/game/create_score.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ Future<Response> _onPost(RequestContext context) async {
return Response(statusCode: HttpStatus.badRequest);
}

final unwantedInitials = await leaderboardRepository.getInitialsBlacklist();

if (unwantedInitials.contains(initials.toUpperCase()) ||
initials.length != 3) {
return Response(statusCode: HttpStatus.badRequest);
}

try {
await leaderboardRepository.createScore(user.id, initials, mascot);
} catch (e, s) {
Expand Down
55 changes: 55 additions & 0 deletions api/test/routes/game/create_score_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,65 @@ void main() {
.thenAnswer((_) async => {'initials': 'AAA', 'mascot': 'dash'});
when(() => leaderboardRepository.createScore(any(), any(), any()))
.thenAnswer((_) async {});
when(() => leaderboardRepository.getInitialsBlacklist())
.thenAnswer((_) async => ['FUU', 'FAA']);

final response = await route.onRequest(context);
expect(response.statusCode, equals(HttpStatus.created));
},
);

test(
'responds with a ${HttpStatus.badRequest} status when initials are '
'unwanted',
() async {
when(() => request.method).thenReturn(HttpMethod.post);
when(request.json)
.thenAnswer((_) async => {'initials': 'FUU', 'mascot': 'dash'});
when(() => leaderboardRepository.createScore(any(), any(), any()))
.thenAnswer((_) async {});
when(() => leaderboardRepository.getInitialsBlacklist())
.thenAnswer((_) async => ['FUU', 'FAA']);

final response = await route.onRequest(context);
expect(response.statusCode, equals(HttpStatus.badRequest));
},
);

test(
'responds with a ${HttpStatus.badRequest} status when initials are '
'smaller than 3',
() async {
when(() => request.method).thenReturn(HttpMethod.post);
when(request.json)
.thenAnswer((_) async => {'initials': 'AA', 'mascot': 'dash'});
when(() => leaderboardRepository.createScore(any(), any(), any()))
.thenAnswer((_) async {});
when(() => leaderboardRepository.getInitialsBlacklist())
.thenAnswer((_) async => ['FUU', 'FAA']);

final response = await route.onRequest(context);
expect(response.statusCode, equals(HttpStatus.badRequest));
},
);

test(
'responds with a ${HttpStatus.badRequest} status when initials are '
'bigger than 3',
() async {
when(() => request.method).thenReturn(HttpMethod.post);
when(request.json)
.thenAnswer((_) async => {'initials': 'AAAA', 'mascot': 'dash'});
when(() => leaderboardRepository.createScore(any(), any(), any()))
.thenAnswer((_) async {});
when(() => leaderboardRepository.getInitialsBlacklist())
.thenAnswer((_) async => ['FUU', 'FAA']);

final response = await route.onRequest(context);
expect(response.statusCode, equals(HttpStatus.badRequest));
},
);

test(
'responds with a HttpStatus.badRequest status when the initials '
'or mascot parameters are missing',
Expand All @@ -76,6 +129,8 @@ void main() {
.thenAnswer((_) async => {'initials': 'AAA', 'mascot': 'dash'});
when(() => leaderboardRepository.createScore(any(), any(), any()))
.thenThrow(Exception());
when(() => leaderboardRepository.getInitialsBlacklist())
.thenAnswer((_) async => ['FUU', 'FAA']);

final response = route.onRequest(context);
expect(response, throwsA(isA<Exception>()));
Expand Down

0 comments on commit 6cb33e7

Please sign in to comment.