Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit b7ef3c0

Browse files
authored
Update mongodb node module in test (#1108)
1 parent c723788 commit b7ef3c0

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

test/restTestCasesKarma/scripts/authorization/init/initdb.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ var currentVersion = '1.0';
1515
var fs = require('fs');
1616
var path = require('path');
1717

18+
var MongoClient = require('mongodb').MongoClient;
19+
var connectOption = {
20+
useUnifiedTopology: true,
21+
};
22+
var client;
1823
var db;
1924
var cipher = require('./cipher');
2025

@@ -23,6 +28,9 @@ var configFile = path.join(dirName, 'management_api.toml');
2328
var sampleServiceFile = path.resolve(dirName, '../environment.js');
2429

2530
function prepareDB(next) {
31+
if (dbURL.indexOf('mongodb://') !== 0) {
32+
dbURL = 'mongodb://' + dbURL;
33+
}
2634
if (fs.existsSync(cipher.astore)) {
2735
cipher.unlock(cipher.k, cipher.astore, function cb (err, authConfig) {
2836
if (!err) {
@@ -34,20 +42,34 @@ function prepareDB(next) {
3442
} else {
3543
log.error('Failed to get mongodb auth:', err);
3644
}
37-
db = require('mongojs')(dbURL, ['services', 'infos', 'rooms']);
38-
next();
45+
MongoClient.connect(dbURL, connectOption, function(err, cli) {
46+
if (!err) {
47+
client = cli;
48+
db = client.db();
49+
next();
50+
} else {
51+
console.error('Failed to connect mongodb:', err);
52+
}
53+
});
3954
});
4055
} else {
41-
db = require('mongojs')(dbURL, ['services', 'infos', 'rooms']);
42-
next();
56+
MongoClient.connect(dbURL, connectOption, function(err, cli) {
57+
if (!err) {
58+
client = cli;
59+
db = client.db();
60+
next();
61+
} else {
62+
console.error('Failed to connect mongodb:', err);
63+
}
64+
});
4365
}
4466
}
4567

4668
function upgradeH264(next) {
47-
db.rooms.find({}).toArray(function (err, rooms) {
69+
db.collection('rooms').find({}).toArray(function (err, rooms) {
4870
if (err) {
4971
console.log('Error in getting rooms:', err.message);
50-
db.close();
72+
client.close();
5173
return;
5274
}
5375
if (!rooms || rooms.length === 0) {
@@ -83,7 +105,7 @@ function upgradeH264(next) {
83105
}
84106
});
85107

86-
db.rooms.save(room, function (e, saved) {
108+
db.collection('rooms').updateOne({_id: room._id}, room, function (e, saved) {
87109
if (e) {
88110
console.log('Error in upgrading room:', room._id, e.message);
89111
}
@@ -97,28 +119,28 @@ function upgradeH264(next) {
97119
}
98120

99121
function checkVersion (next) {
100-
db.infos.findOne({_id: 1}, function cb (err, info) {
122+
db.collection('infos').findOne({_id: 1}, function cb (err, info) {
101123
if (err) {
102124
console.log('mongodb: error in query info');
103-
return db.close();
125+
return client.close();
104126
}
105127
if (info) {
106128
if (info.version === '1.0') {
107129
next();
108130
}
109131
} else {
110-
db.services.findOne({}, function cb (err, service) {
132+
db.collection('services').findOne({}, function cb (err, service) {
111133
if (err) {
112134
console.log('mongodb: error in query service');
113-
return db.close();
135+
return client.close();
114136
}
115137
var upgradeNext = function(next) {
116138
upgradeH264(function() {
117139
info = {_id: 1, version: currentVersion};
118-
db.infos.save(info, function cb (e, s) {
140+
db.collection('infos').save(info, function cb (e, s) {
119141
if (e) {
120142
console.log('mongodb: error in updating version');
121-
return db.close();
143+
return client.close();
122144
}
123145
next();
124146
});
@@ -156,15 +178,15 @@ function checkVersion (next) {
156178
}
157179

158180
function prepareService (serviceName, next) {
159-
db.services.findOne({name: serviceName}, function cb (err, service) {
181+
db.collection('services').findOne({name: serviceName}, function cb (err, service) {
160182
if (err || !service) {
161183
var crypto = require('crypto');
162184
var key = crypto.pbkdf2Sync(crypto.randomBytes(64).toString('hex'), crypto.randomBytes(32).toString('hex'), 4000, 128, 'sha256').toString('base64');
163185
service = {name: serviceName, key: cipher.encrypt(cipher.k, key), encrypted: true, rooms: [], __v: 0};
164-
db.services.save(service, function cb (err, saved) {
186+
db.collection('services').save(service, function cb (err, saved) {
165187
if (err) {
166188
console.log('mongodb: error in adding', serviceName);
167-
return db.close();
189+
return client.close();
168190
}
169191
saved.key = key;
170192
next(saved);
@@ -222,7 +244,7 @@ prepareDB(function() {
222244
var sampleServiceKey = service.key;
223245
console.log('sampleServiceId:', sampleServiceId);
224246
console.log('sampleServiceKey:', sampleServiceKey);
225-
db.close();
247+
client.close();
226248

227249
writeSampleFile(sampleServiceId, sampleServiceKey);
228250
});

test/restTestCasesKarma/scripts/authorization/init/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"express": "*",
99
"fraction.js": "*",
1010
"log4js": "^6.3.0",
11-
"mongojs": "",
11+
"mongodb": "^3.6.4",
1212
"mongoose": "^5.3.9",
1313
"toml": "*"
1414
},

0 commit comments

Comments
 (0)