Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@codefresh-io/authenticated-entity

Package Overview
Dependencies
Maintainers
14
Versions
62
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@codefresh-io/authenticated-entity - npm Package Compare versions

Comparing version 1.0.12 to 1.0.13

171

lib/tests/User.unit.spec.js

@@ -6,9 +6,8 @@ 'use strict';

const _ = require('lodash');
const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const expect = chai.expect;
const User = require('./../User');
const _ = require('lodash');
const chai = require('chai');
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
const expect = chai.expect;
const User = require('./../User');
chai.use(sinonChai);

@@ -18,2 +17,4 @@

const encodedFakeUser = 'eyJuYW1lIjoiZmFrZS11c2VyTmFtZSIsIl9pZCI6ImZha2UtdXNlci1pZC0xIiwiYWNjb3VudHMiOlt7Im5hbWUiOiJmYWtlLWFjY291bnQtbmFtZS0xIiwiX2lkIjoiZmFrZS1hY2NvdW50SWQtMSIsImlzQWRtaW4iOnRydWV9LHsibmFtZSI6ImZha2UtYWNjb3VudC1uYW1lLTIiLCJfaWQiOiJmYWtlLWFjY291bnRJZC0yIiwiaXNBZG1pbiI6ZmFsc2V9XX0=';
function fakeUserData() {

@@ -55,61 +56,123 @@ return {

it('Should construct a User', () => {
const user = new User(fakeUserData());
expect(user).to.be.an.instanceOf(User);
});
describe('From user model', () => {
it('Should throw error while constructing non user data', () => {
const fake = {
key: 'val'
};
try {
new User(fake);
throw new Error('Should not throw this');
} catch (err) {
expect(err.message).to.be.deep.equal('Could not construct User entity with given data');
}
});
it('Should construct a User', () => {
const user = User.constructFromUserEntity(fakeUserData());
expect(user).to.be.an.instanceOf(User);
});
it('Should get user object', () => {
const user = new User(fakeUserData());
expect(user.toJson()).to.be.deep.equal(jsonValidUser);
});
it('Should throw error while constructing non user data', () => {
const fake = {
key: 'val'
};
try {
User.constructFromUserEntity(fake);
throw new Error('Should not throw this');
} catch (err) {
expect(err.message)
.to
.be
.deep
.equal('Could not construct User entity with given data');
}
});
it('Should get only the minimum set of properties', () => {
const user = new User(_.merge(fakeUserData(), { key: 'val' }));
expect(user.toJson()).to.be.deep.equal(jsonValidUser);
});
it('Should get account by the name', () => {
const user = new User(fakeUserData());
expect(user.getAccountByName('fake-account-name-1')).to.be.deep.equal({
"_id": "fake-accountId-1",
"isAdmin": true,
"name": "fake-account-name-1"
describe('From encoded string', () => {
it('Should convert encoded string into user object', () => {
expect(User.constructFromEncodedString(encodedFakeUser).toJson())
.to
.be
.deep
.equal(jsonValidUser);
});
});
it('Should return undefined when account not found', () => {
const user = new User(fakeUserData());
expect(user.getAccountByName('account-not-found')).to.be.undefined;
});
it('Should throw an error if the user is not string', () => {
try{
User.constructFromEncodedString({})
} catch(err){
expect(err.message).to.be.deep.equal('User.constructFromEncodedString: failed. Given parameter is not string');
}
});
it('Should return true when user is admin of an accout', () => {
const user = new User(fakeUserData());
expect(user.isAccountAdmin('fake-account-name-1')).to.be.true;
it('Should throw an error if the encoded string is not valid user json', () => {
try{
User.constructFromEncodedString('jndsaajkdsa');
} catch(err){
expect(err.message).to.be.deep.equal('User.constructFromEncodedString: falid. Given parameter is not in JSON format');
}
});
});
it('Should return false when user is not admin of an accout', () => {
const user = new User(fakeUserData());
expect(user.isAccountAdmin('fake-account-name-2')).to.be.false;
describe('Operations on the entity', () => {
it('Should get user object', () => {
const user = User.constructFromUserEntity(fakeUserData());
expect(user.toJson()).to.be.deep.equal(jsonValidUser);
});
it('Should get only the minimum set of properties', () => {
const user = User.constructFromUserEntity(_.merge(fakeUserData(), { key: 'val' }));
expect(user.toJson()).to.be.deep.equal(jsonValidUser);
});
it('Should get account by the name', () => {
const user = User.constructFromEncodedString(encodedFakeUser);
expect(user.getAccountByName('fake-account-name-1')).to.be.deep.equal({
"_id": "fake-accountId-1",
"isAdmin": true,
"name": "fake-account-name-1"
});
});
it('Should return undefined when account not found', () => {
const user = User.constructFromUserEntity(fakeUserData());
expect(user.getAccountByName('account-not-found')).to.be.undefined;
});
it('Should return true when user is admin of an accout', () => {
const user = User.constructFromUserEntity(fakeUserData());
expect(user.isAccountAdmin('fake-account-name-1')).to.be.true;
});
it('Should return false when user is not admin of an accout', () => {
const user = User.constructFromUserEntity(fakeUserData());
expect(user.isAccountAdmin('fake-account-name-2')).to.be.false;
});
it('Should throw an error when account not found', () => {
const user = User.constructFromUserEntity(fakeUserData());
try {
user.isAccountAdmin('fake-account-2')
} catch (err) {
expect(err.message).to.be.deep.equal('Account not found');
}
});
it('Should stringify user', () => {
expect((User.constructFromUserEntity(fakeUserData())).toString())
.to
.be
.deep
.equal(`{"name":"fake-userName","_id":"fake-user-id-1","accounts":[{"name":"fake-account-name-1","_id":"fake-accountId-1","isAdmin":true},{"name":"fake-account-name-2","_id":"fake-accountId-2","isAdmin":false}]}`)
});
it('Should encode user to base64', () => {
expect((User.constructFromUserEntity(fakeUserData())).encodeToBase64String())
.to
.be
.deep
.equal(encodedFakeUser)
});
});
it('Should throw an error when account not found', () => {
const user = new User(fakeUserData());
try {
user.isAccountAdmin('fake-account-2')
} catch (err) {
expect(err.message).to.be.deep.equal('Account not found');
}
});
});

@@ -10,2 +10,3 @@ 'use stirct';

];
/**

@@ -17,5 +18,30 @@ * Basic user should have

*/
class User {
constructor(user) {
/**
* Use this static method to create new User entity encoded string
* @param encoded String
* @return {User}
*/
static constructFromEncodedString(encoded) {
if (!_.isString(encoded)) {
throw new Error('User.constructFromEncodedString: failed. Given parameter is not string');
}
const jsonStr = Buffer.from(encoded, 'base64').toString();
try {
return new User(JSON.parse(jsonStr));
} catch (err) {
throw new Error("User.constructFromEncodedString: falid. Given parameter is not in JSON format");
}
}
/**
* Use this static method to create new User entity from given user model
* @param user
* @return {User}
*/
static constructFromUserEntity(user){
const isValid = _.pick(user, requiredParams);

@@ -26,3 +52,3 @@ if (_.chain(isValid).keys().size().value() !== _.chain(requiredParams).size().value()) {

const userId = user._id.toString();
this._user = {
const data = {
name: user.userName,

@@ -39,5 +65,14 @@ _id: userId,

).value()
}
};
return new User(data);
}
/**
* Should not be used directly
* @param data
*/
constructor(data) {
this._user = data;
}
toJson() {

@@ -47,2 +82,10 @@ return this._user;

toString() {
return JSON.stringify(this._user);
}
encodeToBase64String() {
return new Buffer(this.toString()).toString('base64');
}
getAccountByName(name) {

@@ -64,2 +107,4 @@ return _.chain(this._user)

}
}

@@ -66,0 +111,0 @@

{
"name": "@codefresh-io/authenticated-entity",
"version": "1.0.12",
"version": "1.0.13",
"description": "",

@@ -5,0 +5,0 @@ "main": "index.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc