mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-24 10:27:10 +02:00
Server: Added test units for notification handler
This commit is contained in:
parent
fb5eb1a47b
commit
c70f023fe0
@ -1,4 +1,4 @@
|
||||
import { testAssetDir, createUserAndSession, createUser, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb, models, controllers } from '../../utils/testUtils';
|
||||
import { testAssetDir, createUserAndSession, createUser, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb, models, controllers } from '../../utils/testing/testUtils';
|
||||
import * as fs from 'fs-extra';
|
||||
import { ChangeType, File } from '../../db';
|
||||
import { ErrorConflict, ErrorForbidden, ErrorNotFound, ErrorUnprocessableEntity } from '../../utils/errors';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createUser, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb, controllers } from '../../utils/testUtils';
|
||||
import { createUser, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb, controllers } from '../../utils/testing/testUtils';
|
||||
import { ErrorForbidden } from '../../utils/errors';
|
||||
|
||||
describe('SessionController', function() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { models, controllers, createUserAndSession, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb } from '../../utils/testUtils';
|
||||
import { models, controllers, createUserAndSession, checkThrowAsync, beforeAllDb, afterAllDb, beforeEachDb } from '../../utils/testing/testUtils';
|
||||
import { File, User } from '../../db';
|
||||
import { ErrorForbidden, ErrorUnprocessableEntity } from '../../utils/errors';
|
||||
|
||||
|
73
packages/server/src/middleware/notificationHandler.test.ts
Normal file
73
packages/server/src/middleware/notificationHandler.test.ts
Normal file
@ -0,0 +1,73 @@
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, koaAppContext, koaNext } from '../utils/testing/testUtils';
|
||||
import { defaultAdminEmail, defaultAdminPassword, Notification } from '../db';
|
||||
import notificationHandler from './notificationHandler';
|
||||
|
||||
describe('notificationHandler', function() {
|
||||
|
||||
beforeAll(async () => {
|
||||
await beforeAllDb('notificationHandler');
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await afterAllDb();
|
||||
});
|
||||
|
||||
beforeEach(async () => {
|
||||
await beforeEachDb();
|
||||
});
|
||||
|
||||
test('should check admin password', async function() {
|
||||
const { user } = await createUserAndSession(1, true);
|
||||
|
||||
const admin = await models().user({ userId: user.id }).save({
|
||||
email: defaultAdminEmail,
|
||||
password: defaultAdminPassword,
|
||||
is_admin: 1,
|
||||
});
|
||||
|
||||
{
|
||||
const context = await koaAppContext({ owner: user });
|
||||
await notificationHandler(context, koaNext);
|
||||
|
||||
const notifications: Notification[] = await models().notification().all();
|
||||
expect(notifications.length).toBe(1);
|
||||
expect(notifications[0].key).toBe('change_admin_password');
|
||||
expect(notifications[0].read).toBe(0);
|
||||
|
||||
expect(context.notifications.length).toBe(1);
|
||||
}
|
||||
|
||||
{
|
||||
await models().user({ userId: admin.id }).save({
|
||||
id: admin.id,
|
||||
password: 'changed!',
|
||||
});
|
||||
|
||||
const context = await koaAppContext({ owner: user });
|
||||
await notificationHandler(context, koaNext);
|
||||
|
||||
const notifications: Notification[] = await models().notification().all();
|
||||
expect(notifications.length).toBe(1);
|
||||
expect(notifications[0].key).toBe('change_admin_password');
|
||||
expect(notifications[0].read).toBe(1);
|
||||
|
||||
expect(context.notifications.length).toBe(0);
|
||||
}
|
||||
});
|
||||
|
||||
test('should not check admin password for non-admin', async function() {
|
||||
const { user } = await createUserAndSession(1, false);
|
||||
|
||||
await createUserAndSession(2, true, {
|
||||
email: defaultAdminEmail,
|
||||
password: defaultAdminPassword,
|
||||
});
|
||||
|
||||
const context = await koaAppContext({ owner: user });
|
||||
await notificationHandler(context, koaNext);
|
||||
|
||||
const notifications: Notification[] = await models().notification().all();
|
||||
expect(notifications.length).toBe(0);
|
||||
});
|
||||
|
||||
});
|
@ -1,4 +1,4 @@
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, expectThrow } from '../utils/testUtils';
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, expectThrow } from '../utils/testing/testUtils';
|
||||
import { ChangeType, File } from '../db';
|
||||
import FileModel from './FileModel';
|
||||
import { msleep } from '../utils/time';
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, createFileTree } from '../utils/testUtils';
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, createFileTree } from '../utils/testing/testUtils';
|
||||
import { File } from '../db';
|
||||
|
||||
describe('FileModel', function() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, expectThrow } from '../utils/testUtils';
|
||||
import { createUserAndSession, beforeAllDb, afterAllDb, beforeEachDb, models, expectThrow } from '../utils/testing/testUtils';
|
||||
import { Notification, NotificationLevel } from '../db';
|
||||
|
||||
describe('NotificationModel', function() {
|
||||
|
@ -1,4 +1,4 @@
|
||||
import { expectThrow } from '../../utils/testUtils';
|
||||
import { expectThrow } from '../../utils/testing/testUtils';
|
||||
import { defaultPagination, Pagination, createPaginationLinks, requestPagination } from './pagination';
|
||||
|
||||
describe('pagination', function() {
|
||||
|
@ -1,11 +1,12 @@
|
||||
import { User, Session, DbConnection, connectDb, disconnectDb, File, truncateTables } from '../db';
|
||||
import { createDb } from '../tools/dbTools';
|
||||
import modelFactory from '../models/factory';
|
||||
import controllerFactory from '../controllers/factory';
|
||||
import baseConfig from '../config-tests';
|
||||
import { Config } from './types';
|
||||
import { initConfig } from '../config';
|
||||
import FileModel from '../models/FileModel';
|
||||
import { User, Session, DbConnection, connectDb, disconnectDb, File, truncateTables } from '../../db';
|
||||
import { createDb } from '../../tools/dbTools';
|
||||
import modelFactory from '../../models/factory';
|
||||
import controllerFactory from '../../controllers/factory';
|
||||
import baseConfig from '../../config-tests';
|
||||
import { AppContext, Config, Env } from '../types';
|
||||
import { initConfig } from '../../config';
|
||||
import FileModel from '../../models/FileModel';
|
||||
import Logger from '@joplin/lib/Logger';
|
||||
|
||||
// Takes into account the fact that this file will be inside the /dist directory
|
||||
// when it runs.
|
||||
@ -38,6 +39,47 @@ export async function beforeEachDb() {
|
||||
await truncateTables(db_);
|
||||
}
|
||||
|
||||
interface AppContextTestOptions {
|
||||
path?: string;
|
||||
owner?: User;
|
||||
}
|
||||
|
||||
let koaAppContextUserAndSession_: UserAndSession = null;
|
||||
|
||||
export async function koaAppContextUserAndSession(): Promise<UserAndSession> {
|
||||
if (koaAppContextUserAndSession_) return koaAppContextUserAndSession_;
|
||||
koaAppContextUserAndSession_ = await createUserAndSession(1, false);
|
||||
return koaAppContextUserAndSession_;
|
||||
}
|
||||
|
||||
export async function koaAppContext(options: AppContextTestOptions = null): Promise<AppContext> {
|
||||
if (!db_) throw new Error('Database must be initialized first');
|
||||
|
||||
options = {
|
||||
path: '/home',
|
||||
...options,
|
||||
};
|
||||
|
||||
const appLogger = Logger.create('AppTest');
|
||||
|
||||
const appContext: any = {};
|
||||
|
||||
appContext.env = Env.Dev;
|
||||
appContext.db = db_;
|
||||
appContext.models = models();
|
||||
appContext.controllers = controllers();
|
||||
appContext.appLogger = () => appLogger;
|
||||
|
||||
appContext.path = options.path;
|
||||
appContext.owner = options.owner;
|
||||
|
||||
return appContext as AppContext;
|
||||
}
|
||||
|
||||
export function koaNext(): Promise<void> {
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
export const testAssetDir = `${packageRootDir}/assets/tests`;
|
||||
|
||||
interface UserAndSession {
|
||||
@ -61,12 +103,22 @@ export function controllers() {
|
||||
return controllerFactory(models());
|
||||
}
|
||||
|
||||
export const createUserAndSession = async function(index: number = 1, isAdmin: boolean = false): Promise<UserAndSession> {
|
||||
interface CreateUserAndSessionOptions {
|
||||
email?: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
export const createUserAndSession = async function(index: number = 1, isAdmin: boolean = false, options: CreateUserAndSessionOptions = null): Promise<UserAndSession> {
|
||||
const sessionController = controllers().apiSession();
|
||||
|
||||
const email: string = `user${index}@localhost`;
|
||||
const user = await models().user().save({ email: email, password: '123456', is_admin: isAdmin ? 1 : 0 }, { skipValidation: true });
|
||||
const session = await sessionController.authenticate(email, '123456');
|
||||
options = {
|
||||
email: `user${index}@localhost`,
|
||||
password: '123456',
|
||||
...options,
|
||||
};
|
||||
|
||||
const user = await models().user().save({ email: options.email, password: options.password, is_admin: isAdmin ? 1 : 0 }, { skipValidation: true });
|
||||
const session = await sessionController.authenticate(options.email, options.password);
|
||||
|
||||
return {
|
||||
user: user,
|
Loading…
Reference in New Issue
Block a user