summaryrefslogtreecommitdiff
path: root/jstests/core/list_sessions.js
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2017-08-08 18:16:49 -0400
committerSara Golemon <sara.golemon@mongodb.com>2017-08-23 09:20:45 -0400
commit0b09cf4477986074237c3f53afa6830a89fd4592 (patch)
treec2ecdbcf7c266c3fdb6a65897e0c53b09e499730 /jstests/core/list_sessions.js
parenta09f19822fb2a1e5b662bd8d542dd8e2f2607fc6 (diff)
downloadmongo-0b09cf4477986074237c3f53afa6830a89fd4592.tar.gz
SERVER-29628 $listSessions aggregation stage
Diffstat (limited to 'jstests/core/list_sessions.js')
-rw-r--r--jstests/core/list_sessions.js57
1 files changed, 57 insertions, 0 deletions
diff --git a/jstests/core/list_sessions.js b/jstests/core/list_sessions.js
new file mode 100644
index 00000000000..0636377a3be
--- /dev/null
+++ b/jstests/core/list_sessions.js
@@ -0,0 +1,57 @@
+// Basic tests for the $listSessions aggregation stage.
+
+(function() {
+ 'use strict';
+ load('jstests/aggregation/extras/utils.js');
+
+ const admin = db.getSiblingDB('admin');
+ const pipeline = [{'$listSessions': {}}];
+ function listSessions() {
+ return admin.system.sessions.aggregate(pipeline);
+ }
+
+ // Start a new session and capture its sessionId.
+ const myid = assert.commandWorked(admin.runCommand({startSession: 1})).id.id;
+ assert(myid !== undefined);
+
+ // Sync cache to collection and ensure it arrived.
+ assert.commandWorked(admin.runCommand({refreshLogicalSessionCacheNow: 1}));
+ var resultArray;
+ assert.soon(function() {
+ resultArray = listSessions().toArray();
+ if (resultArray.length < 1) {
+ return false;
+ }
+ const resultArrayMine = resultArray
+ .map(function(sess) {
+ return sess._id.id;
+ })
+ .filter(function(id) {
+ return 0 == bsonWoCompare({x: id}, {x: myid});
+ });
+ return resultArrayMine.length == 1;
+ }, "Failed to locate session in collection");
+
+ // Try asking for the session by username.
+ const myusername = (function() {
+ if (0 == bsonWoCompare({x: resultArray[0]._id.uid}, {x: computeSHA256Block("")})) {
+ // Code for "we're running in no-auth mode"
+ return {user: "", db: ""};
+ }
+ const connstats = assert.commandWorked(db.runCommand({connectionStatus: 1}));
+ const authUsers = connstats.authInfo.authenticatedUsers;
+ assert(authUsers !== undefined);
+ assert.eq(authUsers.length, 1);
+ assert(authUsers[0].user !== undefined);
+ assert(authUsers[0].db !== undefined);
+ return {user: authUsers[0].user, db: authUsers[0].db};
+ })();
+ function listMySessions() {
+ return admin.system.sessions.aggregate([{'$listSessions': {users: [myusername]}}]);
+ }
+ const myArray = listMySessions().toArray();
+ assert.eq(resultArray.length, myArray.length);
+
+ // Make sure pipelining other collections fail.
+ assertErrorCode(admin.system.collections, pipeline, ErrorCodes.InvalidNamespace);
+})();