|
| 1 | +/** |
| 2 | + * SPDX-License-Identifier: GPL-2.0-only |
| 3 | + * |
| 4 | + * This file is part of openstreetmap-cgimap (https://github.com/zerebubuth/openstreetmap-cgimap/). |
| 5 | + * |
| 6 | + * Copyright (C) 2009-2023 by the CGImap developer community. |
| 7 | + * For a full list of authors see the git log. |
| 8 | + */ |
| 9 | + |
| 10 | +#include <iostream> |
| 11 | +#include <stdexcept> |
| 12 | +#include <fmt/core.h> |
| 13 | +#include <boost/program_options.hpp> |
| 14 | + |
| 15 | +#include <sys/time.h> |
| 16 | +#include <cstdio> |
| 17 | +#include <memory> |
| 18 | + |
| 19 | +#include "cgimap/config.hpp" |
| 20 | +#include "cgimap/time.hpp" |
| 21 | +#include "cgimap/options.hpp" |
| 22 | +#include "cgimap/rate_limiter.hpp" |
| 23 | +#include "cgimap/routes.hpp" |
| 24 | +#include "cgimap/process_request.hpp" |
| 25 | + |
| 26 | +#include "test_formatter.hpp" |
| 27 | +#include "test_database.hpp" |
| 28 | +#include "test_request.hpp" |
| 29 | +#include "test_empty_selection.hpp" |
| 30 | + |
| 31 | +#define CATCH_CONFIG_MAIN |
| 32 | +#include <catch2/catch.hpp> |
| 33 | + |
| 34 | +using roles_t = std::set<osm_user_role_t>; |
| 35 | + |
| 36 | + |
| 37 | +class DatabaseTestsFixture |
| 38 | +{ |
| 39 | +protected: |
| 40 | + DatabaseTestsFixture() = default; |
| 41 | + static test_database tdb; |
| 42 | +}; |
| 43 | + |
| 44 | +test_database DatabaseTestsFixture::tdb{}; |
| 45 | + |
| 46 | +struct CGImapListener : Catch::TestEventListenerBase, DatabaseTestsFixture { |
| 47 | + |
| 48 | + using TestEventListenerBase::TestEventListenerBase; // inherit constructor |
| 49 | + |
| 50 | + void testRunStarting( Catch::TestRunInfo const& testRunInfo ) override { |
| 51 | + // load database schema when starting up tests |
| 52 | + tdb.setup(); |
| 53 | + } |
| 54 | + |
| 55 | + void testCaseStarting( Catch::TestCaseInfo const& testInfo ) override { |
| 56 | + tdb.testcase_starting(); |
| 57 | + } |
| 58 | + |
| 59 | + void testCaseEnded( Catch::TestCaseStats const& testCaseStats ) override { |
| 60 | + tdb.testcase_ended(); |
| 61 | + } |
| 62 | +}; |
| 63 | + |
| 64 | +CATCH_REGISTER_LISTENER( CGImapListener ) |
| 65 | + |
| 66 | +std::ostream &operator<<( |
| 67 | + std::ostream &out, const std::set<osm_user_role_t> &roles) { |
| 68 | + |
| 69 | + out << "{"; |
| 70 | + bool first = true; |
| 71 | + for (osm_user_role_t r : roles) { |
| 72 | + if (first) { first = false; } else { out << ", "; } |
| 73 | + if (r == osm_user_role_t::moderator) { |
| 74 | + out << "moderator"; |
| 75 | + } else if (r == osm_user_role_t::administrator) { |
| 76 | + out << "administrator"; |
| 77 | + } |
| 78 | + } |
| 79 | + out << "}"; |
| 80 | + return out; |
| 81 | +} |
| 82 | + |
| 83 | +template <> struct fmt::formatter<roles_t> { |
| 84 | + template <typename FormatContext> |
| 85 | + auto format(const roles_t& r, FormatContext& ctx) -> decltype(ctx.out()) { |
| 86 | + // ctx.out() is an output iterator to write to. |
| 87 | + std::ostringstream ostr; |
| 88 | + ostr << r; |
| 89 | + return format_to(ctx.out(), "{}", ostr.str()); |
| 90 | + } |
| 91 | + constexpr auto parse(const format_parse_context& ctx) const { return ctx.begin(); } |
| 92 | +}; |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +template<typename T> |
| 98 | +std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt) |
| 99 | +{ |
| 100 | + return opt ? os << opt.value() : os; |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +TEST_CASE_METHOD(DatabaseTestsFixture, "test_get_roles_for_user", "[roles][db]" ) { |
| 105 | + |
| 106 | + auto sel = tdb.get_data_selection(); |
| 107 | + |
| 108 | + SECTION("Initialize test data") { |
| 109 | + |
| 110 | + |
| 111 | + tdb.run_sql( |
| 112 | + "INSERT INTO users (id, email, pass_crypt, creation_time, display_name, data_public) " |
| 113 | + "VALUES " |
| 114 | + " (1, 'user_1@example.com', '', '2017-02-20T11:41:00Z', 'user_1', true), " |
| 115 | + " (2, 'user_2@example.com', '', '2017-02-20T11:41:00Z', 'user_2', true), " |
| 116 | + " (3, 'user_3@example.com', '', '2017-02-20T11:41:00Z', 'user_3', true); " |
| 117 | + |
| 118 | + "INSERT INTO user_roles (id, user_id, role, granter_id) " |
| 119 | + "VALUES " |
| 120 | + " (1, 1, 'administrator', 1), " |
| 121 | + " (2, 1, 'moderator', 1), " |
| 122 | + " (3, 2, 'moderator', 1);" |
| 123 | + ""); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + // user 3 has no roles -> should return empty set |
| 128 | + REQUIRE(roles_t() == sel->get_roles_for_user(3)); |
| 129 | + |
| 130 | + // user 2 is a moderator |
| 131 | + REQUIRE(roles_t({osm_user_role_t::moderator}) == sel->get_roles_for_user(2)); |
| 132 | + |
| 133 | + // user 1 is an administrator and a moderator |
| 134 | + REQUIRE(roles_t({osm_user_role_t::moderator, osm_user_role_t::administrator}) == sel->get_roles_for_user(1)); |
| 135 | +} |
| 136 | + |
| 137 | + |
0 commit comments