famedlysdk/test/event_test.dart

254 lines
8.5 KiB
Dart
Raw Normal View History

2019-06-11 09:48:31 +00:00
/*
* Copyright (c) 2019 Zender & Kurtz GbR.
*
* Authors:
* Christian Pauly <krille@famedly.com>
* Marcel Radzio <mtrnord@famedly.com>
*
* This file is part of famedlysdk.
*
* famedlysdk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* famedlysdk 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with famedlysdk. If not, see <http://www.gnu.org/licenses/>.
2019-06-11 09:48:31 +00:00
*/
2019-07-22 09:54:06 +00:00
import 'dart:convert';
2019-07-24 08:13:02 +00:00
import 'package:famedlysdk/famedlysdk.dart';
import 'package:famedlysdk/src/event.dart';
2019-10-04 09:44:32 +00:00
import 'package:test/test.dart';
2019-06-11 09:48:31 +00:00
import 'fake_matrix_api.dart';
2019-07-24 08:13:02 +00:00
2019-06-11 09:48:31 +00:00
void main() {
/// All Tests related to the Event
group('Event', () {
final timestamp = DateTime.now().millisecondsSinceEpoch;
final id = '!4fsdfjisjf:server.abc';
final senderID = '@alice:server.abc';
final type = 'm.room.message';
final msgtype = 'm.text';
final body = 'Hello World';
final formatted_body = '<b>Hello</b> World';
final contentJson =
2020-02-11 11:06:54 +00:00
'{"msgtype":"$msgtype","body":"$body","formatted_body":"$formatted_body","m.relates_to":{"m.in_reply_to":{"event_id":"\$1234:example.com"}}}';
2019-06-11 09:48:31 +00:00
var jsonObj = <String, dynamic>{
'event_id': id,
'sender': senderID,
'origin_server_ts': timestamp,
'type': type,
'room_id': '1234',
'status': 2,
'content': contentJson,
2019-07-22 09:54:06 +00:00
};
test('Create from json', () async {
var event = Event.fromJson(jsonObj, null);
jsonObj.remove('status');
jsonObj['content'] = json.decode(contentJson);
2019-12-12 12:19:18 +00:00
expect(event.toJson(), jsonObj);
jsonObj['content'] = contentJson;
2019-06-11 09:48:31 +00:00
2019-08-07 10:27:02 +00:00
expect(event.eventId, id);
expect(event.senderId, senderID);
expect(event.status, 2);
2019-06-11 09:48:31 +00:00
expect(event.text, body);
expect(event.formattedText, formatted_body);
2020-01-14 11:27:26 +00:00
expect(event.body, body);
2020-01-04 09:31:27 +00:00
expect(event.type, EventTypes.Message);
2020-02-11 11:06:54 +00:00
expect(event.isReply, true);
jsonObj['state_key'] = '';
var state = Event.fromJson(jsonObj, null);
2019-08-08 09:57:40 +00:00
expect(state.eventId, id);
expect(state.stateKey, '');
2020-01-14 11:27:26 +00:00
expect(state.status, 2);
2019-06-11 09:48:31 +00:00
});
test('Test all EventTypes', () async {
2019-07-22 09:54:06 +00:00
Event event;
jsonObj['type'] = 'm.room.avatar';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomAvatar);
jsonObj['type'] = 'm.room.name';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomName);
jsonObj['type'] = 'm.room.topic';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomTopic);
jsonObj['type'] = 'm.room.aliases';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomAliases);
jsonObj['type'] = 'm.room.canonical_alias';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomCanonicalAlias);
jsonObj['type'] = 'm.room.create';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomCreate);
jsonObj['type'] = 'm.room.join_rules';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomJoinRules);
jsonObj['type'] = 'm.room.member';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomMember);
jsonObj['type'] = 'm.room.power_levels';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.RoomPowerLevels);
jsonObj['type'] = 'm.room.guest_access';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.GuestAccess);
jsonObj['type'] = 'm.room.history_visibility';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
expect(event.type, EventTypes.HistoryVisibility);
jsonObj['type'] = 'm.room.message';
jsonObj['content'] = json.decode(jsonObj['content']);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.notice';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Notice);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.emote';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Emote);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.image';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Image);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.video';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Video);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.audio';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Audio);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.file';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.File);
2019-07-22 09:54:06 +00:00
jsonObj['content']['msgtype'] = 'm.location';
2019-07-22 09:54:06 +00:00
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Location);
2019-07-22 09:54:06 +00:00
jsonObj['type'] = 'm.room.message';
jsonObj['content']['msgtype'] = 'm.text';
jsonObj['content']['m.relates_to'] = {};
jsonObj['content']['m.relates_to']['m.in_reply_to'] = {
'event_id': '1234',
2019-07-22 09:54:06 +00:00
};
event = Event.fromJson(jsonObj, null);
2020-01-04 09:31:27 +00:00
expect(event.messageType, MessageTypes.Reply);
2019-07-22 09:54:06 +00:00
});
2019-07-24 08:13:02 +00:00
test('redact', () async {
final room = Room(id: '1234', client: Client('testclient', debug: true));
final redactionEventJson = {
'content': {'reason': 'Spamming'},
'event_id': '143273582443PhrSn:example.org',
'origin_server_ts': 1432735824653,
'redacts': id,
'room_id': '1234',
'sender': '@example:example.org',
'type': 'm.room.redaction',
'unsigned': {'age': 1234}
2019-12-12 12:19:18 +00:00
};
var redactedBecause = Event.fromJson(redactionEventJson, room);
var event = Event.fromJson(jsonObj, room);
2019-12-12 12:19:18 +00:00
event.setRedactionEvent(redactedBecause);
expect(event.redacted, true);
expect(event.redactedBecause.toJson(), redactedBecause.toJson());
expect(event.content.isEmpty, true);
redactionEventJson.remove('redacts');
expect(event.unsigned['redacted_because'], redactionEventJson);
2019-12-12 12:19:18 +00:00
});
test('remove', () async {
var event = Event.fromJson(
jsonObj, Room(id: '1234', client: Client('testclient', debug: true)));
final removed1 = await event.remove();
2019-07-24 08:13:02 +00:00
event.status = 0;
final removed2 = await event.remove();
2019-07-24 08:48:13 +00:00
expect(removed1, false);
expect(removed2, true);
2019-07-24 08:13:02 +00:00
});
test('sendAgain', () async {
var matrix = Client('testclient', debug: true);
2020-01-02 14:09:49 +00:00
matrix.httpClient = FakeMatrixApi();
await matrix.checkServer('https://fakeServer.notExisting');
await matrix.login('test', '1234');
2019-07-24 08:13:02 +00:00
var event = Event.fromJson(
jsonObj, Room(id: '!1234:example.com', client: matrix));
final resp1 = await event.sendAgain();
2019-07-24 08:13:02 +00:00
event.status = -1;
final resp2 = await event.sendAgain(txid: '1234');
2019-07-24 08:13:02 +00:00
expect(resp1, null);
expect(resp2, '42');
2020-05-19 09:28:13 +00:00
await matrix.dispose(closeDatabase: true);
2019-07-24 08:13:02 +00:00
});
2020-02-21 15:05:19 +00:00
test('requestKey', () async {
var matrix = Client('testclient', debug: true);
2020-02-21 15:05:19 +00:00
matrix.httpClient = FakeMatrixApi();
await matrix.checkServer('https://fakeServer.notExisting');
await matrix.login('test', '1234');
2020-02-21 15:05:19 +00:00
var event = Event.fromJson(
jsonObj, Room(id: '!1234:example.com', client: matrix));
2020-02-21 15:05:19 +00:00
String exception;
try {
await event.requestKey();
} catch (e) {
exception = e;
}
expect(exception, 'Session key not unknown');
2020-02-21 15:05:19 +00:00
event = Event.fromJson({
'event_id': id,
'sender': senderID,
'origin_server_ts': timestamp,
'type': 'm.room.encrypted',
'room_id': '1234',
'status': 2,
'content': json.encode({
'msgtype': 'm.bad.encrypted',
'body': DecryptError.UNKNOWN_SESSION,
'algorithm': 'm.megolm.v1.aes-sha2',
'ciphertext': 'AwgAEnACgAkLmt6qF84IK++J7UDH2Za1YVchHyprqTqsg...',
'device_id': 'RJYKSTBOIE',
'sender_key': 'IlRMeOPX2e0MurIyfWEucYBRVOEEUMrOHqn/8mLqMjA',
'session_id': 'X3lUlvLELLYxeTx4yOVu6UDpasGEVO0Jbu+QFnm0cKQ'
2020-02-21 15:05:19 +00:00
}),
}, Room(id: '!1234:example.com', client: matrix));
2020-02-21 15:05:19 +00:00
await event.requestKey();
2020-05-19 09:28:13 +00:00
await matrix.dispose(closeDatabase: true);
2020-02-21 15:05:19 +00:00
});
2019-06-11 09:48:31 +00:00
});
}