rename signIn to login

This commit is contained in:
pikiou 2021-11-29 13:53:03 +01:00
parent e9bbbdc897
commit 976fbded67
3 changed files with 14 additions and 14 deletions

View File

@ -18,7 +18,7 @@ import certbotRouter from "../routes/certbot"
import { jeuJavListGet } from "./gsheets/jeuJav" import { jeuJavListGet } from "./gsheets/jeuJav"
import { envieListGet, envieAdd } from "./gsheets/envies" import { envieListGet, envieAdd } from "./gsheets/envies"
import { membreGet, membreSet } from "./gsheets/membres" import { membreGet, membreSet } from "./gsheets/membres"
import signInHandler from "./userManagement/signIn" import loginHandler from "./userManagement/login"
import config from "../config" import config from "../config"
const app = express() const app = express()
@ -56,7 +56,7 @@ app.post("/MembreSet", membreSet)
app.post("/EnvieAdd", envieAdd) app.post("/EnvieAdd", envieAdd)
// Sign in & up API // Sign in & up API
app.post("/api/user/login", signInHandler) app.post("/api/user/login", loginHandler)
// Use React server-side rendering middleware // Use React server-side rendering middleware
app.get("*", ssr) app.get("*", ssr)

View File

@ -2,7 +2,7 @@
* @jest-environment jsdom * @jest-environment jsdom
*/ */
import { signIn } from "../signIn" import { login } from "../login"
// Could do a full test with wget --header='Content-Type:application/json' --post-data='{"email":"pikiou.sub@gmail.com","password":"mot de passe"}' http://localhost:3000/api/user/login // Could do a full test with wget --header='Content-Type:application/json' --post-data='{"email":"pikiou.sub@gmail.com","password":"mot de passe"}' http://localhost:3000/api/user/login
@ -16,9 +16,9 @@ jest.mock("../../gsheets/accessors", () => () => ({
listGet: () => [mockUser], listGet: () => [mockUser],
})) }))
describe("signIn with", () => { describe("login with", () => {
it("right password", async () => { it("right password", async () => {
const res = await signIn("my.email@gmail.com", "12345678") const res = await login("my.email@gmail.com", "12345678")
expect(res).toEqual({ expect(res).toEqual({
membre: { membre: {
prenom: mockUser.prenom, prenom: mockUser.prenom,
@ -27,29 +27,29 @@ describe("signIn with", () => {
}) })
it("invalid password length", async () => { it("invalid password length", async () => {
await expect(signIn("my.email@gmail.com", "123")).rejects.toThrow("Mot de passe trop court") await expect(login("my.email@gmail.com", "123")).rejects.toThrow("Mot de passe trop court")
}) })
it("empty password", async () => { it("empty password", async () => {
await expect(signIn("my.email@gmail.com", " ")).rejects.toThrow("Mot de passe nécessaire") await expect(login("my.email@gmail.com", " ")).rejects.toThrow("Mot de passe nécessaire")
}) })
it("wrong password", async () => { it("wrong password", async () => {
await expect(signIn("my.email@gmail.com", "1234567891011")).rejects.toThrow( await expect(login("my.email@gmail.com", "1234567891011")).rejects.toThrow(
"Mauvais mot de passe pour cet email" "Mauvais mot de passe pour cet email"
) )
}) })
it("invalid email format", async () => { it("invalid email format", async () => {
await expect(signIn("my.email@gmail", "12345678")).rejects.toThrow("Email invalid") await expect(login("my.email@gmail", "12345678")).rejects.toThrow("Email invalid")
}) })
it("empty email", async () => { it("empty email", async () => {
await expect(signIn(" ", "12345678")).rejects.toThrow("Email invalid") await expect(login(" ", "12345678")).rejects.toThrow("Email invalid")
}) })
it("unknown email", async () => { it("unknown email", async () => {
await expect(signIn("mon.emailBidon@gmail.com", "12345678")).rejects.toThrow( await expect(login("mon.emailBidon@gmail.com", "12345678")).rejects.toThrow(
"Cet email ne correspond à aucun utilisateur" "Cet email ne correspond à aucun utilisateur"
) )
}) })

View File

@ -5,7 +5,7 @@ import getAccessors from "../gsheets/accessors"
const { listGet } = getAccessors("Membres", new Membre()) const { listGet } = getAccessors("Membres", new Membre())
export default async function signInHandler( export default async function loginHandler(
request: Request, request: Request,
response: Response, response: Response,
_next: NextFunction _next: NextFunction
@ -14,7 +14,7 @@ export default async function signInHandler(
if (typeof request.body.email !== "string" || typeof request.body.password !== "string") { if (typeof request.body.email !== "string" || typeof request.body.password !== "string") {
throw Error() throw Error()
} }
const res = await signIn(request.body.email, request.body.password) const res = await login(request.body.email, request.body.password)
response.status(200).json(res) response.status(200).json(res)
} catch (e: any) { } catch (e: any) {
if (e.message) { if (e.message) {
@ -25,7 +25,7 @@ export default async function signInHandler(
} }
} }
export async function signIn(rawEmail: string, rawPassword: string): Promise<MemberLogin> { export async function login(rawEmail: string, rawPassword: string): Promise<MemberLogin> {
const email = rawEmail.replace(/^\s*/, "").replace(/\s*$/, "") const email = rawEmail.replace(/^\s*/, "").replace(/\s*$/, "")
if (!emailRegexp.test(email)) { if (!emailRegexp.test(email)) {
throw Error("Email invalid") throw Error("Email invalid")