Skip to content

Commit 8cd97ec

Browse files
authored
Merge pull request #615 from sidorares/promise-changeuser
fix tests for #614
2 parents 5927562 + e4009ae commit 8cd97ec

File tree

2 files changed

+90
-1
lines changed

2 files changed

+90
-1
lines changed

promise.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ PromiseConnection.prototype.prepare = function(options) {
180180
});
181181
};
182182

183+
PromiseConnection.prototype.changeUser = function(options) {
184+
var c = this.connection;
185+
const localErr = new Error();
186+
return new this.Promise(function(resolve, reject) {
187+
c.changeUser(options, function(err) {
188+
if (err) {
189+
localErr.message = err.message;
190+
localErr.code = err.code;
191+
localErr.errno = err.errno;
192+
localErr.sqlState = err.sqlState;
193+
reject(localErr);
194+
} else {
195+
resolve();
196+
}
197+
});
198+
});
199+
};
200+
183201
function PromisePreparedStatementInfo(statement, promiseImpl) {
184202
this.statement = statement;
185203
this.Promise = promiseImpl;

test/integration/promise-wrappers/test-promise-wrappers.js

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
var config = require('../../common.js').config;
2+
var Buffer = require('safe-buffer').Buffer;
23

34
var skipTest = false;
45
if (typeof Promise == 'undefined') {
@@ -23,6 +24,7 @@ var doneEventsConnect = false;
2324
var doneCalledPool = false;
2425
var exceptionCaughtPool = false;
2526
var doneEventsPool = false;
27+
var doneChangeUser = false;
2628

2729
function testBasic() {
2830
var connResolved;
@@ -65,7 +67,6 @@ function testErrors() {
6567
return connResolved.query('select 2+2 as qqq');
6668
})
6769
.catch(function(err) {
68-
console.log(err);
6970
exceptionCaught = true;
7071
if (connResolved) {
7172
connResolved.end();
@@ -247,6 +248,74 @@ function testEventsPool() {
247248
pool.pool.emit('release');
248249
}
249250

251+
function testChangeUser() {
252+
var onlyUsername = function(name) {
253+
return name.substring(0, name.indexOf('@'));
254+
};
255+
var connResolved;
256+
var connPromise = createConnection(config)
257+
.then(function(conn) {
258+
connResolved = conn;
259+
return connResolved.query(
260+
"GRANT ALL ON *.* TO 'changeuser1'@'%' IDENTIFIED BY 'changeuser1pass'"
261+
);
262+
})
263+
.then(function() {
264+
return connResolved.query(
265+
"GRANT ALL ON *.* TO 'changeuser2'@'%' IDENTIFIED BY 'changeuser2pass'"
266+
);
267+
})
268+
.then(function() {
269+
return connResolved.query('FLUSH PRIVILEGES');
270+
})
271+
.then(function() {
272+
return connResolved.changeUser({
273+
user: 'changeuser1',
274+
password: 'changeuser1pass'
275+
});
276+
})
277+
.then(function() {
278+
return connResolved.query('select current_user()');
279+
})
280+
.then(function(result) {
281+
const rows = result[0];
282+
assert.deepEqual(onlyUsername(rows[0]['current_user()']), 'changeuser1');
283+
return connResolved.changeUser({
284+
user: 'changeuser2',
285+
password: 'changeuser2pass'
286+
});
287+
})
288+
.then(function() {
289+
return connResolved.query('select current_user()');
290+
})
291+
.then(function(result) {
292+
const rows = result[0];
293+
assert.deepEqual(onlyUsername(rows[0]['current_user()']), 'changeuser2');
294+
return connResolved.changeUser({
295+
user: 'changeuser1',
296+
passwordSha1: Buffer.from(
297+
'f961d39c82138dcec42b8d0dcb3e40a14fb7e8cd',
298+
'hex'
299+
) // sha1(changeuser1pass)
300+
});
301+
})
302+
.then(function() {
303+
return connResolved.query('select current_user()');
304+
})
305+
.then(function(result) {
306+
const rows = result[0];
307+
assert.deepEqual(onlyUsername(rows[0]['current_user()']), 'changeuser1');
308+
doneChangeUser = true;
309+
return connResolved.end();
310+
})
311+
.catch(function(err) {
312+
if (connResolved) {
313+
connResolved.end();
314+
}
315+
throw err;
316+
});
317+
}
318+
250319
testBasic();
251320
testErrors();
252321
testObjParams();
@@ -256,6 +325,7 @@ testBasicPool();
256325
testErrorsPool();
257326
testObjParamsPool();
258327
testEventsPool();
328+
testChangeUser();
259329

260330
process.on('exit', function() {
261331
if (skipTest) {
@@ -267,6 +337,7 @@ process.on('exit', function() {
267337
assert.equal(doneCalledPool, true);
268338
assert.equal(exceptionCaughtPool, true);
269339
assert.equal(doneEventsPool, true);
340+
assert.equal(doneChangeUser, true);
270341
});
271342

272343
process.on('unhandledRejection', function(err) {

0 commit comments

Comments
 (0)