Skip to content

Commit

Permalink
Rename remaining bits of apidb_backend_oauth test to roles apidb test
Browse files Browse the repository at this point in the history
  • Loading branch information
mmd-osm committed Dec 21, 2023
1 parent 445d65d commit 5af7d97
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ endif
if ENABLE_APIDB
TESTS += \
test/test_apidb_backend_nodes \
test/test_apidb_backend_oauth \
test/test_apidb_backend_roles \
test/test_apidb_backend_oauth2 \
test/test_apidb_backend_historic \
test/test_apidb_backend_changesets \
Expand Down
13 changes: 7 additions & 6 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if ENABLE_APIDB
lib_LTLIBRARIES+=libcgimap_apidb.la
check_PROGRAMS+=\
../test/test_apidb_backend_nodes \
../test/test_apidb_backend_oauth \
../test/test_apidb_backend_roles \
../test/test_apidb_backend_oauth2 \
../test/test_apidb_backend_historic \
../test/test_apidb_backend_changesets \
Expand All @@ -29,7 +29,7 @@ check_PROGRAMS+=\
___openstreetmap_cgimap_LDADD+=libcgimap_apidb.la
___test_test_core_LDADD+=libcgimap_apidb.la
___test_test_apidb_backend_nodes_LDADD=libcgimap_core.la libcgimap_apidb.la
___test_test_apidb_backend_oauth_LDADD=libcgimap_core.la libcgimap_apidb.la
___test_test_apidb_backend_roles_LDADD=libcgimap_core.la libcgimap_apidb.la
___test_test_apidb_backend_oauth2_LDADD=libcgimap_core.la libcgimap_apidb.la
___test_test_apidb_backend_historic_LDADD=libcgimap_core.la libcgimap_apidb.la
___test_test_apidb_backend_changesets_LDADD=libcgimap_core.la libcgimap_apidb.la
Expand All @@ -42,7 +42,7 @@ ___openstreetmap_cgimap_LDADD+=libcgimap_staticxml.la
___test_test_core_LDADD+=libcgimap_staticxml.la
if ENABLE_APIDB
___test_test_apidb_backend_nodes_LDADD+=libcgimap_staticxml.la
___test_test_apidb_backend_oauth_LDADD+=libcgimap_staticxml.la
___test_test_apidb_backend_roles_LDADD+=libcgimap_staticxml.la
___test_test_apidb_backend_oauth2_LDADD+=libcgimap_staticxml.la
___test_test_apidb_backend_historic_LDADD+=libcgimap_staticxml.la
___test_test_apidb_backend_changesets_LDADD+=libcgimap_staticxml.la
Expand Down Expand Up @@ -71,7 +71,7 @@ ___test_test_parse_options_LDADD=libcgimap_core.la @LIBXML_LIBS@ @BOOST_PROGRAM_

if ENABLE_APIDB
___test_test_apidb_backend_nodes_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_apidb_backend_oauth_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_apidb_backend_roles_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_apidb_backend_oauth2_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_apidb_backend_historic_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
___test_test_apidb_backend_changesets_LDADD+=libcgimap_core.la @BOOST_PROGRAM_OPTIONS_LIB@ @LIBPQXX_LIBS@ @CRYPTOPP_LIBS@ @ARGON2_LIBS@
Expand Down Expand Up @@ -129,8 +129,9 @@ ___test_test_apidb_backend_nodes_SOURCES=\
../test/test_formatter.cpp \
../test/test_database.cpp \
../test/test_request.cpp
___test_test_apidb_backend_oauth_SOURCES=\
../test/test_apidb_backend_oauth.cpp \
___test_test_apidb_backend_roles_CPPFLAGS=$(AM_CPPFLAGS) $(CATCH2_CPPFLAGS)
___test_test_apidb_backend_roles_SOURCES=\
../test/test_apidb_backend_roles.cpp \
../test/test_formatter.cpp \
../test/test_database.cpp \
../test/test_request.cpp
Expand Down
145 changes: 0 additions & 145 deletions test/test_apidb_backend_oauth.cpp

This file was deleted.

