From d98857768046d2a739207856ce24a696c7e8840f Mon Sep 17 00:00:00 2001 From: Alexander Reichert Date: Wed, 13 Dec 2023 08:35:03 +0100 Subject: [PATCH] device: implement and test device license activation --- daemon/modules/console/CMakeLists.txt | 40 +++++----- daemon/modules/console/__mocks__/console.h | 75 +++++++++++++++++++ daemon/modules/device/CMakeLists.txt | 20 +++++ daemon/modules/device/device.h | 2 +- daemon/modules/device/impl/device_impl.h | 3 +- daemon/modules/device/src/device.cpp | 2 +- .../modules/device/src/impl/device_impl.cpp | 15 +++- daemon/modules/device/test/CMakeLists.txt | 5 +- daemon/modules/device/test/test_device.cpp | 21 +++++- 9 files changed, 159 insertions(+), 24 deletions(-) create mode 100644 daemon/modules/console/__mocks__/console.h diff --git a/daemon/modules/console/CMakeLists.txt b/daemon/modules/console/CMakeLists.txt index 9386edd04..32a939c50 100644 --- a/daemon/modules/console/CMakeLists.txt +++ b/daemon/modules/console/CMakeLists.txt @@ -12,29 +12,35 @@ # See the License for the specific language governing permissions and # limitations under the License. +project(modules.console.types) + +add_library(${PROJECT_NAME} STATIC + src/types/activate_response.cpp + src/types/auth_response.cpp + src/types/base_response.cpp + src/types/error_response.cpp + src/types/feature_flags.cpp + src/types/jwt.cpp + src/types/user.cpp + src/types/validate_response.cpp + types/activate_response.h + types/auth_response.h + types/base_response.h + types/error_response.h + types/feature_flags.h + types/jwt.h + types/user.h + types/validate_response.h + types.h +) + flecs_add_module( MODULE_NAME console ADDITIONAL_HEADERS impl/console_impl.h - types/activate_response.h - types/auth_response.h - types/base_response.h - types/error_response.h - types/feature_flags.h - types/jwt.h - types/user.h - types/validate_response.h - types.h ADDITIONAL_SOURCES src/impl/console_impl.cpp - src/types/activate_response.cpp - src/types/auth_response.cpp - src/types/base_response.cpp - src/types/error_response.cpp - src/types/feature_flags.cpp - src/types/jwt.cpp - src/types/user.cpp - src/types/validate_response.cpp + LIBS_PUBLIC cpr::cpr modules.console.types ) add_subdirectory(test) diff --git a/daemon/modules/console/__mocks__/console.h b/daemon/modules/console/__mocks__/console.h new file mode 100644 index 000000000..2e3493b0b --- /dev/null +++ b/daemon/modules/console/__mocks__/console.h @@ -0,0 +1,75 @@ +// Copyright 2021-2023 FLECS Technologies GmbH +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#pragma once + +#include + +#include "daemon/modules/console/types.h" +#include "daemon/modules/factory/factory.h" +#include "daemon/modules/module_base/module.h" + +namespace flecs { +namespace module { + +namespace impl { +class console_t +{ +public: + ~console_t() = default; +}; +} // namespace impl + +class console_t FLECS_FINAL_UNLESS_TESTED : public base_t +{ + friend class factory_t; + +public: + ~console_t() override = default; + + static constexpr auto base_url() // + -> std::string_view; + + MOCK_METHOD((const console::auth_response_data_t&), authentication, (), (const, noexcept)); + MOCK_METHOD((result_t), activate_license, (std::string session_id), ()); + MOCK_METHOD((result_t), validate_license, (std::string_view session_id), ()); + +protected: + console_t() = default; + + MOCK_METHOD((void), do_init, (), (override)); + MOCK_METHOD((void), do_deinit, (), (override)); + + MOCK_METHOD((crow::response), store_authentication, (console::auth_response_data_t auth), ()); + MOCK_METHOD((crow::response), delete_authentication, (), ()); + + std::unique_ptr _impl; +}; + +constexpr auto console_t::base_url() // + -> std::string_view +{ + using std::operator""sv; + +#if defined FLECS_UNIT_TEST + return "http://127.0.0.1:18952"sv; +#elif defined NDEBUG + return "https://console.flecs.tech"sv; +#else + return "https://console-dev.flecs.tech"sv; +#endif // FLECS_UNIT_TEST +} + +} // namespace module +} // namespace flecs diff --git a/daemon/modules/device/CMakeLists.txt b/daemon/modules/device/CMakeLists.txt index 3ac5a40bb..0336015b5 100644 --- a/daemon/modules/device/CMakeLists.txt +++ b/daemon/modules/device/CMakeLists.txt @@ -16,6 +16,26 @@ flecs_add_module( MODULE_NAME device ADDITIONAL_HEADERS impl/device_impl.h ADDITIONAL_SOURCES src/impl/device_impl.cpp + LIBS_PUBLIC daemon.modules.console +) + +add_library(daemon.modules.device.mocked_modules STATIC + src/device.cpp + src/impl/device_impl.cpp + device.h + impl/device_impl.h +) + +target_link_libraries( + daemon.modules.device.mocked_modules PUBLIC + GTest::gmock + daemon.modules.factory + daemon.modules.module_base +) + +target_compile_definitions( + daemon.modules.device.mocked_modules PRIVATE + -DFLECS_MOCK_MODULES ) add_subdirectory(test) diff --git a/daemon/modules/device/device.h b/daemon/modules/device/device.h index 8799d685b..f7b65adb9 100644 --- a/daemon/modules/device/device.h +++ b/daemon/modules/device/device.h @@ -14,7 +14,7 @@ #pragma once -#include "module_base/module.h" +#include "daemon/modules/module_base/module.h" namespace flecs { namespace module { diff --git a/daemon/modules/device/impl/device_impl.h b/daemon/modules/device/impl/device_impl.h index 8d9189db8..809b1d804 100644 --- a/daemon/modules/device/impl/device_impl.h +++ b/daemon/modules/device/impl/device_impl.h @@ -28,7 +28,7 @@ class device_t friend class flecs::module::device_t; private: - device_t(); + device_t(flecs::module::device_t* parent); auto do_init() // -> void; @@ -51,6 +51,7 @@ class device_t auto do_validate_license() // -> result_t; + flecs::module::device_t* _parent; std::string _session_id; }; diff --git a/daemon/modules/device/src/device.cpp b/daemon/modules/device/src/device.cpp index 1676c1f19..15601d7eb 100644 --- a/daemon/modules/device/src/device.cpp +++ b/daemon/modules/device/src/device.cpp @@ -25,7 +25,7 @@ register_module_t _reg("device"); } device_t::device_t() - : _impl{new impl::device_t{}} + : _impl{new impl::device_t{this}} {} device_t::~device_t() = default; diff --git a/daemon/modules/device/src/impl/device_impl.cpp b/daemon/modules/device/src/impl/device_impl.cpp index 18052895d..afc2eefb7 100644 --- a/daemon/modules/device/src/impl/device_impl.cpp +++ b/daemon/modules/device/src/impl/device_impl.cpp @@ -19,13 +19,21 @@ #include #include +#ifdef FLECS_MOCK_MODULES +#include "daemon/modules/console/__mocks__/console.h" +#else +#include "daemon/modules/console/console.h" +#endif // FLECS_MOCK_MODULES +#include "daemon/modules/factory/factory.h" #include "util/string/string_utils.h" namespace flecs { namespace module { namespace impl { -device_t::device_t() +device_t::device_t(flecs::module::device_t* parent) + : _parent{parent} + , _session_id{} {} auto device_t::do_init() // @@ -93,7 +101,10 @@ auto device_t::do_session_id() // auto device_t::do_activate_license() // -> result_t { - return {0, {}}; + auto console_api = + std::dynamic_pointer_cast(api::query_module("console")); + + return console_api->activate_license(_parent->session_id()); } auto device_t::do_validate_license() // diff --git a/daemon/modules/device/test/CMakeLists.txt b/daemon/modules/device/test/CMakeLists.txt index 554a9b87e..c6a44528a 100644 --- a/daemon/modules/device/test/CMakeLists.txt +++ b/daemon/modules/device/test/CMakeLists.txt @@ -25,7 +25,10 @@ if(BUILD_TESTING) target_link_libraries(${PROJECT_NAME} PRIVATE GTest::gtest GTest::gtest_main - daemon.modules.device + GTest::gmock + modules.console.types + daemon.modules.device.mocked_modules + daemon.modules.factory util.signal_handler ) diff --git a/daemon/modules/device/test/test_device.cpp b/daemon/modules/device/test/test_device.cpp index fd2c94b04..adc197482 100644 --- a/daemon/modules/device/test/test_device.cpp +++ b/daemon/modules/device/test/test_device.cpp @@ -12,16 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include #include #include +#include "daemon/modules/console/__mocks__/console.h" #include "daemon/modules/device/device.h" +#include "daemon/modules/factory/factory.h" class test_module_device_t : public flecs::module::device_t { public: - test_module_device_t() = default; + test_module_device_t() + { + flecs::module::register_module_t("console"); + } }; const auto session_id_regex = std::regex{"[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}"}; @@ -50,3 +56,16 @@ TEST(device, session_id) uut.save("."); } } + +TEST(device, activate_license) +{ + auto uut = test_module_device_t{}; + const auto session_id = uut.session_id(); + + auto mock_console = + std::dynamic_pointer_cast(flecs::api::query_module("console")); + + EXPECT_CALL(*mock_console.get(), activate_license(session_id)); + + uut.activate_license(); +}