1
// Test the behavior when two users from the same database are authenticated serially on a single
2
// connection. Expected behavior is that the first user is implicitly logged out by the second
5
// Regression test for SERVER-8144.
7
// Raises an exception if "status" is not a GetLastError object indicating success.
8
function assertGLEOK(status) {
9
assert(status.ok && status.err === null,
10
"Expected OK status object; found " + tojson(status));
13
// Raises an exception if "status" is not a GetLastError object indicating failure.
14
function assertGLENotOK(status) {
15
assert(status.ok && status.err !== null,
16
"Expected not-OK status object; found " + tojson(status));
19
// Asserts that inserting "obj" into "collection" succeeds.
20
function assertInsertSucceeds(collection, obj) {
21
collection.insert(obj);
22
assertGLEOK(collection.getDB().getLastErrorObj());
25
// Asserts that inserting "obj" into "collection" fails.
26
function assertInsertFails(collection, obj) {
27
collection.insert(obj);
28
assertGLENotOK(collection.getDB().getLastErrorObj());
32
var conn = MongoRunner.runMongod({ auth: "", smallfiles: "" });
33
var admin = conn.getDB("admin");
34
var test = conn.getDB("test");
36
admin.addUser('admin', 'a');
37
assert(admin.auth('admin', 'a'));
38
test.addUser({user: 'reader', pwd: 'a', roles: [ "read" ]});
39
test.addUser({user: 'writer', pwd: 'a', roles: [ "readWrite" ]});
42
// Nothing logged in, can neither read nor write.
43
assertInsertFails(test.docs, { value: 0 });
44
assert.throws(function() { test.foo.findOne() });
46
// Writer logged in, can read and write.
47
test.auth('writer', 'a');
48
assertInsertSucceeds(test.docs, { value: 1 });
51
// Reader logged in, replacing writer, can only read.
52
test.auth('reader', 'a');
53
assertInsertFails(test.docs, { value: 2 });