Mercurial > malikania
changeset 112:90c51ffdbbce
Server: add broken_account for tests, #682
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 05 Sep 2017 19:57:36 +0200 |
parents | 8963c68f023c |
children | 7bf22341dcc4 |
files | libserver-test/CMakeLists.txt libserver-test/malikania/server/db/broken_account.cpp libserver-test/malikania/server/db/broken_account.hpp tests/libserver/db/account/main.cpp |
diffstat | 4 files changed, 274 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/libserver-test/CMakeLists.txt Tue Sep 05 14:24:14 2017 +0200 +++ b/libserver-test/CMakeLists.txt Tue Sep 05 19:57:36 2017 +0200 @@ -20,12 +20,14 @@ set( HEADERS + ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/broken_account.hpp ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/test_account.hpp ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/test_database.hpp ) set( SOURCES + ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/broken_account.cpp ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/test_account.cpp ${libmlk-server-test_SOURCE_DIR}/malikania/server/db/test_database.cpp )
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/db/broken_account.cpp Tue Sep 05 19:57:36 2017 +0200 @@ -0,0 +1,61 @@ +/* + * broken_account.cpp -- database account object (broken implementation) + * + * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <stdexcept> + +#include "broken_account.hpp" + +namespace mlk { + +namespace server { + +void broken_account::do_save() +{ + if (!bool(allow_ & allow_flags::save)) + throw std::runtime_error("broken do_save"); + + id_ = 1; +} + +void broken_account::do_set_password(const std::string&) +{ + if (!bool(allow_ & allow_flags::set_password)) + throw std::runtime_error("broken do_set_password"); +} + +void broken_account::do_set_email(const std::string&) +{ + if (!bool(allow_ & allow_flags::set_email)) + throw std::runtime_error("broken do_set_email"); +} + +void broken_account::do_set_firstname(const std::string&) +{ + if (!bool(allow_ & allow_flags::set_firstname)) + throw std::runtime_error("broken do_set_firstname"); +} + +void broken_account::do_set_lastname(const std::string&) +{ + if (!bool(allow_ & allow_flags::set_lastname)) + throw std::runtime_error("broken do_set_lastname"); +} + +} // !server + +} // !mlk
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libserver-test/malikania/server/db/broken_account.hpp Tue Sep 05 19:57:36 2017 +0200 @@ -0,0 +1,143 @@ +/* + * broken_account.hpp -- database account object (broken implementation) + * + * Copyright (c) 2013-2017 David Demelier <markand@malikania.fr> + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef MALIKANIA_SERVER_BROKEN_ACCOUNT_HPP +#define MALIKANIA_SERVER_BROKEN_ACCOUNT_HPP + +/** + * \file broken_account.hpp + * \brief Database account object (broken implementation). + */ + +#include <malikania/server/db/account.hpp> + +namespace mlk { + +namespace server { + +/** + * \brief Database account object (broken implementation). + */ +class broken_account : public account { +public: + /** + * \brief Define which function should not be broken. + */ + enum class allow_flags { + none = 0, //!< everything is broken + save = (1 << 0), //!< allow do_save + set_password = (1 << 1), //!< allow do_set_password + set_email = (1 << 2), //!< allow do_set_email + set_firstname = (1 << 3), //!< allow do_set_firstname + set_lastname = (1 << 4) //!< allow do_set_lastname + }; + +private: + allow_flags allow_; + +protected: + /** + * \copydoc account::do_save + */ + void do_save() override; + + /** + * \copydoc account::do_set_password + */ + void do_set_password(const std::string& password) override; + + /** + * \copydoc account::do_set_email + */ + void do_set_email(const std::string& email) override; + + /** + * \copydoc account::do_set_firstname + */ + void do_set_firstname(const std::string& name) override; + + /** + * \copydoc account::do_set_lastname + */ + void do_set_lastname(const std::string& name) override; + +public: + /** + * Construct a broken account. + * + * \param login the login + * \param password the password + * \param flags the flags + */ + inline broken_account(std::string login, + std::string password, + allow_flags flags = allow_flags::none) noexcept + : account(std::move(login), std::move(password)) + , allow_(flags) + { + } +}; + +/** + * \cond ENUM_HIDDEN_SYMBOLS + */ + +inline broken_account::allow_flags operator^(broken_account::allow_flags v1, broken_account::allow_flags v2) noexcept +{ + return static_cast<broken_account::allow_flags>(static_cast<unsigned>(v1) ^ static_cast<unsigned>(v2)); +} + +inline broken_account::allow_flags operator&(broken_account::allow_flags v1, broken_account::allow_flags v2) noexcept +{ + return static_cast<broken_account::allow_flags>(static_cast<unsigned>(v1) & static_cast<unsigned>(v2)); +} + +inline broken_account::allow_flags operator|(broken_account::allow_flags v1, broken_account::allow_flags v2) noexcept +{ + return static_cast<broken_account::allow_flags>(static_cast<unsigned>(v1) | static_cast<unsigned>(v2)); +} + +inline broken_account::allow_flags operator~(broken_account::allow_flags v) noexcept +{ + return static_cast<broken_account::allow_flags>(~static_cast<unsigned>(v)); +} + +inline broken_account::allow_flags& operator|=(broken_account::allow_flags& v1, broken_account::allow_flags v2) noexcept +{ + return v1 = v1 | v2; +} + +inline broken_account::allow_flags& operator&=(broken_account::allow_flags& v1, broken_account::allow_flags v2) noexcept +{ + return v1 = v1 & v2; +} + +inline broken_account::allow_flags& operator^=(broken_account::allow_flags& v1, broken_account::allow_flags v2) noexcept +{ + return v1 = v1 ^ v2; +} + +/** + * \endcond + */ + +} // !server + +} // !mlk + +#endif // !MALIKANIA_SERVER_BROKEN_ACCOUNT_HPP
--- a/tests/libserver/db/account/main.cpp Tue Sep 05 14:24:14 2017 +0200 +++ b/tests/libserver/db/account/main.cpp Tue Sep 05 19:57:36 2017 +0200 @@ -20,6 +20,7 @@ #include <boost/test/unit_test.hpp> #include <malikania/server/db/test_database.hpp> +#include <malikania/server/db/broken_account.hpp> namespace mlk { @@ -173,6 +174,73 @@ BOOST_AUTO_TEST_SUITE_END() +BOOST_AUTO_TEST_SUITE(broken) + +BOOST_AUTO_TEST_CASE(save) +{ + broken_account ac("markand", "nopassword"); + + try { + ac.save(); + } catch (...) {} + + BOOST_TEST(ac.is_draft()); +} + +BOOST_AUTO_TEST_CASE(set_password) +{ + broken_account ac("markand", "nopassword", broken_account::allow_flags::save); + + ac.save(); + + try { + ac.set_password("newpassword"); + } catch (...) {} + + BOOST_TEST(ac.password() == "nopassword"); +} + +BOOST_AUTO_TEST_CASE(set_email) +{ + broken_account ac("markand", "nopassword", broken_account::allow_flags::save); + + ac.save(); + + try { + ac.set_email("markand@malikania.fr"); + } catch (...) {} + + BOOST_TEST(ac.email() == ""); +} + +BOOST_AUTO_TEST_CASE(set_firstname) +{ + broken_account ac("markand", "nopassword", broken_account::allow_flags::save); + + ac.save(); + + try { + ac.set_firstname("David"); + } catch (...) {} + + BOOST_TEST(ac.firstname() == ""); +} + +BOOST_AUTO_TEST_CASE(set_lastname) +{ + broken_account ac("markand", "nopassword", broken_account::allow_flags::save); + + ac.save(); + + try { + ac.set_lastname("David"); + } catch (...) {} + + BOOST_TEST(ac.lastname() == ""); +} + +BOOST_AUTO_TEST_SUITE_END() + } // !server } // !mlk