From d7ed1196bdd44d7734d41d91a5e72a71785e0a08 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Thu, 5 Jan 2023 02:09:07 +0100 Subject: switched to single quotes string in eslint --- .eslintrc.json | 2 +- controllers/games/gameId.js | 8 ++++---- controllers/games/root.js | 22 +++++++++++----------- controllers/root/login.js | 22 +++++++++++----------- controllers/root/logout.js | 4 ++-- controllers/root/root.js | 4 ++-- database/db.js | 18 +++++++++--------- database/games.js | 8 ++++---- database/users.js | 10 +++++----- main.js | 22 +++++++++++----------- middlewares/formParser.js | 2 +- middlewares/helmet.js | 4 ++-- middlewares/requireAuth.js | 2 +- middlewares/sessions.js | 20 ++++++++++---------- routes/games.js | 20 ++++++++++---------- routes/root.js | 22 +++++++++++----------- tests/games.spec.js | 30 +++++++++++++++--------------- tests/root.spec.js | 44 ++++++++++++++++++++++---------------------- utils/board.js | 30 +++++++++++++++--------------- utils/checks.js | 14 +++++++------- 20 files changed, 154 insertions(+), 154 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 8be50d1..4550533 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -25,7 +25,7 @@ "node/no-unpublished-import": ["error", {"allowModules": ["vitest", "supertest"]}], "quotes": [ "error", - "double" + "single" ], "semi": [ "error", diff --git a/controllers/games/gameId.js b/controllers/games/gameId.js index d3363fd..9487743 100644 --- a/controllers/games/gameId.js +++ b/controllers/games/gameId.js @@ -1,8 +1,8 @@ -import { getGame } from "../../database/games.js"; +import { getGame } from '../../database/games.js'; function makePageData(user, game) { return { - title: "Jouer", + title: 'Jouer', user: user, data: game, }; @@ -18,7 +18,7 @@ export function gameId_get(req, res) { game.letters = game.player2.letters; delete game.player1.letters; } - return res.render("game", makePageData(req.session.user, game)); + return res.render('game', makePageData(req.session.user, game)); } - return res.redirect("/games"); + return res.redirect('/games'); } diff --git a/controllers/games/root.js b/controllers/games/root.js index 4a4e80d..8b6fc22 100644 --- a/controllers/games/root.js +++ b/controllers/games/root.js @@ -1,17 +1,17 @@ -import { validationResult } from "express-validator"; +import { validationResult } from 'express-validator'; -import { getUserByUsername } from "../../database/users.js"; -import { listGames, newGame } from "../../database/games.js"; -import { emptyBoard, Bag } from "../../utils/board.js"; +import { getUserByUsername } from '../../database/users.js'; +import { listGames, newGame } from '../../database/games.js'; +import { emptyBoard, Bag } from '../../utils/board.js'; function makePageData(user) { return { - title: "Parties", + title: 'Parties', user: user, games: listGames(user.id), formdata: { - name: "", - username: "", + name: '', + username: '', }, errors: {}, }; @@ -20,7 +20,7 @@ function makePageData(user) { export function root_get(req, res) { let page = makePageData(req.session.user); page.games.forEach(g => g.data = JSON.parse(g.data)); - return res.render("games", page); + return res.render('games', page); } function makeNewGameData(name, player1, player2) { @@ -54,11 +54,11 @@ export function root_post(req, res) { if (gameId) { return res.redirect(302, `/games/${gameId}`); } else { - page.errors.mismatch = "Erreur du serveur: la création de partie a échoué"; + page.errors.mismatch = 'Erreur du serveur: la création de partie a échoué'; } } else { - page.errors.username = { msg: "L'identifiant n'existe pas." }; + page.errors.username = { msg: 'L\'identifiant n\'existe pas.' }; } } - return res.render("games", page); + return res.render('games', page); } diff --git a/controllers/root/login.js b/controllers/root/login.js index ac6a471..431f7f9 100644 --- a/controllers/root/login.js +++ b/controllers/root/login.js @@ -1,14 +1,14 @@ -import { validationResult } from "express-validator"; +import { validationResult } from 'express-validator'; -import { login } from "../../database/users.js"; +import { login } from '../../database/users.js'; function makePageData(user) { return { - title: "Connexion", + title: 'Connexion', user: user, data: { - username: "", - password: "", + username: '', + password: '', }, errors: {}, }; @@ -16,14 +16,14 @@ function makePageData(user) { export function login_get(req, res) { if (req.session.user !== undefined) { - return res.redirect(302, "/games"); + return res.redirect(302, '/games'); } - return res.render("login", makePageData(req.session.user)); + return res.render('login', makePageData(req.session.user)); } export async function login_post(req, res) { if (req.session.user !== undefined) { - return res.redirect(302, "/games"); + return res.redirect(302, '/games'); } let page = makePageData(req.session.user); page.data = req.body; @@ -33,11 +33,11 @@ export async function login_post(req, res) { if (user !== null) { req.session.user = user; } else { - page.errors.mismatch = "L'identifiant et le mot de passe ne correspondent pas, ou l'identifiant n'existe pas."; + page.errors.mismatch = 'L\'identifiant et le mot de passe ne correspondent pas, ou l\'identifiant n\'existe pas.'; } } if (Object.keys(page.errors).length === 0) { - return res.redirect(302, "/games"); + return res.redirect(302, '/games'); } - return res.render("login", page); + return res.render('login', page); } diff --git a/controllers/root/logout.js b/controllers/root/logout.js index 078df9a..d8b1b75 100644 --- a/controllers/root/logout.js +++ b/controllers/root/logout.js @@ -1,7 +1,7 @@ export function logout_get(req, res) { if (req.session.user !== undefined) { - res.clearCookie("JDMSessionId"); + res.clearCookie('JDMSessionId'); req.session.destroy(); } - return res.redirect(302, "/"); + return res.redirect(302, '/'); } diff --git a/controllers/root/root.js b/controllers/root/root.js index 062edf4..6349983 100644 --- a/controllers/root/root.js +++ b/controllers/root/root.js @@ -1,6 +1,6 @@ export function root_get(req, res) { if (req.session.user !== undefined) { - return res.redirect(302, "/games"); + return res.redirect(302, '/games'); } - return res.redirect(302, "/login"); + return res.redirect(302, '/login'); } diff --git a/database/db.js b/database/db.js index 1e8b50d..1d8a3f0 100644 --- a/database/db.js +++ b/database/db.js @@ -1,27 +1,27 @@ -import fs from "fs"; -import Database from "better-sqlite3"; +import fs from 'fs'; +import Database from 'better-sqlite3'; const allMigrations = [ - "database/000_init.sql", - "database/001_games.sql", + 'database/000_init.sql', + 'database/001_games.sql', ]; const db = new Database( - process.env.NODE_ENV === "test" ? "testjdm.db" : "jdm.db", - process.env.NODE_ENV === "development" ? { verbose: console.log } : null + process.env.NODE_ENV === 'test' ? 'testjdm.db' : 'jdm.db', + process.env.NODE_ENV === 'development' ? { verbose: console.log } : null ); -db.pragma("foreign_keys = ON"); +db.pragma('foreign_keys = ON'); db.transaction(function migrate() { let version; try { - version = db.prepare("SELECT version FROM schema_version").get().version; + version = db.prepare('SELECT version FROM schema_version').get().version; } catch { version = 0; } if (version === allMigrations.length) return; while (version < allMigrations.length) { - db.exec(fs.readFileSync(allMigrations[version], "utf8")); + db.exec(fs.readFileSync(allMigrations[version], 'utf8')); version++; } db.exec(`DELETE FROM schema_version; INSERT INTO schema_version (version) VALUES (${version});`); diff --git a/database/games.js b/database/games.js index 3517d0a..ac9a4ad 100644 --- a/database/games.js +++ b/database/games.js @@ -1,8 +1,8 @@ -import db from "./db.js"; +import db from './db.js'; -const getGameStatement = db.prepare("SELECT * from games where id = ?;"); -const listGamesStatement = db.prepare("SELECT * from games where player1 = ?1 OR player2 = ?1 ORDER BY last_move_at;"); -const newGameStatement = db.prepare("INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);"); +const getGameStatement = db.prepare('SELECT * from games where id = ?;'); +const listGamesStatement = db.prepare('SELECT * from games where player1 = ?1 OR player2 = ?1 ORDER BY last_move_at;'); +const newGameStatement = db.prepare('INSERT INTO games (player1, player2, data) VALUES (?, ?, ?);'); export function getGame(id) { try { diff --git a/database/users.js b/database/users.js index b24e3b4..cc50bdc 100644 --- a/database/users.js +++ b/database/users.js @@ -1,12 +1,12 @@ -import bcrypt from "bcrypt"; +import bcrypt from 'bcrypt'; -import db from "./db.js"; +import db from './db.js'; const saltRounds = 10; -const createUserStatement = db.prepare("INSERT INTO users (username, hash, email) VALUES (?, ?, ?);"); -const getUserByUsernameStatement = db.prepare("SELECT id, username, email from users WHERE username = ?;"); -const loginStatement = db.prepare("SELECT id, username, hash, email FROM users WHERE username = ?;"); +const createUserStatement = db.prepare('INSERT INTO users (username, hash, email) VALUES (?, ?, ?);'); +const getUserByUsernameStatement = db.prepare('SELECT id, username, email from users WHERE username = ?;'); +const loginStatement = db.prepare('SELECT id, username, hash, email FROM users WHERE username = ?;'); export async function createUser(username, password, email) { const hash = await bcrypt.hash(password, saltRounds); diff --git a/main.js b/main.js index 391a4fe..0b04edd 100644 --- a/main.js +++ b/main.js @@ -1,21 +1,21 @@ -import express from "express"; +import express from 'express'; -import helmet from "./middlewares/helmet.js"; -import gamesRouter from "./routes/games.js"; -import rootRouter from "./routes/root.js"; +import helmet from './middlewares/helmet.js'; +import gamesRouter from './routes/games.js'; +import rootRouter from './routes/root.js'; const app = express(); -app.set("trust proxy", 1); +app.set('trust proxy', 1); app.use(helmet); -app.set("views", "./views"); -app.set("view engine", "ejs"); +app.set('views', './views'); +app.set('view engine', 'ejs'); -app.use("/games", gamesRouter); -app.use("/static", express.static("static")); -app.use("/", rootRouter); +app.use('/games', gamesRouter); +app.use('/static', express.static('static')); +app.use('/', rootRouter); -if (process.env.NODE_ENV !== "test") { +if (process.env.NODE_ENV !== 'test') { const port = process.env.PORT || 3000; app.listen(port, () => console.log(`listening on port ${port}`)); } diff --git a/middlewares/formParser.js b/middlewares/formParser.js index 41134ac..b117045 100644 --- a/middlewares/formParser.js +++ b/middlewares/formParser.js @@ -1,3 +1,3 @@ -import bodyParser from "body-parser"; +import bodyParser from 'body-parser'; export default bodyParser.urlencoded({ extended: false }); diff --git a/middlewares/helmet.js b/middlewares/helmet.js index 5ced2c3..67d6e98 100644 --- a/middlewares/helmet.js +++ b/middlewares/helmet.js @@ -1,10 +1,10 @@ -import helmet from "helmet"; +import helmet from 'helmet'; const myHelmet = helmet({ contentSecurityPolicy: { directives: { ...helmet.contentSecurityPolicy.getDefaultDirectives(), - "script-src": ["'self'", "'unsafe-inline'"], + 'script-src': ['\'self\'', '\'unsafe-inline\''], }, }, }); diff --git a/middlewares/requireAuth.js b/middlewares/requireAuth.js index 1ea6deb..4d85e43 100644 --- a/middlewares/requireAuth.js +++ b/middlewares/requireAuth.js @@ -2,7 +2,7 @@ function requireAuth(req, res, next) { if (req.session.user !== undefined) { return next(); } - return res.redirect(302, "/login"); + return res.redirect(302, '/login'); } export default requireAuth; diff --git a/middlewares/sessions.js b/middlewares/sessions.js index 901c262..30f5c49 100644 --- a/middlewares/sessions.js +++ b/middlewares/sessions.js @@ -1,21 +1,21 @@ -import expressSession from "express-session"; -import Database from "better-sqlite3"; -import sqliteStore from "better-sqlite3-session-store"; +import expressSession from 'express-session'; +import Database from 'better-sqlite3'; +import sqliteStore from 'better-sqlite3-session-store'; const SqliteStore = sqliteStore(expressSession); const db = new Database( - process.env.NODE_ENV === "test" ? "testsessions.db" : "sessions.db", - process.env.NODE_ENV === "development" ? { verbose: console.log } : null + process.env.NODE_ENV === 'test' ? 'testsessions.db' : 'sessions.db', + process.env.NODE_ENV === 'development' ? { verbose: console.log } : null ); -const secret = process.env.SESSION_SECRET || "secret"; +const secret = process.env.SESSION_SECRET || 'secret'; const session = expressSession({ cookie: { httpOnly: true, maxAge: 1000 * 60 * 60 * 24 * 15, // 15 days - sameSite: "Strict", - secure: process.env.NODE_ENV === "production" ? true : false, + sameSite: 'Strict', + secure: process.env.NODE_ENV === 'production' ? true : false, }, - name: "JDMSessionId", + name: 'JDMSessionId', saveUninitialized: false, secret: secret, store: new SqliteStore({ @@ -26,7 +26,7 @@ const session = expressSession({ } }), resave: false, - unset: "destroy", + unset: 'destroy', }); export default session; diff --git a/routes/games.js b/routes/games.js index 3a27cdb..5c4022c 100644 --- a/routes/games.js +++ b/routes/games.js @@ -1,18 +1,18 @@ -import express from "express"; +import express from 'express'; -import { gameId_get } from "../controllers/games/gameId.js"; -import { root_get, root_post } from "../controllers/games/root.js"; -import bodyParser from "../middlewares/formParser.js"; -import requireAuth from "../middlewares/requireAuth.js"; -import session from "../middlewares/sessions.js"; -import { checkName, checkUsername } from "../utils/checks.js"; +import { gameId_get } from '../controllers/games/gameId.js'; +import { root_get, root_post } from '../controllers/games/root.js'; +import bodyParser from '../middlewares/formParser.js'; +import requireAuth from '../middlewares/requireAuth.js'; +import session from '../middlewares/sessions.js'; +import { checkName, checkUsername } from '../utils/checks.js'; const router = express.Router(); router.use(session); router.use(requireAuth); -router.get("/", root_get); -router.post("/", [bodyParser, checkName, checkUsername], root_post); -router.get("/:gameId(\\d+)", gameId_get); +router.get('/', root_get); +router.post('/', [bodyParser, checkName, checkUsername], root_post); +router.get('/:gameId(\\d+)', gameId_get); export default router; diff --git a/routes/root.js b/routes/root.js index 863b99d..ee63748 100644 --- a/routes/root.js +++ b/routes/root.js @@ -1,18 +1,18 @@ -import express from "express"; +import express from 'express'; -import { login_get, login_post } from "../controllers/root/login.js"; -import { logout_get } from "../controllers/root/logout.js"; -import { root_get } from "../controllers/root/root.js"; -import bodyParser from "../middlewares/formParser.js"; -import session from "../middlewares/sessions.js"; -import { checkUsername, checkPassword } from "../utils/checks.js"; +import { login_get, login_post } from '../controllers/root/login.js'; +import { logout_get } from '../controllers/root/logout.js'; +import { root_get } from '../controllers/root/root.js'; +import bodyParser from '../middlewares/formParser.js'; +import session from '../middlewares/sessions.js'; +import { checkUsername, checkPassword } from '../utils/checks.js'; const router = express.Router(); router.use(session); -router.get("/", root_get); -router.get("/login", login_get); -router.post("/login", [bodyParser, checkUsername, checkPassword], login_post); -router.get("/logout", logout_get); +router.get('/', root_get); +router.get('/login', login_get); +router.post('/login', [bodyParser, checkUsername, checkPassword], login_post); +router.get('/logout', logout_get); export default router; diff --git a/tests/games.spec.js b/tests/games.spec.js index 00ae1e7..1d5333c 100644 --- a/tests/games.spec.js +++ b/tests/games.spec.js @@ -1,23 +1,23 @@ -import { beforeEach, describe, test } from "vitest"; -import supertest from "supertest"; +import { beforeEach, describe, test } from 'vitest'; +import supertest from 'supertest'; -import app from "../main.js"; +import app from '../main.js'; -describe.concurrent("Games handlers tests", function() { - describe.concurrent("When not logged in", function() { - test("GET /games", async function() { await supertest(app).get("/games").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); - test("GET /games/1", async function() { await supertest(app).get("/games").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); +describe.concurrent('Games handlers tests', function() { + describe.concurrent('When not logged in', function() { + test('GET /games', async function() { await supertest(app).get('/games').expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/login$/); }); + test('GET /games/1', async function() { await supertest(app).get('/games').expect('Content-Type', /text\/plain/).expect(302, /Redirecting to \/login$/); }); }); - describe.concurrent("With valid credentials", function() { + describe.concurrent('With valid credentials', function() { beforeEach(async function(ctx) { - const authResponse = await supertest(app).post("/login") - .send("username=Alice&password=Alice42!") - .expect("Content-Type", /text\/plain/) - .expect("set-cookie", /JDMSessionId=/) + const authResponse = await supertest(app).post('/login') + .send('username=Alice&password=Alice42!') + .expect('Content-Type', /text\/plain/) + .expect('set-cookie', /JDMSessionId=/) .expect(302, /Redirecting to \/games$/); - ctx.cookie = authResponse.get("Set-Cookie"); + ctx.cookie = authResponse.get('Set-Cookie'); }); - test("GET /games", async function(ctx) { await supertest(app).get("/games").set("Cookie", ctx.cookie).expect("Content-Type", /text\/html/).expect(200, /