129 lines
4.5 KiB
C++
129 lines
4.5 KiB
C++
#include <eedb/db/data/PgUsers.hpp>
|
|
|
|
#include <eedb/data/AuthIdentity.hpp>
|
|
|
|
#include <eedb/db/model/auth_identity.h>
|
|
#include <eedb/db/model/auth_info.h>
|
|
#include <eedb/db/model/auth_token.h>
|
|
#include <eedb/db/model/user_action.h>
|
|
#include <eedb/db/model/user_audit.h>
|
|
#include <sqlpp11/sqlpp11.h>
|
|
|
|
#include <utils/DbTestBase.hpp>
|
|
|
|
constexpr eedb::user_audit t_user_audit;
|
|
constexpr eedb::user_action t_user_action;
|
|
constexpr eedb::auth_identity t_auth_identity;
|
|
constexpr eedb::auth_info t_auth_info;
|
|
constexpr eedb::auth_token t_auth_token;
|
|
|
|
class PgUsersTest : public DbTestBase< PgUsersTest > {
|
|
public:
|
|
PgUsersTest() {
|
|
sut = std::make_unique< eedb::PgUsers >(db());
|
|
db().native()->start_transaction();
|
|
}
|
|
|
|
~PgUsersTest() {
|
|
db().native()->rollback_transaction(false);
|
|
}
|
|
|
|
auto createUser(std::string name, std::string email) {
|
|
auto info_ = std::make_unique< eedb::AuthInfo >(
|
|
eedb::Password{"bcrypt", "OM/Z1c4WBFXvwkxh", "$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q"}, eedb::Email{email});
|
|
auto identity_ = std::make_unique< eedb::ConstAuthIdentity >(name, "loginname");
|
|
return sut->addUser(std::move(info_), std::move(identity_));
|
|
}
|
|
|
|
auto createTestUsers() {
|
|
createUser("test_user_1", "test_user_1@eedb.pl");
|
|
createUser("test_user_2", "test_user_2@eedb.pl");
|
|
createUser("test_user_3", "test_user_3@eedb.pl");
|
|
createUser("test_user_4", "test_user_4@eedb.pl");
|
|
createUser("test_user_5", "test_user_5@eedb.pl");
|
|
createUser("test_user_6", "test_user_6@eedb.pl");
|
|
}
|
|
|
|
auto idMap() {
|
|
std::map< std::string, int64_t > _userIdMap;
|
|
auto ids = db()( //
|
|
select(t_auth_info.id, t_auth_identity.identity)
|
|
.from(t_auth_info //
|
|
.inner_join(t_auth_identity)
|
|
.on(t_auth_identity.auth_info_id == t_auth_info.id))
|
|
.unconditionally());
|
|
|
|
for(const auto & row : ids) {
|
|
_userIdMap[row.identity] = row.id;
|
|
}
|
|
|
|
return _userIdMap;
|
|
}
|
|
|
|
protected:
|
|
std::unique_ptr< eedb::PgUsers > sut;
|
|
};
|
|
|
|
template <>
|
|
std::unique_ptr< PgTestDatabasePrepare > DbTestBase< PgUsersTest >::_test_db = {};
|
|
|
|
using namespace sqlpp;
|
|
|
|
TEST_F(PgUsersTest, createValidUser) {
|
|
auto info_ = std::make_unique< eedb::AuthInfo >(
|
|
eedb::Password{"bcrypt", "OM/Z1c4WBFXvwkxh", "$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q"},
|
|
eedb::Email{"none@eedb.pl"});
|
|
auto identity_ = std::make_unique< eedb::ConstAuthIdentity >("username", "loginname");
|
|
|
|
auto new_user = sut->addUser(std::move(info_), std::move(identity_));
|
|
ASSERT_TRUE(new_user);
|
|
|
|
auto info = db()(select(sqlpp::all_of(t_auth_info)).from(t_auth_info).where(t_auth_info.id == new_user->uid()));
|
|
EXPECT_FALSE(info.empty());
|
|
|
|
EXPECT_EQ(info.front().password_hash, "$2y$07$RyytUhDhLDbAPjf0b0r2Y.dsg.FlQ7L.xzWHMmoelI81u0MfBrW7q");
|
|
EXPECT_EQ(info.front().password_method, "bcrypt");
|
|
EXPECT_EQ(info.front().password_salt, "OM/Z1c4WBFXvwkxh");
|
|
EXPECT_EQ(info.front().email, "none@eedb.pl");
|
|
EXPECT_EQ(info.front().status.value(), 0); /// ODO status:Waiting for verification
|
|
EXPECT_EQ(info.front().id.value(), new_user->uid());
|
|
|
|
auto identity = db()(select(t_auth_identity.provider, t_auth_identity.identity)
|
|
.from(t_auth_identity)
|
|
.where(t_auth_identity.auth_info_id == info.front().id));
|
|
|
|
EXPECT_EQ(identity.front().provider, "loginname");
|
|
EXPECT_EQ(identity.front().identity, "username");
|
|
}
|
|
|
|
TEST_F(PgUsersTest, createSameUserMustFail) {
|
|
createUser("_test_user_", "test_email@eedb.pl");
|
|
EXPECT_THROW(createUser("_test_user_", "test_email@eedb.pl"), sqlpp::postgresql::integrity_constraint_violation);
|
|
}
|
|
|
|
TEST_F(PgUsersTest, createWithSameEmailMustFail) {
|
|
createUser("_test_user_1", "test_email@eedb.pl");
|
|
EXPECT_THROW(createUser("_test_user_2", "test_email@eedb.pl"), sqlpp::postgresql::integrity_constraint_violation);
|
|
}
|
|
|
|
// TEST_F(PgUsersTest, findById) {
|
|
// createTestUsers();
|
|
|
|
// auto u = sut->findWith();
|
|
// EXPECT_EQ(idMap().at("test_user_1"), u->uid());
|
|
//}
|
|
|
|
TEST_F(PgUsersTest, findByIdentity) {
|
|
createTestUsers();
|
|
|
|
auto u = sut->findWith(eedb::ConstAuthIdentity{"test_user_1", "loginname"});
|
|
EXPECT_EQ(idMap().at("test_user_1"), u->uid());
|
|
}
|
|
|
|
TEST_F(PgUsersTest, findByEmail) {
|
|
createTestUsers();
|
|
|
|
auto u = sut->findWith(eedb::Email{"test_user_5@eedb.pl"});
|
|
EXPECT_EQ(idMap().at("test_user_5"), u->uid());
|
|
}
|