File size: 4,138 Bytes
c02f046 | 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | import BPromise from "bluebird";
import chai, {expect} from "chai";
import sinon from "sinon";
import sinonChai from "sinon-chai";
import chaiAsPromised from "chai-as-promised";
import {RpcError} from "lambda-rpc";
chai.use(chaiAsPromised);
chai.use(sinonChai);
import * as createUser from "methods/create-user";
describe("`validateParams`", function () {
it("throws a 400 `RpcError` if the options body is malformed", function () {
var troublemaker = function () {
createUser.validateParams({
email: "Not an email",
password: "password",
});
};
expect(troublemaker).to.throw(RpcError);
expect(troublemaker).to.throw("Bad request parameters");
});
it("doesn't throws if the option body is correct", function () {
var peacemaker = function () {
createUser.validateParams({
email: "test@email.com",
password: "password",
});
};
expect(peacemaker).not.to.throw(RpcError);
});
});
describe("`ensureEmailUniqueness`", function () {
var mongodb = {};
before(function () {
createUser.__Rewire__("mongodb", mongodb);
});
after(function () {
createUser.__ResetDependency__("mongodb");
});
it("throws a 401 `RpcError` if the email already exist in db", function () {
mongodb.findOne = sinon.stub().returns(BPromise.resolve("Not null"));
var promise = createUser.ensureEmailUniqueness("test@email.com");
return expect(promise).to.be.rejectedWith("User already registered");
});
it("doesn't throws if the email not exist in db", function () {
mongodb.findOne = sinon.stub().returns(BPromise.resolve(null));
var promise = createUser.ensureEmailUniqueness("test@email.com");
return expect(promise).not.be.rejectedWith("User already registered");
});
});
describe("`putNewUserInKinesis`", function () {
var id = "110ec58a-a0f2-4ac4-8393-c866d813b8d1";
var date = 1443694430074;
var hash = "$2a$12$JbHoP.KA/Fw1sJOlHVQSq.Rc8i5E/O9JOFfZ6gxvyzNCQO8y79iUy";
var kinesis = {
putRecord: sinon.stub().returns(BPromise.resolve({}))
};
var generateUuid = sinon.stub().returns(id);
var hash = sinon.stub().returns(hash);
beforeEach(function () {
kinesis.putRecord.reset();
createUser.__Rewire__("kinesis", kinesis);
createUser.__Rewire__("generateUuid", generateUuid);
createUser.__Rewire__("hash", hash);
process.env.KINESIS_STREAM_NAME = "stream";
sinon.stub(Date, "now").returns(date);
});
afterEach(function () {
createUser.__ResetDependency__("kinesis");
createUser.__ResetDependency__("hash");
createUser.__ResetDependency__("generateUuid");
Date.now.restore();
});
it("should be called with the correct object", function () {
var options = {
email: "test@email.com",
password: "password"
};
var element = {
emails: [{
address: options.email,
verified: false
}],
profile: options.profile || {},
services: {
email: {
verificationTokens: [{
address: options.email,
token: generateUuid()
}]
},
password: {
bcrypt: hash(options.password)
},
resume: {
loginTokens: []
}
}
};
var expectedObject = {
Data: JSON.stringify({
id: id,
data: {element, id},
timestamp: date,
type: "element inserted in collection users"
}),
PartitionKey: "users",
StreamName: process.env.KINESIS_STREAM_NAME
};
createUser.putNewUserInKinesis(options);
expect(kinesis.putRecord).to.have.been.calledWith(expectedObject);
});
});
|