|
| 1 | +import 'package:flutter_test/flutter_test.dart'; |
| 2 | +import 'package:famedlysdk/src/Client.dart'; |
| 3 | +import 'package:famedlysdk/src/Connection.dart'; |
| 4 | +import 'package:famedlysdk/src/sync/EventUpdate.dart'; |
| 5 | +import 'package:famedlysdk/src/sync/RoomUpdate.dart'; |
| 6 | +import 'package:famedlysdk/src/responses/ErrorResponse.dart'; |
| 7 | +import 'dart:async'; |
| 8 | +import 'FakeMatrixApi.dart'; |
| 9 | + |
| 10 | +void main() { |
| 11 | + Client matrix; |
| 12 | + |
| 13 | + Future<List<RoomUpdate>> roomUpdateListFuture; |
| 14 | + Future<List<EventUpdate>> eventUpdateListFuture; |
| 15 | + |
| 16 | + /// All Tests related to the Login |
| 17 | + group("FluffyMatrix", () { |
| 18 | + /// Check if all Elements get created |
| 19 | +
|
| 20 | + final create = (WidgetTester tester) { |
| 21 | + |
| 22 | + matrix = Client("testclient"); |
| 23 | + matrix.connection.httpClient = FakeMatrixApi(); |
| 24 | + matrix.homeserver = "https://fakeServer.notExisting"; |
| 25 | + |
| 26 | + roomUpdateListFuture = matrix.connection.onRoomUpdate.stream.toList(); |
| 27 | + eventUpdateListFuture = matrix.connection.onEvent.stream.toList(); |
| 28 | + }; |
| 29 | + testWidgets('should get created', create); |
| 30 | + |
| 31 | + test("Get version", () async { |
| 32 | + final versionResp = |
| 33 | + await matrix.connection.jsonRequest(type: "GET", action: "/client/versions"); |
| 34 | + expect(versionResp is ErrorResponse, false); |
| 35 | + expect(versionResp["versions"].indexOf("r0.4.0") != -1, true); |
| 36 | + matrix.matrixVersions = List<String>.from(versionResp["versions"]); |
| 37 | + matrix.lazyLoadMembers = true; |
| 38 | + }); |
| 39 | + |
| 40 | + test("Get login types", () async { |
| 41 | + final resp = |
| 42 | + await matrix.connection.jsonRequest(type: "GET", action: "/client/r0/login"); |
| 43 | + expect(resp is ErrorResponse, false); |
| 44 | + expect(resp["flows"] is List<dynamic>, true); |
| 45 | + bool hasMLoginType = false; |
| 46 | + for (int i = 0; i < resp["flows"].length; i++) |
| 47 | + if (resp["flows"][i]["type"] is String && |
| 48 | + resp["flows"][i]["type"] == "m.login.password") { |
| 49 | + hasMLoginType = true; |
| 50 | + break; |
| 51 | + } |
| 52 | + expect(hasMLoginType, true); |
| 53 | + }); |
| 54 | + |
| 55 | + final loginText = () async{ |
| 56 | + final resp = await matrix |
| 57 | + .connection.jsonRequest(type: "POST", action: "/client/r0/login", data: { |
| 58 | + "type": "m.login.password", |
| 59 | + "user": "test", |
| 60 | + "password": "1234", |
| 61 | + "initial_device_display_name": "Fluffy Matrix Client" |
| 62 | + }); |
| 63 | + expect(resp is ErrorResponse, false); |
| 64 | + |
| 65 | + Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; |
| 66 | + Future<bool> firstSyncFuture = matrix.connection.onFirstSync.stream.first; |
| 67 | + Future<dynamic> syncFuture = matrix.connection.onSync.stream.first; |
| 68 | + |
| 69 | + matrix.connection.connect( |
| 70 | + newToken: resp["access_token"], |
| 71 | + newUserID: resp["user_id"], |
| 72 | + newHomeserver: matrix.homeserver, |
| 73 | + newDeviceName: "Text Matrix Client", |
| 74 | + newDeviceID: resp["device_id"], |
| 75 | + newMatrixVersions: matrix.matrixVersions, |
| 76 | + newLazyLoadMembers: matrix.lazyLoadMembers); |
| 77 | + |
| 78 | + expect(matrix.accessToken == resp["access_token"], true); |
| 79 | + expect(matrix.deviceName == "Text Matrix Client", true); |
| 80 | + expect(matrix.deviceID == resp["device_id"], true); |
| 81 | + expect(matrix.userID == resp["user_id"], true); |
| 82 | + |
| 83 | + LoginState loginState = await loginStateFuture; |
| 84 | + bool firstSync = await firstSyncFuture; |
| 85 | + dynamic sync = await syncFuture; |
| 86 | + |
| 87 | + expect(loginState, LoginState.logged); |
| 88 | + expect(firstSync, true); |
| 89 | + expect(sync["next_batch"] == matrix.prevBatch, true); |
| 90 | + }; |
| 91 | + |
| 92 | + test('Login', loginText); |
| 93 | + |
| 94 | + test('Try to get ErrorResponse', () async{ |
| 95 | + final resp = await matrix |
| 96 | + .connection.jsonRequest(type: "PUT", action: "/non/existing/path"); |
| 97 | + expect(resp is ErrorResponse, true); |
| 98 | + }); |
| 99 | + |
| 100 | + test('Logout', () async{ |
| 101 | + final dynamic resp = await matrix |
| 102 | + .connection.jsonRequest(type: "POST", action: "/client/r0/logout"); |
| 103 | + expect(resp is ErrorResponse, false); |
| 104 | + |
| 105 | + Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; |
| 106 | + |
| 107 | + matrix.connection.clear(); |
| 108 | + |
| 109 | + expect(matrix.accessToken == null, true); |
| 110 | + expect(matrix.homeserver == null, true); |
| 111 | + expect(matrix.userID == null, true); |
| 112 | + expect(matrix.deviceID == null, true); |
| 113 | + expect(matrix.deviceName == null, true); |
| 114 | + expect(matrix.matrixVersions == null, true); |
| 115 | + expect(matrix.lazyLoadMembers == null, true); |
| 116 | + expect(matrix.prevBatch == null, true); |
| 117 | + |
| 118 | + LoginState loginState = await loginStateFuture; |
| 119 | + expect(loginState, LoginState.loggedOut); |
| 120 | + }); |
| 121 | + |
| 122 | + test('Room Update Test', () async{ |
| 123 | + matrix.connection.onRoomUpdate.close(); |
| 124 | + |
| 125 | + List<RoomUpdate> roomUpdateList = await roomUpdateListFuture; |
| 126 | + |
| 127 | + expect(roomUpdateList.length,3); |
| 128 | + |
| 129 | + expect(roomUpdateList[0].id=="!726s6s6q:example.com", true); |
| 130 | + expect(roomUpdateList[0].membership=="join", true); |
| 131 | + expect(roomUpdateList[0].prev_batch=="t34-23535_0_0", true); |
| 132 | + expect(roomUpdateList[0].limitedTimeline==true, true); |
| 133 | + expect(roomUpdateList[0].notification_count==2, true); |
| 134 | + expect(roomUpdateList[0].highlight_count==2, true); |
| 135 | + |
| 136 | + expect(roomUpdateList[1].id=="!696r7674:example.com", true); |
| 137 | + expect(roomUpdateList[1].membership=="invite", true); |
| 138 | + expect(roomUpdateList[1].prev_batch=="", true); |
| 139 | + expect(roomUpdateList[1].limitedTimeline==false, true); |
| 140 | + expect(roomUpdateList[1].notification_count==0, true); |
| 141 | + expect(roomUpdateList[1].highlight_count==0, true); |
| 142 | + |
| 143 | + expect(roomUpdateList[2].id=="!5345234234:example.com", true); |
| 144 | + expect(roomUpdateList[2].membership=="leave", true); |
| 145 | + expect(roomUpdateList[2].prev_batch=="", true); |
| 146 | + expect(roomUpdateList[2].limitedTimeline==false, true); |
| 147 | + expect(roomUpdateList[2].notification_count==0, true); |
| 148 | + expect(roomUpdateList[2].highlight_count==0, true); |
| 149 | + }); |
| 150 | + |
| 151 | + test('Event Update Test', () async{ |
| 152 | + matrix.connection.onEvent.close(); |
| 153 | + |
| 154 | + List<EventUpdate> eventUpdateList = await eventUpdateListFuture; |
| 155 | + |
| 156 | + expect(eventUpdateList.length,10); |
| 157 | + |
| 158 | + expect(eventUpdateList[0].eventType=="m.room.member", true); |
| 159 | + expect(eventUpdateList[0].roomID=="!726s6s6q:example.com", true); |
| 160 | + expect(eventUpdateList[0].type=="state", true); |
| 161 | + |
| 162 | + expect(eventUpdateList[1].eventType=="m.room.member", true); |
| 163 | + expect(eventUpdateList[1].roomID=="!726s6s6q:example.com", true); |
| 164 | + expect(eventUpdateList[1].type=="timeline", true); |
| 165 | + |
| 166 | + expect(eventUpdateList[2].eventType=="m.room.message", true); |
| 167 | + expect(eventUpdateList[2].roomID=="!726s6s6q:example.com", true); |
| 168 | + expect(eventUpdateList[2].type=="timeline", true); |
| 169 | + |
| 170 | + expect(eventUpdateList[3].eventType=="m.tag", true); |
| 171 | + expect(eventUpdateList[3].roomID=="!726s6s6q:example.com", true); |
| 172 | + expect(eventUpdateList[3].type=="account_data", true); |
| 173 | + |
| 174 | + expect(eventUpdateList[4].eventType=="org.example.custom.room.config", true); |
| 175 | + expect(eventUpdateList[4].roomID=="!726s6s6q:example.com", true); |
| 176 | + expect(eventUpdateList[4].type=="account_data", true); |
| 177 | + |
| 178 | + expect(eventUpdateList[5].eventType=="m.room.name", true); |
| 179 | + expect(eventUpdateList[5].roomID=="!696r7674:example.com", true); |
| 180 | + expect(eventUpdateList[5].type=="invite_state", true); |
| 181 | + |
| 182 | + expect(eventUpdateList[6].eventType=="m.room.member", true); |
| 183 | + expect(eventUpdateList[6].roomID=="!696r7674:example.com", true); |
| 184 | + expect(eventUpdateList[6].type=="invite_state", true); |
| 185 | + |
| 186 | + expect(eventUpdateList[7].eventType=="m.presence", true); |
| 187 | + expect(eventUpdateList[7].roomID=="presence", true); |
| 188 | + expect(eventUpdateList[7].type=="presence", true); |
| 189 | + |
| 190 | + expect(eventUpdateList[8].eventType=="org.example.custom.config", true); |
| 191 | + expect(eventUpdateList[8].roomID=="account_data", true); |
| 192 | + expect(eventUpdateList[8].type=="account_data", true); |
| 193 | + |
| 194 | + expect(eventUpdateList[9].eventType=="m.new_device", true); |
| 195 | + expect(eventUpdateList[9].roomID=="to_device", true); |
| 196 | + expect(eventUpdateList[9].type=="to_device", true); |
| 197 | + |
| 198 | + |
| 199 | + }); |
| 200 | + |
| 201 | + testWidgets('should get created', create); |
| 202 | + |
| 203 | + test('Login', loginText); |
| 204 | + |
| 205 | + test('Logout when token is unknown', () async{ |
| 206 | + Future<LoginState> loginStateFuture = matrix.connection.onLoginStateChanged.stream.first; |
| 207 | + final resp = await matrix |
| 208 | + .connection.jsonRequest(type: "DELETE", action: "/unknown/token"); |
| 209 | + |
| 210 | + LoginState state = await loginStateFuture; |
| 211 | + expect(state, LoginState.loggedOut); |
| 212 | + expect(matrix.isLogged(), false); |
| 213 | + }); |
| 214 | + |
| 215 | + }); |
| 216 | +} |
0 commit comments