Skip to content

Commit

Permalink
passport spec
Browse files Browse the repository at this point in the history
  • Loading branch information
iambochen committed Aug 20, 2018
1 parent dedf06a commit 3093bcd
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 21 deletions.
4 changes: 2 additions & 2 deletions models/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ module.exports = (sequelize, DataTypes) => {

Passport.associate = function (models) {
Passport.belongsTo(models.User);
},
};

Passport.providerCode = function () {
return new Enum({ 'SLACK': 0, 'FIREBASE': 1, 'LOCAL': 2,});
return new Enum({ SLACK: 0, FIREBASE: 1, LOCAL: 2 });
};
return Passport;
};
95 changes: 76 additions & 19 deletions test/unit/models/passport.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,49 @@ const fakeData = {
create: {
token: faker.name.findName(),
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
update: {
token: faker.name.findName(),
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
updateNewData: {
token: faker.name.findName(),
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
updateNewData2: {
token: faker.name.findName(),
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
findOne: {
token: 'faker.name.findName()',
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
findAll: [
{
token: 'faker.name.findName()',
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
{
token: 'faker.name.findName()',
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
{
token: 'faker.name.findName()',
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
],
destroy: {
token: faker.name.findName(),
workspaceName: faker.name.findName(),
passwordHash: faker.internet.password(),
},
keyword: {
token: 'faker.name.findName()',
Expand All @@ -54,10 +63,10 @@ describe(`models/${modelName}`, () => {

describe('Create model data', () => {
it('creates a data', async () => {
// TODO:
// TODO:
// 1. model.create(fakeData.create)...
// 2. use data from `fakeData.create`
let createdModel = null;
let createdModel = await models[modelName].create(fakeData.create);

Object.keys(fakeData.create).forEach(e => {
expect(createdModel[e]).to.equal(fakeData.create[e]);
Expand All @@ -77,23 +86,55 @@ describe(`models/${modelName}`, () => {
// 1. use model.find(modelId === updatedModel.id) then model.save();
// 2. use data from `fakeData.updateNewData`
// 3. use `findAndUpdatedModel` as target, `modelDataForUpdate.id` as condition.
let findAndUpdatedModel = null;

Object.keys(fakeData.updateNewData).forEach(e => {
expect(findAndUpdatedModel[e]).to.equal(fakeData.updateNewData[e]);
});
try {
let findAndUpdatedModel = await models[modelName].findOne({
where: {
id: modelDataForUpdate.id,
},
});

if (findAndUpdatedModel) {
findAndUpdatedModel.token = fakeData.updateNewData.token;
findAndUpdatedModel.workspaceName =
fakeData.updateNewData.workspaceName;
findAndUpdatedModel.passwordHash =
fakeData.updateNewData.passwordHash;

await findAndUpdatedModel.save();

Object.keys(fakeData.updateNewData).forEach(e => {
expect(findAndUpdatedModel[e]).to.equal(fakeData.updateNewData[e]);
});
}
} catch (error) {
console.log('find and save => ', error);
}
});

it('update a model with update method', async () => {
// TODO:
// 1. use model.update()...
// 2. use data from `fakeData.updateNewData2`
// 3. use `updateModel` as target, `modelDataForUpdate.id` as condition.
let updateModel = null;

Object.keys(fakeData.updateNewData2).forEach(e => {
expect(updateModel[e]).to.equal(fakeData.updateNewData2[e]);
});
try {
await models[modelName].update(fakeData.updateNewData2, {
where: {
id: modelDataForUpdate.id,
},
});

let updateModel = await models[modelName].findOne({
where: {
id: modelDataForUpdate.id,
},
});

Object.keys(fakeData.updateNewData2).forEach(e => {
expect(updateModel[e]).to.equal(fakeData.updateNewData2[e]);
});
} catch (error) {
console.log('update => ', error);
}
});
});

Expand All @@ -109,7 +150,11 @@ describe(`models/${modelName}`, () => {
// 1. use model.findOne...
// 2. use data from `fakeData.findOne`
// 3. use `findModel` as target, `modelDateForFind.id` as option.
let findModel = null;
let findModel = await models[modelName].findOne({
where: {
id: modelDateForFind.id,
},
});

Object.keys(fakeData.findOne).forEach(e => {
expect(findModel[e]).to.equal(fakeData.findOne[e]);
Expand All @@ -129,7 +174,7 @@ describe(`models/${modelName}`, () => {
// 1. use model.findAll...
// 2. use data from `fakeData.findAll`
// 3. use `findAllModel` as target.
let findAllModel = null;
let findAllModel = await models[modelName].findAll();

expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1);
});
Expand All @@ -139,11 +184,15 @@ describe(`models/${modelName}`, () => {
// 1. use model.findAll...
// 2. use data from `fakeData.findAll`
// 3. use `findAllModel` as target, and use `fakeData.keyword` as option.
let findAllModel = null;
let findAllModel = await models[modelName].findAll({
where: fakeData.keyword,
});

expect(findAllModel.length).to.greaterThan(fakeData.findAll.length - 1);
Object.keys(fakeData.findAll).forEach(e => {
expect(findAllModel[e]).to.equal(fakeData.findAll[e]);
fakeData.findAll.forEach(i => {
Object.keys(i).forEach(e => {
expect(findAllModel[e]).to.equal(fakeData.findAll[e]);
});
});
});
});
Expand All @@ -160,8 +209,16 @@ describe(`models/${modelName}`, () => {
// 1. use model.destroy...
// 2. use data from `fakeData.findAll`
// 3. use `findAllModel` as target, and use `modelDataForDestroy.id` as option.
let deleteModel = null;
let findDeleteModel = null;
let deleteModel = await models[modelName].destroy({
where: {
id: modelDataForDestroy.id,
},
});
let findDeleteModel = await models[modelName].findOne({
where: {
id: modelDataForDestroy.id,
},
});
expect(deleteModel).to.equal(1);
expect(findDeleteModel).to.equal(null);
});
Expand Down

0 comments on commit 3093bcd

Please sign in to comment.