RepoRole RepoBSONFactory::makeRepoRole( const std::string &roleName, const std::string &database, const std::vector<RepoPermission> &permissions, const RepoRole &oldRole ) { RepoRole updatedOldRole = oldRole.cloneAndUpdatePermissions(permissions); return _makeRepoRole(roleName, database, updatedOldRole.getPrivileges(), updatedOldRole.getInheritedRoles()); }
TEST(RepoBSONTest, MakeRepoRoleTest) { std::string roleName = "repoRole"; std::string databaseName = "admin"; std::vector<RepoPermission> permissions; std::string proDB1 = "database"; std::string proName1 = "project1"; AccessRight proAccess1 = AccessRight::READ; std::string proDB2 = "databaseb"; std::string proName2 = "project2"; AccessRight proAccess2 = AccessRight::WRITE; std::string proDB3 = "databasec"; std::string proName3 = "project3"; AccessRight proAccess3 = AccessRight::READ_WRITE; permissions.push_back({ proDB1, proName1, proAccess1 }); permissions.push_back({ proDB2, proName2, proAccess2 }); permissions.push_back({ proDB3, proName3, proAccess3 }); RepoRole role = RepoBSONFactory::makeRepoRole(roleName, databaseName, permissions); EXPECT_EQ(databaseName, role.getDatabase()); EXPECT_EQ(roleName, role.getName()); EXPECT_EQ(0, role.getInheritedRoles().size()); std::vector<RepoPermission> accessRights = role.getProjectAccessRights(); ASSERT_EQ(permissions.size(), accessRights.size()); for (int i = 0; i < accessRights.size(); ++i) { //Order is not guaranteed, this is a long winded way to find the same permission again //but it should be fine as it's only 3 members bool found = false; for (int j = 0; j < permissions.size(); ++j) { found |= permissions[j].database == accessRights[i].database && permissions[j].project == accessRights[i].project && permissions[j].permission == accessRights[i].permission; } EXPECT_TRUE(found); } }
TEST(RepoBSONTest, MakeRepoRoleTest2) { std::string roleName = "repoRole"; std::string databaseName = "admin"; std::vector<RepoPrivilege> privileges; std::vector <std::pair<std::string, std::string>> inheritedRoles; privileges.push_back({ "db1", "col1", {DBActions::FIND} }); privileges.push_back({ "db2", "col2", { DBActions::INSERT, DBActions::CREATE_USER } }); privileges.push_back({ "db1", "col2", { DBActions::FIND, DBActions::DROP_ROLE } }); inheritedRoles.push_back(std::pair < std::string, std::string > {"orange", "superUser"}); RepoRole role = RepoBSONFactory::_makeRepoRole(roleName, databaseName, privileges, inheritedRoles); EXPECT_EQ(databaseName, role.getDatabase()); EXPECT_EQ(roleName, role.getName()); auto inheritedRolesOut = role.getInheritedRoles(); ASSERT_EQ(inheritedRoles.size(), inheritedRolesOut.size()); for (int i = 0; i < inheritedRolesOut.size(); ++i) { EXPECT_EQ(inheritedRolesOut[i].first, inheritedRoles[i].first); EXPECT_EQ(inheritedRolesOut[i].second, inheritedRoles[i].second); } auto privOut = role.getPrivileges(); ASSERT_EQ(privOut.size(), privileges.size()); for (int i = 0; i < privOut.size(); ++i) { EXPECT_EQ(privOut[i].database, privileges[i].database); EXPECT_EQ(privOut[i].collection, privileges[i].collection); EXPECT_EQ(privOut[i].actions.size(), privileges[i].actions.size()); } }