All files / strategies/local/db/rethinkdbdash verify.spec.js

100% Statements 49/49
100% Branches 6/6
100% Functions 19/19
100% Lines 48/48
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 1071x 1x   1x 1x   1x   1x   1x   1x 1x 1x   1x 1x   1x 3x 1x   2x     12x                         1x     1x 1x 1x   1x   1x   1x 1x       1x   1x 1x 1x   1x         1x   1x 1x 1x         1x   1x 1x 1x         1x   1x 1x 1x 1x   1x         1x   1x 1x 1x            
import chai from 'chai';
import 'regenerator-runtime/runtime';
 
import RethinkDBHashDriver from '../../../../db/rethinkdbdash';
import r, { freshUserTable } from '../../../../db/rethinkdbdash-instance';
 
import _verify from './verify';
 
const should = chai.should();
 
describe('Strategies - local - db - rethinkdbdash - verify', () => {
 
  const context = {};
  const verify = _verify.bind(context);
  const db = context.db = new RethinkDBHashDriver(r, "users");
 
  const error = new Error('test error');
  context.error = null;
 
  context.comparePassword = function(pass1, pass2, cb) {
    if (context.error)
      cb(context.error);
    else
      cb(null, pass1 === pass2);
  };
 
  before(async () => {
    await freshUserTable();
    await db.users.insert([
      {
        id: "user1",
        emails: [
          { address: "test@test.com" }
        ],
        password: "test123"
      }
    ]).run();
  });
 
  it('should catch rethinkdb errors and pass to callback', (done) => {
 
    // Super simple API that satisfied the query in verify.js but rejects.
    context.db = {
      filter: () => context.db,
      limit: () => context.db,
      r: {
        row: () => context.db
      },
      contains: () => context.db.users,
      run() {
        return new Promise((resolve, reject) => {
          reject(error);
        });
      }
    };
    context.db.users = context.db;
 
    verify("foo", "bar", (err, user) => {
      err.should.equal(error);
      should.equal(user, undefined);
      context.db = db; // reset for next test
      done();
    });
 
  });
 
  it('should fail on no matching email', (done) => {
 
    verify("non-existing-email", "test123", (err, user) => {
      should.equal(user, false /*, "Invalid email" */);
      done();
    });
 
  });
 
  it('should fail on no matching password', (done) => {
 
    verify("test@test.com", "non-matching-password", (err, user) => {
      should.equal(user, false /*, "Invalid password" */);
      done();
    });
 
  });
 
  it('should call cb(err) on a thrown error', (done) => {
 
    context.error = error;
    verify("test@test.com", "test123", (err, user) => {
      err.should.equal(error);
      should.equal(user, undefined);
      context.error = null; // reset for next test
      done();
    });
 
  });
 
  it('return a matching user with verified password', (done) => {
 
    verify("test@test.com", "test123", (err, user) => {
      user.id.should.equal("user1");
      done();
    });
 
  });
 
});