summaryrefslogtreecommitdiff
path: root/jstests/auth/user_cache_doc_source.js
blob: a4b57eb66ae169956224732e6f21da04d617422b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Tests the user cache document source

(function() {
'use strict';

var mongod = MongoRunner.runMongod({auth: ""});
var db = mongod.getDB("admin");
db.createUser({user: "root", pwd: "root", roles: ["userAdminAnyDatabase"]});
db.auth("root", "root");
db.createUser({user: "readOnlyUser", pwd: "foobar", roles: ["readAnyDatabase"]});
var readUserCache = function() {
    var ret = db.aggregate([{$listCachedAndActiveUsers: {}}, {$sort: {'active': -1}}]).toArray();
    print(tojson(ret));
    return ret;
};

const expectedOnlyRoot = [{username: "root", db: "admin", active: true}];
assert.eq(expectedOnlyRoot, readUserCache());

/* This is broken because of SERVER-36384
var newConn = new Mongo(mongod.name);
assert.eq(newConn.getDB("admin").auth("readOnlyUser", "foobar"), 1);

const expectedBothActive = [
    { username: "root", db: "admin", active: true },
    { username: "readOnlyUser", db: "admin", active: true }
];
assert.eq(expectedBothActive, readUserCache());

newConn.close();
*/

var awaitShell = startParallelShell(function() {
    assert.eq(db.getSiblingDB("admin").auth("readOnlyUser", "foobar"), 1);
}, mongod.port);

const expectedReadOnlyInactive = [
    {username: "root", db: "admin", active: true},
    {username: "readOnlyUser", db: "admin", active: false},
];
assert.soon(function() {
    return friendlyEqual(expectedReadOnlyInactive, readUserCache());
});

MongoRunner.stopMongod(mongod);
awaitShell({checkExitSuccess: false});
})();