1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Server: Clean up test units

This commit is contained in:
Laurent Cozic 2021-01-14 11:55:27 +00:00
parent 6a80b2ae9e
commit 80580ba54d
6 changed files with 8 additions and 117 deletions

View File

@ -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<User> {
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<User> {
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<void> {
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<void> {
const user = await this.initSession(sessionId);
const userModel = this.models.user({ userId: user.id });
await userModel.delete(userId);
}
}

View File

@ -2,7 +2,6 @@ import { Models } from '../models/factory';
import FileController from './api/FileController'; import FileController from './api/FileController';
// import OAuthController from './api/OAuthController'; // import OAuthController from './api/OAuthController';
import SessionController from './api/SessionController'; import SessionController from './api/SessionController';
import UserController from './api/UserController';
import IndexLoginController from './index/LoginController'; import IndexLoginController from './index/LoginController';
import IndexHomeController from './index/HomeController'; import IndexHomeController from './index/HomeController';
import IndexProfileController from './index/ProfileController'; import IndexProfileController from './index/ProfileController';
@ -30,10 +29,6 @@ export class Controllers {
return new SessionController(this.models_); return new SessionController(this.models_);
} }
public apiUser() {
return new UserController(this.models_);
}
public indexLogin() { public indexLogin() {
return new IndexLoginController(this.models_); return new IndexLoginController(this.models_);
} }
@ -43,7 +38,7 @@ export class Controllers {
} }
public indexProfile() { public indexProfile() {
return new IndexProfileController(this.models_, this.apiUser()); return new IndexProfileController(this.models_);
} }
public indexUser() { public indexUser() {

View File

@ -2,18 +2,9 @@ import BaseController from '../BaseController';
import { View } from '../../services/MustacheService'; import { View } from '../../services/MustacheService';
import defaultView from '../../utils/defaultView'; import defaultView from '../../utils/defaultView';
import { User } from '../../db'; import { User } from '../../db';
import { Models } from '../../models/factory';
import UserController from '../api/UserController';
export default class ProfileController extends BaseController { 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<View> { public async getIndex(sessionId: string, user: User = null, error: any = null): Promise<View> {
const owner = await this.initSession(sessionId); const owner = await this.initSession(sessionId);
@ -24,8 +15,4 @@ export default class ProfileController extends BaseController {
return view; return view;
} }
public async patchIndex(sessionId: string, user: User): Promise<void> {
await this.userController_.patchUser(sessionId, user);
}
} }

View File

@ -32,7 +32,8 @@ const route: Route = {
user.password = hashPassword(body.fields.password); 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`); return redirect(ctx, `${baseUrl()}/profile`);
} catch (error) { } catch (error) {
return ctx.controllers.indexProfile().getIndex(sessionId, user, error); return ctx.controllers.indexProfile().getIndex(sessionId, user, error);

View File

@ -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;

View File

@ -43,14 +43,16 @@ const route: Route = {
const fields = body.fields; const fields = body.fields;
user = makeUser(isNew, fields); user = makeUser(isNew, fields);
const userModel = ctx.models.user({ userId: ctx.owner.id });
if (fields.post_button) { if (fields.post_button) {
if (isNew) { if (isNew) {
await ctx.controllers.apiUser().postUser(sessionId, user); await userModel.save(userModel.fromApiInput(user));
} else { } else {
await ctx.controllers.apiUser().patchUser(sessionId, user); await userModel.save(userModel.fromApiInput(user), { isNew: false });
} }
} else if (fields.delete_button) { } else if (fields.delete_button) {
await ctx.controllers.apiUser().deleteUser(sessionId, path.id); await userModel.delete(path.id);
} else { } else {
throw new Error('Invalid form button'); throw new Error('Invalid form button');
} }