jstests/auth/lib/commands_builtin_roles.js
/**
 * Parameters:
 *   conn -- connection, either to standalone mongod,
 *      or to mongos in sharded cluster
 *   t -- a test object from the tests array in jstests/auth/commands.js
 *   testcase -- the particular testcase from t to test
 *   r -- a role object from the "roles" array above
 *
 * Returns:
 *   An empty string on success, or an error string
 *   on test failure.
 */


An empty string on success, or an error string
on test failure.
An empty string on success or an error string on test failure .
function testProperAuthorization(conn, t, testcase, r) {
    var out = "";
    var runOnDb = conn.getDB(testcase.runOnDb);
    var state = authCommandsLib.setup(conn, t, runOnDb);
    assert(r.db.auth("user|" + r.key, "password"));
    authCommandsLib.authenticatedSetup(t, runOnDb);
    var command = t.command;
    if (typeof (command) === "function") {
        command = t.command(state, testcase.commandArgs);
    }
    var res = runOnDb.runCommand(command);
    if (testcase.roles[r.key]) {
        if (res.ok == 0 && res.code == authErrCode) {
            out = "expected authorization success" +
                " but received " + tojson(res) + " on db " + testcase.runOnDb + " with role " +
                r.key;
        } else if (res.ok == 0 && !testcase.expectFail && res.code != commandNotSupportedCode) {
            out = "command failed with " + tojson(res) + " on db " + testcase.runOnDb +
                " with role " + r.key;
        }
    } else {
        if (res.ok == 1 ||
            (res.ok == 0 && res.code != authErrCode && res.code !== ErrorCodes.CommandNotFound)) {
            out = "expected authorization failure" +
                " but received result " + tojson(res) + " on db " + testcase.runOnDb +
                " with role " + r.key;
        }
    }
    r.db.logout();
    authCommandsLib.teardown(conn, t, runOnDb, res);
    return out;
}
function testProperAuthorization ( conn , t , testcase , r ) { var out = "" ; var runOnDb = conn . getDB ( testcase . runOnDb ) ; var state = authCommandsLib . setup ( conn , t , runOnDb ) ; assert ( r . db . auth ( "user|" + r . key , "password" ) ) ; authCommandsLib . authenticatedSetup ( t , runOnDb ) ; var command = t . command ; if ( typeof ( command ) === "function" ) { command = t . command ( state , testcase . commandArgs ) ; } var res = runOnDb . runCommand ( command ) ; if ( testcase . roles [ r . key ] ) { if ( res . ok == 0 && res . code == authErrCode ) { out = "expected authorization success" + " but received " + tojson ( res ) + " on db " + testcase . runOnDb + " with role " + r . key ; } else if ( res . ok == 0 && ! testcase . expectFail && res . code != commandNotSupportedCode ) { out = "command failed with " + tojson ( res ) + " on db " + testcase . runOnDb + " with role " + r . key ; } } else { if ( res . ok == 1 || ( res . ok == 0 && res . code != authErrCode && res . code !== ErrorCodes . CommandNotFound ) ) { out = "expected authorization failure" + " but received result " + tojson ( res ) + " on db " + testcase . runOnDb + " with role " + r . key ; } } r . db . logout ( ) ; authCommandsLib . teardown ( conn , t , runOnDb , res ) ; return out ; }
Parameters:
   conn -- connection, either to standalone mongod,
      or to mongos in sharded cluster
   t -- a test object from the tests array in jstests/auth/commands.js
   testcase -- the particular testcase from t to test
   r -- a role object from the "roles" array above
Parameters : conn -- connection either to standalone mongod or to mongos in sharded cluster t -- a test object from the tests array in jstests / auth / commands . js testcase -- the particular testcase from t to test r -- a role object from the " roles " array above
// don't error if the test failed with code commandNotSupported since // some storage engines don't support some commands. // Don't error if the test failed with CommandNotFound rather than an authorization failure // because some commands may be guarded by feature flags.