summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2016-10-04 17:38:58 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2016-10-04 17:38:58 +0100
commitd66be554303f6ff6e82dbb8485cbf576780ea54d (patch)
treed705271b2a75b1b753f7eb45411cadee094948bd
parentAdd method for getting package use flag options (diff)
downloadgentoobrowse-api-d66be554303f6ff6e82dbb8485cbf576780ea54d.tar.bz2
gentoobrowse-api-d66be554303f6ff6e82dbb8485cbf576780ea54d.tar.xz
gentoobrowse-api-d66be554303f6ff6e82dbb8485cbf576780ea54d.zip
Add basics of user service
-rw-r--r--gentoobrowse-api/api/users.ice15
-rw-r--r--gentoobrowse-api/domain/user-models.ice16
-rw-r--r--gentoobrowse-api/service/main.cpp2
-rw-r--r--gentoobrowse-api/service/sql/users/authenticate.sql5
-rw-r--r--gentoobrowse-api/service/sql/users/verify.sql6
-rw-r--r--gentoobrowse-api/service/usersimpl.cpp26
-rw-r--r--gentoobrowse-api/service/usersimpl.h21
-rw-r--r--gentoobrowse-api/unittests/Jamfile.jam8
-rw-r--r--gentoobrowse-api/unittests/fixtures/users.dat1
-rw-r--r--gentoobrowse-api/unittests/mockDefs.cpp3
-rw-r--r--gentoobrowse-api/unittests/mockDefs.h2
-rw-r--r--gentoobrowse-api/unittests/testUsers.cpp45
12 files changed, 149 insertions, 1 deletions
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 <user-models.ice>
+#include <sqlExceptions.ice>
+
+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 <Ice/ObjectAdapter.h>
#include <icetrayService.h>
#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 <modifycommand.h>
+#include <sql/users/authenticate.sql.h>
+#include <sql/users/verify.sql.h>
+
+namespace Gentoo {
+namespace Service {
+Users::Users(boost::shared_ptr<AdHoc::ResourcePool<DB::Connection>> d) :
+ IceTray::AbstractCachingDatabaseClient(d)
+{
+}
+
+Gentoo::UserPtr
+Users::authenticate(const std::string & username, const std::string & password, const Ice::Current &)
+{
+ return fetchCache<Gentoo::UserPtr>(sql::users::authenticate, 30, username, password);
+}
+
+Gentoo::UserPtr
+Users::verify(const std::string & username, const std::string & verifyguid, const Ice::Current &)
+{
+ return fetch<Gentoo::UserPtr>(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 <users.h>
+#include <visibility.h>
+#include <abstractCachingDatabaseClient.h>
+
+namespace Gentoo {
+namespace Service {
+class DLL_PUBLIC Users : public Gentoo::Users, IceTray::AbstractCachingDatabaseClient {
+ public:
+ Users(boost::shared_ptr<AdHoc::ResourcePool<DB::Connection>> 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
@@ -90,6 +90,14 @@ run
: testPortage ;
run
+ testUsers.cpp
+ : : :
+ <dependency>../db/schema.sql
+ <define>BOOST_TEST_DYN_LINK
+ <library>testCommon
+ : testUsers ;
+
+run
testBugs.cpp
: : :
<dependency>../db/schema.sql
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<Gentoo::MaintenancePrx>("maintenance")),
- p(getProxy<Gentoo::PortagePrx>("portage"))
+ p(getProxy<Gentoo::PortagePrx>("portage")),
+ u(getProxy<Gentoo::UsersPrx>("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 <visibility.h>
#include <dryice.h>
#include <portage.h>
+#include <users.h>
#include <maintenance.h>
#include <selectcommandUtil.impl.h>
@@ -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 <boost/test/unit_test.hpp>
+
+#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()
+