137 changes: 137 additions & 0 deletions test/test_apidb_backend_roles.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* SPDX-License-Identifier: GPL-2.0-only
*
* This file is part of openstreetmap-cgimap (https://github.com/zerebubuth/openstreetmap-cgimap/).
*
* Copyright (C) 2009-2023 by the CGImap developer community.
* For a full list of authors see the git log.
*/

#include <iostream>
#include <stdexcept>
#include <fmt/core.h>
#include <boost/program_options.hpp>

#include <sys/time.h>
#include <cstdio>
#include <memory>

#include "cgimap/config.hpp"
#include "cgimap/time.hpp"
#include "cgimap/options.hpp"
#include "cgimap/rate_limiter.hpp"
#include "cgimap/routes.hpp"
#include "cgimap/process_request.hpp"

#include "test_formatter.hpp"
#include "test_database.hpp"
#include "test_request.hpp"
#include "test_empty_selection.hpp"

#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>

using roles_t = std::set<osm_user_role_t>;


class DatabaseTestsFixture
{
protected:
DatabaseTestsFixture() = default;
static test_database tdb;
};

test_database DatabaseTestsFixture::tdb{};

struct CGImapListener : Catch::TestEventListenerBase, DatabaseTestsFixture {

using TestEventListenerBase::TestEventListenerBase; // inherit constructor

void testRunStarting( Catch::TestRunInfo const& testRunInfo ) override {
// load database schema when starting up tests
tdb.setup();
}

void testCaseStarting( Catch::TestCaseInfo const& testInfo ) override {
tdb.testcase_starting();
}

void testCaseEnded( Catch::TestCaseStats const& testCaseStats ) override {
tdb.testcase_ended();
}
};

CATCH_REGISTER_LISTENER( CGImapListener )

std::ostream &operator<<(
std::ostream &out, const std::set<osm_user_role_t> &roles) {

out << "{";
bool first = true;
for (osm_user_role_t r : roles) {
if (first) { first = false; } else { out << ", "; }
if (r == osm_user_role_t::moderator) {
out << "moderator";
} else if (r == osm_user_role_t::administrator) {
out << "administrator";
}
}
out << "}";
return out;
}

template <> struct fmt::formatter<roles_t> {
template <typename FormatContext>
auto format(const roles_t& r, FormatContext& ctx) -> decltype(ctx.out()) {
// ctx.out() is an output iterator to write to.
std::ostringstream ostr;
ostr << r;
return format_to(ctx.out(), "{}", ostr.str());
}
constexpr auto parse(const format_parse_context& ctx) const { return ctx.begin(); }
};




template<typename T>
std::ostream& operator<<(std::ostream& os, std::optional<T> const& opt)
{
return opt ? os << opt.value() : os;
}


TEST_CASE_METHOD(DatabaseTestsFixture, "test_get_roles_for_user", "[roles][db]" ) {

auto sel = tdb.get_data_selection();

SECTION("Initialize test data") {


tdb.run_sql(
"INSERT INTO users (id, email, pass_crypt, creation_time, display_name, data_public) "
"VALUES "
" (1, '[email protected]', '', '2017-02-20T11:41:00Z', 'user_1', true), "
" (2, '[email protected]', '', '2017-02-20T11:41:00Z', 'user_2', true), "
" (3, '[email protected]', '', '2017-02-20T11:41:00Z', 'user_3', true); "

"INSERT INTO user_roles (id, user_id, role, granter_id) "
"VALUES "
" (1, 1, 'administrator', 1), "
" (2, 1, 'moderator', 1), "
" (3, 2, 'moderator', 1);"
"");

}

// user 3 has no roles -> should return empty set
REQUIRE(roles_t() == sel->get_roles_for_user(3));

// user 2 is a moderator
REQUIRE(roles_t({osm_user_role_t::moderator}) == sel->get_roles_for_user(2));

// user 1 is an administrator and a moderator
REQUIRE(roles_t({osm_user_role_t::moderator, osm_user_role_t::administrator}) == sel->get_roles_for_user(1));
}


0 comments on commit 5af7d97

Please sign in to comment.