From 80580ba54dae0464bdc1ebd908c51c7e3c25b283 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Thu, 14 Jan 2021 11:55:27 +0000 Subject: [PATCH] Server: Clean up test units --- .../src/controllers/api/UserController.ts | 33 ---------- packages/server/src/controllers/factory.ts | 7 +-- .../controllers/index/ProfileController.ts | 13 ---- packages/server/src/routes/index/profile.ts | 3 +- packages/server/src/routes/index/user.ts | 61 ------------------- packages/server/src/routes/index/users.ts | 8 ++- 6 files changed, 8 insertions(+), 117 deletions(-) delete mode 100644 packages/server/src/controllers/api/UserController.ts delete mode 100644 packages/server/src/routes/index/user.ts diff --git a/packages/server/src/controllers/api/UserController.ts b/packages/server/src/controllers/api/UserController.ts deleted file mode 100644 index c10bb559e..000000000 --- a/packages/server/src/controllers/api/UserController.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { User } from '../../db'; -import BaseController from '../BaseController'; - -export default class UserController extends BaseController { - - public async postUser(sessionId: string, user: User): Promise { - const owner = await this.initSession(sessionId, true); - const userModel = this.models.user({ userId: owner.id }); - let newUser = userModel.fromApiInput(user); - newUser = await userModel.save(newUser); - return userModel.toApiOutput(newUser); - } - - public async getUser(sessionId: string, userId: string): Promise { - const owner = await this.initSession(sessionId); - const userModel = this.models.user({ userId: owner.id }); - return userModel.toApiOutput(await userModel.load(userId)); - } - - public async patchUser(sessionId: string, user: User): Promise { - const owner = await this.initSession(sessionId); - const userModel = this.models.user({ userId: owner.id }); - const newUser = userModel.fromApiInput(user); - await userModel.save(newUser, { isNew: false }); - } - - public async deleteUser(sessionId: string, userId: string): Promise { - const user = await this.initSession(sessionId); - const userModel = this.models.user({ userId: user.id }); - await userModel.delete(userId); - } - -} diff --git a/packages/server/src/controllers/factory.ts b/packages/server/src/controllers/factory.ts index 2f3d9f432..64e750068 100644 --- a/packages/server/src/controllers/factory.ts +++ b/packages/server/src/controllers/factory.ts @@ -2,7 +2,6 @@ import { Models } from '../models/factory'; import FileController from './api/FileController'; // import OAuthController from './api/OAuthController'; import SessionController from './api/SessionController'; -import UserController from './api/UserController'; import IndexLoginController from './index/LoginController'; import IndexHomeController from './index/HomeController'; import IndexProfileController from './index/ProfileController'; @@ -30,10 +29,6 @@ export class Controllers { return new SessionController(this.models_); } - public apiUser() { - return new UserController(this.models_); - } - public indexLogin() { return new IndexLoginController(this.models_); } @@ -43,7 +38,7 @@ export class Controllers { } public indexProfile() { - return new IndexProfileController(this.models_, this.apiUser()); + return new IndexProfileController(this.models_); } public indexUser() { diff --git a/packages/server/src/controllers/index/ProfileController.ts b/packages/server/src/controllers/index/ProfileController.ts index 5d16a6398..9ec4ad344 100644 --- a/packages/server/src/controllers/index/ProfileController.ts +++ b/packages/server/src/controllers/index/ProfileController.ts @@ -2,18 +2,9 @@ import BaseController from '../BaseController'; import { View } from '../../services/MustacheService'; import defaultView from '../../utils/defaultView'; import { User } from '../../db'; -import { Models } from '../../models/factory'; -import UserController from '../api/UserController'; export default class ProfileController extends BaseController { - private userController_: UserController; - - public constructor(models: Models, userController: UserController) { - super(models); - this.userController_ = userController; - } - public async getIndex(sessionId: string, user: User = null, error: any = null): Promise { const owner = await this.initSession(sessionId); @@ -24,8 +15,4 @@ export default class ProfileController extends BaseController { return view; } - public async patchIndex(sessionId: string, user: User): Promise { - await this.userController_.patchUser(sessionId, user); - } - } diff --git a/packages/server/src/routes/index/profile.ts b/packages/server/src/routes/index/profile.ts index c76952ec5..488d2c820 100644 --- a/packages/server/src/routes/index/profile.ts +++ b/packages/server/src/routes/index/profile.ts @@ -32,7 +32,8 @@ const route: Route = { user.password = hashPassword(body.fields.password); } - await ctx.controllers.indexProfile().patchIndex(sessionId, user); + const userModel = this.models.user({ userId: ctx.owner.id }); + await userModel.save(userModel.fromApiInput(user), { isNew: false }); return redirect(ctx, `${baseUrl()}/profile`); } catch (error) { return ctx.controllers.indexProfile().getIndex(sessionId, user, error); diff --git a/packages/server/src/routes/index/user.ts b/packages/server/src/routes/index/user.ts deleted file mode 100644 index cd1e63865..000000000 --- a/packages/server/src/routes/index/user.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Not used?? - -// import { SubPath, Route, redirect } from '../../utils/routeUtils'; -// import { AppContext } from '../../utils/types'; -// import { contextSessionId, formParse } from '../../utils/requestUtils'; -// import { ErrorMethodNotAllowed, ErrorUnprocessableEntity } from '../../utils/errors'; -// import { User } from '../../db'; -// import { baseUrl } from '../../config'; - -// function makeUser(isNew: boolean, fields: any): User { -// const user: User = { -// email: fields.email, -// full_name: fields.full_name, -// }; - -// if (fields.password) { -// if (fields.password !== fields.password2) throw new ErrorUnprocessableEntity('Passwords do not match'); -// user.password = fields.password; -// } - -// if (!isNew) user.id = fields.id; - -// return user; -// } - -// const route: Route = { - -// exec: async function(_path: SubPath, ctx: AppContext) { -// const sessionId = contextSessionId(ctx); - -// // if (ctx.method === 'GET') { -// // return ctx.controllers.indexUser().getOne(sessionId); -// // } - -// if (ctx.method === 'POST') { -// const user: User = {}; - -// try { -// const body = await formParse(ctx.req); -// const fields = body.fields; -// const isNew = !!Number(fields.is_new); -// const user = makeUser(isNew, fields); - -// if (isNew) { -// await ctx.controllers.apiUser().postUser(sessionId, user); -// } else { -// await ctx.controllers.apiUser().patchUser(sessionId, user); -// } - -// return redirect(ctx, `${baseUrl()}/users`); -// } catch (error) { -// return ctx.controllers.indexProfile().getIndex(sessionId, user, error); -// } -// } - -// throw new ErrorMethodNotAllowed(); -// }, - -// }; - -// export default route; diff --git a/packages/server/src/routes/index/users.ts b/packages/server/src/routes/index/users.ts index ae39388a1..e69900cc6 100644 --- a/packages/server/src/routes/index/users.ts +++ b/packages/server/src/routes/index/users.ts @@ -43,14 +43,16 @@ const route: Route = { const fields = body.fields; user = makeUser(isNew, fields); + const userModel = ctx.models.user({ userId: ctx.owner.id }); + if (fields.post_button) { if (isNew) { - await ctx.controllers.apiUser().postUser(sessionId, user); + await userModel.save(userModel.fromApiInput(user)); } else { - await ctx.controllers.apiUser().patchUser(sessionId, user); + await userModel.save(userModel.fromApiInput(user), { isNew: false }); } } else if (fields.delete_button) { - await ctx.controllers.apiUser().deleteUser(sessionId, path.id); + await userModel.delete(path.id); } else { throw new Error('Invalid form button'); }