From 35ab891dd88e2379f604f596e028f4f043965654 Mon Sep 17 00:00:00 2001 From: Nicolas Werner <nicolas.werner@hotmail.de> Date: Fri, 8 Jan 2021 23:55:51 +0100 Subject: [PATCH] Fix room directory tests --- lib/http/client.cpp | 76 ++++---- lib/structs/requests.cpp | 11 +- lib/structs/responses/public_rooms.cpp | 55 +++--- tests/client_api.cpp | 242 ++++++++++++++----------- tests/requests.cpp | 60 +++--- tests/responses.cpp | 6 +- 6 files changed, 243 insertions(+), 207 deletions(-) diff --git a/lib/http/client.cpp b/lib/http/client.cpp index 6c5e82667..35dbf1bcb 100644 --- a/lib/http/client.cpp +++ b/lib/http/client.cpp @@ -965,55 +965,65 @@ Client::send_to_device(const std::string &event_type, } void -Client::get_room_visibility(const std::string &room_id, - Callback<mtx::responses::RoomVisibility> cb) +Client::get_room_visibility(const std::string &room_id, Callback<mtx::responses::RoomVisibility> cb) { - const auto api_path = "/client/r0/directory/list/room/" + - mtx::client::utils::url_encode(room_id); + const auto api_path = + "/client/r0/directory/list/room/" + mtx::client::utils::url_encode(room_id); - get<mtx::responses::RoomVisibility>(api_path, - [cb](const mtx::responses::RoomVisibility &res, - HeaderFields, - RequestErr err) { cb(res, err); }); + get<mtx::responses::RoomVisibility>( + api_path, [cb](const mtx::responses::RoomVisibility &res, HeaderFields, RequestErr err) { + cb(res, err); + }); } -void +void Client::put_room_visibility(const std::string &room_id, - const mtx::requests::RoomVisibility &req, - ErrCallback cb) + const mtx::requests::RoomVisibility &req, + ErrCallback cb) { - const auto api_path = "/client/r0/directory/list/room/" - + mtx::client::utils::url_encode(room_id); - std::cout << api_path << "\n"; + const auto api_path = + "/client/r0/directory/list/room/" + mtx::client::utils::url_encode(room_id); + std::cout << api_path << "\n"; put<mtx::requests::RoomVisibility>(api_path, req, cb); std::cout << "Does ths work?\n"; } void -Client::post_public_rooms(const mtx::requests::PublicRooms &req, - Callback<mtx::responses::PublicRooms> cb, const std::string &server) -{ - const auto api_path = "/client/r0/publicRooms?" + - mtx::client::utils::query_params({{"server", server}}); - post<mtx::requests::PublicRooms, mtx::responses::PublicRooms>( - api_path, req, cb); +Client::post_public_rooms(const mtx::requests::PublicRooms &req, + Callback<mtx::responses::PublicRooms> cb, + const std::string &server) +{ + std::string api_path = "/client/r0/publicRooms"; + + if (!server.empty()) + api_path += "?" + mtx::client::utils::query_params({{"server", server}}); + post<mtx::requests::PublicRooms, mtx::responses::PublicRooms>(api_path, req, cb); } void -Client::get_public_rooms(Callback<mtx::responses::PublicRooms> cb, const std::string &server, - size_t limit, const std::string &since) +Client::get_public_rooms(Callback<mtx::responses::PublicRooms> cb, + const std::string &server, + size_t limit, + const std::string &since) { - const auto api_path = - "/client/r0/publicRooms?" + - mtx::client::utils::query_params({{"server", server}, {"limit", std::to_string(limit)}, {"since", since}}); - - get<mtx::responses::PublicRooms>(api_path, - [cb](const mtx::responses::PublicRooms &res, - HeaderFields, - RequestErr err) { - cb(res, err); }); -} + std::string api_path = "/client/r0/publicRooms"; + std::map<std::string, std::string> params; + if (!server.empty()) + params["server"] = server; + if (limit > 0) + params["limit"] = std::to_string(limit); + if (!since.empty()) + params["since"] = since; + + if (!params.empty()) + api_path += "?" + mtx::client::utils::query_params(params); + + get<mtx::responses::PublicRooms>( + api_path, [cb](const mtx::responses::PublicRooms &res, HeaderFields, RequestErr err) { + cb(res, err); + }); +} // // Group related endpoints. diff --git a/lib/structs/requests.cpp b/lib/structs/requests.cpp index 7de4ae20c..8a63565a9 100644 --- a/lib/structs/requests.cpp +++ b/lib/structs/requests.cpp @@ -129,23 +129,24 @@ to_json(json &obj, const PublicRooms &request) if (request.limit > 0) { obj["limit"] = request.limit; } - + if (!request.since.empty()) { obj["since"] = request.since; } - + if (!request.filter.generic_search_term.empty()) { obj["filter"] = request.filter; } - + // Based on the spec, third_party_instance_id can only be used if // include_all_networks is false. A case where the latter is true and // the former is set is invalid. if (request.include_all_networks && !request.third_party_instance_id.empty()) { - throw std::invalid_argument("third_party_instance_id can only be set if include_all_networks is false"); + throw std::invalid_argument( + "third_party_instance_id can only be set if include_all_networks is false"); } else if (!request.third_party_instance_id.empty()) { obj["third_party_instance_id"] = request.third_party_instance_id; - obj["include_all_networks"] = false; + obj["include_all_networks"] = false; } else { obj["include_all_networks"] = true; } diff --git a/lib/structs/responses/public_rooms.cpp b/lib/structs/responses/public_rooms.cpp index 7dacb3d1b..382b9f524 100644 --- a/lib/structs/responses/public_rooms.cpp +++ b/lib/structs/responses/public_rooms.cpp @@ -11,50 +11,51 @@ namespace responses { void from_json(const nlohmann::json &obj, RoomVisibility &res) { - res.visibility = stringToVisibility(obj.at("visibility").get<std::string>()); + res.visibility = stringToVisibility(obj.at("visibility").get<std::string>()); } void -from_json(const nlohmann::json &obj, PublicRoomsChunk &res) +from_json(const nlohmann::json &obj, PublicRoomsChunk &res) { - std::cout << obj.dump(4) << std::endl; - - res.aliases = obj.value("aliases", std::vector<std::string>{}); + std::cout << obj.dump(4) << std::endl; - res.canonical_alias = obj.value("canonical_alias", std::string{}); + res.aliases = obj.value("aliases", std::vector<std::string>{}); - res.name = obj.value("name", std::string{}); + res.canonical_alias = obj.value("canonical_alias", std::string{}); - res.num_joined_members = obj.at("num_joined_members").get<int>(); + res.name = obj.value("name", std::string{}); - res.room_id = obj.at("room_id").get<std::string>(); + res.num_joined_members = obj.at("num_joined_members").get<int>(); - res.topic = obj.value("topic", std::string{}); + res.room_id = obj.at("room_id").get<std::string>(); - res.world_readable = obj.at("world_readable").get<bool>(); + res.topic = obj.value("topic", std::string{}); - res.guest_can_join = obj.at("guest_can_join").get<bool>(); + res.world_readable = obj.at("world_readable").get<bool>(); - res.avatar_url = obj.value("avatar_url", std::string{}); + res.guest_can_join = obj.at("guest_can_join").get<bool>(); + + res.avatar_url = obj.value("avatar_url", std::string{}); } void from_json(const nlohmann::json &obj, PublicRooms &publicRooms) { - // PublicRoomsChunk is CopyConstructible & DefaultConstructible - publicRooms.chunk = obj.at("chunk").get<std::vector<PublicRoomsChunk>>(); - - if (obj.count("next_batch")) { - publicRooms.next_batch = obj.at("next_batch").get<std::string>(); - } - - if (obj.count("prev_batch")) { - publicRooms.prev_batch = obj.at("prev_batch").get<std::string>(); - } - - if (obj.count("total_room_count_estimate")) { - publicRooms.total_room_count_estimate = obj.at("total_room_count_estimate").get<int>(); - } + // PublicRoomsChunk is CopyConstructible & DefaultConstructible + publicRooms.chunk = obj.at("chunk").get<std::vector<PublicRoomsChunk>>(); + + if (obj.count("next_batch")) { + publicRooms.next_batch = obj.at("next_batch").get<std::string>(); + } + + if (obj.count("prev_batch")) { + publicRooms.prev_batch = obj.at("prev_batch").get<std::string>(); + } + + if (obj.count("total_room_count_estimate")) { + publicRooms.total_room_count_estimate = + obj.at("total_room_count_estimate").get<int>(); + } } } // namespace responses diff --git a/tests/client_api.cpp b/tests/client_api.cpp index e80c16377..fa7739085 100644 --- a/tests/client_api.cpp +++ b/tests/client_api.cpp @@ -292,7 +292,7 @@ TEST(ClientAPI, TagRoom) mtx_client->create_room( req, [mtx_client](const mtx::responses::CreateRoom &res, RequestErr err) { auto room_id = res.room_id; - check_error(err); + check_error(err); mtx_client->put_tag( room_id.to_string(), "u.Test", {0.5}, [mtx_client, room_id](RequestErr err) { @@ -1576,7 +1576,7 @@ TEST(Groups, DISABLED_Profiles) alice->close(); } -TEST(ClientAPI, PublicRooms) +TEST(ClientAPI, PublicRooms) { // Setup : Create a new (public) room with some settings, add a user to the room auto alice = std::make_shared<Client>("localhost"); @@ -1597,125 +1597,149 @@ TEST(ClientAPI, PublicRooms) std::cout << "Bob AT: " << bob->access_token() << "\n"; mtx::requests::CreateRoom req; - req.name = "Public Room"; - req.topic = "Test"; - req.visibility = Visibility::Private; - req.invite = {"@bob:localhost"}; + req.name = "Public Room"; + req.topic = "Test"; + req.visibility = Visibility::Public; + req.invite = {"@bob:localhost"}; + req.room_alias_name = alice->generate_txn_id(); + req.preset = Preset::PublicChat; // req.room_alias_name = "foo"; alice->create_room( - req, [alice, bob](const mtx::responses::CreateRoom &res, RequestErr err){ - check_error(err); - auto room_id = res.room_id; + req, [alice, bob](const mtx::responses::CreateRoom &res, RequestErr err) { + check_error(err); + auto room_id = res.room_id; - // TEST 1: endpoints to set and get the visibility of the room we just created - mtx::requests::RoomVisibility r; - r.visibility = mtx::requests::Visibility::Public; + // TEST 1: endpoints to set and get the visibility of the room we just created + mtx::requests::RoomVisibility r; + r.visibility = mtx::requests::Visibility::Public; - std::cout << "HERE\n"; + std::cout << "HERE\n"; - alice->put_room_visibility(room_id.to_string(), r, [alice, bob, room_id](RequestErr err){ - check_error(err); + alice->put_room_visibility( + room_id.to_string(), r, [alice, bob, room_id](RequestErr err) { + check_error(err); - std::cout << "PLS WORK!\n"; + std::cout << "PLS WORK!\n"; - alice->get_room_visibility - ("", [alice, room_id](const mtx::responses::RoomVisibility &, RequestErr err) { - ASSERT_TRUE(err); - EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), + alice->get_room_visibility( + "", + [alice, room_id](const mtx::responses::RoomVisibility &, + RequestErr err) { + ASSERT_TRUE(err); + EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), "M_NOT_FOUND"); - }); - - std::atomic<bool> done = false; - - alice->get_room_visibility - (room_id.to_string(), [alice, bob, room_id, &done](const mtx::responses::RoomVisibility &res, RequestErr err){ - - check_error(err); - EXPECT_EQ(visibilityToString(res.visibility), "public"); - done = true; - - // TEST 2: endpoints to add and list the public rooms on the server - mtx::requests::PublicRooms room_req; - room_req.limit = 1; - room_req.include_all_networks = true; - json j = room_req; - std::cout << j.dump(4) << "\n"; - - while (!done) { - sleep(); - } - - alice->post_public_rooms - (room_req, [alice, bob, room_id, room_req](const mtx::responses::PublicRooms &, RequestErr err) { + }); + + std::atomic<bool> done = false; + + alice->get_room_visibility( + room_id.to_string(), + [alice, bob, room_id, &done]( + const mtx::responses::RoomVisibility &res, RequestErr err) { + check_error(err); + EXPECT_EQ(visibilityToString(res.visibility), "public"); + + // TEST 2: endpoints to add and list the public rooms on the + // server + mtx::requests::PublicRooms room_req; + room_req.limit = 1; + room_req.include_all_networks = true; + json j = room_req; + std::cout << j.dump(4) << "\n"; + + alice->post_public_rooms( + room_req, + [alice, bob, room_id, room_req]( + const mtx::responses::PublicRooms &, RequestErr err) { check_error(err); std::cout << "POST req\n"; - // alice->get_public_rooms - // ([alice, bob, room_id](const mtx::responses::PublicRooms &res, RequestErr err) { - // check_error(err); - // std::cout << res.chunk.size() << std::endl; - // EXPECT_EQ(res.chunk[0].name, "Public Room"); - // EXPECT_EQ(res.chunk[0].topic, "Test"); - // EXPECT_EQ(res.chunk[0].num_joined_members, 2); - // }, "", 1); - }); - }); - }); - - // bob->join_room(room_id.to_string(), [alice, bob, room_id](const mtx::responses::RoomId &, RequestErr err) { - // check_error(err); - - // // TEST 1: endpoints to set and get the visibility of the room we just created - // mtx::requests::RoomVisibility r; - // r.visibility = mtx::requests::Visibility::Private; - - // std::cout << "HERE\n"; - - // alice->put_room_visibility(room_id.to_string(), r, [alice, bob, room_id](RequestErr err){ - // check_error(err); - - // std::cout << "PLS WORK!\n"; - - // alice->get_room_visibility - // ("", [alice, room_id](const mtx::responses::RoomVisibility &, RequestErr err) { - // check_error(err); - // ASSERT_TRUE(err); - // EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), - // "M_NOT_FOUND"); - // }); - - // // alice->get_room_visibility - // // (room_id, [alice](const mtx::responses::RoomVisibility &res, RequestErr err){ - - // // check_error(err); - // // EXPECT_EQ(visibilityToString(res.visibility), "public"); - - // // // TEST 2: endpoints to add and list the public rooms on the server - // // mtx::requests::PublicRooms room_req; - // // room_req.limit = 1; - // // room_req.include_all_networks = true; - // // json j = room_req; - // // std::cout << j.dump(4) << "\n"; - - // // alice->post_public_rooms - // // (room_req, [alice](const mtx::responses::PublicRooms &, RequestErr err) { - // // check_error(err); - - // // alice->get_public_rooms - // // ([alice](const mtx::responses::PublicRooms &res, RequestErr err) { - // // check_error(err); - // // std::cout << res.chunk.size() << std::endl; - // // EXPECT_EQ(res.chunk[0].name, "Public Room"); - // // EXPECT_EQ(res.chunk[0].topic, "Test"); - // // EXPECT_EQ(res.chunk[0].num_joined_members, 2); - // // }, "", 1); - // // }); - // // }); - // }); - // }); - }); + alice->get_public_rooms( + [alice, bob, room_id]( + const mtx::responses::PublicRooms &res, + RequestErr err) { + check_error(err); + std::cout << res.chunk.size() + << std::endl; + // EXPECT_EQ(res.chunk[0].name, + // "Public Room"); + // EXPECT_EQ(res.chunk[0].topic, + // "Test"); + // EXPECT_EQ(res.chunk[0].num_joined_members, + // 2); + }, + "localhost", + 1); + }, + "localhost"); + }); + }); + + // bob->join_room(room_id.to_string(), [alice, bob, room_id](const + // mtx::responses::RoomId &, RequestErr err) { + // check_error(err); + + // // TEST 1: endpoints to set and get the visibility of the room we just + // created mtx::requests::RoomVisibility r; r.visibility = + // mtx::requests::Visibility::Private; + + // std::cout << "HERE\n"; + + // alice->put_room_visibility(room_id.to_string(), r, [alice, bob, + // room_id](RequestErr err){ + // check_error(err); + + // std::cout << "PLS WORK!\n"; + + // alice->get_room_visibility + // ("", [alice, room_id](const + // mtx::responses::RoomVisibility &, RequestErr err) { + // check_error(err); + // ASSERT_TRUE(err); + // EXPECT_EQ(mtx::errors::to_string(err->matrix_error.errcode), + // "M_NOT_FOUND"); + // }); + + // // alice->get_room_visibility + // // (room_id, [alice](const + // mtx::responses::RoomVisibility &res, RequestErr err){ + + // // check_error(err); + // // EXPECT_EQ(visibilityToString(res.visibility), + // "public"); + + // // // TEST 2: endpoints to add and list the public + // rooms on the server + // // mtx::requests::PublicRooms room_req; + // // room_req.limit = 1; + // // room_req.include_all_networks = true; + // // json j = room_req; + // // std::cout << j.dump(4) << "\n"; + + // // alice->post_public_rooms + // // (room_req, [alice](const mtx::responses::PublicRooms + // &, RequestErr err) { + // // check_error(err); + + // // alice->get_public_rooms + // // ([alice](const mtx::responses::PublicRooms + // &res, RequestErr err) { + // // check_error(err); + // // std::cout << res.chunk.size() << + // std::endl; + // // EXPECT_EQ(res.chunk[0].name, "Public + // Room"); + // // EXPECT_EQ(res.chunk[0].topic, + // "Test"); + // // EXPECT_EQ(res.chunk[0].num_joined_members, 2); + // // }, "", 1); + // // }); + // // }); + // }); + // }); + }); alice->close(); - bob->close(); + bob->close(); } diff --git a/tests/requests.cpp b/tests/requests.cpp index c7ba5d322..ffb1a38cb 100644 --- a/tests/requests.cpp +++ b/tests/requests.cpp @@ -283,32 +283,32 @@ TEST(Requests, UserInteractiveAuth) TEST(Requests, RoomVisibility) { - RoomVisibility req; - req.visibility = Visibility::Private; - json j = req; - EXPECT_EQ(j, R"({ + RoomVisibility req; + req.visibility = Visibility::Private; + json j = req; + EXPECT_EQ(j, R"({ "visibility" : "private" })"_json); - req.visibility = Visibility::Public; - j = req; - EXPECT_EQ(j, R"({ + req.visibility = Visibility::Public; + j = req; + EXPECT_EQ(j, R"({ "visibility" : "public" })"_json); } -TEST(Requests, PublicRooms) -{ - PublicRooms b1, b2, b3; +TEST(Requests, PublicRooms) +{ + PublicRooms b1, b2, b3; + + b1.limit = 10; + b1.filter.generic_search_term = "foo"; + b1.include_all_networks = false; + b1.third_party_instance_id = "irc"; - b1.limit = 10; - b1.filter.generic_search_term = "foo"; - b1.include_all_networks = false; - b1.third_party_instance_id = "irc"; + json j = b1; - json j = b1; - - EXPECT_EQ(j, R"({ + EXPECT_EQ(j, R"({ "limit" : 10, "filter" : { "generic_search_term" : "foo" @@ -316,23 +316,23 @@ TEST(Requests, PublicRooms) "include_all_networks" : false, "third_party_instance_id" : "irc" })"_json); - - // if third_party_instance_id is set, then the include_all_networks flag should - // default to false - b2.limit = 10; - b2.third_party_instance_id = "matrix"; - j = b2; - EXPECT_EQ(j, R"({ + + // if third_party_instance_id is set, then the include_all_networks flag should + // default to false + b2.limit = 10; + b2.third_party_instance_id = "matrix"; + j = b2; + EXPECT_EQ(j, R"({ "limit" : 10, "include_all_networks" : false, "third_party_instance_id" : "matrix" })"_json); - // if include_all_networks is true, then third_party_instance_id cannot be used. - // if it is somehow set, then we expect an exception to be thrown - b3.limit = 2; - b3.include_all_networks = true; - b3.third_party_instance_id = "irc"; + // if include_all_networks is true, then third_party_instance_id cannot be used. + // if it is somehow set, then we expect an exception to be thrown + b3.limit = 2; + b3.include_all_networks = true; + b3.third_party_instance_id = "irc"; - // EXPECT_THROW(json req = b3, std::invalid_argument); + // EXPECT_THROW(json req = b3, std::invalid_argument); } diff --git a/tests/responses.cpp b/tests/responses.cpp index e3b232fa4..55d2c340b 100644 --- a/tests/responses.cpp +++ b/tests/responses.cpp @@ -1107,16 +1107,16 @@ TEST(Responses, TurnServer) TEST(Responses, RoomVisibility) { - json data = {{"visibility", "public"}}; + json data = {{"visibility", "public"}}; RoomVisibility roomVisibility = data; EXPECT_EQ(roomVisibility.visibility, mtx::responses::Visibility::Public); - data = {{"visibility", "private"}}; + data = {{"visibility", "private"}}; roomVisibility = data; EXPECT_EQ(roomVisibility.visibility, mtx::responses::Visibility::Private); } -TEST(Responses, PublicRooms) +TEST(Responses, PublicRooms) { json data = R"({ "chunk": [ -- GitLab