diff --git a/src/test/database-test.js b/src/test/database-test.js index 9cd6c866e..4b1bca23a 100644 --- a/src/test/database-test.js +++ b/src/test/database-test.js @@ -507,16 +507,30 @@ describe('database', function () { it('delByIdentifierAndClientId succeeds', function (done) { tokendb.delByIdentifierAndClientId(TOKEN_0.identifier, TOKEN_0.clientId, function (error) { expect(error).to.be(null); - done(); + + tokendb.get(TOKEN_0.accessToken, function (error, result) { + expect(error).to.be.a(DatabaseError); + expect(error.reason).to.be(DatabaseError.NOT_FOUND); + expect(result).to.not.be.ok(); + done(); + }); }); }); - it('get of previously deleted token fails', function (done) { - tokendb.get(TOKEN_0.accessToken, function (error, result) { - expect(error).to.be.a(DatabaseError); - expect(error.reason).to.be(DatabaseError.NOT_FOUND); - expect(result).to.not.be.ok(); - done(); + it('delByClientId succeeds', function (done) { + tokendb.add(TOKEN_0.accessToken, TOKEN_0.identifier, TOKEN_0.clientId, TOKEN_0.expires, TOKEN_0.scope, function (error) { + expect(error).to.be(null); + + tokendb.delByClientId(TOKEN_0.clientId, function (error, result) { + expect(error).to.not.be.ok(); + + tokendb.get(TOKEN_0.accessToken, function (error, result) { + expect(error).to.be.a(DatabaseError); + expect(error.reason).to.be(DatabaseError.NOT_FOUND); + expect(result).to.not.be.ok(); + done(); + }); + }); }); }); }); diff --git a/src/tokendb.js b/src/tokendb.js index 90149d6c7..09f34d5b9 100644 --- a/src/tokendb.js +++ b/src/tokendb.js @@ -7,6 +7,7 @@ exports = module.exports = { get: get, add: add, del: del, + delByClientId: delByClientId, getByIdentifier: getByIdentifier, delByIdentifier: delByIdentifier, getByIdentifierAndClientId: getByIdentifierAndClientId, @@ -69,6 +70,18 @@ function del(accessToken, callback) { }); } +function delByClientId(clientId, callback) { + assert.strictEqual(typeof clientId, 'string'); + assert.strictEqual(typeof callback, 'function'); + + database.query('DELETE FROM tokens WHERE clientId = ?', [ clientId ], function (error, result) { + if (error) return callback(new DatabaseError(DatabaseError.INTERNAL_ERROR, error)); + if (result.affectedRows !== 1) return callback(new DatabaseError(DatabaseError.NOT_FOUND)); + + return callback(null); + }); +} + function getByIdentifier(identifier, callback) { assert.strictEqual(typeof identifier, 'string'); assert.strictEqual(typeof callback, 'function');