From cbf5069e32ccd93d3594a7990129d316e56163da Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Mon, 25 May 2020 09:34:43 +0000 Subject: [PATCH] Add more tests --- lib/src/utils/matrix_localizations.dart | 7 +- lib/src/utils/open_id_credentials.dart | 6 - lib/src/utils/public_rooms_response.dart | 20 -- lib/src/utils/pusher.dart | 16 +- lib/src/utils/well_known_informations.dart | 4 - test/client_test.dart | 6 + test/matrix_default_localizations.dart | 213 +++++++++++++++++++++ test/matrix_exception_test.dart | 71 +++++++ test/matrix_file_test.dart | 52 +++++ test/matrix_id_string_extension_test.dart | 2 + test/matrix_localizations_test.dart | 65 +++++++ test/public_rooms_response_test.dart | 59 ++++++ test/pusher_test.dart | 45 +++++ test/well_known_informations_test.dart | 40 ++++ 14 files changed, 558 insertions(+), 48 deletions(-) create mode 100644 test/matrix_default_localizations.dart create mode 100644 test/matrix_exception_test.dart create mode 100644 test/matrix_file_test.dart create mode 100644 test/matrix_localizations_test.dart create mode 100644 test/public_rooms_response_test.dart create mode 100644 test/pusher_test.dart create mode 100644 test/well_known_informations_test.dart diff --git a/lib/src/utils/matrix_localizations.dart b/lib/src/utils/matrix_localizations.dart index 1b8fefd..ad70da9 100644 --- a/lib/src/utils/matrix_localizations.dart +++ b/lib/src/utils/matrix_localizations.dart @@ -1,6 +1,7 @@ import '../room.dart'; abstract class MatrixLocalizations { + const MatrixLocalizations(); String get emptyChat; String get invitedUsersOnly; @@ -121,9 +122,8 @@ extension HistoryVisibilityDisplayString on HistoryVisibility { return i18n.visibleForAllParticipants; case HistoryVisibility.world_readable: return i18n.visibleForEveryone; - default: - return toString().replaceAll('HistoryVisibility.', ''); } + return null; } } @@ -134,9 +134,8 @@ extension GuestAccessDisplayString on GuestAccess { return i18n.guestsCanJoin; case GuestAccess.forbidden: return i18n.guestsAreForbidden; - default: - return toString().replaceAll('GuestAccess.', ''); } + return null; } } diff --git a/lib/src/utils/open_id_credentials.dart b/lib/src/utils/open_id_credentials.dart index a2c79b6..4a0852e 100644 --- a/lib/src/utils/open_id_credentials.dart +++ b/lib/src/utils/open_id_credentials.dart @@ -4,12 +4,6 @@ class OpenIdCredentials { String matrixServerName; num expiresIn; - OpenIdCredentials( - {this.accessToken, - this.tokenType, - this.matrixServerName, - this.expiresIn}); - OpenIdCredentials.fromJson(Map json) { accessToken = json['access_token']; tokenType = json['token_type']; diff --git a/lib/src/utils/public_rooms_response.dart b/lib/src/utils/public_rooms_response.dart index 738ca59..3f833be 100644 --- a/lib/src/utils/public_rooms_response.dart +++ b/lib/src/utils/public_rooms_response.dart @@ -7,14 +7,6 @@ class PublicRoomsResponse { final int totalRoomCountEstimate; Client client; - PublicRoomsResponse({ - this.publicRooms, - this.nextBatch, - this.prevBatch, - this.totalRoomCountEstimate, - this.client, - }); - PublicRoomsResponse.fromJson(Map json, Client client) : nextBatch = json['next_batch'], prevBatch = json['prev_batch'], @@ -42,18 +34,6 @@ class PublicRoomEntry { Future join() => client.joinRoomById(roomId); - PublicRoomEntry({ - this.aliases, - this.avatarUrl, - this.guestCanJoin, - this.name, - this.numJoinedMembers, - this.roomId, - this.topic, - this.worldReadable, - this.client, - }); - PublicRoomEntry.fromJson(Map json, Client client) : aliases = json.containsKey('aliases') ? json['aliases'].cast() : [], diff --git a/lib/src/utils/pusher.dart b/lib/src/utils/pusher.dart index 810afd8..d4bd5da 100644 --- a/lib/src/utils/pusher.dart +++ b/lib/src/utils/pusher.dart @@ -8,16 +8,6 @@ class Pusher { String lang; PusherData data; - Pusher( - {this.pushkey, - this.kind, - this.appId, - this.appDisplayName, - this.deviceDisplayName, - this.profileTag, - this.lang, - this.data}); - Pusher.fromJson(Map json) { pushkey = json['pushkey']; kind = json['kind']; @@ -49,8 +39,6 @@ class PusherData { String url; String format; - PusherData({this.url, this.format}); - PusherData.fromJson(Map json) { url = json['url']; format = json['format']; @@ -58,8 +46,8 @@ class PusherData { Map toJson() { final data = {}; - data['url'] = url; - data['format'] = format; + if (url != null) data['url'] = url; + if (format != null) data['format'] = format; return data; } } diff --git a/lib/src/utils/well_known_informations.dart b/lib/src/utils/well_known_informations.dart index 46ab78e..359dbd0 100644 --- a/lib/src/utils/well_known_informations.dart +++ b/lib/src/utils/well_known_informations.dart @@ -3,8 +3,6 @@ class WellKnownInformations { MHomeserver mIdentityServer; Map content; - WellKnownInformations({this.mHomeserver, this.mIdentityServer}); - WellKnownInformations.fromJson(Map json) { content = json; mHomeserver = json['m.homeserver'] != null @@ -19,8 +17,6 @@ class WellKnownInformations { class MHomeserver { String baseUrl; - MHomeserver({this.baseUrl}); - MHomeserver.fromJson(Map json) { baseUrl = json['base_url']; } diff --git a/test/client_test.dart b/test/client_test.dart index 2d5b66e..fc3b939 100644 --- a/test/client_test.dart +++ b/test/client_test.dart @@ -411,6 +411,12 @@ void main() { expect(openId.tokenType, 'Bearer'); expect(openId.matrixServerName, 'example.com'); expect(openId.expiresIn, 3600); + expect(openId.toJson(), { + 'access_token': 'SomeT0kenHere', + 'token_type': 'Bearer', + 'matrix_server_name': 'example.com', + 'expires_in': 3600 + }); }); test('createRoom', () async { diff --git a/test/matrix_default_localizations.dart b/test/matrix_default_localizations.dart new file mode 100644 index 0000000..7ec3d6d --- /dev/null +++ b/test/matrix_default_localizations.dart @@ -0,0 +1,213 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/famedlysdk.dart'; + +class MatrixDefaultLocalizations extends MatrixLocalizations { + const MatrixDefaultLocalizations(); + @override + String acceptedTheInvitation(String targetName) => + '$targetName accepted the invitation'; + + @override + String activatedEndToEndEncryption(String senderName) => + '$senderName activated end to end encryption'; + + @override + String get anyoneCanJoin => 'Anyone can join'; + + @override + String bannedUser(String senderName, String targetName) => + '$senderName banned $targetName'; + + @override + String changedTheChatAvatar(String senderName) => + '$senderName changed the chat avatar'; + + @override + String changedTheChatDescriptionTo(String senderName, String content) => + '$senderName changed the chat description to $content'; + + @override + String changedTheChatNameTo(String senderName, String content) => + '$senderName changed the chat name to $content'; + + @override + String changedTheChatPermissions(String senderName) => + '$senderName changed the chat permissions'; + + @override + String changedTheDisplaynameTo(String targetName, String newDisplayname) => + '$targetName changed the displayname to $newDisplayname'; + + @override + String changedTheGuestAccessRules(String senderName) => + '$senderName changed the guest access rules'; + + @override + String changedTheGuestAccessRulesTo( + String senderName, String localizedString) => + '$senderName changed the guest access rules to $localizedString'; + + @override + String changedTheHistoryVisibility(String senderName) => + '$senderName changed the history visibility'; + + @override + String changedTheHistoryVisibilityTo( + String senderName, String localizedString) => + '$senderName changed the history visibility to $localizedString'; + + @override + String changedTheJoinRules(String senderName) => + '$senderName changed the join rules'; + + @override + String changedTheJoinRulesTo(String senderName, String localizedString) => + '$senderName changed the join rules to $localizedString'; + + @override + String changedTheProfileAvatar(String targetName) => + '$targetName changed the profile avatar'; + + @override + String changedTheRoomAliases(String senderName) => + '$senderName changed the room aliases'; + + @override + String changedTheRoomInvitationLink(String senderName) => + '$senderName changed the room invitation link'; + + @override + String get channelCorruptedDecryptError => + 'The secure channel has been corrupted'; + + @override + String couldNotDecryptMessage(String errorText) => + 'Could not decrypt message: $errorText'; + + @override + String createdTheChat(String senderName) => '$senderName created the chat'; + + @override + String get emptyChat => 'Empty chat'; + + @override + String get encryptionNotEnabled => 'Encryption not enabled'; + + @override + String get fromJoining => 'From joining'; + + @override + String get fromTheInvitation => 'From the invitation'; + + @override + String groupWith(String displayname) => 'Group with $displayname'; + + @override + String get guestsAreForbidden => 'Guests are forbidden'; + + @override + String get guestsCanJoin => 'Guests can join'; + + @override + String hasWithdrawnTheInvitationFor(String senderName, String targetName) => + '$senderName has withdrawn the invitation for $targetName'; + + @override + String invitedUser(String senderName, String targetName) => + '$senderName has invited $targetName'; + + @override + String get invitedUsersOnly => 'Invited users only'; + + @override + String joinedTheChat(String targetName) => '$targetName joined the chat'; + + @override + String kicked(String senderName, String targetName) => + '$senderName kicked $targetName'; + + @override + String kickedAndBanned(String senderName, String targetName) => + '$senderName banned $targetName'; + + @override + String get needPantalaimonWarning => 'Need pantalaimon'; + + @override + String get noPermission => 'No permission'; + + @override + String redactedAnEvent(String senderName) => '$senderName redacted an event'; + + @override + String rejectedTheInvitation(String targetName) => + '$targetName rejected the invitation'; + + @override + String removedBy(String calcDisplayname) => 'Removed by $calcDisplayname'; + + @override + String get roomHasBeenUpgraded => 'Room has been upgraded'; + + @override + String sentAFile(String senderName) => '$senderName sent a file'; + + @override + String sentAPicture(String senderName) => '$senderName sent a picture'; + + @override + String sentASticker(String senderName) => '$senderName sent a sticker'; + + @override + String sentAVideo(String senderName) => '$senderName sent a video'; + + @override + String sentAnAudio(String senderName) => '$senderName sent an audio'; + + @override + String sharedTheLocation(String senderName) => + '$senderName shared the location'; + + @override + String unbannedUser(String senderName, String targetName) => + '$senderName unbanned $targetName'; + + @override + String get unknownEncryptionAlgorithm => 'Unknown encryption algorithm'; + + @override + String unknownEvent(String typeKey) => 'Unknown event $typeKey'; + + @override + String userLeftTheChat(String targetName) => '$targetName left the chat'; + + @override + String get visibleForAllParticipants => 'Visible for all participants'; + + @override + String get visibleForEveryone => 'Visible for everyone'; + + @override + String get you => 'You'; +} diff --git a/test/matrix_exception_test.dart b/test/matrix_exception_test.dart new file mode 100644 index 0000000..77ec04c --- /dev/null +++ b/test/matrix_exception_test.dart @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:http/http.dart'; +import 'package:test/test.dart'; + +void main() { + /// All Tests related to device keys + group('Matrix Exception', () { + test('Matrix Exception', () async { + final matrixException = MatrixException( + Response( + '{"flows":[{"stages":["example.type.foo"]}],"params":{"example.type.baz":{"example_key":"foobar"}},"session":"xxxxxxyz","completed":["example.type.foo"]}', + 401, + ), + ); + expect(matrixException.errcode, 'M_FORBIDDEN'); + final flows = matrixException.authenticationFlows; + expect(flows.length, 1); + expect(flows.first.stages.length, 1); + expect(flows.first.stages.first, 'example.type.foo'); + expect( + matrixException.authenticationParams['example.type.baz'], + {'example_key': 'foobar'}, + ); + expect(matrixException.completedAuthenticationFlows.length, 1); + expect(matrixException.completedAuthenticationFlows.first, + 'example.type.foo'); + expect(matrixException.session, 'xxxxxxyz'); + }); + test('Unknown Exception', () async { + final matrixException = MatrixException( + Response( + '{"errcode":"M_HAHA","error":"HAHA","retry_after_ms":500}', + 401, + ), + ); + expect(matrixException.error, MatrixError.M_UNKNOWN); + expect(matrixException.retryAfterMs, 500); + }); + test('Missing Exception', () async { + final matrixException = MatrixException( + Response( + '{"error":"HAHA"}', + 401, + ), + ); + expect(matrixException.error, MatrixError.M_UNKNOWN); + }); + }); +} diff --git a/test/matrix_file_test.dart b/test/matrix_file_test.dart new file mode 100644 index 0000000..498c230 --- /dev/null +++ b/test/matrix_file_test.dart @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ + +import 'dart:typed_data'; + +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:test/test.dart'; +import 'package:olm/olm.dart' as olm; + +void main() { + /// All Tests related to device keys + group('Matrix File', () { + test('Decrypt', () async { + final text = 'hello world'; + final file = MatrixFile( + path: '/path/to/file.txt', + bytes: Uint8List.fromList(text.codeUnits), + ); + var olmEnabled = true; + try { + await olm.init(); + olm.Account(); + } catch (_) { + olmEnabled = false; + } + if (olmEnabled) { + final encryptedFile = await file.encrypt(); + expect(encryptedFile != null, true); + } + }); + }); +} diff --git a/test/matrix_id_string_extension_test.dart b/test/matrix_id_string_extension_test.dart index 4b0b425..f5ae04c 100644 --- a/test/matrix_id_string_extension_test.dart +++ b/test/matrix_id_string_extension_test.dart @@ -45,6 +45,8 @@ void main() { expect('\$test:example.com'.sigil, '\$'); expect(mxId.localpart, 'test'); expect(mxId.domain, 'example.com'); + expect(mxId.equals('@Test:example.com'), true); + expect(mxId.equals('@test:example.org'), false); }); }); } diff --git a/test/matrix_localizations_test.dart b/test/matrix_localizations_test.dart new file mode 100644 index 0000000..044073b --- /dev/null +++ b/test/matrix_localizations_test.dart @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:test/test.dart'; + +import 'matrix_default_localizations.dart'; + +void main() { + /// All Tests related to device keys + group('Matrix Localizations', () { + test('Matrix Localizations', () { + expect( + HistoryVisibility.invited + .getLocalizedString(MatrixDefaultLocalizations()), + 'From the invitation'); + expect( + HistoryVisibility.joined + .getLocalizedString(MatrixDefaultLocalizations()), + 'From joining'); + expect( + HistoryVisibility.shared + .getLocalizedString(MatrixDefaultLocalizations()), + 'Visible for all participants'); + expect( + HistoryVisibility.world_readable + .getLocalizedString(MatrixDefaultLocalizations()), + 'Visible for everyone'); + expect( + GuestAccess.can_join.getLocalizedString(MatrixDefaultLocalizations()), + 'Guests can join'); + expect( + GuestAccess.forbidden + .getLocalizedString(MatrixDefaultLocalizations()), + 'Guests are forbidden'); + expect(JoinRules.invite.getLocalizedString(MatrixDefaultLocalizations()), + 'Invited users only'); + expect(JoinRules.public.getLocalizedString(MatrixDefaultLocalizations()), + 'Anyone can join'); + expect(JoinRules.private.getLocalizedString(MatrixDefaultLocalizations()), + 'private'); + expect(JoinRules.knock.getLocalizedString(MatrixDefaultLocalizations()), + 'knock'); + }); + }); +} diff --git a/test/public_rooms_response_test.dart b/test/public_rooms_response_test.dart new file mode 100644 index 0000000..b76a7d4 --- /dev/null +++ b/test/public_rooms_response_test.dart @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:test/test.dart'; + +import 'fake_matrix_api.dart'; + +void main() { + /// All Tests related to device keys + group('Public Rooms Response', () { + Client client; + test('Public Rooms Response', () async { + client = Client('testclient', debug: true); + client.httpClient = FakeMatrixApi(); + + await client.checkServer('https://fakeServer.notExisting'); + final responseMap = { + 'chunk': [ + { + 'aliases': ['#murrays:cheese.bar'], + 'avatar_url': 'mxc://bleeker.street/CHEDDARandBRIE', + 'guest_can_join': false, + 'name': 'CHEESE', + 'num_joined_members': 37, + 'room_id': '1234', + 'topic': 'Tasty tasty cheese', + 'world_readable': true + } + ], + 'next_batch': 'p190q', + 'prev_batch': 'p1902', + 'total_room_count_estimate': 115 + }; + final publicRoomsResponse = + PublicRoomsResponse.fromJson(responseMap, client); + await publicRoomsResponse.publicRooms.first.join(); + }); + }); +} diff --git a/test/pusher_test.dart b/test/pusher_test.dart new file mode 100644 index 0000000..fe2dfa2 --- /dev/null +++ b/test/pusher_test.dart @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/src/utils/pusher.dart'; +import 'package:test/test.dart'; + +void main() { + /// All Tests related to device keys + group('Pusher', () { + test('Pusher', () { + final rawPusher = { + 'pushkey': 'Xp/MzCt8/9DcSNE9cuiaoT5Ac55job3TdLSSmtmYl4A=', + 'kind': 'http', + 'app_id': 'face.mcapp.appy.prod', + 'app_display_name': 'Appy McAppface', + 'device_display_name': "Alice's Phone", + 'profile_tag': 'xyz', + 'lang': 'en-US', + 'data': {'url': 'https://example.com/_matrix/push/v1/notify'} + }; + + final pusher = Pusher.fromJson(rawPusher); + expect(pusher.toJson(), rawPusher); + }); + }); +} diff --git a/test/well_known_informations_test.dart b/test/well_known_informations_test.dart new file mode 100644 index 0000000..7417d10 --- /dev/null +++ b/test/well_known_informations_test.dart @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2019 Zender & Kurtz GbR. + * + * Authors: + * Christian Pauly + * Marcel Radzio + * + * 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 . + */ +import 'package:famedlysdk/famedlysdk.dart'; +import 'package:test/test.dart'; + +void main() { + /// All Tests related to device keys + group('WellKnownInformations', () { + test('WellKnownInformations', () { + final json = { + 'm.homeserver': {'base_url': 'https://matrix.example.com'}, + 'm.identity_server': {'base_url': 'https://identity.example.com'}, + 'org.example.custom.property': { + 'app_url': 'https://custom.app.example.org' + } + }; + WellKnownInformations.fromJson(json); + }); + }); +}