-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/board-full-render
- Loading branch information
Showing
8 changed files
with
314 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: deploy_api_prod | ||
name: deploy_api_staging | ||
|
||
on: workflow_dispatch | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
name: deploy_app_dev | ||
name: deploy_app_staging | ||
|
||
on: workflow_dispatch | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
packages/api_client/lib/src/resources/leaderboard_resource.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:api_client/api_client.dart'; | ||
import 'package:game_domain/game_domain.dart'; | ||
|
||
/// {@template leaderboard_resource} | ||
/// An api resource for interacting with the leaderboard. | ||
/// {@endtemplate} | ||
class LeaderboardResource { | ||
/// {@macro leaderboard_resource} | ||
LeaderboardResource({ | ||
required ApiClient apiClient, | ||
}) : _apiClient = apiClient; | ||
|
||
final ApiClient _apiClient; | ||
|
||
/// Get /game/leaderboard/results | ||
/// | ||
/// Returns a list of [LeaderboardPlayer]. | ||
Future<List<LeaderboardPlayer>> getLeaderboardResults() async { | ||
final response = await _apiClient.get('/game/leaderboard/results'); | ||
|
||
if (response.statusCode != HttpStatus.ok) { | ||
throw ApiClientError( | ||
'GET /leaderboard/results returned status ${response.statusCode} ' | ||
'with the following response: "${response.body}"', | ||
StackTrace.current, | ||
); | ||
} | ||
|
||
try { | ||
final json = jsonDecode(response.body) as Map<String, dynamic>; | ||
final leaderboardPlayers = json['leaderboardPlayers'] as List; | ||
|
||
return leaderboardPlayers | ||
.map( | ||
(json) => LeaderboardPlayer.fromJson(json as Map<String, dynamic>), | ||
) | ||
.toList(); | ||
} catch (error, stackTrace) { | ||
throw ApiClientError( | ||
'GET /leaderboard/results returned invalid response "${response.body}"', | ||
stackTrace, | ||
); | ||
} | ||
} | ||
|
||
/// Get /game/leaderboard/initials_blacklist | ||
/// | ||
/// Returns a [List<String>]. | ||
Future<List<String>> getInitialsBlacklist() async { | ||
final response = | ||
await _apiClient.get('/game/leaderboard/initials_blacklist'); | ||
|
||
if (response.statusCode == HttpStatus.notFound) { | ||
return []; | ||
} | ||
|
||
if (response.statusCode != HttpStatus.ok) { | ||
throw ApiClientError( | ||
'GET /leaderboard/initials_blacklist returned status ' | ||
'${response.statusCode} with the following response: ' | ||
'"${response.body}"', | ||
StackTrace.current, | ||
); | ||
} | ||
|
||
try { | ||
final json = jsonDecode(response.body) as Map<String, dynamic>; | ||
return (json['list'] as List).cast<String>(); | ||
} catch (error, stackTrace) { | ||
throw ApiClientError( | ||
'GET /leaderboard/initials_blacklist ' | ||
'returned invalid response "${response.body}"', | ||
stackTrace, | ||
); | ||
} | ||
} | ||
|
||
/// Post /game/leaderboard/initials | ||
Future<void> addLeaderboardPlayer({ | ||
required LeaderboardPlayer leaderboardPlayer, | ||
}) async { | ||
final response = await _apiClient.post( | ||
'/game/leaderboard/initials', | ||
body: jsonEncode(leaderboardPlayer.toJson()), | ||
); | ||
|
||
if (response.statusCode != HttpStatus.noContent) { | ||
throw ApiClientError( | ||
'POST /leaderboard/initials returned status ${response.statusCode} ' | ||
'with the following response: "${response.body}"', | ||
StackTrace.current, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
204 changes: 204 additions & 0 deletions
204
packages/api_client/test/src/resources/leaderboard_resource_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
// ignore_for_file: prefer_const_constructors | ||
|
||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:api_client/api_client.dart'; | ||
import 'package:api_client/src/resources/leaderboard_resource.dart'; | ||
import 'package:game_domain/game_domain.dart'; | ||
import 'package:http/http.dart' as http; | ||
import 'package:mocktail/mocktail.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
class _MockApiClient extends Mock implements ApiClient {} | ||
|
||
class _MockResponse extends Mock implements http.Response {} | ||
|
||
void main() { | ||
group('LeaderboardResource', () { | ||
late ApiClient apiClient; | ||
late http.Response response; | ||
late LeaderboardResource resource; | ||
|
||
setUp(() { | ||
apiClient = _MockApiClient(); | ||
response = _MockResponse(); | ||
|
||
resource = LeaderboardResource(apiClient: apiClient); | ||
}); | ||
|
||
group('getLeaderboardResults', () { | ||
setUp(() { | ||
when(() => apiClient.get(any())).thenAnswer((_) async => response); | ||
}); | ||
|
||
test('makes the correct call ', () async { | ||
final leaderboardPlayer = LeaderboardPlayer( | ||
userId: 'id', | ||
score: 10, | ||
initials: 'TST', | ||
); | ||
|
||
when(() => response.statusCode).thenReturn(HttpStatus.ok); | ||
when(() => response.body).thenReturn( | ||
jsonEncode( | ||
{ | ||
'leaderboardPlayers': [leaderboardPlayer.toJson()], | ||
}, | ||
), | ||
); | ||
|
||
final results = await resource.getLeaderboardResults(); | ||
|
||
expect(results, equals([leaderboardPlayer])); | ||
}); | ||
|
||
test('throws ApiClientError when request fails', () async { | ||
when(() => response.statusCode) | ||
.thenReturn(HttpStatus.internalServerError); | ||
when(() => response.body).thenReturn('Oops'); | ||
|
||
await expectLater( | ||
resource.getLeaderboardResults, | ||
throwsA( | ||
isA<ApiClientError>().having( | ||
(e) => e.cause, | ||
'cause', | ||
equals( | ||
'GET /leaderboard/results returned status 500 with the following response: "Oops"', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('throws ApiClientError when request response is invalid', () async { | ||
when(() => response.statusCode).thenReturn(HttpStatus.ok); | ||
when(() => response.body).thenReturn('Oops'); | ||
|
||
await expectLater( | ||
resource.getLeaderboardResults, | ||
throwsA( | ||
isA<ApiClientError>().having( | ||
(e) => e.cause, | ||
'cause', | ||
equals( | ||
'GET /leaderboard/results returned invalid response "Oops"', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
group('getInitialsBlacklist', () { | ||
setUp(() { | ||
when(() => apiClient.get(any())).thenAnswer((_) async => response); | ||
}); | ||
|
||
test('gets initials blacklist', () async { | ||
const blacklist = ['WTF']; | ||
|
||
when(() => response.statusCode).thenReturn(HttpStatus.ok); | ||
when(() => response.body).thenReturn(jsonEncode({'list': blacklist})); | ||
final result = await resource.getInitialsBlacklist(); | ||
|
||
expect(result, equals(blacklist)); | ||
}); | ||
|
||
test('gets empty blacklist if endpoint not found', () async { | ||
const emptyList = <String>[]; | ||
|
||
when(() => response.statusCode).thenReturn(HttpStatus.notFound); | ||
final result = await resource.getInitialsBlacklist(); | ||
|
||
expect(result, equals(emptyList)); | ||
}); | ||
|
||
test('throws ApiClientError when request fails', () async { | ||
when(() => response.statusCode) | ||
.thenReturn(HttpStatus.internalServerError); | ||
when(() => response.body).thenReturn('Oops'); | ||
|
||
await expectLater( | ||
resource.getInitialsBlacklist, | ||
throwsA( | ||
isA<ApiClientError>().having( | ||
(e) => e.cause, | ||
'cause', | ||
equals( | ||
'GET /leaderboard/initials_blacklist returned status 500 with the following response: "Oops"', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
|
||
test('throws ApiClientError when request response is invalid', () async { | ||
when(() => response.statusCode).thenReturn(HttpStatus.ok); | ||
when(() => response.body).thenReturn('Oops'); | ||
|
||
await expectLater( | ||
resource.getInitialsBlacklist, | ||
throwsA( | ||
isA<ApiClientError>().having( | ||
(e) => e.cause, | ||
'cause', | ||
equals( | ||
'GET /leaderboard/initials_blacklist returned invalid response "Oops"', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
|
||
group('addLeaderboardPlayer', () { | ||
final leaderboardPlayer = LeaderboardPlayer( | ||
userId: 'id', | ||
score: 10, | ||
initials: 'TST', | ||
); | ||
|
||
setUp(() { | ||
when(() => apiClient.post(any(), body: any(named: 'body'))) | ||
.thenAnswer((_) async => response); | ||
}); | ||
|
||
test('makes the correct call', () async { | ||
when(() => response.statusCode).thenReturn(HttpStatus.noContent); | ||
await resource.addLeaderboardPlayer( | ||
leaderboardPlayer: leaderboardPlayer, | ||
); | ||
|
||
verify( | ||
() => apiClient.post( | ||
'/game/leaderboard/initials', | ||
body: jsonEncode(leaderboardPlayer.toJson()), | ||
), | ||
).called(1); | ||
}); | ||
|
||
test('throws ApiClientError when request fails', () async { | ||
when(() => response.statusCode) | ||
.thenReturn(HttpStatus.internalServerError); | ||
when(() => response.body).thenReturn('Oops'); | ||
|
||
await expectLater( | ||
() => resource.addLeaderboardPlayer( | ||
leaderboardPlayer: leaderboardPlayer, | ||
), | ||
throwsA( | ||
isA<ApiClientError>().having( | ||
(e) => e.cause, | ||
'cause', | ||
equals( | ||
'POST /leaderboard/initials returned status 500 with the following response: "Oops"', | ||
), | ||
), | ||
), | ||
); | ||
}); | ||
}); | ||
}); | ||
} |