Skip to content

Commit 2ed83f3

Browse files
committed
test [nfc]: Introduce eg.apiBadRequest
This changes the message string in a few of the call sites, where the string was already an arbitrary one rather than a realistic specific string. That's still NFC because the tests weren't depending on the specific string.
1 parent 4fa79bb commit 2ed83f3

7 files changed

+30
-55
lines changed

test/api/route/messages_test.dart

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ void main() {
6565
test('modern; message not found', () {
6666
return FakeApiConnection.with_((connection) async {
6767
final message = eg.streamMessage();
68-
final fakeResponseJson = {
69-
'code': 'BAD_REQUEST',
70-
'msg': 'Invalid message(s)',
71-
'result': 'error',
72-
};
73-
connection.prepare(httpStatus: 400, json: fakeResponseJson);
68+
connection.prepare(
69+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
7470
final result = await checkGetMessageCompat(connection,
7571
expectLegacy: false,
7672
messageId: message.id,

test/example_data.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@ Object nullCheckError() {
2727
try { null!; } catch (e) { return e; } // ignore: null_check_always_fails
2828
}
2929

30+
/// A Zulip API error with the generic "BAD_REQUEST" error code.
31+
///
32+
/// The server returns this error code for a wide range of error conditions;
33+
/// it's the default within the server code when no more-specific code is chosen.
34+
ZulipApiException apiBadRequest({
35+
String routeName = 'someRoute', String message = 'Something failed'}) {
36+
return ZulipApiException(
37+
routeName: routeName,
38+
httpStatus: 400, code: 'BAD_REQUEST',
39+
data: {}, message: message);
40+
}
41+
3042
/// The error the server gives when the client's credentials
3143
/// (API key together with email and realm URL) are no longer valid.
3244
///

test/model/message_list_test.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ void main() {
247247
await prepareMessages(foundOldest: false, messages: initialMessages);
248248
check(connection.takeRequests()).single;
249249

250-
connection.prepare(httpStatus: 400, json: {
251-
'result': 'error', 'code': 'BAD_REQUEST', 'msg': 'Bad request'});
250+
connection.prepare(apiException: eg.apiBadRequest());
252251
check(async.pendingTimers).isEmpty();
253252
await check(model.fetchOlder()).throws<ZulipApiException>();
254253
checkNotified(count: 2);
@@ -1061,8 +1060,7 @@ void main() {
10611060
addTearDown(() => BackoffMachine.debugDuration = null);
10621061
await prepareNarrow(narrow, initialMessages);
10631062

1064-
connection.prepare(httpStatus: 400, json: {
1065-
'result': 'error', 'code': 'BAD_REQUEST', 'msg': 'Bad request'});
1063+
connection.prepare(apiException: eg.apiBadRequest());
10661064
BackoffMachine.debugDuration = const Duration(seconds: 1);
10671065
await check(model.fetchOlder()).throws<ZulipApiException>();
10681066
final backoffTimerA = async.pendingTimers.single;
@@ -1094,8 +1092,7 @@ void main() {
10941092
check(model).fetchOlderCoolingDown.isFalse();
10951093
check(backoffTimerA.isActive).isTrue();
10961094

1097-
connection.prepare(httpStatus: 400, json: {
1098-
'result': 'error', 'code': 'BAD_REQUEST', 'msg': 'Bad request'});
1095+
connection.prepare(apiException: eg.apiBadRequest());
10991096
BackoffMachine.debugDuration = const Duration(seconds: 2);
11001097
await check(model.fetchOlder()).throws<ZulipApiException>();
11011098
final backoffTimerB = async.pendingTimers.last;

test/widgets/action_sheet_test.dart

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,7 @@ void main() {
100100
}
101101

102102
void prepareRawContentResponseError() {
103-
final fakeResponseJson = {
104-
'code': 'BAD_REQUEST',
105-
'msg': 'Invalid message(s)',
106-
'result': 'error',
107-
};
108-
connection.prepare(httpStatus: 400, json: fakeResponseJson);
103+
connection.prepare(apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
109104
}
110105

111106
group('topic action sheet', () {
@@ -377,8 +372,7 @@ void main() {
377372
isChannelMuted: false,
378373
visibilityPolicy: UserTopicVisibilityPolicy.followed);
379374

380-
connection.prepare(httpStatus: 400, json: {
381-
'result': 'error', 'code': 'BAD_REQUEST', 'msg': ''});
375+
connection.prepare(apiException: eg.apiBadRequest());
382376
await tester.tap(unfollow);
383377
await tester.pumpAndSettle();
384378

@@ -629,11 +623,8 @@ void main() {
629623
final message = eg.streamMessage();
630624
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
631625

632-
connection.prepare(httpStatus: 400, json: {
633-
'code': 'BAD_REQUEST',
634-
'msg': 'Invalid message(s)',
635-
'result': 'error',
636-
});
626+
connection.prepare(
627+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
637628
await tapButton(tester);
638629
await tester.pump(Duration.zero); // error arrives; error dialog shows
639630

@@ -698,11 +689,8 @@ void main() {
698689
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
699690
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
700691

701-
connection.prepare(httpStatus: 400, json: {
702-
'code': 'BAD_REQUEST',
703-
'msg': 'Invalid message(s)',
704-
'result': 'error',
705-
});
692+
connection.prepare(
693+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
706694
await tapButton(tester);
707695
await tester.pump(Duration.zero); // error arrives; error dialog shows
708696

@@ -716,11 +704,8 @@ void main() {
716704
await setupToMessageActionSheet(tester, message: message, narrow: TopicNarrow.ofMessage(message));
717705
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
718706

719-
connection.prepare(httpStatus: 400, json: {
720-
'code': 'BAD_REQUEST',
721-
'msg': 'Invalid message(s)',
722-
'result': 'error',
723-
});
707+
connection.prepare(
708+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
724709
await tapButton(tester, starred: true);
725710
await tester.pump(Duration.zero); // error arrives; error dialog shows
726711

test/widgets/compose_box_test.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,13 +553,8 @@ void main() {
553553

554554
testWidgets('ZulipApiException', (tester) async {
555555
await setupAndTapSend(tester, prepareResponse: (message) {
556-
connection.prepare(
557-
httpStatus: 400,
558-
json: {
559-
'result': 'error',
560-
'code': 'BAD_REQUEST',
561-
'msg': 'You do not have permission to initiate direct message conversations.',
562-
});
556+
connection.prepare(apiException: eg.apiBadRequest(
557+
message: 'You do not have permission to initiate direct message conversations.'));
563558
});
564559
final zulipLocalizations = GlobalLocalizations.zulipLocalizations;
565560
await tester.tap(find.byWidget(checkErrorDialog(tester,

test/widgets/emoji_reaction_test.dart

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,7 @@ void main() {
452452

453453
connection.prepare(
454454
delay: const Duration(seconds: 2),
455-
httpStatus: 400, json: {
456-
'code': 'BAD_REQUEST',
457-
'msg': 'Invalid message(s)',
458-
'result': 'error',
459-
});
460-
455+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
461456
await tester.tap(find.descendant(
462457
of: find.byType(BottomSheet),
463458
matching: find.text('\u{1f4a4}'))); // 'zzz' emoji

test/widgets/message_list_test.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -595,15 +595,10 @@ void main() {
595595
await setupMessageListPage(tester,
596596
narrow: narrow, messages: [message], unreadMsgs: unreadMsgs);
597597
check(isMarkAsReadButtonVisible(tester)).isTrue();
598-
599-
connection.prepare(httpStatus: 400, json: {
600-
'code': 'BAD_REQUEST',
601-
'msg': 'Invalid message(s)',
602-
'result': 'error',
603-
});
604-
605598
checkAppearsLoading(tester, false);
606599

600+
connection.prepare(
601+
apiException: eg.apiBadRequest(message: 'Invalid message(s)'));
607602
await tester.tap(find.byType(MarkAsReadWidget));
608603
await tester.pump();
609604
checkAppearsLoading(tester, true);

0 commit comments

Comments
 (0)