More tests for Client and Connection

This commit is contained in:
Christian Pauly 2019-06-18 11:37:06 +02:00
parent ce64c110c8
commit e1352e0f9e
2 changed files with 268 additions and 256 deletions

View file

@ -24,6 +24,7 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/Connection.dart';
import 'package:famedlysdk/src/User.dart';
import 'package:famedlysdk/src/sync/EventUpdate.dart';
import 'package:famedlysdk/src/sync/RoomUpdate.dart';
import 'package:famedlysdk/src/sync/UserUpdate.dart';
@ -43,10 +44,8 @@ void main() {
/// Check if all Elements get created
final create = (WidgetTester tester) {
matrix = Client("testclient");
matrix = Client("testclient", debug: true);
matrix.connection.httpClient = FakeMatrixApi();
matrix.homeserver = "https://fakeServer.notExisting";
roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList();
eventUpdateListFuture = matrix.connection.onEvent.stream.toList();
@ -54,33 +53,23 @@ void main() {
};
testWidgets('should get created', create);
test("Get version", () async {
final versionResp =
await matrix.connection.jsonRequest(type: "GET", action: "/client/versions");
expect(versionResp is ErrorResponse, false);
expect(versionResp["versions"].indexOf("r0.4.0") != -1, true);
matrix.matrixVersions = List<String>.from(versionResp["versions"]);
matrix.lazyLoadMembers = true;
});
test('Login', () async {
Future<ErrorResponse> errorFuture =
matrix.connection.onError.stream.first;
test("Get login types", () async {
final resp =
await matrix.connection.jsonRequest(type: "GET", action: "/client/r0/login");
expect(resp is ErrorResponse, false);
expect(resp["flows"] is List<dynamic>, true);
bool hasMLoginType = false;
for (int i = 0; i < resp["flows"].length; i++)
if (resp["flows"][i]["type"] is String &&
resp["flows"][i]["type"] == "m.login.password") {
hasMLoginType = true;
break;
}
expect(hasMLoginType, true);
});
final bool checkResp1 =
await matrix.checkServer("https://fakeServer.wrongaddress");
final bool checkResp2 =
await matrix.checkServer("https://fakeServer.notExisting");
final loginText = () async{
final resp = await matrix
.connection.jsonRequest(type: "POST", action: "/client/r0/login", data: {
ErrorResponse checkError = await errorFuture;
expect(checkResp1, false);
expect(checkResp2, true);
expect(checkError.errcode, "NO_RESPONSE");
final resp = await matrix.connection
.jsonRequest(type: "POST", action: "/client/r0/login", data: {
"type": "m.login.password",
"user": "test",
"password": "1234",
@ -88,7 +77,8 @@ void main() {
});
expect(resp is ErrorResponse, false);
Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first;
Future<LoginState> loginStateFuture =
matrix.connection.onLoginStateChanged.stream.first;
Future<bool> firstSyncFuture = matrix.connection.onFirstSync.stream.first;
Future<dynamic> syncFuture = matrix.connection.onSync.stream.first;
@ -113,22 +103,21 @@ void main() {
expect(loginState, LoginState.logged);
expect(firstSync, true);
expect(sync["next_batch"] == matrix.prevBatch, true);
};
});
test('Login', loginText);
test('Try to get ErrorResponse', () async{
final resp = await matrix
.connection.jsonRequest(type: "PUT", action: "/non/existing/path");
test('Try to get ErrorResponse', () async {
final resp = await matrix.connection
.jsonRequest(type: "PUT", action: "/non/existing/path");
expect(resp is ErrorResponse, true);
});
test('Logout', () async{
final dynamic resp = await matrix
.connection.jsonRequest(type: "POST", action: "/client/r0/logout");
test('Logout', () async {
final dynamic resp = await matrix.connection
.jsonRequest(type: "POST", action: "/client/r0/logout");
expect(resp is ErrorResponse, false);
Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first;
Future<LoginState> loginStateFuture =
matrix.connection.onLoginStateChanged.stream.first;
matrix.connection.clear();
@ -145,101 +134,119 @@ void main() {
expect(loginState, LoginState.loggedOut);
});
test('Room Update Test', () async{
test('Room Update Test', () async {
matrix.connection.onRoomUpdate.close();
List<RoomUpdate> roomUpdateList = await roomUpdateListFuture;
expect(roomUpdateList.length,3);
expect(roomUpdateList.length, 3);
expect(roomUpdateList[0].id=="!726s6s6q:example.com", true);
expect(roomUpdateList[0].membership=="join", true);
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[0].id == "!726s6s6q:example.com", true);
expect(roomUpdateList[0].membership == "join", true);
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=="invite", true);
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);
expect(roomUpdateList[1].id == "!696r7674:example.com", true);
expect(roomUpdateList[1].membership == "invite", true);
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);
expect(roomUpdateList[2].id=="!5345234234:example.com", true);
expect(roomUpdateList[2].membership=="leave", true);
expect(roomUpdateList[2].prev_batch=="", true);
expect(roomUpdateList[2].limitedTimeline==false, true);
expect(roomUpdateList[2].notification_count==0, true);
expect(roomUpdateList[2].highlight_count==0, true);
expect(roomUpdateList[2].id == "!5345234234:example.com", true);
expect(roomUpdateList[2].membership == "leave", true);
expect(roomUpdateList[2].prev_batch == "", true);
expect(roomUpdateList[2].limitedTimeline == false, true);
expect(roomUpdateList[2].notification_count == 0, true);
expect(roomUpdateList[2].highlight_count == 0, true);
});
test('Event Update Test', () async{
test('Event Update Test', () async {
matrix.connection.onEvent.close();
List<EventUpdate> eventUpdateList = await eventUpdateListFuture;
expect(eventUpdateList.length,7);
expect(eventUpdateList.length, 7);
expect(eventUpdateList[0].eventType=="m.room.member", true);
expect(eventUpdateList[0].roomID=="!726s6s6q:example.com", true);
expect(eventUpdateList[0].type=="state", true);
expect(eventUpdateList[0].eventType == "m.room.member", true);
expect(eventUpdateList[0].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[0].type == "state", true);
expect(eventUpdateList[1].eventType=="m.room.member", true);
expect(eventUpdateList[1].roomID=="!726s6s6q:example.com", true);
expect(eventUpdateList[1].type=="timeline", true);
expect(eventUpdateList[1].eventType == "m.room.member", true);
expect(eventUpdateList[1].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[1].type == "timeline", true);
expect(eventUpdateList[2].eventType=="m.room.message", true);
expect(eventUpdateList[2].roomID=="!726s6s6q:example.com", true);
expect(eventUpdateList[2].type=="timeline", true);
expect(eventUpdateList[2].eventType == "m.room.message", true);
expect(eventUpdateList[2].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[2].type == "timeline", true);
expect(eventUpdateList[3].eventType=="m.tag", true);
expect(eventUpdateList[3].roomID=="!726s6s6q:example.com", true);
expect(eventUpdateList[3].type=="account_data", true);
expect(eventUpdateList[3].eventType == "m.tag", true);
expect(eventUpdateList[3].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[3].type == "account_data", true);
expect(eventUpdateList[4].eventType=="org.example.custom.room.config", true);
expect(eventUpdateList[4].roomID=="!726s6s6q:example.com", true);
expect(eventUpdateList[4].type=="account_data", true);
expect(eventUpdateList[4].eventType == "org.example.custom.room.config",
true);
expect(eventUpdateList[4].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[4].type == "account_data", true);
expect(eventUpdateList[5].eventType=="m.room.name", true);
expect(eventUpdateList[5].roomID=="!696r7674:example.com", true);
expect(eventUpdateList[5].type=="invite_state", true);
expect(eventUpdateList[5].eventType == "m.room.name", true);
expect(eventUpdateList[5].roomID == "!696r7674:example.com", true);
expect(eventUpdateList[5].type == "invite_state", true);
expect(eventUpdateList[6].eventType=="m.room.member", true);
expect(eventUpdateList[6].roomID=="!696r7674:example.com", true);
expect(eventUpdateList[6].type=="invite_state", true);
expect(eventUpdateList[6].eventType == "m.room.member", true);
expect(eventUpdateList[6].roomID == "!696r7674:example.com", true);
expect(eventUpdateList[6].type == "invite_state", true);
});
test('User Update Test', () async{
test('User Update Test', () async {
matrix.connection.onUserEvent.close();
List<UserUpdate> eventUpdateList = await userUpdateListFuture;
expect(eventUpdateList.length,3);
expect(eventUpdateList.length, 3);
expect(eventUpdateList[0].eventType=="m.presence", true);
expect(eventUpdateList[0].type=="presence", true);
expect(eventUpdateList[0].eventType == "m.presence", true);
expect(eventUpdateList[0].type == "presence", true);
expect(eventUpdateList[1].eventType=="org.example.custom.config", true);
expect(eventUpdateList[1].type=="account_data", true);
expect(eventUpdateList[1].eventType == "org.example.custom.config", true);
expect(eventUpdateList[1].type == "account_data", true);
expect(eventUpdateList[2].eventType=="m.new_device", true);
expect(eventUpdateList[2].type=="to_device", true);
expect(eventUpdateList[2].eventType == "m.new_device", true);
expect(eventUpdateList[2].type == "to_device", true);
});
testWidgets('should get created', create);
test('Login', loginText);
test('Login', () async {
final bool checkResp =
await matrix.checkServer("https://fakeServer.notExisting");
test('Logout when token is unknown', () async{
Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first;
final resp = await matrix
.connection.jsonRequest(type: "DELETE", action: "/unknown/token");
final bool loginResp = await matrix.login("test", "1234");
expect(checkResp, true);
expect(loginResp, true);
});
test('createGroup', () async {
final List<User> users = [
User("@alice:fakeServer.notExisting"),
User("@bob:fakeServer.notExisting")
];
final String newID = await matrix.createGroup(users);
expect(newID, "!1234:fakeServer.notExisting");
});
test('Logout when token is unknown', () async {
Future<LoginState> loginStateFuture =
matrix.connection.onLoginStateChanged.stream.first;
final resp = await matrix.connection
.jsonRequest(type: "DELETE", action: "/unknown/token");
LoginState state = await loginStateFuture;
expect(state, LoginState.loggedOut);
expect(matrix.isLogged(), false);
});
});
}

View file

@ -30,186 +30,191 @@ import 'package:http/http.dart';
class FakeMatrixApi extends MockClient {
FakeMatrixApi()
: super((request) async {
// Collect data from Request
final String action = request.url.path.split("/_matrix")[1];
final String method = request.method;
final dynamic data =
method == "GET" ? request.url.queryParameters : request.body;
var res = {};
// Collect data from Request
final String action = request.url.path.split("/_matrix")[1];
final String method = request.method;
final dynamic data =
method == "GET" ? request.url.queryParameters : request.body;
var res = {};
//print("$method request to $action with Data: $data");
//print("$method request to $action with Data: $data");
// Sync requests with timeout
if (data is Map<String, dynamic> && data["timeout"] is String) {
await new Future.delayed(Duration(seconds: 5));
}
// Sync requests with timeout
if (data is Map<String, dynamic> && data["timeout"] is String) {
await new Future.delayed(Duration(seconds: 5));
}
// Call API
if (api.containsKey(method) && api[method].containsKey(action))
res = api[method][action](data);
else
res = {
"errcode": "M_UNRECOGNIZED",
"error": "Unrecognized request"
};
if (request.url.origin != "https://fakeserver.notexisting")
return Response(
"<html><head></head><body>Not found...</body></html>", 50);
return Response(json.encode(res), 100);
});
// Call API
if (api.containsKey(method) && api[method].containsKey(action))
res = api[method][action](data);
else
res = {
"errcode": "M_UNRECOGNIZED",
"error": "Unrecognized request"
};
return Response(json.encode(res), 100);
});
static final Map<String, Map<String, dynamic>> api = {
"GET": {
"/client/versions": (var req) => {
"versions": ["r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0"],
"unstable_features": {"m.lazy_load_members": true},
},
"versions": ["r0.0.1", "r0.1.0", "r0.2.0", "r0.3.0", "r0.4.0"],
"unstable_features": {"m.lazy_load_members": true},
},
"/client/r0/login": (var req) => {
"flows": [
{"type": "m.login.password"}
]
},
"flows": [
{"type": "m.login.password"}
]
},
"/client/r0/sync": (var req) => {
"next_batch": Random().nextDouble().toString(),
"presence": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.presence",
"content": {"presence": "online"}
}
]
},
"account_data": {
"events": [
{
"type": "org.example.custom.config",
"content": {"custom_config_key": "custom_config_value"}
}
]
},
"to_device": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.new_device",
"content": {
"device_id": "XYZABCDE",
"rooms": ["!726s6s6q:example.com"]
}
}
]
},
"rooms": {
"join": {
"!726s6s6q:example.com": {
"unread_notifications": {
"highlight_count": 2,
"notification_count": 2,
},
"state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.member",
"state_key": "@alice:example.com",
"content": {"membership": "join"},
"origin_server_ts": 1417731086795,
"event_id": "66697273743031:example.com"
}
]
},
"timeline": {
"events": [
{
"sender": "@bob:example.com",
"type": "m.room.member",
"state_key": "@bob:example.com",
"content": {"membership": "join"},
"prev_content": {"membership": "invite"},
"origin_server_ts": 1417731086795,
"event_id": "7365636s6r6432:example.com"
},
{
"sender": "@alice:example.com",
"type": "m.room.message",
"txn_id": "1234",
"content": {"body": "I am a fish", "msgtype": "m.text"},
"origin_server_ts": 1417731086797,
"event_id": "74686972643033:example.com"
}
],
"limited": true,
"prev_batch": "t34-23535_0_0"
},
"ephemeral": {
"events": [
{
"type": "m.typing",
"content": {
"user_ids": ["@alice:example.com"]
}
}
]
},
"account_data": {
"events": [
{
"type": "m.tag",
"content": {
"tags": {
"work": {"order": 1}
}
}
},
{
"type": "org.example.custom.room.config",
"content": {"custom_config_key": "custom_config_value"}
}
]
}
}
},
"invite": {
"!696r7674:example.com": {
"invite_state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.name",
"state_key": "",
"content": {"name": "My Room Name"}
},
{
"sender": "@alice:example.com",
"type": "m.room.member",
"state_key": "@bob:example.com",
"content": {"membership": "invite"}
}
]
}
}
},
"leave": {
"!5345234234:example.com": {
"timeline": {"events": []}
"next_batch": Random().nextDouble().toString(),
"presence": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.presence",
"content": {"presence": "online"}
}
]
},
"account_data": {
"events": [
{
"type": "org.example.custom.config",
"content": {"custom_config_key": "custom_config_value"}
}
]
},
"to_device": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.new_device",
"content": {
"device_id": "XYZABCDE",
"rooms": ["!726s6s6q:example.com"]
}
}
]
},
"rooms": {
"join": {
"!726s6s6q:example.com": {
"unread_notifications": {
"highlight_count": 2,
"notification_count": 2,
},
"state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.member",
"state_key": "@alice:example.com",
"content": {"membership": "join"},
"origin_server_ts": 1417731086795,
"event_id": "66697273743031:example.com"
}
]
},
"timeline": {
"events": [
{
"sender": "@bob:example.com",
"type": "m.room.member",
"state_key": "@bob:example.com",
"content": {"membership": "join"},
"prev_content": {"membership": "invite"},
"origin_server_ts": 1417731086795,
"event_id": "7365636s6r6432:example.com"
},
{
"sender": "@alice:example.com",
"type": "m.room.message",
"txn_id": "1234",
"content": {"body": "I am a fish", "msgtype": "m.text"},
"origin_server_ts": 1417731086797,
"event_id": "74686972643033:example.com"
}
],
"limited": true,
"prev_batch": "t34-23535_0_0"
},
"ephemeral": {
"events": [
{
"type": "m.typing",
"content": {
"user_ids": ["@alice:example.com"]
}
}
]
},
"account_data": {
"events": [
{
"type": "m.tag",
"content": {
"tags": {
"work": {"order": 1}
}
}
},
{
"type": "org.example.custom.room.config",
"content": {"custom_config_key": "custom_config_value"}
}
]
}
}
},
"invite": {
"!696r7674:example.com": {
"invite_state": {
"events": [
{
"sender": "@alice:example.com",
"type": "m.room.name",
"state_key": "",
"content": {"name": "My Room Name"}
},
{
"sender": "@alice:example.com",
"type": "m.room.member",
"state_key": "@bob:example.com",
"content": {"membership": "invite"}
}
]
}
}
},
"leave": {
"!5345234234:example.com": {
"timeline": {"events": []}
},
},
}
},
}
},
},
"POST": {
"/client/r0/login": (var req) => {
"user_id": "@test:fakeServer.notExisting",
"access_token": "abc123",
"device_id": "GHTYAJCE"
},
"user_id": "@test:fakeServer.notExisting",
"access_token": "abc123",
"device_id": "GHTYAJCE"
},
"/client/r0/logout": (var reqI) => {},
"/client/r0/logout/all": (var reqI) => {},
"/client/r0/createRoom": (var reqI) => {
"room_id": "!1234:fakeServer.notExisting",
}
},
"PUT": {},
"DELETE": {
"/unknown/token": (var req) => {
"errcode": "M_UNKNOWN_TOKEN"
},
"/unknown/token": (var req) => {"errcode": "M_UNKNOWN_TOKEN"},
},
};
}