Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

device: implement and test device license activation #697

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 23 additions & 17 deletions daemon/modules/console/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
75 changes: 75 additions & 0 deletions daemon/modules/console/__mocks__/console.h
Original file line number Diff line number Diff line change
@@ -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 <gmock/gmock.h>

#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::console_t> _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
20 changes: 20 additions & 0 deletions daemon/modules/device/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion daemon/modules/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

#pragma once

#include "module_base/module.h"
#include "daemon/modules/module_base/module.h"

namespace flecs {
namespace module {
Expand Down
3 changes: 2 additions & 1 deletion daemon/modules/device/impl/device_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -51,6 +51,7 @@ class device_t
auto do_validate_license() //
-> result_t;

flecs::module::device_t* _parent;
std::string _session_id;
};

Expand Down
2 changes: 1 addition & 1 deletion daemon/modules/device/src/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ register_module_t<device_t> _reg("device");
}

device_t::device_t()
: _impl{new impl::device_t{}}
: _impl{new impl::device_t{this}}
{}

device_t::~device_t() = default;
Expand Down
15 changes: 13 additions & 2 deletions daemon/modules/device/src/impl/device_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,21 @@
#include <boost/uuid/uuid_io.hpp>
#include <fstream>

#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() //
Expand Down Expand Up @@ -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<flecs::module::console_t>(api::query_module("console"));

return console_api->activate_license(_parent->session_id());
}

auto device_t::do_validate_license() //
Expand Down
5 changes: 4 additions & 1 deletion daemon/modules/device/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down
21 changes: 20 additions & 1 deletion daemon/modules/device/test/test_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <regex>

#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<flecs::module::console_t>("console");
}
};

const auto session_id_regex = std::regex{"[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}"};
Expand Down Expand Up @@ -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::module::console_t>(flecs::api::query_module("console"));

EXPECT_CALL(*mock_console.get(), activate_license(session_id));

uut.activate_license();
}