Commit d0d5df9c authored by Steven Murray's avatar Steven Murray
Browse files

Added unit-test isAdmin_true

parent 46104174
......@@ -36,9 +36,10 @@ cta::catalogue::SqliteCatalogue::SqliteCatalogue(): m_conn(":memory:") {
void cta::catalogue::SqliteCatalogue::createDbSchema() {
const char *const sql =
"CREATE TABLE ADMIN_USER("
"USER_NAME TEXT,"
"GROUP_NAME TEXT,"
"COMMENT TEXT,"
"USER_NAME TEXT,"
"GROUP_NAME TEXT,"
"COMMENT TEXT,"
"CREATION_LOG_USER_NAME TEXT,"
"CREATION_LOG_GROUP_NAME TEXT,"
......@@ -54,8 +55,9 @@ void cta::catalogue::SqliteCatalogue::createDbSchema() {
");"
"CREATE TABLE ADMIN_HOST("
"HOST_NAME TEXT,"
"COMMENT TEXT,"
"HOST_NAME TEXT,"
"COMMENT TEXT,"
"CREATION_LOG_USER_NAME TEXT,"
"CREATION_LOG_GROUP_NAME TEXT,"
......@@ -110,6 +112,7 @@ void cta::catalogue::SqliteCatalogue::insertAdminUser(
"INSERT INTO ADMIN_USER("
"USER_NAME,"
"GROUP_NAME,"
"COMMENT,"
"CREATION_LOG_USER_NAME,"
......@@ -159,6 +162,7 @@ void cta::catalogue::SqliteCatalogue::insertAdminHost(
const char *const sql =
"INSERT INTO ADMIN_HOST("
"HOST_NAME,"
"COMMENT,"
"CREATION_LOG_USER_NAME,"
......@@ -172,6 +176,7 @@ void cta::catalogue::SqliteCatalogue::insertAdminHost(
"LAST_MOD_TIME)"
"VALUES("
":HOST_NAME,"
":COMMENT,"
":CREATION_LOG_USER_NAME,"
......@@ -229,7 +234,8 @@ std::list<cta::common::dataStructures::AdminUser>
"SELECT "
"USER_NAME AS USER_NAME,"
"GROUP_NAME AS GROUP_NAME,"
"COMMENT AS COMMENT,"
"COMMENT AS COMMENT,"
"CREATION_LOG_USER_NAME AS CREATION_LOG_USER_NAME,"
"CREATION_LOG_GROUP_NAME AS CREATION_LOG_GROUP_NAME,"
......@@ -321,7 +327,8 @@ std::list<cta::common::dataStructures::AdminHost> cta::catalogue::SqliteCatalogu
const char *const sql =
"SELECT "
"HOST_NAME AS HOST_NAME,"
"COMMENT AS COMMENT,"
"COMMENT AS COMMENT,"
"CREATION_LOG_USER_NAME AS CREATION_LOG_USER_NAME,"
"CREATION_LOG_GROUP_NAME AS CREATION_LOG_GROUP_NAME,"
......@@ -380,7 +387,12 @@ void cta::catalogue::SqliteCatalogue::modifyAdminHostComment(const common::dataS
//------------------------------------------------------------------------------
// createStorageClass
//------------------------------------------------------------------------------
void cta::catalogue::SqliteCatalogue::createStorageClass(const common::dataStructures::SecurityIdentity &cliIdentity, const std::string &name, const uint64_t nbCopies, const std::string &comment) {}
void cta::catalogue::SqliteCatalogue::createStorageClass(
const common::dataStructures::SecurityIdentity &cliIdentity,
const std::string &name,
const uint64_t nbCopies,
const std::string &comment) {
}
//------------------------------------------------------------------------------
// deleteStorageClass
......@@ -774,5 +786,43 @@ cta::common::dataStructures::MountPolicy cta::catalogue::SqliteCatalogue::
//------------------------------------------------------------------------------
bool cta::catalogue::SqliteCatalogue::isAdmin(
const common::dataStructures::SecurityIdentity &cliIdentity) {
return false;
return userIsAdmin(cliIdentity.user.name) && hostIsAdmin(cliIdentity.host);
}
//------------------------------------------------------------------------------
// userIsAdmin
//------------------------------------------------------------------------------
bool cta::catalogue::SqliteCatalogue::userIsAdmin(const std::string &userName)
const {
const char *const sql =
"SELECT "
"USER_NAME AS USER_NAME "
"FROM ADMIN_USER WHERE "
"USER_NAME = :USER_NAME;";
SqliteStmt stmt(m_conn, sql);
stmt.bind(":USER_NAME", userName);
if(SQLITE_ROW == stmt.step()) {
return true;
} else {
return false;
}
}
//------------------------------------------------------------------------------
// hostIsAdmin
//------------------------------------------------------------------------------
bool cta::catalogue::SqliteCatalogue::hostIsAdmin(const std::string &hostName)
const {
const char *const sql =
"SELECT "
"HOST_NAME AS HOST_NAME "
"FROM ADMIN_HOST WHERE "
"HOST_NAME = :HOST_NAME;";
SqliteStmt stmt(m_conn, sql);
stmt.bind(":HOST_NAME", hostName);
if(SQLITE_ROW == stmt.step()) {
return true;
} else {
return false;
}
}
......@@ -197,6 +197,24 @@ private:
const std::string &comment,
const common::dataStructures::EntryLog &creationLog);
/**
* Returns true if the specified user name is listed in the ADMIN_USER table.
*
* @param userName The name of the user to be search for in the ADMIN_USER
* table.
* @return true if the specified user name is listed in the ADMIN_USER table.
*/
bool userIsAdmin(const std::string &userName) const;
/**
* Returns true if the specified host name is listed in the ADMIN_HOST table.
*
* @param userName The name of the host to be search for in the ADMIN_HOST
* table.
* @return true if the specified host name is listed in the ADMIN_HOST table.
*/
bool hostIsAdmin(const std::string &userName) const;
}; // class SqliteCatalogue
} // namespace catalogue
......
......@@ -63,25 +63,17 @@ protected:
cta::common::dataStructures::SecurityIdentity m_adminSI;
};
TEST_F(cta_catalogue_SqliteCatalogueTest, constructor) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
}
TEST_F(cta_catalogue_SqliteCatalogueTest, createBootstrapAdminAndHostNoAuth) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
catalogue->createBootstrapAdminAndHostNoAuth(
catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment);
{
std::list<common::dataStructures::AdminUser> admins;
ASSERT_NO_THROW(admins = catalogue->getAdminUsers());
ASSERT_NO_THROW(admins = catalogue.getAdminUsers());
ASSERT_EQ(1, admins.size());
const common::dataStructures::AdminUser admin = admins.front();
......@@ -99,7 +91,7 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createBootstrapAdminAndHostNoAuth) {
{
std::list<common::dataStructures::AdminHost> hosts;
ASSERT_NO_THROW(hosts = catalogue->getAdminHosts());
ASSERT_NO_THROW(hosts = catalogue.getAdminHosts());
ASSERT_EQ(1, hosts.size());
const common::dataStructures::AdminHost host = hosts.front();
......@@ -119,15 +111,14 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createBootstrapAdminAndHostNoAuth) {
TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminUser) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
ASSERT_NO_THROW(catalogue->createBootstrapAdminAndHostNoAuth(
ASSERT_NO_THROW(catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment));
{
std::list<common::dataStructures::AdminUser> admins;
ASSERT_NO_THROW(admins = catalogue->getAdminUsers());
ASSERT_NO_THROW(admins = catalogue.getAdminUsers());
ASSERT_EQ(1, admins.size());
const common::dataStructures::AdminUser admin = admins.front();
......@@ -144,12 +135,12 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminUser) {
}
const std::string createAdminUserComment = "create admin user comment";
ASSERT_NO_THROW(catalogue->createAdminUser(m_bootstrapAdminSI, m_adminUI,
ASSERT_NO_THROW(catalogue.createAdminUser(m_bootstrapAdminSI, m_adminUI,
createAdminUserComment));
{
std::list<common::dataStructures::AdminUser> admins;
admins = catalogue->getAdminUsers();
admins = catalogue.getAdminUsers();
ASSERT_EQ(2, admins.size());
const common::dataStructures::AdminUser a1 = admins.front();
......@@ -195,15 +186,14 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminUser) {
TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminUser_same_admin_twice) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
ASSERT_NO_THROW(catalogue->createBootstrapAdminAndHostNoAuth(
ASSERT_NO_THROW(catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment));
{
std::list<common::dataStructures::AdminUser> admins;
ASSERT_NO_THROW(admins = catalogue->getAdminUsers());
ASSERT_NO_THROW(admins = catalogue.getAdminUsers());
ASSERT_EQ(1, admins.size());
const common::dataStructures::AdminUser admin = admins.front();
......@@ -219,25 +209,24 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminUser_same_admin_twice) {
ASSERT_EQ(creationLog, lastModificationLog);
}
ASSERT_NO_THROW(catalogue->createAdminUser(m_bootstrapAdminSI, m_adminUI,
ASSERT_NO_THROW(catalogue.createAdminUser(m_bootstrapAdminSI, m_adminUI,
"comment 1"));
ASSERT_THROW(catalogue->createAdminUser(m_bootstrapAdminSI, m_adminUI,
ASSERT_THROW(catalogue.createAdminUser(m_bootstrapAdminSI, m_adminUI,
"comment 2"), exception::Exception);
}
TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminHost) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
ASSERT_NO_THROW(catalogue->createBootstrapAdminAndHostNoAuth(
ASSERT_NO_THROW(catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment));
{
std::list<common::dataStructures::AdminUser> admins;
ASSERT_NO_THROW(admins = catalogue->getAdminUsers());
ASSERT_NO_THROW(admins = catalogue.getAdminUsers());
ASSERT_EQ(1, admins.size());
const common::dataStructures::AdminUser admin = admins.front();
......@@ -255,12 +244,12 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminHost) {
const std::string createAdminHostComment = "create host user comment";
const std::string anotherAdminHost = "another_admin_host";
ASSERT_NO_THROW(catalogue->createAdminHost(m_bootstrapAdminSI,
ASSERT_NO_THROW(catalogue.createAdminHost(m_bootstrapAdminSI,
anotherAdminHost, createAdminHostComment));
{
std::list<common::dataStructures::AdminHost> hosts;
hosts = catalogue->getAdminHosts();
hosts = catalogue.getAdminHosts();
ASSERT_EQ(2, hosts.size());
const common::dataStructures::AdminHost h1 = hosts.front();
......@@ -309,15 +298,14 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminHost) {
TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminHost_same_host_twice) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
ASSERT_NO_THROW(catalogue->createBootstrapAdminAndHostNoAuth(
ASSERT_NO_THROW(catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment));
{
std::list<common::dataStructures::AdminUser> admins;
ASSERT_NO_THROW(admins = catalogue->getAdminUsers());
ASSERT_NO_THROW(admins = catalogue.getAdminUsers());
ASSERT_EQ(1, admins.size());
const common::dataStructures::AdminUser admin = admins.front();
......@@ -335,20 +323,30 @@ TEST_F(cta_catalogue_SqliteCatalogueTest, createAdminHost_same_host_twice) {
const std::string anotherAdminHost = "another_admin_host";
ASSERT_NO_THROW(catalogue->createAdminHost(m_bootstrapAdminSI,
ASSERT_NO_THROW(catalogue.createAdminHost(m_bootstrapAdminSI,
anotherAdminHost, "comment 1"));
ASSERT_THROW(catalogue->createAdminHost(m_bootstrapAdminSI,
ASSERT_THROW(catalogue.createAdminHost(m_bootstrapAdminSI,
anotherAdminHost, "coment 2"), exception::Exception);
}
TEST_F(cta_catalogue_SqliteCatalogueTest, isAdmin_notAdmin) {
TEST_F(cta_catalogue_SqliteCatalogueTest, isAdmin_false) {
using namespace cta;
std::unique_ptr<catalogue::Catalogue> catalogue;
ASSERT_NO_THROW(catalogue.reset(new catalogue::SqliteCatalogue()));
catalogue::SqliteCatalogue catalogue;
ASSERT_FALSE(catalogue.isAdmin(m_cliSI));
}
TEST_F(cta_catalogue_SqliteCatalogueTest, isAdmin_true) {
using namespace cta;
catalogue::SqliteCatalogue catalogue;
catalogue.createBootstrapAdminAndHostNoAuth(
m_cliSI, m_bootstrapAdminUI, m_bootstrapAdminSI.host, m_bootstrapComment);
ASSERT_FALSE(catalogue->isAdmin(m_cliSI));
ASSERT_TRUE(catalogue.isAdmin(m_bootstrapAdminSI));
}
} // namespace unitTests
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment