mongo/jstests/auth/sbe_plan_cache_user_roles.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

55 lines
1.9 KiB
JavaScript

/**
* Test $$USER_ROLES works correctly with the SBE plan cache. The same query should return the
* updated user role info when a different user logs in.
* @tags: [
* # Multiple servers can mess up the plan cache list.
* assumes_standalone_mongod,
* # During fcv upgrade/downgrade the engine might not be what we expect.
* cannot_run_during_upgrade_downgrade,
* ]
*/
import {checkSbeFullyEnabled} from "jstests/libs/query/sbe_util.js";
const mongod = MongoRunner.runMongod();
const dbName = "test";
const db = mongod.getDB(dbName);
const sbeEnabled = checkSbeFullyEnabled(db);
// Create two users, each with different roles.
assert.commandWorked(db.runCommand({createUser: "user1", pwd: "pwd", roles: [{role: "read", db: dbName}]}));
assert.commandWorked(db.runCommand({createUser: "user2", pwd: "pwd", roles: [{role: "readWrite", db: dbName}]}));
const coll = db.sbe_plan_cache_user_roles;
coll.drop();
const verifyPlanCache = function (role) {
if (sbeEnabled) {
const caches = coll.getPlanCache().list();
assert.eq(1, caches.length, caches);
assert.eq(caches[0].cachedPlan.stages.includes(role), false, caches);
}
};
assert.commandWorked(coll.insert({_id: 1}));
// While logged in as user1, we should see user1's roles.
db.auth("user1", "pwd");
let results = coll.find({}, {roles: "$$USER_ROLES"}).toArray();
assert.eq(results.length, 1);
assert.eq(results[0].roles, [{_id: "test.read", role: "read", db: "test"}]);
// It can take two executions of a query for a plan to get cached.
coll.find({}, {roles: "$$USER_ROLES"}).toArray();
verifyPlanCache("test.read");
db.logout();
// While logged in as user2, we should see user2's roles.
db.auth("user2", "pwd");
results = coll.find({}, {roles: "$$USER_ROLES"}).toArray();
assert.eq(results.length, 1);
assert.eq(results[0].roles, [{_id: "test.readWrite", role: "readWrite", db: "test"}]);
verifyPlanCache("test.readWrite");
db.logout();
MongoRunner.stopMongod(mongod);