ddd-tool-kit
Version:
A development tool kit for using Domain Driven Design in your Web API Node.js
89 lines (88 loc) ⢠4.13 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const password_errors_1 = require("./password.errors");
const password_value_object_1 = require("./password.value-object");
describe('user-password.value-object.spec', () => {
const password = 'FuiComprarPĆ£oNaPadariaDepoisFuiNoMercado';
it('should be defined', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
expect(valueObject).toBeInstanceOf(password_value_object_1.PasswordValueObject);
});
it('should be init password with success', () => {
const initPassword = password_value_object_1.PasswordValueObject.init({
value: password,
});
const passwordValueObject = initPassword.result;
expect(initPassword.isSuccess).toBeTruthy();
expect(passwordValueObject).toBeInstanceOf(password_value_object_1.PasswordValueObject);
expect(passwordValueObject.value).toEqual(password);
});
it('should be hash password', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
expect(valueObject.value).toEqual(password);
const hash = valueObject.hash();
expect(hash.isSuccess).toBeTruthy();
expect(valueObject.value).not.toEqual(password);
});
it('should be verify is hashed password', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
expect(valueObject.isHashed).toBeFalsy();
valueObject.hash();
expect(valueObject.isHashed).toBeTruthy();
});
it('should be fail to hash password two times', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
valueObject.hash();
const hash = valueObject.hash();
expect(hash.isFailure).toBeTruthy();
expect(hash.result).toEqual(password_errors_1.INVALID_PASSWORD_ALREADY_HASHED);
});
it('should be compare passwords', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
valueObject.hash();
const compareCorrect = valueObject.compare(password);
const compareIncorrect = valueObject.compare('password');
expect(compareCorrect.isSuccess).toBeTruthy();
expect(compareIncorrect.isFailure).toBeTruthy();
expect(compareIncorrect.result).toEqual(password_errors_1.INVALID_INCORRECT_PASSWORD);
});
it('should be fail to compare passwords', () => {
const valueObject = password_value_object_1.PasswordValueObject.init({
value: password,
}).result;
const compareCorrect = valueObject.compare(password);
expect(compareCorrect.isFailure).toBeTruthy();
expect(compareCorrect.result).toEqual(password_errors_1.INVALID_PASSWORD_IS_NOT_HASHED);
});
it('should be fail to init password without min length', () => {
const initPassword = password_value_object_1.PasswordValueObject.init({
value: 'Password',
});
expect(initPassword.isFailure).toBeTruthy();
expect(initPassword.result).toEqual(password_errors_1.INVALID_USER_PASSWORD);
});
it('should be fail to init password without uppercase char', () => {
const initPassword = password_value_object_1.PasswordValueObject.init({
value: password.toLocaleLowerCase(),
});
expect(initPassword.isFailure).toBeTruthy();
expect(initPassword.result).toEqual(password_errors_1.INVALID_USER_PASSWORD);
});
it('should be fail to init password without lowercase char', () => {
const initPassword = password_value_object_1.PasswordValueObject.init({
value: password.toUpperCase(),
});
expect(initPassword.isFailure).toBeTruthy();
expect(initPassword.result).toEqual(password_errors_1.INVALID_USER_PASSWORD);
});
});