| 1 | #pragma once |
| 2 | |
| 3 | #include "gtest/gtest.h" |
| 4 | #include <cstdlib> |
| 5 | #include <iostream> |
| 6 | #include <limits> |
| 7 | #include <random> |
| 8 | #include <string> |
| 9 | #include <thread> |
| 10 | #include <vector> |
| 11 | |
| 12 | #include <mtx/responses/login.hpp> |
| 13 | #include <mtxclient/http/client.hpp> |
| 14 | |
| 15 | inline int |
| 16 | random_number() |
| 17 | { |
| 18 | std::random_device rng; |
| 19 | std::uniform_int_distribution<int> dist(1, std::numeric_limits<int>::max()); |
| 20 | |
| 21 | return dist(rng); |
| 22 | } |
| 23 | |
| 24 | inline void |
| 25 | sleep() |
| 26 | { |
| 27 | std::this_thread::sleep_for(std::chrono::milliseconds(100)); |
| 28 | } |
| 29 | |
| 30 | #define WAIT_UNTIL(condition) \ |
| 31 | while (!(condition)) { \ |
| 32 | sleep(); \ |
| 33 | }; |
| 34 | |
| 35 | inline void |
| 36 | check_error(mtx::http::RequestErr err) |
| 37 | { |
| 38 | if (err) { |
| 39 | std::cout << "matrix (error) : " << err->matrix_error.error << "\n"; |
| 40 | std::cout << "matrix (errcode): " << mtx::errors::to_string(err->matrix_error.errcode) |
| 41 | << "\n"; |
| 42 | std::cout << "error_code : " << err->error_code << "\n"; |
| 43 | std::cout << "status_code : " << err->status_code << "\n"; |
| 44 | |
| 45 | if (!err->parse_error.empty()) |
| 46 | std::cout << "parse_error : " << err->parse_error << "\n"; |
| 47 | } |
| 48 | |
| 49 | ASSERT_FALSE(err); |
| 50 | } |
| 51 | |
| 52 | inline std::string |
| 53 | server_name() |
| 54 | { |
| 55 | const char *server_ = std::getenv("MTXCLIENT_SERVER"); |
| 56 | return server_ ? server_ : std::string("localhost"); |
| 57 | } |
| 58 | |
| 59 | inline auto |
| 60 | make_test_client() |
| 61 | { |
| 62 | auto client = std::make_shared<mtx::http::Client>(server_name(), 8008); |
| 63 | client->verify_certificates(false); |
| 64 | return client; |
| 65 | } |
| 66 | |
| 67 | inline void |
| 68 | check_login(const mtx::responses::Login &, mtx::http::RequestErr err) |
| 69 | { |
| 70 | check_error(err); |
| 71 | } |
| 72 | |
| 73 | inline void |
| 74 | validate_login(const std::string &user, const mtx::responses::Login &res) |
| 75 | { |
| 76 | EXPECT_EQ(res.user_id.to_string(), user); |
| 77 | ASSERT_TRUE(res.access_token.size() > 20); |
| 78 | ASSERT_TRUE(res.device_id.size() > 5); |
| 79 | } |
| 80 | |
| 81 | template<class Collection> |
| 82 | std::vector<std::string> |
| 83 | get_event_ids(const std::vector<Collection> &events) |
| 84 | { |
| 85 | std::vector<std::string> ids; |
| 86 | |
| 87 | for (const auto &e : events) |
| 88 | ids.push_back(std::visit([](auto msg) { return msg.event_id; }, e)); |
| 89 | |
| 90 | return ids; |
| 91 | } |
| 92 | |
| 93 | inline std::string |
| 94 | fixture_prefix() |
| 95 | { |
| 96 | auto var = std::getenv("FIXTURE_PREFIX"); |
| 97 | |
| 98 | if (var) |
| 99 | return var; |
| 100 | else |
| 101 | return "."; |
| 102 | } |