|
| 1 | +import 'package:checks/checks.dart'; |
| 2 | +import 'package:http/http.dart' as http; |
| 3 | +import 'package:flutter_test/flutter_test.dart'; |
| 4 | +import 'package:zulip/api/model/model.dart'; |
| 5 | +import 'package:zulip/api/route/channels.dart'; |
| 6 | + |
| 7 | +import '../../stdlib_checks.dart'; |
| 8 | +import '../fake_api.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + test('smoke updateUserTopic', () { |
| 12 | + return FakeApiConnection.with_((connection) async { |
| 13 | + connection.prepare(json: {}); |
| 14 | + await updateUserTopic(connection, |
| 15 | + streamId: 1, topic: 'topic', |
| 16 | + visibilityPolicy: UserTopicVisibilityPolicy.followed); |
| 17 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 18 | + ..method.equals('POST') |
| 19 | + ..url.path.equals('/api/v1/user_topics') |
| 20 | + ..bodyFields.deepEquals({ |
| 21 | + 'stream_id': '1', |
| 22 | + 'topic': 'topic', |
| 23 | + 'visibility_policy': '3', |
| 24 | + }); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + test('updateUserTopic only accepts valid visibility policy', () { |
| 29 | + return FakeApiConnection.with_((connection) async { |
| 30 | + check(() => updateUserTopic(connection, |
| 31 | + streamId: 1, topic: 'topic', |
| 32 | + visibilityPolicy: UserTopicVisibilityPolicy.unknown), |
| 33 | + ).throws<AssertionError>(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + test('updateUserTopicCompat when FL >= 170', () { |
| 38 | + return FakeApiConnection.with_((connection) async { |
| 39 | + connection.prepare(json: {}); |
| 40 | + await updateUserTopicCompat(connection, |
| 41 | + streamId: 1, topic: 'topic', |
| 42 | + visibilityPolicy: UserTopicVisibilityPolicy.followed); |
| 43 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 44 | + ..method.equals('POST') |
| 45 | + ..url.path.equals('/api/v1/user_topics') |
| 46 | + ..bodyFields.deepEquals({ |
| 47 | + 'stream_id': '1', |
| 48 | + 'topic': 'topic', |
| 49 | + 'visibility_policy': '3', |
| 50 | + }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + group('legacy: use muteTopic when FL < 170', () { |
| 55 | + test('updateUserTopic throws AssertionError when FL < 170', () { |
| 56 | + return FakeApiConnection.with_(zulipFeatureLevel: 169, (connection) async { |
| 57 | + check(() => updateUserTopic(connection, |
| 58 | + streamId: 1, topic: 'topic', |
| 59 | + visibilityPolicy: UserTopicVisibilityPolicy.followed), |
| 60 | + ).throws<AssertionError>(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + test('policy: none -> op: remove', () { |
| 65 | + return FakeApiConnection.with_(zulipFeatureLevel: 169, (connection) async { |
| 66 | + connection.prepare(json: {}); |
| 67 | + await updateUserTopicCompat(connection, |
| 68 | + streamId: 1, topic: 'topic', |
| 69 | + visibilityPolicy: UserTopicVisibilityPolicy.none); |
| 70 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 71 | + ..method.equals('PATCH') |
| 72 | + ..url.path.equals('/api/v1/users/me/subscriptions/muted_topics') |
| 73 | + ..bodyFields.deepEquals({ |
| 74 | + 'stream_id': '1', |
| 75 | + 'topic': 'topic', |
| 76 | + 'op': 'remove', |
| 77 | + }); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + test('policy: muted -> op: add', () { |
| 82 | + return FakeApiConnection.with_(zulipFeatureLevel: 169, (connection) async { |
| 83 | + connection.prepare(json: {}); |
| 84 | + await updateUserTopicCompat(connection, |
| 85 | + streamId: 1, topic: 'topic', |
| 86 | + visibilityPolicy: UserTopicVisibilityPolicy.muted); |
| 87 | + check(connection.takeRequests()).single.isA<http.Request>() |
| 88 | + ..method.equals('PATCH') |
| 89 | + ..url.path.equals('/api/v1/users/me/subscriptions/muted_topics') |
| 90 | + ..bodyFields.deepEquals({ |
| 91 | + 'stream_id': '1', |
| 92 | + 'topic': 'topic', |
| 93 | + 'op': 'add', |
| 94 | + }); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | +} |
0 commit comments