Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fetch initials blacklist from firestore #173

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class LeaderboardRepository {
/// Retrieves the blacklist for player initials.
Future<List<String>> getInitialsBlacklist() async {
final blacklistData = await _dbClient.getById(
'initials_blacklist',
'initialsBlacklist',
jsgalarraga marked this conversation as resolved.
Show resolved Hide resolved
_blacklistDocumentId,
);

Expand All @@ -66,7 +66,7 @@ class LeaderboardRepository {
String mascot,
) async {
return _dbClient.set(
'score_cards',
'scoreCards',
DbEntityRecord(
id: userId,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void main() {
const blacklist = ['AAA', 'BBB', 'CCC'];

test('returns the blacklist', () async {
when(() => dbClient.getById('initials_blacklist', blacklistDocumentId))
when(() => dbClient.getById('initialsBlacklist', blacklistDocumentId))
.thenAnswer(
(_) async => DbEntityRecord(
id: blacklistDocumentId,
Expand All @@ -129,7 +129,7 @@ void main() {
});

test('returns empty list if not found', () async {
when(() => dbClient.getById('initials_blacklist', any())).thenAnswer(
when(() => dbClient.getById('initialsBlacklist', any())).thenAnswer(
(_) async => null,
);

Expand All @@ -142,7 +142,7 @@ void main() {
test('completes when writing in the db is successful', () async {
when(
() => dbClient.set(
'score_cards',
'scoreCards',
DbEntityRecord(
id: 'userId',
data: {
Expand Down
9 changes: 6 additions & 3 deletions lib/game_intro/bloc/game_intro_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ class GameIntroBloc extends Bloc<GameIntroEvent, GameIntroState> {
BlacklistRequested event,
Emitter<GameIntroState> emit,
) async {
// TODO(jaime): fetch blacklist from server
final blacklist = ['TST'];
emit(state.copyWith(initialsBlacklist: blacklist));
try {
final blacklist = await _leaderboardResource.getInitialsBlacklist();
emit(state.copyWith(initialsBlacklist: blacklist));
} catch (e, s) {
addError(e, s);
}
}

Future<void> _onBoardProgressRequested(
Expand Down
8 changes: 4 additions & 4 deletions packages/api_client/lib/src/api_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class ApiClient {
headers: _headers..addContentTypeJson(),
);

return response.decrypted;
return response;
});
}

Expand All @@ -113,7 +113,7 @@ class ApiClient {
headers: _headers..addContentTypeJson(),
);

return response.decrypted;
return response;
});
}

Expand All @@ -129,7 +129,7 @@ class ApiClient {
headers: _headers..addContentTypeJson(),
);

return response.decrypted;
return response;
});
}

Expand All @@ -147,7 +147,7 @@ class ApiClient {
headers: _headers,
);

return response.decrypted;
return response;
});
}

Expand Down
21 changes: 6 additions & 15 deletions packages/api_client/test/src/api_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:convert';
import 'dart:io';

import 'package:api_client/api_client.dart';
import 'package:encrypt/encrypt.dart';
import 'package:http/http.dart' as http;
import 'package:mocktail/mocktail.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -40,18 +39,10 @@ void main() {
const mockNewIdToken = 'mockNewIdToken';
const mockAppCheckToken = 'mockAppCheckToken';

// Since the key and iv are set from the environment variables, we can
// reference the default values here.
final key = Key.fromUtf8('encryption_key_not_set_123456789');
final iv = IV.fromUtf8('iv_not_set_12345');
final encrypter = Encrypter(AES(key));

final testJson = {'data': 'test'};

final encrypted = encrypter.encrypt(jsonEncode(testJson), iv: iv).base64;

final encryptedResponse = http.Response(encrypted, 200);
final expectedResponse = http.Response(testJson.toString(), 200);
final defaultResponse = http.Response(jsonEncode(testJson), 200);
final expectedResponse = http.Response(jsonEncode(testJson), 200);

late ApiClient subject;
late _MockHttpClient httpClient;
Expand All @@ -68,31 +59,31 @@ void main() {
any(),
headers: any(named: 'headers'),
),
).thenAnswer((_) async => encryptedResponse);
).thenAnswer((_) async => defaultResponse);

when(
() => httpClient.post(
any(),
body: any(named: 'body'),
headers: any(named: 'headers'),
),
).thenAnswer((_) async => encryptedResponse);
).thenAnswer((_) async => defaultResponse);

when(
() => httpClient.patch(
any(),
body: any(named: 'body'),
headers: any(named: 'headers'),
),
).thenAnswer((_) async => encryptedResponse);
).thenAnswer((_) async => defaultResponse);

when(
() => httpClient.put(
any(),
body: any(named: 'body'),
headers: any(named: 'headers'),
),
).thenAnswer((_) async => encryptedResponse);
).thenAnswer((_) async => defaultResponse);

idTokenStreamController = StreamController.broadcast();
appCheckTokenStreamController = StreamController.broadcast();
Expand Down
4 changes: 4 additions & 0 deletions test/game_intro/bloc/game_intro_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ void main() {

blocTest<GameIntroBloc, GameIntroState>(
'sets blacklist when BlacklistRequested is added',
setUp: () {
when(() => leaderboardResource.getInitialsBlacklist())
.thenAnswer((_) => Future.value(['TST']));
},
build: () => GameIntroBloc(
boardInfoRepository: boardInfoRepository,
leaderboardResource: leaderboardResource,
Expand Down
Loading