Skip to content

Commit de424c2

Browse files
committed
test [nfc]: Pull out an example UNAUTHORIZED API exception, and add doc
Prompted by seeing we'll need more copies of this soon: #1183 (comment)
1 parent 6caa937 commit de424c2

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

test/example_data.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:convert';
22
import 'dart:math';
33

4+
import 'package:zulip/api/exception.dart';
45
import 'package:zulip/api/model/events.dart';
56
import 'package:zulip/api/model/initial_snapshot.dart';
67
import 'package:zulip/api/model/model.dart';
@@ -18,10 +19,28 @@ void _checkPositive(int? value, String description) {
1819
assert(value == null || value > 0, '$description should be positive');
1920
}
2021

22+
////////////////////////////////////////////////////////////////
23+
// Error objects.
24+
//
25+
2126
Object nullCheckError() {
2227
try { null!; } catch (e) { return e; } // ignore: null_check_always_fails
2328
}
2429

30+
/// The error the server gives when the client's credentials
31+
/// (API key together with email and realm URL) are no longer valid.
32+
///
33+
/// This isn't really documented, but comes from experiment and from
34+
/// reading the server implementation. See:
35+
/// https://github.com/zulip/zulip-flutter/pull/1183#discussion_r1945865983
36+
/// https://chat.zulip.org/#narrow/channel/378-api-design/topic/general.20handling.20HTTP.20status.20code.20401/near/2090024
37+
ZulipApiException apiExceptionUnauthorized({String routeName = 'someRoute'}) {
38+
return ZulipApiException(
39+
routeName: routeName,
40+
httpStatus: 401, code: 'UNAUTHORIZED',
41+
data: {}, message: 'Invalid API key');
42+
}
43+
2544
////////////////////////////////////////////////////////////////
2645
// Realm-wide (or server-wide) metadata.
2746
//

test/model/actions_test.dart

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,7 @@ void main() {
102102
check(testBinding.globalStore).accountIds.single.equals(eg.selfAccount.id);
103103
const unregisterDelay = Duration(seconds: 5);
104104
assert(unregisterDelay > TestGlobalStore.removeAccountDuration);
105-
final exception = ZulipApiException(
106-
httpStatus: 401,
107-
code: 'UNAUTHORIZED',
108-
data: {},
109-
routeName: 'removeEtcEtcToken',
110-
message: 'Invalid API key',
111-
);
105+
final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
112106
final newConnection = separateConnection()
113107
..prepare(delay: unregisterDelay, exception: exception);
114108

@@ -170,14 +164,9 @@ void main() {
170164
test('connection closed if request errors', () => awaitFakeAsync((async) async {
171165
await prepare(ackedPushToken: '123');
172166

167+
final exception = eg.apiExceptionUnauthorized(routeName: 'removeEtcEtcToken');
173168
final newConnection = separateConnection()
174-
..prepare(exception: ZulipApiException(
175-
httpStatus: 401,
176-
code: 'UNAUTHORIZED',
177-
data: {},
178-
routeName: 'removeEtcEtcToken',
179-
message: 'Invalid API key',
180-
));
169+
..prepare(exception: exception);
181170
final future = unregisterToken(testBinding.globalStore, eg.selfAccount.id);
182171
async.elapse(Duration.zero);
183172
await future;

0 commit comments

Comments
 (0)