From d66be554303f6ff6e82dbb8485cbf576780ea54d Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Tue, 4 Oct 2016 17:38:58 +0100 Subject: Add basics of user service --- gentoobrowse-api/api/users.ice | 15 ++++++++ gentoobrowse-api/domain/user-models.ice | 16 ++++++++ gentoobrowse-api/service/main.cpp | 2 + .../service/sql/users/authenticate.sql | 5 +++ gentoobrowse-api/service/sql/users/verify.sql | 6 +++ gentoobrowse-api/service/usersimpl.cpp | 26 +++++++++++++ gentoobrowse-api/service/usersimpl.h | 21 ++++++++++ gentoobrowse-api/unittests/Jamfile.jam | 8 ++++ gentoobrowse-api/unittests/fixtures/users.dat | 1 + gentoobrowse-api/unittests/mockDefs.cpp | 3 +- gentoobrowse-api/unittests/mockDefs.h | 2 + gentoobrowse-api/unittests/testUsers.cpp | 45 ++++++++++++++++++++++ 12 files changed, 149 insertions(+), 1 deletion(-) create mode 100644 gentoobrowse-api/api/users.ice create mode 100644 gentoobrowse-api/domain/user-models.ice create mode 100644 gentoobrowse-api/service/sql/users/authenticate.sql create mode 100644 gentoobrowse-api/service/sql/users/verify.sql create mode 100644 gentoobrowse-api/service/usersimpl.cpp create mode 100644 gentoobrowse-api/service/usersimpl.h create mode 100644 gentoobrowse-api/unittests/testUsers.cpp diff --git a/gentoobrowse-api/api/users.ice b/gentoobrowse-api/api/users.ice new file mode 100644 index 0000000..798ab52 --- /dev/null +++ b/gentoobrowse-api/api/users.ice @@ -0,0 +1,15 @@ +#ifndef GENTOO_USERS +#define GENTOO_USERS + +#include +#include + +module Gentoo { + interface Users { + User authenticate(string username, string password) throws Slicer::NoRowsReturned; + User verify(string username, string verifyguid) throws Slicer::NoRowsReturned; + }; +}; + +#endif + diff --git a/gentoobrowse-api/domain/user-models.ice b/gentoobrowse-api/domain/user-models.ice new file mode 100644 index 0000000..ccbda99 --- /dev/null +++ b/gentoobrowse-api/domain/user-models.ice @@ -0,0 +1,16 @@ +#ifndef GENTOO_USER_MODELS +#define GENTOO_USER_MODELS + +module Gentoo { + class User { + int userid; + string username; + string userrealname; + string useremail; + }; + + exception InvalidCredentials { }; +}; + +#endif + diff --git a/gentoobrowse-api/service/main.cpp b/gentoobrowse-api/service/main.cpp index a936b74..bdf6ed5 100644 --- a/gentoobrowse-api/service/main.cpp +++ b/gentoobrowse-api/service/main.cpp @@ -2,6 +2,7 @@ #include #include #include "portageimpl.h" +#include "usersimpl.h" #include "maintenanceimpl.h" namespace Gentoo { @@ -13,6 +14,7 @@ class Api : public IceTray::Service { auto db = getConnectionPool(ic, "postgresql", "GentooBrowseAPI"); adp->add(new Portage(db), ic->stringToIdentity("portage")); adp->add(new Maintenance(db), ic->stringToIdentity("maintenance")); + adp->add(new Users(db), ic->stringToIdentity("users")); } }; diff --git a/gentoobrowse-api/service/sql/users/authenticate.sql b/gentoobrowse-api/service/sql/users/authenticate.sql new file mode 100644 index 0000000..86b8cc1 --- /dev/null +++ b/gentoobrowse-api/service/sql/users/authenticate.sql @@ -0,0 +1,5 @@ +SELECT userid, username, userrealname, useremail +FROM gentoobrowse.users +WHERE username = ? +AND userpassword = ? +AND verifyguid IS NULL diff --git a/gentoobrowse-api/service/sql/users/verify.sql b/gentoobrowse-api/service/sql/users/verify.sql new file mode 100644 index 0000000..91a7151 --- /dev/null +++ b/gentoobrowse-api/service/sql/users/verify.sql @@ -0,0 +1,6 @@ +-- libdbpp:no-cursor +UPDATE gentoobrowse.users SET + verifyguid = NULL +WHERE username = ? +AND verifyguid = ? +RETURNING userid, username, userrealname, useremail diff --git a/gentoobrowse-api/service/usersimpl.cpp b/gentoobrowse-api/service/usersimpl.cpp new file mode 100644 index 0000000..dc3769b --- /dev/null +++ b/gentoobrowse-api/service/usersimpl.cpp @@ -0,0 +1,26 @@ +#include "usersimpl.h" +#include +#include +#include + +namespace Gentoo { +namespace Service { +Users::Users(boost::shared_ptr> d) : + IceTray::AbstractCachingDatabaseClient(d) +{ +} + +Gentoo::UserPtr +Users::authenticate(const std::string & username, const std::string & password, const Ice::Current &) +{ + return fetchCache(sql::users::authenticate, 30, username, password); +} + +Gentoo::UserPtr +Users::verify(const std::string & username, const std::string & verifyguid, const Ice::Current &) +{ + return fetch(sql::users::verify, username, verifyguid); +} +} +} + diff --git a/gentoobrowse-api/service/usersimpl.h b/gentoobrowse-api/service/usersimpl.h new file mode 100644 index 0000000..76a6ade --- /dev/null +++ b/gentoobrowse-api/service/usersimpl.h @@ -0,0 +1,21 @@ +#ifndef USERSIMPL_H +#define USERSIMPL_H + +#include +#include +#include + +namespace Gentoo { +namespace Service { +class DLL_PUBLIC Users : public Gentoo::Users, IceTray::AbstractCachingDatabaseClient { + public: + Users(boost::shared_ptr> d); + + Gentoo::UserPtr authenticate(const std::string &, const std::string &, const Ice::Current &) override; + Gentoo::UserPtr verify(const std::string &, const std::string &, const Ice::Current &) override; +}; +} +} + +#endif + diff --git a/gentoobrowse-api/unittests/Jamfile.jam b/gentoobrowse-api/unittests/Jamfile.jam index 97f5c45..0bedd4f 100644 --- a/gentoobrowse-api/unittests/Jamfile.jam +++ b/gentoobrowse-api/unittests/Jamfile.jam @@ -89,6 +89,14 @@ run testCommon : testPortage ; +run + testUsers.cpp + : : : + ../db/schema.sql + BOOST_TEST_DYN_LINK + testCommon + : testUsers ; + run testBugs.cpp : : : diff --git a/gentoobrowse-api/unittests/fixtures/users.dat b/gentoobrowse-api/unittests/fixtures/users.dat index 4544513..a1f60e5 100644 --- a/gentoobrowse-api/unittests/fixtures/users.dat +++ b/gentoobrowse-api/unittests/fixtures/users.dat @@ -1 +1,2 @@ 1 randomdan Dan Goodliffe p@55w0rd dan@randomdan.homeip.net \N +2 unverified Dan Goodliffe p@55w0rd something@randomdan.homeip.net a24eb4fa-8a42-11e6-a04e-c86000a60e12 diff --git a/gentoobrowse-api/unittests/mockDefs.cpp b/gentoobrowse-api/unittests/mockDefs.cpp index 91b8164..70f5b69 100644 --- a/gentoobrowse-api/unittests/mockDefs.cpp +++ b/gentoobrowse-api/unittests/mockDefs.cpp @@ -21,7 +21,8 @@ Maintenance::Maintenance() : TestClient::TestClient() : m(getProxy("maintenance")), - p(getProxy("portage")) + p(getProxy("portage")), + u(getProxy("users")) { } diff --git a/gentoobrowse-api/unittests/mockDefs.h b/gentoobrowse-api/unittests/mockDefs.h index 792f875..9a35c1f 100644 --- a/gentoobrowse-api/unittests/mockDefs.h +++ b/gentoobrowse-api/unittests/mockDefs.h @@ -5,6 +5,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ class DLL_PUBLIC TestClient : public IceTray::DryIceClient { Gentoo::MaintenancePrx m; Gentoo::PortagePrx p; + Gentoo::UsersPrx u; }; #define SQL_REQUIRE_EQUAL(sql, type, expected) \ diff --git a/gentoobrowse-api/unittests/testUsers.cpp b/gentoobrowse-api/unittests/testUsers.cpp new file mode 100644 index 0000000..93c8fdd --- /dev/null +++ b/gentoobrowse-api/unittests/testUsers.cpp @@ -0,0 +1,45 @@ +#define BOOST_TEST_MODULE TestUsers +#include + +#include "mockDefs.h" + +BOOST_GLOBAL_FIXTURE( Service ); + +BOOST_FIXTURE_TEST_SUITE(tp, TestClient) + +BOOST_AUTO_TEST_CASE( authenticateGood ) +{ + auto user = u->authenticate("randomdan", "p@55w0rd"); + BOOST_REQUIRE_EQUAL(1, user->userid); + BOOST_REQUIRE_EQUAL("randomdan", user->username); + BOOST_REQUIRE_EQUAL("Dan Goodliffe", user->userrealname); + BOOST_REQUIRE_EQUAL("dan@randomdan.homeip.net", user->useremail); +} + +BOOST_AUTO_TEST_CASE( authenticateBad ) +{ + BOOST_REQUIRE_THROW(u->authenticate("bad", "login"), Slicer::NoRowsReturned); +} + +BOOST_AUTO_TEST_CASE( authenticateUnverified ) +{ + BOOST_REQUIRE_THROW(u->authenticate("unverified", "p@55w0rd"), Slicer::NoRowsReturned); +} + +BOOST_AUTO_TEST_CASE( authenticateVerifyBad ) +{ + BOOST_REQUIRE_THROW(u->verify("unverified", "c2075a2e-8a45-11e6-bb50-c86000a60e12"), Slicer::NoRowsReturned); +} + +BOOST_AUTO_TEST_CASE( authenticateVerify ) +{ + auto verified = u->verify("unverified", "a24eb4fa-8a42-11e6-a04e-c86000a60e12"); + BOOST_REQUIRE(verified); + auto user = u->authenticate("unverified", "p@55w0rd"); + BOOST_REQUIRE(user); + BOOST_REQUIRE_EQUAL(verified->userid, user->userid); + BOOST_REQUIRE_THROW(u->verify("unverified", "a24eb4fa-8a42-11e6-a04e-c86000a60e12"), Slicer::NoRowsReturned); +} + +BOOST_AUTO_TEST_SUITE_END() + -- cgit v1.2.3