From 638fa3e55d6e560e18fe3effcef54cc8aa5e635e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Sun, 17 Nov 2019 19:59:08 +0100 Subject: [PATCH] tests: Add initial AsyncClient test. --- Cargo.toml | 2 + tests/async_client_tests.rs | 24 +++ tests/data/context.json | 211 +++++++++++++++++++++ tests/data/delete_devices.json | 18 ++ tests/data/devices.json | 10 + tests/data/get_avatar_response.json | 3 + tests/data/get_displayname_response.json | 3 + tests/data/get_profile_response.json | 5 + tests/data/joined_members_response.json | 8 + tests/data/keys_claim.json | 17 ++ tests/data/keys_query.json | 27 +++ tests/data/keys_upload.json | 6 + tests/data/limit_exceeded_error.json | 5 + tests/data/login_info.json | 8 + tests/data/login_invalid_format.json | 6 + tests/data/login_response.json | 6 + tests/data/login_response_error.json | 4 + tests/data/logout_response.json | 1 + tests/data/room_id.json | 3 + tests/data/room_messages.json | 45 +++++ tests/data/sync.json | 222 +++++++++++++++++++++++ tests/data/upload_response.json | 3 + 22 files changed, 637 insertions(+) create mode 100644 tests/async_client_tests.rs create mode 100644 tests/data/context.json create mode 100644 tests/data/delete_devices.json create mode 100644 tests/data/devices.json create mode 100644 tests/data/get_avatar_response.json create mode 100644 tests/data/get_displayname_response.json create mode 100644 tests/data/get_profile_response.json create mode 100644 tests/data/joined_members_response.json create mode 100644 tests/data/keys_claim.json create mode 100644 tests/data/keys_query.json create mode 100644 tests/data/keys_upload.json create mode 100644 tests/data/limit_exceeded_error.json create mode 100644 tests/data/login_info.json create mode 100644 tests/data/login_invalid_format.json create mode 100644 tests/data/login_response.json create mode 100644 tests/data/login_response_error.json create mode 100644 tests/data/logout_response.json create mode 100644 tests/data/room_id.json create mode 100644 tests/data/room_messages.json create mode 100644 tests/data/sync.json create mode 100644 tests/data/upload_response.json diff --git a/Cargo.toml b/Cargo.toml index 0ad0d59d..d96d8149 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,3 +31,5 @@ features = ["derive"] [dev-dependencies] tokio = "0.2.0-alpha.6" +url = "2.1.0" +mockito = "0.22.0" diff --git a/tests/async_client_tests.rs b/tests/async_client_tests.rs new file mode 100644 index 00000000..116854c8 --- /dev/null +++ b/tests/async_client_tests.rs @@ -0,0 +1,24 @@ +use matrix_nio::{AsyncClient, AsyncClientConfig}; +use mockito::mock; +use std::str::FromStr; +use tokio::runtime::Runtime; +use url::Url; + +#[test] +fn login() { + let rt = Runtime::new().unwrap(); + + let homeserver = Url::from_str(&mockito::server_url()).unwrap(); + + let _m = mock("POST", "/_matrix/client/r0/login") + .with_status(200) + .with_body_from_file("tests/data/login_response.json") + .create(); + + let mut client = AsyncClient::new(homeserver, None).unwrap(); + + rt.block_on(client.login("example", "wordpass", None)) + .unwrap(); + + assert!(client.logged_in(), "Clint should be logged in"); +} diff --git a/tests/data/context.json b/tests/data/context.json new file mode 100644 index 00000000..be92b59b --- /dev/null +++ b/tests/data/context.json @@ -0,0 +1,211 @@ +{ + "events_before": [], + "events_after": [ + { + "content": { + "body": "yeah, let's do that", + "msgtype": "m.text" + }, + "event_id": "$15163623196QOZxj:localhost", + "origin_server_ts": 1516362319505, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "type": "m.room.message", + "unsigned": { + "age": 43464955731 + }, + "user_id": "@example:localhost", + "age": 43464955731 + } + ], + "start": "t182-189_0_0_0_0_0_0_0_0", + "end": "t184-190_0_0_0_0_0_0_0_0", + "event": { + "content": { + "body": "ok, let's handle invites, joins and parts", + "msgtype": "m.text" + }, + "event_id": "$15163622445EBvZJ:localhost", + "origin_server_ts": 1516362244026, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example2:localhost", + "type": "m.room.message", + "unsigned": { + "age": 43465031210 + }, + "user_id": "@example2:localhost", + "age": 43465031210 + }, + "state": [ + { + "content": { + "topic": "amazing work" + }, + "event_id": "$151568196747dxLZM:localhost", + "origin_server_ts": 1515681967443, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.topic", + "unsigned": { + "replaces_state": "$151567214844LzHAk:localhost", + "age": 44145307793 + }, + "user_id": "@example:localhost", + "age": 44145307793, + "replaces_state": "$151567214844LzHAk:localhost" + }, + { + "content": { + "aliases": [ + "#tutorial:localhost" + ] + }, + "event_id": "$15139375516NUgtD:localhost", + "origin_server_ts": 1513937551720, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "localhost", + "type": "m.room.aliases", + "unsigned": { + "age": 45889723516 + }, + "user_id": "@example:localhost", + "age": 45889723516 + }, + { + "content": { + "history_visibility": "shared" + }, + "event_id": "$15139375515VaJEY:localhost", + "origin_server_ts": 1513937551613, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.history_visibility", + "unsigned": { + "age": 45889723623 + }, + "user_id": "@example:localhost", + "age": 45889723623 + }, + { + "content": { + "join_rule": "public" + }, + "event_id": "$15139375514WsgmR:localhost", + "origin_server_ts": 1513937551539, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.join_rules", + "unsigned": { + "age": 45889723697 + }, + "user_id": "@example:localhost", + "age": 45889723697 + }, + { + "content": { + "alias": "#tutorial:localhost" + }, + "event_id": "$15139375513VdeRF:localhost", + "origin_server_ts": 1513937551461, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.canonical_alias", + "unsigned": { + "age": 45889723775 + }, + "user_id": "@example:localhost", + "age": 45889723775 + }, + { + "content": { + "ban": 50, + "events": { + "m.room.avatar": 50, + "m.room.canonical_alias": 50, + "m.room.history_visibility": 100, + "m.room.name": 50, + "m.room.power_levels": 100 + }, + "events_default": 0, + "invite": 0, + "kick": 50, + "redact": 50, + "state_default": 50, + "users": { + "@example:localhost": 100 + }, + "users_default": 0 + }, + "event_id": "$15139375512JaHAW:localhost", + "origin_server_ts": 1513937551359, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.power_levels", + "unsigned": { + "age": 45889723877 + }, + "user_id": "@example:localhost", + "age": 45889723877 + }, + { + "content": { + "creator": "@example:localhost" + }, + "event_id": "$15139375510KUZHi:localhost", + "origin_server_ts": 1513937551203, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.create", + "unsigned": { + "age": 45889724033 + }, + "user_id": "@example:localhost", + "age": 45889724033 + }, + { + "content": { + "avatar_url": null, + "displayname": "example2", + "membership": "join" + }, + "event_id": "$151396611913abyeC:localhost", + "membership": "join", + "origin_server_ts": 1513966119908, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example2:localhost", + "state_key": "@example2:localhost", + "type": "m.room.member", + "unsigned": { + "age": 45861155328 + }, + "user_id": "@example2:localhost", + "age": 45861155328 + }, + { + "content": { + "avatar_url": null, + "displayname": "example", + "membership": "join" + }, + "event_id": "$15139375511GBYDY:localhost", + "membership": "join", + "origin_server_ts": 1513937551274, + "room_id": "!SVkFJHzfwvuaIEawgC:localhost", + "sender": "@example:localhost", + "state_key": "@example:localhost", + "type": "m.room.member", + "unsigned": { + "age": 45889723962 + }, + "user_id": "@example:localhost", + "age": 45889723962 + } + ] +} diff --git a/tests/data/delete_devices.json b/tests/data/delete_devices.json new file mode 100644 index 00000000..aa4c1330 --- /dev/null +++ b/tests/data/delete_devices.json @@ -0,0 +1,18 @@ +{ + "completed": [ + "example.type.foo" + ], + "flows": [ + { + "stages": [ + "example.type.foo" + ] + } + ], + "params": { + "example.type.baz": { + "example_key": "foobar" + } + }, + "session": "xxxxxxyz" +} diff --git a/tests/data/devices.json b/tests/data/devices.json new file mode 100644 index 00000000..a3af5a3d --- /dev/null +++ b/tests/data/devices.json @@ -0,0 +1,10 @@ +{ + "devices": [ + { + "device_id": "QBUAZIFURK", + "display_name": "android", + "last_seen_ip": "1.2.3.4", + "last_seen_ts": 1474491775024 + } + ] +} diff --git a/tests/data/get_avatar_response.json b/tests/data/get_avatar_response.json new file mode 100644 index 00000000..9227a544 --- /dev/null +++ b/tests/data/get_avatar_response.json @@ -0,0 +1,3 @@ +{ + "avatar_url": "mxc://matrix.org/SDGdghriugerRg" +} diff --git a/tests/data/get_displayname_response.json b/tests/data/get_displayname_response.json new file mode 100644 index 00000000..fed57979 --- /dev/null +++ b/tests/data/get_displayname_response.json @@ -0,0 +1,3 @@ +{ + "displayname": "Bob" +} diff --git a/tests/data/get_profile_response.json b/tests/data/get_profile_response.json new file mode 100644 index 00000000..4abbd6fb --- /dev/null +++ b/tests/data/get_profile_response.json @@ -0,0 +1,5 @@ +{ + "displayname": "Bob", + "avatar_url": "mxc://matrix.org/SDGdghriugerRg", + "something_else": 123 +} diff --git a/tests/data/joined_members_response.json b/tests/data/joined_members_response.json new file mode 100644 index 00000000..c2458a29 --- /dev/null +++ b/tests/data/joined_members_response.json @@ -0,0 +1,8 @@ +{ + "joined": { + "@bar:example.com": { + "avatar_url": null, + "display_name": "Bar" + } + } +} diff --git a/tests/data/keys_claim.json b/tests/data/keys_claim.json new file mode 100644 index 00000000..1712d52e --- /dev/null +++ b/tests/data/keys_claim.json @@ -0,0 +1,17 @@ +{ + "failures": {}, + "one_time_keys": { + "@alice:example.com": { + "JLAFKJWSCS": { + "signed_curve25519:AAAAHg": { + "key": "zKbLg+NrIjpnagy+pIY6uPL4ZwEG2v+8F9lmgsnlZzs", + "signatures": { + "@alice:example.com": { + "ed25519:JLAFKJWSCS": "FLWxXqGbwrb8SM3Y795eB6OA8bwBcoMZFXBqnTn58AYWZSqiD45tlBVcDa2L7RwdKXebW/VzDlnfVJ+9jok1Bw" + } + } + } + } + } + } +} diff --git a/tests/data/keys_query.json b/tests/data/keys_query.json new file mode 100644 index 00000000..66b112df --- /dev/null +++ b/tests/data/keys_query.json @@ -0,0 +1,27 @@ +{ + "device_keys": { + "@alice:example.org": { + "JLAFKJWSCS": { + "algorithms": [ + "m.olm.v1.curve25519-aes-sha2", + "m.megolm.v1.aes-sha2" + ], + "device_id": "JLAFKJWSCS", + "user_id": "@alice:example.org", + "keys": { + "curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4", + "ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM" + }, + "signatures": { + "@alice:example.org": { + "ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA" + } + }, + "unsigned": { + "device_display_name": "Alice's mobile phone" + } + } + } + }, + "failures": {} +} diff --git a/tests/data/keys_upload.json b/tests/data/keys_upload.json new file mode 100644 index 00000000..e32a62c4 --- /dev/null +++ b/tests/data/keys_upload.json @@ -0,0 +1,6 @@ +{ + "one_time_key_counts": { + "curve25519": 10, + "signed_curve25519": 20 + } +} diff --git a/tests/data/limit_exceeded_error.json b/tests/data/limit_exceeded_error.json new file mode 100644 index 00000000..84b23a56 --- /dev/null +++ b/tests/data/limit_exceeded_error.json @@ -0,0 +1,5 @@ +{ + "errcode": "M_LIMIT_EXCEEDED", + "error": "Too many requests", + "retry_after_ms": 500 +} diff --git a/tests/data/login_info.json b/tests/data/login_info.json new file mode 100644 index 00000000..968f4955 --- /dev/null +++ b/tests/data/login_info.json @@ -0,0 +1,8 @@ +{ + "flows": [ + { + "type": "m.login.sso", + "type": "m.login.password" + } + ] +} diff --git a/tests/data/login_invalid_format.json b/tests/data/login_invalid_format.json new file mode 100644 index 00000000..af084720 --- /dev/null +++ b/tests/data/login_invalid_format.json @@ -0,0 +1,6 @@ +{ + "access_token": "abc123", + "device_id": "GHTYAJCE", + "home_server": "matrix.org", + "user_id": "cheeky_monkey" +} diff --git a/tests/data/login_response.json b/tests/data/login_response.json new file mode 100644 index 00000000..6bff348a --- /dev/null +++ b/tests/data/login_response.json @@ -0,0 +1,6 @@ +{ + "access_token": "abc123", + "device_id": "GHTYAJCE", + "home_server": "matrix.org", + "user_id": "@cheeky_monkey:matrix.org" +} diff --git a/tests/data/login_response_error.json b/tests/data/login_response_error.json new file mode 100644 index 00000000..9e573c1a --- /dev/null +++ b/tests/data/login_response_error.json @@ -0,0 +1,4 @@ +{ + "errcode": "M_FORBIDDEN", + "error": "Invalid password" +} diff --git a/tests/data/logout_response.json b/tests/data/logout_response.json new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/tests/data/logout_response.json @@ -0,0 +1 @@ +{} diff --git a/tests/data/room_id.json b/tests/data/room_id.json new file mode 100644 index 00000000..75974b00 --- /dev/null +++ b/tests/data/room_id.json @@ -0,0 +1,3 @@ +{ + "room_id": "!testroom:example.org" +} diff --git a/tests/data/room_messages.json b/tests/data/room_messages.json new file mode 100644 index 00000000..b394266e --- /dev/null +++ b/tests/data/room_messages.json @@ -0,0 +1,45 @@ +{ + "chunk": [ + { + "age": 1042, + "content": { + "body": "hello world", + "msgtype": "m.text" + }, + "event_id": "$1444812213350496Caaaa:example.com", + "origin_server_ts": 1444812213737, + "room_id": "!Xq3620DUiqCaoxq:example.com", + "sender": "@alice:example.com", + "type": "m.room.message" + }, + { + "age": 20123, + "content": { + "body": "the world is big", + "msgtype": "m.text" + }, + "event_id": "$1444812213350496Cbbbb:example.com", + "origin_server_ts": 1444812194656, + "room_id": "!Xq3620DUiqCaoxq:example.com", + "sender": "@bob:example.com", + "type": "m.room.message" + }, + { + "age": 50789, + "content": { + "name": "New room name" + }, + "event_id": "$1444812213350496Ccccc:example.com", + "origin_server_ts": 1444812163990, + "prev_content": { + "name": "Old room name" + }, + "room_id": "!Xq3620DUiqCaoxq:example.com", + "sender": "@bob:example.com", + "state_key": "", + "type": "m.room.name" + } + ], + "end": "t47409-4357353_219380_26003_2265", + "start": "t47429-4392820_219380_26003_2265" +} diff --git a/tests/data/sync.json b/tests/data/sync.json new file mode 100644 index 00000000..a43cb577 --- /dev/null +++ b/tests/data/sync.json @@ -0,0 +1,222 @@ +{ + "device_one_time_keys_count": {}, + "next_batch": "s526_47314_0_7_1_1_1_11444_1", + "device_lists": { + "changed": [ + "@example:example.org" + ], + "left": [] + }, + + "rooms": { + "invite": {}, + "join": { + "!SVkFJHzfwvuaIEawgC:localhost": { + "account_data": { + "events": [] + }, + "ephemeral": { + "events": [ + { + "content": { + "$151680659217152dPKjd:localhost": { + "m.read": { + "@example:localhost": { + "ts": 1516809890615 + } + } + } + }, + "type": "m.receipt" + } + ] + }, + "state": { + "events": [ + { + "content": { + "join_rule": "public" + }, + "event_id": "$15139375514WsgmR:localhost", + "origin_server_ts": 1513937551539, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.join_rules", + "unsigned": { + "age": 7034220355 + } + }, + { + "content": { + "avatar_url": null, + "displayname": "example", + "membership": "join" + }, + "event_id": "$151800140517rfvjc:localhost", + "membership": "join", + "origin_server_ts": 1518001405556, + "sender": "@example:localhost", + "state_key": "@example:localhost", + "type": "m.room.member", + "unsigned": { + "age": 2970366338, + "replaces_state": "$151800111315tsynI:localhost" + } + }, + { + "content": { + "history_visibility": "shared" + }, + "event_id": "$15139375515VaJEY:localhost", + "origin_server_ts": 1513937551613, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.history_visibility", + "unsigned": { + "age": 7034220281 + } + }, + { + "content": { + "creator": "@example:localhost" + }, + "event_id": "$15139375510KUZHi:localhost", + "origin_server_ts": 1513937551203, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.create", + "unsigned": { + "age": 7034220691 + } + }, + { + "content": { + "aliases": [ + "#tutorial:localhost" + ] + }, + "event_id": "$15139375516NUgtD:localhost", + "origin_server_ts": 1513937551720, + "sender": "@example:localhost", + "state_key": "localhost", + "type": "m.room.aliases", + "unsigned": { + "age": 7034220174 + } + }, + { + "content": { + "topic": "\ud83d\ude00" + }, + "event_id": "$151957878228ssqrJ:localhost", + "origin_server_ts": 1519578782185, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.topic", + "unsigned": { + "age": 1392989709, + "prev_content": { + "topic": "test" + }, + "prev_sender": "@example:localhost", + "replaces_state": "$151957069225EVYKm:localhost" + } + }, + { + "content": { + "ban": 50, + "events": { + "m.room.avatar": 50, + "m.room.canonical_alias": 50, + "m.room.history_visibility": 100, + "m.room.name": 50, + "m.room.power_levels": 100 + }, + "events_default": 0, + "invite": 0, + "kick": 50, + "redact": 50, + "state_default": 50, + "users": { + "@example:localhost": 100 + }, + "users_default": 0 + }, + "event_id": "$15139375512JaHAW:localhost", + "origin_server_ts": 1513937551359, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.power_levels", + "unsigned": { + "age": 7034220535 + } + }, + { + "content": { + "alias": "#tutorial:localhost" + }, + "event_id": "$15139375513VdeRF:localhost", + "origin_server_ts": 1513937551461, + "sender": "@example:localhost", + "state_key": "", + "type": "m.room.canonical_alias", + "unsigned": { + "age": 7034220433 + } + }, + { + "content": { + "avatar_url": null, + "displayname": "example2", + "membership": "join" + }, + "event_id": "$152034824468gOeNB:localhost", + "membership": "join", + "origin_server_ts": 1520348244605, + "sender": "@example2:localhost", + "state_key": "@example2:localhost", + "type": "m.room.member", + "unsigned": { + "age": 623527289, + "prev_content": { + "membership": "leave" + }, + "prev_sender": "@example:localhost", + "replaces_state": "$152034819067QWJxM:localhost" + } + } + ] + }, + "timeline": { + "events": [ + { + "content": { + "body": "baba", + "format": "org.matrix.custom.html", + "formatted_body": "baba", + "msgtype": "m.text" + }, + "event_id": "$152037280074GZeOm:localhost", + "origin_server_ts": 1520372800469, + "sender": "@example:localhost", + "type": "m.room.message", + "unsigned": { + "age": 598971425 + } + } + ], + "limited": true, + "prev_batch": "t392-516_47314_0_7_1_1_1_11444_1" + }, + "unread_notifications": { + "highlight_count": 0, + "notification_count": 11 + } + } + }, + "leave": {} + }, + "to_device": { + "events": [] + } +} diff --git a/tests/data/upload_response.json b/tests/data/upload_response.json new file mode 100644 index 00000000..24845bf9 --- /dev/null +++ b/tests/data/upload_response.json @@ -0,0 +1,3 @@ +{ + "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" +}