famedlysdk/test/room_test.dart

526 lines
17 KiB
Dart
Raw Normal View History

2019-06-11 10:21:45 +00:00
/*
2020-06-03 10:16:01 +00:00
* Ansible inventory script used at Famedly GmbH for managing many hosts
* Copyright (C) 2019, 2020 Famedly GmbH
2019-06-11 10:21:45 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2019-06-11 10:21:45 +00:00
*
2020-06-03 10:16:01 +00:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2019-06-11 10:21:45 +00:00
*
2020-06-03 10:16:01 +00:00
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2019-06-11 10:21:45 +00:00
*/
2020-06-03 10:16:01 +00:00
import 'package:famedlysdk/matrix_api.dart';
import 'package:famedlysdk/src/client.dart';
import 'package:famedlysdk/src/event.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/user.dart';
import 'package:famedlysdk/src/utils/matrix_file.dart';
2020-05-19 09:28:13 +00:00
import 'package:famedlysdk/src/database/database.dart'
show DbRoom, DbRoomState, DbRoomAccountData;
2019-10-04 09:44:32 +00:00
import 'package:test/test.dart';
2020-06-05 07:59:37 +00:00
import 'fake_client.dart';
2019-06-11 10:21:45 +00:00
import 'dart:typed_data';
2019-06-11 10:21:45 +00:00
void main() {
2019-06-18 10:06:55 +00:00
Client matrix;
Room room;
2019-06-11 10:21:45 +00:00
/// All Tests related to the Event
group('Room', () {
2019-06-18 10:06:55 +00:00
test('Login', () async {
2020-06-05 07:59:37 +00:00
matrix = await getClient();
2019-06-18 10:06:55 +00:00
});
test('Create from json', () async {
final id = '!localpart:server.abc';
final membership = Membership.join;
final notificationCount = 2;
final highlightCount = 1;
final heroes = [
'@alice:matrix.org',
'@bob:example.com',
'@charley:example.org'
2019-08-06 09:47:09 +00:00
];
2019-06-11 10:21:45 +00:00
2020-05-15 18:40:17 +00:00
var dbRoom = DbRoom(
clientId: 1,
roomId: id,
membership: membership.toString().split('.').last,
highlightCount: highlightCount,
notificationCount: notificationCount,
prevBatch: '',
joinedMemberCount: notificationCount,
invitedMemberCount: notificationCount,
newestSortOrder: 0.0,
oldestSortOrder: 0.0,
heroes: heroes.join(','),
);
var states = [
DbRoomState(
clientId: 1,
eventId: '143273582443PhrSn:example.org',
roomId: id,
sortOrder: 0.0,
originServerTs: DateTime.fromMillisecondsSinceEpoch(1432735824653),
sender: '@example:example.org',
type: 'm.room.join_rules',
unsigned: '{"age": 1234}',
content: '{"join_rule": "public"}',
prevContent: '',
stateKey: '',
),
];
var roomAccountData = [
DbRoomAccountData(
clientId: 1,
type: 'com.test.foo',
roomId: id,
content: '{"foo": "bar"}',
),
];
2020-01-04 14:43:53 +00:00
room = await Room.getRoomFromTableRow(
2020-05-15 18:40:17 +00:00
dbRoom,
2020-01-04 14:43:53 +00:00
matrix,
2020-05-15 18:40:17 +00:00
states: states,
roomAccountData: roomAccountData,
2020-01-04 14:43:53 +00:00
);
2019-06-11 10:21:45 +00:00
2019-06-12 06:40:43 +00:00
expect(room.id, id);
2019-06-12 10:30:22 +00:00
expect(room.membership, membership);
2019-06-12 06:40:43 +00:00
expect(room.notificationCount, notificationCount);
expect(room.highlightCount, highlightCount);
2019-08-06 09:47:09 +00:00
expect(room.mJoinedMemberCount, notificationCount);
expect(room.mInvitedMemberCount, notificationCount);
expect(room.mHeroes, heroes);
2020-04-23 09:46:10 +00:00
expect(room.displayname, 'Alice, Bob, Charley');
expect(room.getState('m.room.join_rules').content['join_rule'], 'public');
expect(room.roomAccountData['com.test.foo'].content['foo'], 'bar');
2019-08-08 09:41:42 +00:00
room.states['m.room.canonical_alias'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.canonical_alias',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '123',
content: {'alias': '#testalias:example.com'},
stateKey: '');
expect(room.displayname, 'testalias');
expect(room.canonicalAlias, '#testalias:example.com');
room.states['m.room.name'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.name',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '123',
content: {'name': 'testname'},
stateKey: '');
expect(room.displayname, 'testname');
expect(room.topic, '');
room.states['m.room.topic'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.topic',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '123',
content: {'topic': 'testtopic'},
stateKey: '');
expect(room.topic, 'testtopic');
2020-04-24 07:24:06 +00:00
expect(room.avatar, null);
room.states['m.room.avatar'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.avatar',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '123',
content: {'url': 'mxc://testurl'},
stateKey: '');
2020-04-24 07:24:06 +00:00
expect(room.avatar.toString(), 'mxc://testurl');
2020-06-24 08:41:52 +00:00
expect(room.pinnedEventIds, <String>[]);
room.states['m.room.pinned_events'] = Event(
senderId: '@test:example.com',
type: 'm.room.pinned_events',
roomId: room.id,
room: room,
eventId: '123',
content: {
'pinned': ['1234']
},
stateKey: '');
expect(room.pinnedEventIds.first, '1234');
room.states['m.room.message'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.message',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '12345',
2020-06-03 10:16:01 +00:00
originServerTs: DateTime.now(),
content: {'msgtype': 'm.text', 'body': 'test'},
stateKey: '');
expect(room.lastEvent.eventId, '12345');
expect(room.lastMessage, 'test');
2020-06-03 10:16:01 +00:00
expect(room.timeCreated, room.lastEvent.originServerTs);
2019-06-11 10:21:45 +00:00
});
2019-06-18 10:06:55 +00:00
test('sendReadReceipt', () async {
await room.sendReadReceipt('§1234:fakeServer.notExisting');
2019-06-18 10:06:55 +00:00
});
2020-06-03 10:16:01 +00:00
test('enableEncryption', () async {
await room.enableEncryption();
});
test('requestParticipants', () async {
final participants = await room.requestParticipants();
2019-06-18 10:06:55 +00:00
expect(participants.length, 1);
var user = participants[0];
expect(user.id, '@alice:example.org');
expect(user.displayName, 'Alice Margatroid');
expect(user.membership, Membership.join);
2020-04-24 07:24:06 +00:00
expect(user.avatarUrl.toString(), 'mxc://example.org/SEsfnsuifSDFSSEF');
expect(user.room.id, '!localpart:server.abc');
2019-06-18 10:06:55 +00:00
});
test('getEventByID', () async {
final event = await room.getEventById('1234');
expect(event.eventId, '143273582443PhrSn:example.org');
});
2019-08-08 09:41:42 +00:00
test('setName', () async {
final eventId = await room.setName('Testname');
expect(eventId, '42');
2019-08-08 09:41:42 +00:00
});
test('setDescription', () async {
final eventId = await room.setDescription('Testname');
expect(eventId, '42');
2019-08-08 09:41:42 +00:00
});
test('kick', () async {
await room.kick('Testname');
2019-08-08 09:41:42 +00:00
});
test('ban', () async {
await room.ban('Testname');
2019-08-08 09:41:42 +00:00
});
test('unban', () async {
await room.unban('Testname');
2019-08-08 09:41:42 +00:00
});
test('PowerLevels', () async {
room.states['m.room.power_levels'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.power_levels',
2019-08-08 09:41:42 +00:00
roomId: room.id,
room: room,
eventId: '123',
2019-08-08 09:41:42 +00:00
content: {
'ban': 50,
'events': {'m.room.name': 100, 'm.room.power_levels': 100},
'events_default': 0,
'invite': 50,
'kick': 50,
'notifications': {'room': 20},
'redact': 50,
'state_default': 50,
'users': {'@test:fakeServer.notExisting': 100},
'users_default': 10
2019-08-08 09:41:42 +00:00
},
stateKey: '');
2019-08-08 09:41:42 +00:00
expect(room.ownPowerLevel, 100);
expect(room.getPowerLevelByUserId(matrix.userID), room.ownPowerLevel);
expect(room.getPowerLevelByUserId('@nouser:example.com'), 10);
2019-11-26 06:38:44 +00:00
expect(room.ownPowerLevel, 100);
expect(room.canBan, true);
expect(room.canInvite, true);
expect(room.canKick, true);
expect(room.canRedact, true);
expect(room.canSendDefaultMessages, true);
expect(room.canSendDefaultStates, true);
expect(room.canChangePowerLevel, true);
expect(room.canSendEvent('m.room.name'), true);
expect(room.canSendEvent('m.room.power_levels'), true);
expect(room.canSendEvent('m.room.member'), true);
2019-08-08 09:41:42 +00:00
expect(room.powerLevels,
room.states['m.room.power_levels'].content['users']);
2019-11-26 06:38:44 +00:00
room.states['m.room.power_levels'] = Event(
senderId: '@test:example.com',
2020-06-03 10:16:01 +00:00
type: 'm.room.power_levels',
2019-11-26 06:38:44 +00:00
roomId: room.id,
room: room,
eventId: '123abc',
2019-11-26 06:38:44 +00:00
content: {
'ban': 50,
'events': {'m.room.name': 0, 'm.room.power_levels': 100},
'events_default': 0,
'invite': 50,
'kick': 50,
'notifications': {'room': 20},
'redact': 50,
'state_default': 50,
'users': {},
'users_default': 0
2019-11-26 06:38:44 +00:00
},
stateKey: '');
2019-11-26 06:38:44 +00:00
expect(room.ownPowerLevel, 0);
expect(room.canBan, false);
expect(room.canInvite, false);
expect(room.canKick, false);
expect(room.canRedact, false);
expect(room.canSendDefaultMessages, true);
expect(room.canSendDefaultStates, false);
expect(room.canChangePowerLevel, false);
expect(room.canSendEvent('m.room.name'), true);
expect(room.canSendEvent('m.room.power_levels'), false);
expect(room.canSendEvent('m.room.member'), false);
expect(room.canSendEvent('m.room.message'), true);
final resp = await room.setPower('@test:fakeServer.notExisting', 90);
expect(resp, '42');
2019-08-08 09:41:42 +00:00
});
test('invite', () async {
await room.invite('Testname');
2019-08-08 09:41:42 +00:00
});
test('getParticipants', () async {
2020-07-14 09:46:37 +00:00
var userList = room.getParticipants();
expect(userList.length, 4);
// add new user
2020-01-02 14:09:49 +00:00
room.setState(Event(
senderId: '@alice:test.abc',
2020-06-03 10:16:01 +00:00
type: 'm.room.member',
2019-09-02 10:09:30 +00:00
roomId: room.id,
room: room,
eventId: '12345',
2020-06-03 10:16:01 +00:00
originServerTs: DateTime.now(),
content: {'displayname': 'alice'},
stateKey: '@alice:test.abc'));
2020-07-14 09:46:37 +00:00
userList = room.getParticipants();
expect(userList.length, 5);
expect(userList[4].displayName, 'alice');
2019-09-02 10:09:30 +00:00
});
test('addToDirectChat', () async {
await room.addToDirectChat('Testname');
2019-08-08 09:41:42 +00:00
});
test('getTimeline', () async {
final timeline = await room.getTimeline();
2020-06-05 07:59:37 +00:00
expect(timeline.events.length, 0);
2019-08-08 09:54:39 +00:00
});
2019-08-08 09:41:42 +00:00
test('getUserByMXID', () async {
2019-12-29 10:28:33 +00:00
User user;
try {
user = await room.getUserByMXID('@getme:example.com');
2019-12-29 10:28:33 +00:00
} catch (_) {}
expect(user.stateKey, '@getme:example.com');
2020-06-03 10:16:01 +00:00
expect(user.calcDisplayname(), 'Getme');
2019-08-08 09:54:39 +00:00
});
2019-09-09 13:22:02 +00:00
test('setAvatar', () async {
2020-06-29 12:00:26 +00:00
final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg');
2019-09-09 13:22:02 +00:00
final dynamic resp = await room.setAvatar(testFile);
expect(resp, 'YUwRidLecu:example.com');
2019-09-09 13:22:02 +00:00
});
test('sendEvent', () async {
final dynamic resp = await room.sendEvent(
{'msgtype': 'm.text', 'body': 'hello world'},
txid: 'testtxid');
2020-06-05 09:32:02 +00:00
expect(resp.startsWith('\$event'), true);
2019-09-09 13:22:02 +00:00
});
test('sendEvent', () async {
final dynamic resp =
await room.sendTextEvent('Hello world', txid: 'testtxid');
2020-06-05 09:32:02 +00:00
expect(resp.startsWith('\$event'), true);
2019-09-09 13:22:02 +00:00
});
// Not working because there is no real file to test it...
2019-09-30 12:03:34 +00:00
/*test('sendImageEvent', () async {
2019-09-09 13:22:02 +00:00
final File testFile = File.fromUri(Uri.parse("fake/path/file.jpeg"));
final dynamic resp =
await room.sendImageEvent(testFile, txid: "testtxid");
expect(resp, "42");
2019-09-30 12:03:34 +00:00
});*/
2019-09-09 13:22:02 +00:00
test('sendFileEvent', () async {
2020-06-29 12:00:26 +00:00
final testFile = MatrixFile(bytes: Uint8List(0), name: 'file.jpeg');
final dynamic resp = await room.sendFileEvent(testFile, txid: 'testtxid');
expect(resp, 'mxc://example.com/AQwafuaFswefuhsfAFAgsw');
2019-09-09 13:22:02 +00:00
});
2019-12-04 09:58:47 +00:00
test('pushRuleState', () async {
expect(room.pushRuleState, PushRuleState.mentions_only);
matrix.accountData['m.push_rules'].content['global']['override']
.add(matrix.accountData['m.push_rules'].content['global']['room'][0]);
2019-12-04 09:58:47 +00:00
expect(room.pushRuleState, PushRuleState.dont_notify);
});
test('Enable encryption', () async {
room.setState(
Event(
senderId: '@alice:test.abc',
2020-06-03 10:16:01 +00:00
type: 'm.room.encryption',
roomId: room.id,
room: room,
eventId: '12345',
2020-06-03 10:16:01 +00:00
originServerTs: DateTime.now(),
content: {
'algorithm': 'm.megolm.v1.aes-sha2',
'rotation_period_ms': 604800000,
'rotation_period_msgs': 100
},
stateKey: ''),
);
expect(room.encrypted, true);
expect(room.encryptionAlgorithm, 'm.megolm.v1.aes-sha2');
});
2020-05-19 09:28:13 +00:00
2020-06-03 10:16:01 +00:00
test('setPushRuleState', () async {
await room.setPushRuleState(PushRuleState.notify);
await room.setPushRuleState(PushRuleState.dont_notify);
await room.setPushRuleState(PushRuleState.mentions_only);
await room.setPushRuleState(PushRuleState.notify);
});
test('Test call methods', () async {
await room.inviteToCall('1234', 1234, 'sdp', txid: '1234');
await room.answerCall('1234', 'sdp', txid: '1234');
await room.hangupCall('1234', txid: '1234');
await room.sendCallCandidates('1234', [], txid: '1234');
});
2020-06-24 09:22:08 +00:00
test('Test tag methods', () async {
await room.addTag(TagType.Favourite, order: 0.1);
await room.removeTag(TagType.Favourite);
expect(room.isFavourite, false);
room.roomAccountData['m.tag'] = BasicRoomEvent.fromJson({
'content': {
'tags': {
'm.favourite': {'order': 0.1},
'm.wrong': {'order': 0.2},
}
},
'type': 'm.tag'
});
expect(room.tags.length, 1);
expect(room.tags[TagType.Favourite].order, 0.1);
expect(room.isFavourite, true);
await room.setFavourite(false);
});
2020-06-03 10:16:01 +00:00
test('joinRules', () async {
expect(room.canChangeJoinRules, false);
expect(room.joinRules, JoinRules.public);
room.setState(Event.fromJson({
'content': {'join_rule': 'invite'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'state_key': '',
'type': 'm.room.join_rules',
'unsigned': {'age': 1234}
}, room));
expect(room.joinRules, JoinRules.invite);
await room.setJoinRules(JoinRules.invite);
});
test('guestAccess', () async {
expect(room.canChangeGuestAccess, false);
expect(room.guestAccess, GuestAccess.forbidden);
room.setState(Event.fromJson({
'content': {'guest_access': 'can_join'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'state_key': '',
'type': 'm.room.guest_access',
'unsigned': {'age': 1234}
}, room));
expect(room.guestAccess, GuestAccess.can_join);
await room.setGuestAccess(GuestAccess.can_join);
});
test('historyVisibility', () async {
expect(room.canChangeHistoryVisibility, false);
expect(room.historyVisibility, null);
room.setState(Event.fromJson({
'content': {'history_visibility': 'shared'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'state_key': '',
'type': 'm.room.history_visibility',
'unsigned': {'age': 1234}
}, room));
expect(room.historyVisibility, HistoryVisibility.shared);
await room.setHistoryVisibility(HistoryVisibility.joined);
});
2020-06-10 14:17:57 +00:00
test('setState', () async {
// not set non-state-events
room.setState(Event.fromJson({
'content': {'history_visibility': 'shared'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'type': 'm.custom',
'unsigned': {'age': 1234}
}, room));
expect(room.getState('m.custom') != null, false);
// set state events
room.setState(Event.fromJson({
'content': {'history_visibility': 'shared'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'state_key': '',
'type': 'm.custom',
'unsigned': {'age': 1234}
}, room));
expect(room.getState('m.custom') != null, true);
// sets messages as state events
room.setState(Event.fromJson({
'content': {'history_visibility': 'shared'},
'event_id': '\$143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'room_id': '!jEsUZKDJdhlrceRyVU:example.org',
'sender': '@example:example.org',
'type': 'm.room.message',
'unsigned': {'age': 1234}
}, room));
expect(room.getState('m.room.message') != null, true);
});
2020-05-19 09:28:13 +00:00
test('logout', () async {
await matrix.logout();
});
2019-06-11 10:21:45 +00:00
});
}