Re-create Subscriber model with sequelize-cli to generate migration file, fix eslint errors, disable htmllint for now
This commit is contained in:
Родитель
d6d7ff79b0
Коммит
d78180c3e3
|
@ -0,0 +1,30 @@
|
|||
'use strict';
|
||||
module.exports = {
|
||||
up: (queryInterface, Sequelize) => {
|
||||
return queryInterface.createTable('Subscribers', {
|
||||
id: {
|
||||
allowNull: false,
|
||||
autoIncrement: true,
|
||||
primaryKey: true,
|
||||
type: Sequelize.INTEGER
|
||||
},
|
||||
email: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
verificationToken: {
|
||||
type: Sequelize.STRING
|
||||
},
|
||||
createdAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE
|
||||
},
|
||||
updatedAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE
|
||||
}
|
||||
});
|
||||
},
|
||||
down: (queryInterface, Sequelize) => {
|
||||
return queryInterface.dropTable('Subscribers');
|
||||
}
|
||||
};
|
|
@ -1,10 +1,13 @@
|
|||
'use strict';
|
||||
"use strict";
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
var EmailHash = sequelize.define('EmailHash', {
|
||||
sha1: DataTypes.STRING
|
||||
const EmailHash = sequelize.define("EmailHash", {
|
||||
sha1: DataTypes.STRING,
|
||||
}, {});
|
||||
|
||||
EmailHash.associate = function(models) {
|
||||
EmailHash.belongsToMany(models.Breach, { through: "BreachedHashes" });
|
||||
};
|
||||
|
||||
return EmailHash;
|
||||
};
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
"use strict";
|
||||
|
||||
const crypto = require("crypto");
|
||||
|
||||
module.exports = (sequelize, DataTypes) => {
|
||||
|
||||
const Subscriber = sequelize.define("Subscriber", {
|
||||
email: {
|
||||
type: DataTypes.STRING,
|
||||
validate: { isEmail: true },
|
||||
},
|
||||
verificationToken: {
|
||||
type: DataTypes.STRING,
|
||||
defaultValue: function () {
|
||||
return crypto.randomBytes(40).toString("hex");
|
||||
},
|
||||
},
|
||||
}, {});
|
||||
|
||||
Subscriber.associate = function(models) {
|
||||
Subscriber.hasOne(models.EmailHash);
|
||||
};
|
||||
|
||||
Subscriber.prototype.saveSha1 = async function(models) {
|
||||
const sha1 = crypto.createHash("sha1").update(this.email).digest("hex");
|
||||
const emailHash = await models.EmailHash.findOrCreate( { where: { sha1 }});
|
||||
await this.setEmailHash(emailHash);
|
||||
};
|
||||
|
||||
return Subscriber;
|
||||
};
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -18,7 +18,7 @@
|
|||
"nodemailer": "^4.6.0",
|
||||
"pg": "^7.4.1",
|
||||
"popsicle": "^9.2.0",
|
||||
"request": "^2.83.0",
|
||||
"request": "^2.85.0",
|
||||
"sequelize": "^4.35.2",
|
||||
"sequelize-cli": "^4.0.0"
|
||||
},
|
||||
|
@ -49,7 +49,6 @@
|
|||
},
|
||||
"scripts": {
|
||||
"lint": "npm-run-all lint:*",
|
||||
"lint:html": "htmllint 'views/*.hbs'",
|
||||
"lint:js": "eslint .",
|
||||
"lint:nsp": "nsp check",
|
||||
"pretest": "npm run lint",
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
"use strict";
|
||||
|
||||
const AppConstants = require("../app-constants");
|
||||
|
||||
const crypto = require("crypto");
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ app.use(bodyParser.json());
|
|||
app.use(express.static("public"));
|
||||
|
||||
app.engine("hbs", hbs.express4({
|
||||
layoutsDir: __dirname + "/views/layouts"
|
||||
layoutsDir: __dirname + "/views/layouts",
|
||||
}));
|
||||
app.set("view engine", "hbs");
|
||||
app.set("views", __dirname + "/views");
|
||||
|
|
|
@ -34,6 +34,7 @@ models.sequelize.sync().then(async () => {
|
|||
const foundBreaches = (await emailHash.getBreaches()).map(aBreach => aBreach.dataValues.name);
|
||||
console.log(`\n\n${testEmail} was found in the following breaches:\n`);
|
||||
console.log(foundBreaches);
|
||||
// eslint-disable-next-line no-process-exit
|
||||
process.exit();
|
||||
});
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ const INVALID_EMAIL = "asdfghjkl";
|
|||
|
||||
test("Test building user autogenerates verification token", t => {
|
||||
t.plan(2);
|
||||
const user = models.User.build({ email: VALID_EMAIL });
|
||||
const user = models.Subscriber.build({ email: VALID_EMAIL });
|
||||
t.notEqual(user.verificationToken, null, "verificationToken is not null");
|
||||
t.equal(user.verificationToken.length, 80, "verificationToken is expected length");
|
||||
});
|
||||
|
@ -17,15 +17,15 @@ test("Test building user autogenerates verification token", t => {
|
|||
test("Test building user validates email", t => {
|
||||
t.plan(2);
|
||||
|
||||
let user = models.User.build({ email: VALID_EMAIL });
|
||||
let user = models.Subscriber.build({ email: VALID_EMAIL });
|
||||
user.validate().then(user=>{
|
||||
t.equal(user.email, VALID_EMAIL, "valid email is valid");
|
||||
});
|
||||
|
||||
user = models.User.build({ email: INVALID_EMAIL });
|
||||
user = models.Subscriber.build({ email: INVALID_EMAIL });
|
||||
user.validate().then(user=>{
|
||||
t.fail("invalid email should have rejected");
|
||||
}).catch(models.User.sequelize.ValidationError, err => {
|
||||
}).catch(models.Subscriber.sequelize.ValidationError, err => {
|
||||
t.pass("invalid email was rejected");
|
||||
});
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче