famedlysdk/test/client_test.dart

399 lines
14 KiB
Dart
Raw Normal View History

2019-06-09 11:57:33 +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-09 11:57:33 +00:00
*/
import 'dart:async';
import 'package:famedlysdk/src/account_data.dart';
import 'package:famedlysdk/src/client.dart';
import 'package:famedlysdk/src/presence.dart';
import 'package:famedlysdk/src/room.dart';
import 'package:famedlysdk/src/user.dart';
import 'package:famedlysdk/src/sync/event_update.dart';
import 'package:famedlysdk/src/sync/room_update.dart';
import 'package:famedlysdk/src/sync/user_update.dart';
import 'package:famedlysdk/src/utils/matrix_exception.dart';
import 'package:famedlysdk/src/utils/matrix_file.dart';
import 'package:famedlysdk/src/utils/profile.dart';
2019-10-04 09:44:32 +00:00
import 'package:test/test.dart';
import 'fake_matrix_api.dart';
2019-06-09 10:16:48 +00:00
void main() {
Client matrix;
Future<List<RoomUpdate>> roomUpdateListFuture;
Future<List<EventUpdate>> eventUpdateListFuture;
2019-06-11 12:10:50 +00:00
Future<List<UserUpdate>> userUpdateListFuture;
2019-06-09 10:16:48 +00:00
/// All Tests related to the Login
group("FluffyMatrix", () {
/// Check if all Elements get created
2019-10-04 09:44:32 +00:00
matrix = Client("testclient", debug: true);
2020-01-02 14:09:49 +00:00
matrix.httpClient = FakeMatrixApi();
2019-06-09 10:16:48 +00:00
2020-01-02 14:09:49 +00:00
roomUpdateListFuture = matrix.onRoomUpdate.stream.toList();
eventUpdateListFuture = matrix.onEvent.stream.toList();
userUpdateListFuture = matrix.onUserEvent.stream.toList();
2019-06-09 10:16:48 +00:00
2019-06-18 09:37:06 +00:00
test('Login', () async {
2019-08-08 08:31:39 +00:00
int presenceCounter = 0;
int accountDataCounter = 0;
2020-01-04 10:29:38 +00:00
matrix.onPresence.stream.listen((Presence data) {
2019-08-08 08:31:39 +00:00
presenceCounter++;
2020-01-04 10:29:38 +00:00
});
matrix.onAccountData.stream.listen((AccountData data) {
2019-08-08 08:31:39 +00:00
accountDataCounter++;
2020-01-04 10:29:38 +00:00
});
2019-08-08 08:31:39 +00:00
2019-12-29 10:28:33 +00:00
expect(matrix.homeserver, null);
expect(matrix.matrixVersions, null);
2019-06-18 09:37:06 +00:00
2019-12-29 10:28:33 +00:00
try {
await matrix.checkServer("https://fakeserver.wrongaddress");
} on FormatException catch (exception) {
expect(exception != null, true);
}
await matrix.checkServer("https://fakeserver.notexisting");
expect(matrix.homeserver, "https://fakeserver.notexisting");
expect(matrix.matrixVersions,
["r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0", "r0.5.0"]);
2019-06-18 09:37:06 +00:00
2020-01-02 14:09:49 +00:00
final Map<String, dynamic> resp = await matrix
.jsonRequest(type: HTTPType.POST, action: "/client/r0/login", data: {
2019-06-09 10:16:48 +00:00
"type": "m.login.password",
"user": "test",
"password": "1234",
"initial_device_display_name": "Fluffy Matrix Client"
});
2019-06-18 09:37:06 +00:00
Future<LoginState> loginStateFuture =
2020-01-02 14:09:49 +00:00
matrix.onLoginStateChanged.stream.first;
Future<bool> firstSyncFuture = matrix.onFirstSync.stream.first;
Future<dynamic> syncFuture = matrix.onSync.stream.first;
2019-06-09 10:16:48 +00:00
2020-01-02 14:09:49 +00:00
matrix.connect(
2019-06-09 10:16:48 +00:00
newToken: resp["access_token"],
newUserID: resp["user_id"],
newHomeserver: matrix.homeserver,
newDeviceName: "Text Matrix Client",
newDeviceID: resp["device_id"],
newMatrixVersions: matrix.matrixVersions,
newLazyLoadMembers: matrix.lazyLoadMembers);
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-06-09 10:16:48 +00:00
expect(matrix.accessToken == resp["access_token"], true);
expect(matrix.deviceName == "Text Matrix Client", true);
expect(matrix.deviceID == resp["device_id"], true);
expect(matrix.userID == resp["user_id"], true);
LoginState loginState = await loginStateFuture;
bool firstSync = await firstSyncFuture;
dynamic sync = await syncFuture;
expect(loginState, LoginState.logged);
expect(firstSync, true);
expect(sync["next_batch"] == matrix.prevBatch, true);
2019-08-07 10:27:02 +00:00
2019-12-04 09:58:47 +00:00
expect(matrix.accountData.length, 3);
2019-08-08 07:58:37 +00:00
expect(matrix.getDirectChatFromUserId("@bob:example.com"),
2019-08-08 08:31:39 +00:00
"!726s6s6q:example.com");
2020-01-02 14:09:49 +00:00
expect(matrix.rooms[1].directChatMatrixID, "@bob:example.com");
2019-08-08 07:58:37 +00:00
expect(matrix.directChats, matrix.accountData["m.direct"].content);
expect(matrix.presences.length, 1);
2020-01-02 14:09:49 +00:00
expect(matrix.rooms[1].ephemerals.length, 2);
expect(matrix.rooms[1].typingUsers.length, 1);
expect(matrix.rooms[1].typingUsers[0].id, "@alice:example.com");
expect(matrix.rooms[1].roomAccountData.length, 3);
2019-10-20 09:44:14 +00:00
expect(
2020-01-02 14:09:49 +00:00
matrix.rooms[1].roomAccountData["m.receipt"]
2019-10-25 08:02:56 +00:00
.content["@alice:example.com"]["ts"],
2019-10-20 09:44:14 +00:00
1436451550453);
2019-10-25 08:02:56 +00:00
expect(
2020-01-02 14:09:49 +00:00
matrix.rooms[1].roomAccountData["m.receipt"]
2019-10-25 08:02:56 +00:00
.content["@alice:example.com"]["event_id"],
"7365636s6r6432:example.com");
2020-01-02 14:09:49 +00:00
expect(matrix.rooms.length, 2);
expect(matrix.rooms[1].canonicalAlias,
2019-08-08 07:58:37 +00:00
"#famedlyContactDiscovery:${matrix.userID.split(":")[1]}");
final List<User> contacts = await matrix.loadFamedlyContacts();
expect(contacts.length, 1);
2019-08-08 08:31:39 +00:00
expect(contacts[0].senderId, "@alice:example.org");
expect(
2019-11-30 09:21:11 +00:00
matrix.presences["@alice:example.com"].presence, PresenceType.online);
2019-08-08 08:31:39 +00:00
expect(presenceCounter, 1);
2019-12-04 09:58:47 +00:00
expect(accountDataCounter, 3);
2019-10-14 16:50:10 +00:00
2020-01-02 14:09:49 +00:00
matrix.handleSync({
"rooms": {
"join": {
"!726s6s6q:example.com": {
"state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.canonical_alias",
"content": {"alias": ""},
"state_key": "",
"origin_server_ts": 1417731086799,
"event_id": "66697273743033:example.com"
}
]
}
}
}
}
});
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-10-14 16:50:10 +00:00
expect(
2020-01-02 14:09:49 +00:00
matrix.getRoomByAlias(
2019-10-14 16:50:10 +00:00
"#famedlyContactDiscovery:${matrix.userID.split(":")[1]}"),
null);
final List<User> altContacts = await matrix.loadFamedlyContacts();
expect(altContacts.length, 2);
expect(altContacts[0].senderId, "@alice:example.com");
2019-06-18 09:37:06 +00:00
});
2019-06-09 10:16:48 +00:00
2019-06-18 09:37:06 +00:00
test('Try to get ErrorResponse', () async {
2019-12-29 10:28:33 +00:00
MatrixException expectedException;
try {
2020-01-02 14:09:49 +00:00
await matrix.jsonRequest(
type: HTTPType.PUT, action: "/non/existing/path");
2019-12-29 10:28:33 +00:00
} on MatrixException catch (exception) {
expectedException = exception;
}
expect(expectedException.error, MatrixError.M_UNRECOGNIZED);
2019-06-09 10:16:48 +00:00
});
2019-06-18 09:37:06 +00:00
test('Logout', () async {
2020-01-02 14:09:49 +00:00
await matrix.jsonRequest(
type: HTTPType.POST, action: "/client/r0/logout");
2019-06-09 10:16:48 +00:00
2019-06-18 09:37:06 +00:00
Future<LoginState> loginStateFuture =
2020-01-02 14:09:49 +00:00
matrix.onLoginStateChanged.stream.first;
2019-06-09 10:16:48 +00:00
2020-01-02 14:09:49 +00:00
matrix.clear();
2019-06-09 10:16:48 +00:00
expect(matrix.accessToken == null, true);
expect(matrix.homeserver == null, true);
expect(matrix.userID == null, true);
expect(matrix.deviceID == null, true);
expect(matrix.deviceName == null, true);
expect(matrix.matrixVersions == null, true);
expect(matrix.lazyLoadMembers == null, true);
expect(matrix.prevBatch == null, true);
LoginState loginState = await loginStateFuture;
expect(loginState, LoginState.loggedOut);
});
2019-06-18 09:37:06 +00:00
test('Room Update Test', () async {
2020-01-02 14:33:26 +00:00
await matrix.onRoomUpdate.close();
2019-06-09 10:16:48 +00:00
List<RoomUpdate> roomUpdateList = await roomUpdateListFuture;
2020-01-02 14:09:49 +00:00
expect(roomUpdateList.length, 3);
2019-06-18 09:37:06 +00:00
expect(roomUpdateList[0].id == "!726s6s6q:example.com", true);
expect(roomUpdateList[0].membership == Membership.join, true);
2019-06-18 09:37:06 +00:00
expect(roomUpdateList[0].prev_batch == "t34-23535_0_0", true);
expect(roomUpdateList[0].limitedTimeline == true, true);
expect(roomUpdateList[0].notification_count == 2, true);
expect(roomUpdateList[0].highlight_count == 2, true);
expect(roomUpdateList[1].id == "!696r7674:example.com", true);
expect(roomUpdateList[1].membership == Membership.invite, true);
2019-06-18 09:37:06 +00:00
expect(roomUpdateList[1].prev_batch == "", true);
expect(roomUpdateList[1].limitedTimeline == false, true);
expect(roomUpdateList[1].notification_count == 0, true);
expect(roomUpdateList[1].highlight_count == 0, true);
2019-06-09 10:16:48 +00:00
});
2019-06-18 09:37:06 +00:00
test('Event Update Test', () async {
2020-01-02 14:33:26 +00:00
await matrix.onEvent.close();
2019-06-09 10:16:48 +00:00
List<EventUpdate> eventUpdateList = await eventUpdateListFuture;
2019-10-20 09:44:14 +00:00
expect(eventUpdateList.length, 12);
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[0].eventType, "m.room.member");
expect(eventUpdateList[0].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[0].type, "state");
2019-06-09 10:16:48 +00:00
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[1].eventType, "m.room.canonical_alias");
expect(eventUpdateList[1].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[1].type, "state");
2019-06-09 10:16:48 +00:00
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[2].eventType, "m.room.member");
expect(eventUpdateList[2].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[2].type, "timeline");
2019-06-09 10:16:48 +00:00
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[3].eventType, "m.room.message");
expect(eventUpdateList[3].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[3].type, "timeline");
2019-06-09 10:16:48 +00:00
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[4].eventType, "m.typing");
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[4].roomID, "!726s6s6q:example.com");
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[4].type, "ephemeral");
2019-06-09 10:16:48 +00:00
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[5].eventType, "m.receipt");
2019-08-08 08:31:39 +00:00
expect(eventUpdateList[5].roomID, "!726s6s6q:example.com");
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[5].type, "ephemeral");
expect(eventUpdateList[6].eventType, "m.receipt");
expect(eventUpdateList[6].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[6].type, "account_data");
expect(eventUpdateList[7].eventType, "m.tag");
expect(eventUpdateList[7].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[7].type, "account_data");
expect(eventUpdateList[8].eventType, "org.example.custom.room.config");
expect(eventUpdateList[8].roomID, "!726s6s6q:example.com");
expect(eventUpdateList[8].type, "account_data");
2019-06-09 10:16:48 +00:00
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[9].eventType, "m.room.name");
expect(eventUpdateList[9].roomID, "!696r7674:example.com");
expect(eventUpdateList[9].type, "invite_state");
2019-06-09 10:16:48 +00:00
2019-10-20 09:44:14 +00:00
expect(eventUpdateList[10].eventType, "m.room.member");
expect(eventUpdateList[10].roomID, "!696r7674:example.com");
expect(eventUpdateList[10].type, "invite_state");
2019-06-11 12:10:50 +00:00
});
2019-06-18 09:37:06 +00:00
test('User Update Test', () async {
2020-01-02 14:33:26 +00:00
await matrix.onUserEvent.close();
2019-06-09 10:16:48 +00:00
2019-06-11 12:10:50 +00:00
List<UserUpdate> eventUpdateList = await userUpdateListFuture;
2019-06-09 10:16:48 +00:00
2019-12-04 09:58:47 +00:00
expect(eventUpdateList.length, 5);
2019-06-09 10:16:48 +00:00
2019-12-04 09:58:47 +00:00
expect(eventUpdateList[0].eventType, "m.presence");
expect(eventUpdateList[0].type, "presence");
2019-06-09 10:16:48 +00:00
2019-12-04 09:58:47 +00:00
expect(eventUpdateList[1].eventType, "m.push_rules");
expect(eventUpdateList[1].type, "account_data");
expect(eventUpdateList[2].eventType, "org.example.custom.config");
expect(eventUpdateList[2].type, "account_data");
2019-06-09 10:16:48 +00:00
});
2019-06-18 09:37:06 +00:00
test('Login', () async {
2019-10-04 09:44:32 +00:00
matrix = Client("testclient", debug: true);
2020-01-02 14:09:49 +00:00
matrix.httpClient = FakeMatrixApi();
2019-10-04 09:44:32 +00:00
2020-01-02 14:09:49 +00:00
roomUpdateListFuture = matrix.onRoomUpdate.stream.toList();
eventUpdateListFuture = matrix.onEvent.stream.toList();
userUpdateListFuture = matrix.onUserEvent.stream.toList();
2019-06-18 09:37:06 +00:00
final bool checkResp =
await matrix.checkServer("https://fakeServer.notExisting");
final bool loginResp = await matrix.login("test", "1234");
2019-06-09 10:16:48 +00:00
2019-06-18 09:37:06 +00:00
expect(checkResp, true);
expect(loginResp, true);
});
test('createRoom', () async {
2019-06-18 09:37:06 +00:00
final List<User> users = [
2019-08-08 09:54:39 +00:00
User("@alice:fakeServer.notExisting"),
User("@bob:fakeServer.notExisting")
2019-06-18 09:37:06 +00:00
];
final String newID = await matrix.createRoom(invite: users);
2019-06-18 09:37:06 +00:00
expect(newID, "!1234:fakeServer.notExisting");
});
2019-09-09 13:22:02 +00:00
test('upload', () async {
2019-10-18 11:05:07 +00:00
final MatrixFile testFile =
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
2019-10-16 09:09:09 +00:00
2020-01-02 14:09:49 +00:00
final dynamic resp = await matrix.upload(testFile);
2019-09-09 13:22:02 +00:00
expect(resp, "mxc://example.com/AQwafuaFswefuhsfAFAgsw");
});
test('setAvatar', () async {
2019-10-18 11:05:07 +00:00
final MatrixFile testFile =
MatrixFile(bytes: [], path: "fake/path/file.jpeg");
2019-12-29 10:28:33 +00:00
await matrix.setAvatar(testFile);
2019-09-09 13:22:02 +00:00
});
test('getPushrules', () async {
2020-01-02 14:09:49 +00:00
final pushrules = await matrix.getPushrules();
expect(pushrules != null, true);
});
2019-07-24 07:59:29 +00:00
test('setPushers', () async {
2020-01-02 14:09:49 +00:00
await matrix.setPushers("abcdefg", "http", "com.famedly.famedlysdk",
"famedlySDK", "GitLabCi", "en", "https://examplepushserver.com",
format: "event_id_only");
2019-07-24 07:59:29 +00:00
});
test('joinRoomById', () async {
final String roomID = "1234";
final Map<String, dynamic> resp = await matrix.joinRoomById(roomID);
expect(resp["room_id"], roomID);
});
2019-11-29 16:19:32 +00:00
test('get archive', () async {
2019-12-19 11:26:21 +00:00
List<Room> archive = await matrix.archive;
2019-11-29 16:19:32 +00:00
2020-01-02 14:33:26 +00:00
await Future.delayed(Duration(milliseconds: 50));
2019-12-19 11:26:21 +00:00
expect(archive.length, 2);
expect(archive[0].id, "!5345234234:example.com");
expect(archive[0].membership, Membership.leave);
expect(archive[0].name, "The room name");
expect(archive[0].lastMessage, "This is an example text message");
expect(archive[0].roomAccountData.length, 1);
expect(archive[1].id, "!5345234235:example.com");
expect(archive[1].membership, Membership.leave);
expect(archive[1].name, "The room name 2");
2019-11-29 16:19:32 +00:00
});
2019-11-30 09:36:30 +00:00
test('getProfileFromUserId', () async {
final Profile profile =
await matrix.getProfileFromUserId("@getme:example.com");
expect(profile.avatarUrl.mxc, "mxc://test");
expect(profile.displayname, "You got me");
expect(profile.content["avatar_url"], profile.avatarUrl.mxc);
expect(profile.content["displayname"], profile.displayname);
});
2019-06-18 09:37:06 +00:00
test('Logout when token is unknown', () async {
Future<LoginState> loginStateFuture =
2020-01-02 14:09:49 +00:00
matrix.onLoginStateChanged.stream.first;
2019-12-29 10:28:33 +00:00
try {
2020-01-02 14:09:49 +00:00
await matrix.jsonRequest(
type: HTTPType.DELETE, action: "/unknown/token");
2019-12-29 10:28:33 +00:00
} on MatrixException catch (exception) {
expect(exception.error, MatrixError.M_UNKNOWN_TOKEN);
}
2019-06-09 10:16:48 +00:00
LoginState state = await loginStateFuture;
expect(state, LoginState.loggedOut);
expect(matrix.isLogged(), false);
});
});
}