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:flutter_test/flutter_test.dart';
import 'package:famedlysdk/src/Client.dart'; import 'package:famedlysdk/src/Client.dart';
import 'package:famedlysdk/src/Connection.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/EventUpdate.dart';
import 'package:famedlysdk/src/sync/RoomUpdate.dart'; import 'package:famedlysdk/src/sync/RoomUpdate.dart';
import 'package:famedlysdk/src/sync/UserUpdate.dart'; import 'package:famedlysdk/src/sync/UserUpdate.dart';
@ -43,10 +44,8 @@ void main() {
/// Check if all Elements get created /// Check if all Elements get created
final create = (WidgetTester tester) { final create = (WidgetTester tester) {
matrix = Client("testclient", debug: true);
matrix = Client("testclient");
matrix.connection.httpClient = FakeMatrixApi(); matrix.connection.httpClient = FakeMatrixApi();
matrix.homeserver = "https://fakeServer.notExisting";
roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList(); roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList();
eventUpdateListFuture = matrix.connection.onEvent.stream.toList(); eventUpdateListFuture = matrix.connection.onEvent.stream.toList();
@ -54,33 +53,23 @@ void main() {
}; };
testWidgets('should get created', create); testWidgets('should get created', create);
test("Get version", () async { test('Login', () async {
final versionResp = Future<ErrorResponse> errorFuture =
await matrix.connection.jsonRequest(type: "GET", action: "/client/versions"); matrix.connection.onError.stream.first;
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("Get login types", () async { final bool checkResp1 =
final resp = await matrix.checkServer("https://fakeServer.wrongaddress");
await matrix.connection.jsonRequest(type: "GET", action: "/client/r0/login"); final bool checkResp2 =
expect(resp is ErrorResponse, false); await matrix.checkServer("https://fakeServer.notExisting");
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 loginText = () async{ ErrorResponse checkError = await errorFuture;
final resp = await matrix
.connection.jsonRequest(type: "POST", action: "/client/r0/login", data: { 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", "type": "m.login.password",
"user": "test", "user": "test",
"password": "1234", "password": "1234",
@ -88,7 +77,8 @@ void main() {
}); });
expect(resp is ErrorResponse, false); 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<bool> firstSyncFuture = matrix.connection.onFirstSync.stream.first;
Future<dynamic> syncFuture = matrix.connection.onSync.stream.first; Future<dynamic> syncFuture = matrix.connection.onSync.stream.first;
@ -113,22 +103,21 @@ void main() {
expect(loginState, LoginState.logged); expect(loginState, LoginState.logged);
expect(firstSync, true); expect(firstSync, true);
expect(sync["next_batch"] == matrix.prevBatch, true); expect(sync["next_batch"] == matrix.prevBatch, true);
}; });
test('Login', loginText);
test('Try to get ErrorResponse', () async { test('Try to get ErrorResponse', () async {
final resp = await matrix final resp = await matrix.connection
.connection.jsonRequest(type: "PUT", action: "/non/existing/path"); .jsonRequest(type: "PUT", action: "/non/existing/path");
expect(resp is ErrorResponse, true); expect(resp is ErrorResponse, true);
}); });
test('Logout', () async { test('Logout', () async {
final dynamic resp = await matrix final dynamic resp = await matrix.connection
.connection.jsonRequest(type: "POST", action: "/client/r0/logout"); .jsonRequest(type: "POST", action: "/client/r0/logout");
expect(resp is ErrorResponse, false); expect(resp is ErrorResponse, false);
Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; Future<LoginState> loginStateFuture =
matrix.connection.onLoginStateChanged.stream.first;
matrix.connection.clear(); matrix.connection.clear();
@ -197,7 +186,8 @@ void main() {
expect(eventUpdateList[3].roomID == "!726s6s6q:example.com", true); expect(eventUpdateList[3].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[3].type == "account_data", true); expect(eventUpdateList[3].type == "account_data", true);
expect(eventUpdateList[4].eventType=="org.example.custom.room.config", true); expect(eventUpdateList[4].eventType == "org.example.custom.room.config",
true);
expect(eventUpdateList[4].roomID == "!726s6s6q:example.com", true); expect(eventUpdateList[4].roomID == "!726s6s6q:example.com", true);
expect(eventUpdateList[4].type == "account_data", true); expect(eventUpdateList[4].type == "account_data", true);
@ -229,17 +219,34 @@ void main() {
testWidgets('should get created', create); testWidgets('should get created', create);
test('Login', loginText); test('Login', () async {
final bool checkResp =
await matrix.checkServer("https://fakeServer.notExisting");
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 { test('Logout when token is unknown', () async {
Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; Future<LoginState> loginStateFuture =
final resp = await matrix matrix.connection.onLoginStateChanged.stream.first;
.connection.jsonRequest(type: "DELETE", action: "/unknown/token"); final resp = await matrix.connection
.jsonRequest(type: "DELETE", action: "/unknown/token");
LoginState state = await loginStateFuture; LoginState state = await loginStateFuture;
expect(state, LoginState.loggedOut); expect(state, LoginState.loggedOut);
expect(matrix.isLogged(), false); expect(matrix.isLogged(), false);
}); });
}); });
} }

View file

@ -44,6 +44,10 @@ class FakeMatrixApi extends MockClient {
await new Future.delayed(Duration(seconds: 5)); await new Future.delayed(Duration(seconds: 5));
} }
if (request.url.origin != "https://fakeserver.notexisting")
return Response(
"<html><head></head><body>Not found...</body></html>", 50);
// Call API // Call API
if (api.containsKey(method) && api[method].containsKey(action)) if (api.containsKey(method) && api[method].containsKey(action))
res = api[method][action](data); res = api[method][action](data);
@ -204,12 +208,13 @@ class FakeMatrixApi extends MockClient {
}, },
"/client/r0/logout": (var reqI) => {}, "/client/r0/logout": (var reqI) => {},
"/client/r0/logout/all": (var reqI) => {}, "/client/r0/logout/all": (var reqI) => {},
"/client/r0/createRoom": (var reqI) => {
"room_id": "!1234:fakeServer.notExisting",
}
}, },
"PUT": {}, "PUT": {},
"DELETE": { "DELETE": {
"/unknown/token": (var req) => { "/unknown/token": (var req) => {"errcode": "M_UNKNOWN_TOKEN"},
"errcode": "M_UNKNOWN_TOKEN"
},
}, },
}; };
} }