1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-06 09:19:22 +02:00
Files
joplin/packages/server/src/models/UserModel.test.ts

142 lines
5.0 KiB
TypeScript
Raw Normal View History

import { createUserAndSession, beforeAllDb, afterAllTests, beforeEachDb, models, checkThrowAsync, createItem } from '../utils/testing/testUtils';
2021-05-25 11:49:47 +02:00
import { EmailSender, User } from '../db';
import { ErrorUnprocessableEntity } from '../utils/errors';
2021-08-02 17:43:18 +01:00
import { betaUserDateRange, stripeConfig } from '../utils/stripe';
import { AccountType } from './UserModel';
2021-01-15 22:02:36 +00:00
describe('UserModel', function() {
beforeAll(async () => {
2021-01-15 22:02:36 +00:00
await beforeAllDb('UserModel');
});
afterAll(async () => {
await afterAllTests();
});
beforeEach(async () => {
await beforeEachDb();
});
test('should validate user objects', async function() {
const { user: user1 } = await createUserAndSession(2, false);
const { user: user2 } = await createUserAndSession(3, false);
let error = null;
// Email must be set
error = await checkThrowAsync(async () => await models().user().save({ email: '', password: '1234546' }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
// Password must be set
error = await checkThrowAsync(async () => await models().user().save({ email: 'newone@example.com', password: '' }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
// email must be set
error = await checkThrowAsync(async () => await models().user().save({ id: user1.id, email: '' }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
// password must be set
error = await checkThrowAsync(async () => await models().user().save({ id: user1.id, password: '' }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
// there is already a user with this email
error = await checkThrowAsync(async () => await models().user().save({ id: user1.id, email: user2.email }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
// check that the email is valid
error = await checkThrowAsync(async () => await models().user().save({ id: user1.id, email: 'ohno' }));
expect(error instanceof ErrorUnprocessableEntity).toBe(true);
});
test('should delete a user', async function() {
const { session: session1, user: user1 } = await createUserAndSession(2, false);
const userModel = models().user();
const allUsers: User[] = await userModel.all();
const beforeCount: number = allUsers.length;
await createItem(session1.id, 'root:/test.txt:', 'testing');
// Admin can delete any user
expect(!!(await models().session().load(session1.id))).toBe(true);
expect((await models().item().all()).length).toBe(1);
expect((await models().userItem().all()).length).toBe(1);
await models().user().delete(user1.id);
expect((await userModel.all()).length).toBe(beforeCount - 1);
expect(!!(await models().session().load(session1.id))).toBe(false);
expect((await models().item().all()).length).toBe(0);
expect((await models().userItem().all()).length).toBe(0);
});
2021-05-25 11:49:47 +02:00
test('should push an email when creating a new user', async function() {
const { user: user1 } = await createUserAndSession(1);
const { user: user2 } = await createUserAndSession(2);
const emails = await models().email().all();
expect(emails.length).toBe(2);
expect(emails.find(e => e.recipient_email === user1.email)).toBeTruthy();
expect(emails.find(e => e.recipient_email === user2.email)).toBeTruthy();
const email = emails[0];
expect(email.subject.trim()).toBeTruthy();
expect(email.body.includes('/confirm?token=')).toBeTruthy();
expect(email.sender_id).toBe(EmailSender.NoReply);
expect(email.sent_success).toBe(0);
expect(email.sent_time).toBe(0);
expect(email.error).toBe('');
});
2021-08-02 17:43:18 +01:00
test('should send a beta reminder email', async function() {
stripeConfig().enabled = true;
const { user: user1 } = await createUserAndSession(1, false, { email: 'toto@example.com' });
const range = betaUserDateRange();
await models().user().save({
id: user1.id,
created_time: range[0],
account_type: AccountType.Pro,
});
Date.now = jest.fn(() => range[0] + 6912000 * 1000); // 80 days later
await models().user().handleBetaUserEmails();
expect((await models().email().all()).length).toBe(2);
{
const email = (await models().email().all()).pop();
expect(email.recipient_email).toBe('toto@example.com');
expect(email.subject.indexOf('10 days') > 0).toBe(true);
expect(email.body.indexOf('10 days') > 0).toBe(true);
expect(email.body.indexOf('toto%40example.com') > 0).toBe(true);
expect(email.body.indexOf('account_type=2') > 0).toBe(true);
}
await models().user().handleBetaUserEmails();
// It should not send a second email
expect((await models().email().all()).length).toBe(2);
Date.now = jest.fn(() => range[0] + 7603200 * 1000); // 88 days later
await models().user().handleBetaUserEmails();
expect((await models().email().all()).length).toBe(3);
{
const email = (await models().email().all()).pop();
expect(email.subject.indexOf('2 days') > 0).toBe(true);
expect(email.body.indexOf('2 days') > 0).toBe(true);
}
await models().user().handleBetaUserEmails();
expect((await models().email().all()).length).toBe(3);
stripeConfig().enabled = false;
});
});