UNPKG

apollo-passport-rethinkdbdash

Version:
644 lines (520 loc) 19.3 kB
'use strict'; var _chai = require('chai'); var _chai2 = _interopRequireDefault(_chai); require('regenerator-runtime/runtime'); var _rethinkdbdash = require('rethinkdbdash'); var _rethinkdbdash2 = _interopRequireDefault(_rethinkdbdash); var _index = require('./index'); var _index2 = _interopRequireDefault(_index); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var should = _chai2.default.should(); var host = process.env.RETHINKDB_HOST || '127.0.0.1'; var port = process.env.RETHINKDB_PORT || 28015; var connection = (0, _rethinkdbdash2.default)({ servers: [{ host: host, port: port }], db: 'no-default-database-for-testing' }); // not really disposable, no way to get an instance with a different // database, too expensive to open new connection for each test, but // this will still work as "intended" for single thread tests. function disposable() { var name, r; return regeneratorRuntime.async(function disposable$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: name = "tmp" + Math.floor(Math.random() * 10000); _context2.next = 3; return regeneratorRuntime.awrap(connection.dbCreate(name).run()); case 3: r = Object.create(connection); r.dispose = function _callee() { return regeneratorRuntime.async(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return regeneratorRuntime.awrap(this.dbDrop(name).run()); case 2: case 'end': return _context.stop(); } } }, null, this); }; r._poolMaster._options.db = name; return _context2.abrupt('return', r); case 7: case 'end': return _context2.stop(); } } }, null, this); } describe('apollo-passport-rethinkdbdash', function () { // sufficiently tested by other tests for now describe('constructor()', function () { it('accepts options', function () { var r = { db: function db() {} }; var options = { init: false, userTableName: 'personnel' }; var db = new _index2.default(r, options); db.userTableName.should.equal(options.userTableName); }); }); it('ready()', function _callee2() { var r, db; return regeneratorRuntime.async(function _callee2$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: _context3.next = 2; return regeneratorRuntime.awrap(disposable()); case 2: r = _context3.sent; db = new _index2.default(r); _context3.next = 6; return regeneratorRuntime.awrap(db._ready()); case 6: // now do a fake init db.initted = false; db.readySubs.length.should.equal(0); db._ready(); db.readySubs.length.should.equal(1); db.readySubs.shift().call(); db.initted = true; db.readySubs.length.should.equal(0); db._ready(); db.readySubs.length.should.equal(0); _context3.next = 17; return regeneratorRuntime.awrap(r.dispose()); case 17: case 'end': return _context3.stop(); } } }, null, undefined); }); it('config; database & local output', function _callee3() { var r, db, data; return regeneratorRuntime.async(function _callee3$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return regeneratorRuntime.awrap(disposable()); case 2: r = _context4.sent; db = new _index2.default(r); _context4.next = 6; return regeneratorRuntime.awrap(db.setConfigKey('test', 'test1', { a: 1 })); case 6: _context4.next = 8; return regeneratorRuntime.awrap(db.fetchConfig()); case 8: data = _context4.sent; data.should.deep.equal({ test: { test1: { type: 'test', id: 'test1', a: 1 } } }); _context4.next = 12; return regeneratorRuntime.awrap(r.dispose()); case 12: case 'end': return _context4.stop(); } } }, null, undefined); }); describe('users', function () { describe('createUser()', function () { var r = void 0; before(function _callee4() { return regeneratorRuntime.async(function _callee4$(_context5) { while (1) { switch (_context5.prev = _context5.next) { case 0: _context5.next = 2; return regeneratorRuntime.awrap(disposable()); case 2: r = _context5.sent; case 3: case 'end': return _context5.stop(); } } }, null, undefined); }); after(function _callee5() { return regeneratorRuntime.async(function _callee5$(_context6) { while (1) { switch (_context6.prev = _context6.next) { case 0: _context6.next = 2; return regeneratorRuntime.awrap(r.dispose()); case 2: case 'end': return _context6.stop(); } } }, null, undefined); }); it('inserts a user and returns the id when no id given', function _callee6() { var db, user, id, users, added; return regeneratorRuntime.async(function _callee6$(_context7) { while (1) { switch (_context7.prev = _context7.next) { case 0: db = new _index2.default(r); _context7.next = 3; return regeneratorRuntime.awrap(db._ready()); case 3: _context7.next = 5; return regeneratorRuntime.awrap(db.users.delete({ durability: 'soft' })); case 5: user = { name: 'John Sheppard' }; _context7.next = 8; return regeneratorRuntime.awrap(db.createUser(user)); case 8: id = _context7.sent; _context7.next = 11; return regeneratorRuntime.awrap(db.users.run()); case 11: users = _context7.sent; added = users[0]; added.name.should.equal(user.name); id.should.equal(added.id); _context7.next = 17; return regeneratorRuntime.awrap(db.users.delete({ durability: 'soft' })); case 17: case 'end': return _context7.stop(); } } }, null, undefined); }); it('inserts a user and returns the id when an id is given', function _callee7() { var db, user, id, users, added; return regeneratorRuntime.async(function _callee7$(_context8) { while (1) { switch (_context8.prev = _context8.next) { case 0: db = new _index2.default(r); _context8.next = 3; return regeneratorRuntime.awrap(db._ready()); case 3: _context8.next = 5; return regeneratorRuntime.awrap(db.users.delete({ durability: 'soft' })); case 5: user = { id: 'sheppard', name: 'John Sheppard' }; _context8.next = 8; return regeneratorRuntime.awrap(db.createUser(user)); case 8: id = _context8.sent; _context8.next = 11; return regeneratorRuntime.awrap(db.users.run()); case 11: users = _context8.sent; added = users[0]; added.name.should.equal(user.name); id.should.equal(added.id); _context8.next = 17; return regeneratorRuntime.awrap(db.users.delete({ durability: 'soft' })); case 17: case 'end': return _context8.stop(); } } }, null, undefined); }); }); describe('fetching', function () { var users = [{ id: "sheppard", emails: [{ address: "sheppard@atlantis.net" }], services: { facebook: { id: "1" } } }, { id: "mckay", emails: [{ address: "mckay@atlantis.net" }] }]; var r = void 0, db = void 0; before(function _callee8() { return regeneratorRuntime.async(function _callee8$(_context9) { while (1) { switch (_context9.prev = _context9.next) { case 0: _context9.next = 2; return regeneratorRuntime.awrap(disposable()); case 2: r = _context9.sent; db = new _index2.default(r); _context9.next = 6; return regeneratorRuntime.awrap(db._ready()); case 6: _context9.next = 8; return regeneratorRuntime.awrap(db.users.insert(users).run()); case 8: case 'end': return _context9.stop(); } } }, null, undefined); }); after(function _callee9() { return regeneratorRuntime.async(function _callee9$(_context10) { while (1) { switch (_context10.prev = _context10.next) { case 0: r.dispose(); case 1: case 'end': return _context10.stop(); } } }, null, undefined); }); describe('fetchUserById', function () { it('returns a matching user if one exists', function _callee10() { var user; return regeneratorRuntime.async(function _callee10$(_context11) { while (1) { switch (_context11.prev = _context11.next) { case 0: _context11.next = 2; return regeneratorRuntime.awrap(db.fetchUserById('sheppard')); case 2: user = _context11.sent; user.id.should.equal("sheppard"); case 4: case 'end': return _context11.stop(); } } }, null, undefined); }); it('returns null on no match', function _callee11() { var user; return regeneratorRuntime.async(function _callee11$(_context12) { while (1) { switch (_context12.prev = _context12.next) { case 0: _context12.next = 2; return regeneratorRuntime.awrap(db.fetchUserById('todd')); case 2: user = _context12.sent; should.equal(user, null); case 4: case 'end': return _context12.stop(); } } }, null, undefined); }); }); describe('fetchUserByEmail()', function () { it('returns a matching user if one exists', function _callee12() { var user; return regeneratorRuntime.async(function _callee12$(_context13) { while (1) { switch (_context13.prev = _context13.next) { case 0: _context13.next = 2; return regeneratorRuntime.awrap(db.fetchUserByEmail('mckay@atlantis.net')); case 2: user = _context13.sent; user.id.should.equal("mckay"); case 4: case 'end': return _context13.stop(); } } }, null, undefined); }); it('returns null on no match', function _callee13() { var user; return regeneratorRuntime.async(function _callee13$(_context14) { while (1) { switch (_context14.prev = _context14.next) { case 0: _context14.next = 2; return regeneratorRuntime.awrap(db.fetchUserByEmail('non-existing-email')); case 2: user = _context14.sent; should.equal(user, null); case 4: case 'end': return _context14.stop(); } } }, null, undefined); }); }); describe('fetchUserByServiceIdOrEmail()', function () { it('matches by email', function _callee14() { var user; return regeneratorRuntime.async(function _callee14$(_context15) { while (1) { switch (_context15.prev = _context15.next) { case 0: _context15.next = 2; return regeneratorRuntime.awrap(db.fetchUserByServiceIdOrEmail('facebook', "no-match", 'mckay@atlantis.net')); case 2: user = _context15.sent; user.id.should.equal("mckay"); case 4: case 'end': return _context15.stop(); } } }, null, undefined); }); it('matches by service', function _callee15() { var user; return regeneratorRuntime.async(function _callee15$(_context16) { while (1) { switch (_context16.prev = _context16.next) { case 0: _context16.next = 2; return regeneratorRuntime.awrap(db.fetchUserByServiceIdOrEmail('facebook', "1", 'non-matching-email')); case 2: user = _context16.sent; user.id.should.equal("sheppard"); case 4: case 'end': return _context16.stop(); } } }, null, undefined); }); it('should return null on no match', function _callee16() { var user; return regeneratorRuntime.async(function _callee16$(_context17) { while (1) { switch (_context17.prev = _context17.next) { case 0: _context17.next = 2; return regeneratorRuntime.awrap(db.fetchUserByServiceIdOrEmail('no-service', 'no-id', 'no-email')); case 2: user = _context17.sent; should.equal(user, null); case 4: case 'end': return _context17.stop(); } } }, null, undefined); }); }); describe('assertUserEmailData()', function () { it('adds a new email address', function _callee17() { var user; return regeneratorRuntime.async(function _callee17$(_context18) { while (1) { switch (_context18.prev = _context18.next) { case 0: _context18.next = 2; return regeneratorRuntime.awrap(db.assertUserEmailData('mckay', 'mckay@sgc.mil')); case 2: _context18.next = 4; return regeneratorRuntime.awrap(db.fetchUserByEmail('mckay@sgc.mil')); case 4: user = _context18.sent; should.exist(user); case 6: case 'end': return _context18.stop(); } } }, null, undefined); }); it('updates/replaces an existing email address + data', function _callee18() { var email, user, data; return regeneratorRuntime.async(function _callee18$(_context19) { while (1) { switch (_context19.prev = _context19.next) { case 0: email = 'mckay@atlantis.net'; _context19.next = 3; return regeneratorRuntime.awrap(db.assertUserEmailData('mckay', email, { verified: true })); case 3: _context19.next = 5; return regeneratorRuntime.awrap(db.fetchUserByEmail(email)); case 5: user = _context19.sent; data = user.emails.find(function (data) { return data.address === email; }); data.should.deep.equal({ address: email, verified: true }); case 8: case 'end': return _context19.stop(); } } }, null, undefined); }); }); describe('assertUserServiceData()', function () { it('adds a new service record', function _callee19() { var user; return regeneratorRuntime.async(function _callee19$(_context20) { while (1) { switch (_context20.prev = _context20.next) { case 0: _context20.next = 2; return regeneratorRuntime.awrap(db.assertUserServiceData('mckay', 'facebook', { id: '5' })); case 2: _context20.next = 4; return regeneratorRuntime.awrap(db.fetchUserByServiceIdOrEmail('facebook', '5', null)); case 4: user = _context20.sent; user.services.facebook.id.should.equal('5'); case 6: case 'end': return _context20.stop(); } } }, null, undefined); }); it('updates/replaces an existing service record', function _callee20() { var user; return regeneratorRuntime.async(function _callee20$(_context21) { while (1) { switch (_context21.prev = _context21.next) { case 0: _context21.next = 2; return regeneratorRuntime.awrap(db.assertUserServiceData('mckay', 'facebook', { id: '5' })); case 2: _context21.next = 4; return regeneratorRuntime.awrap(db.fetchUserByServiceIdOrEmail('facebook', '5', null)); case 4: user = _context21.sent; user.services.facebook.id.should.equal('5'); case 6: case 'end': return _context21.stop(); } } }, null, undefined); }); }); it('mapUserToServiceData', function () { var fb = db.mapUserToServiceData(users[0], 'facebook'); fb.should.deep.equal(users[0].services.facebook); }); }); }); });