From 82cf881af2be2e76ac79ba8652ed895bb9aef5e4 Mon Sep 17 00:00:00 2001 From: Mateusz Russak Date: Sat, 16 Dec 2023 18:08:44 +0100 Subject: [PATCH] test: fixing unstable tests and moving old --- packages/repo/src/index.ts | 2 +- packages/server/package.json | 1 + .../app/actions/message/tests/pins.spec.js | 10 +++---- .../src/app/actions/message/tests/seeds.js | 5 ++-- .../src/app/actions/tests/fcmSetup.spec.js | 28 ++++++++++++++++++ packages/server/tests/actions/index.js | 1 - packages/server/tests/actions/message.spec.js | 4 +++ .../server/tests/actions/setupFcm.spec.js | 29 ------------------- packages/server/tests/actions/typing.spec.js | 4 +++ packages/server/tests/environment.js | 19 ++++++------ 10 files changed, 56 insertions(+), 47 deletions(-) create mode 100644 packages/server/src/app/actions/tests/fcmSetup.spec.js delete mode 100644 packages/server/tests/actions/setupFcm.spec.js diff --git a/packages/repo/src/index.ts b/packages/repo/src/index.ts index b6c4f9c0..f42c5e81 100644 --- a/packages/repo/src/index.ts +++ b/packages/repo/src/index.ts @@ -20,7 +20,7 @@ export * from './channel/channelTypes'; export const createRepositories = (databaseUrl) => { const client = init(databaseUrl); return { - db: connect().then(({ db }) => db), + connect, ObjectId, message: new MessageRepo(), channel: new ChannelRepo(), diff --git a/packages/server/package.json b/packages/server/package.json index b9520318..c32cbd03 100644 --- a/packages/server/package.json +++ b/packages/server/package.json @@ -8,6 +8,7 @@ "dev": "nodemon src/index.js", "lint": "eslint --ext .ts,.js .", "test": "NODE_ENV='test' DATABASE_URL='mongodb://chat:chat@localhost:27017/tests?authSource=admin' mocha --watch", + "testx": "NODE_ENV='test' DATABASE_URL='mongodb://chat:chat@localhost:27017/tests?authSource=admin' mocha", "test:ci": "NODE_ENV='test' mocha --exit" }, "author": "", diff --git a/packages/server/src/app/actions/message/tests/pins.spec.js b/packages/server/src/app/actions/message/tests/pins.spec.js index 0b26bd73..6337c81a 100644 --- a/packages/server/src/app/actions/message/tests/pins.spec.js +++ b/packages/server/src/app/actions/message/tests/pins.spec.js @@ -23,7 +23,7 @@ describe('messages:pins', () => { assert.equal(res.type, 'response'); assert.equal(res.status, 'ok'); assert.equal(res.count, 1); - assert.equal(msg.flat, 'Hello pinned'); + assert.equal(msg.flat, 'Hello'); }); it('should have no access to someones private channel', async () => { @@ -38,7 +38,7 @@ describe('messages:pins', () => { assert.equal(res.message, 'ACCESS_DENIED'); }); - it('should ', async () => { + it('should return continuation if after is specified', async () => { const { res, data: [msg1, msg2] } = await api.sendMessage({ type: 'messages:pins', channelId: channel.id, @@ -46,14 +46,14 @@ describe('messages:pins', () => { }, { userId: user.id }); assert.equal(res.type, 'response'); assert.equal(res.status, 'ok'); - const { res: res2, data: [msg] } = await api.sendMessage({ + const { res: res2, data: msgs } = await api.sendMessage({ type: 'messages:pins', channelId: channel.id, after: msg1.id, - limit: 1, + limit: 10, }, { userId: user.id }); assert.equal(res2.type, 'response'); assert.equal(res2.status, 'ok'); - assert.equal(msg.id, msg2.id); + assert.equal(msgs[0].id, msg2.id); }); }); diff --git a/packages/server/src/app/actions/message/tests/seeds.js b/packages/server/src/app/actions/message/tests/seeds.js index 1b282a96..19221abf 100644 --- a/packages/server/src/app/actions/message/tests/seeds.js +++ b/packages/server/src/app/actions/message/tests/seeds.js @@ -12,7 +12,8 @@ module.exports = { } const testChannel = await api.repo.channel.get({ name: 'test' }); if (!testChannel) await api.repo.channel.create({ name: 'test', private: false }); - await api.repo.message.removeMany({ flat: 'Hello' }); + const {db} = await api.repo.connect(); + db.collection('messages').deleteMany({}); await api.repo.badge.removeMany({}); await api.repo.message.createMany([ { @@ -70,7 +71,7 @@ module.exports = { pinned: true, flat: 'Hello', userId: member.id, - createdAt: new Date('2022-01-05'), + createdAt: new Date('2022-01-06'), }, ]); }, diff --git a/packages/server/src/app/actions/tests/fcmSetup.spec.js b/packages/server/src/app/actions/tests/fcmSetup.spec.js new file mode 100644 index 00000000..f51ca7f8 --- /dev/null +++ b/packages/server/src/app/actions/tests/fcmSetup.spec.js @@ -0,0 +1,28 @@ +const assert = require('assert'); +const api = require('./api'); + +describe('fcm:setup', () => { + let user; + before(async () => { + await api.repo.user.update({ login: 'admin' }, { notifications: {} }); + user = await api.repo.user.get({ login: 'admin' }); + }); + it('should update fcm token for current session', async () => { + const token = 'testToken'; + const { res } = await api.sendMessage({ + type: 'fcm:setup', + token, + }, { userId: user.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); + const updatedUser = await api.repo.user.get({ id: user.id }); + assert.ok(updatedUser.notifications[token]); + }); + it('should throw error when token is not present', async () => { + const { res } = await api.sendMessage({ + type: 'fcm:setup', + }).catch((e) => e); + assert.equal(res.status, 'error'); + assert.equal(res.message, '"token" is required'); + }); +}); diff --git a/packages/server/tests/actions/index.js b/packages/server/tests/actions/index.js index 7ea1c76d..8e197690 100644 --- a/packages/server/tests/actions/index.js +++ b/packages/server/tests/actions/index.js @@ -1,7 +1,6 @@ module.exports = (connect) => { describe('actions', () => { require('./message.spec')(connect); - require('./setupFcm.spec')(connect); require('./typing.spec')(connect); }); }; diff --git a/packages/server/tests/actions/message.spec.js b/packages/server/tests/actions/message.spec.js index c91b1cda..f0395a14 100644 --- a/packages/server/tests/actions/message.spec.js +++ b/packages/server/tests/actions/message.spec.js @@ -7,6 +7,10 @@ module.exports = (connect) => { before(async () => { channel = await repo.channel.get({ name: 'main' }); }); + + after(async () => { + await repo.close(); + }); it('should be received by other users', async () => { const member = await connect('member'); const admin = await connect('admin'); diff --git a/packages/server/tests/actions/setupFcm.spec.js b/packages/server/tests/actions/setupFcm.spec.js deleted file mode 100644 index 5f7a17ff..00000000 --- a/packages/server/tests/actions/setupFcm.spec.js +++ /dev/null @@ -1,29 +0,0 @@ -const assert = require('assert'); -const repo = require('../../src/infra/repositories'); - -module.exports = (connect) => { - describe('fcm:setup', () => { - it('should update fcm token for current session', async () => { - const ws = await connect(); - const token = 'testToken'; - const [ret] = await ws.send({ - type: 'fcm:setup', - token, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - const user = await repo.user.get({ id: ws.userId }); - assert.ok(user.notifications[token]); - ws.close(); - }); - it('should throw error when token is not present', async () => { - const ws = await connect(); - const [ret] = await ws.send({ - type: 'fcm:setup', - }).catch((e) => e); - assert.equal(ret.status, 'error'); - assert.equal(ret.message, '"token" is required'); - ws.close(); - }); - }); -}; diff --git a/packages/server/tests/actions/typing.spec.js b/packages/server/tests/actions/typing.spec.js index 1b9bdbfc..e3d9f29d 100644 --- a/packages/server/tests/actions/typing.spec.js +++ b/packages/server/tests/actions/typing.spec.js @@ -8,6 +8,10 @@ module.exports = (connect) => { channel = await repo.channel.get({ name: 'main' }); }); + after(async () => { + await repo.close(); + }); + it('should be received by other users', async () => { const member = await connect('member'); const admin = await connect('admin'); diff --git a/packages/server/tests/environment.js b/packages/server/tests/environment.js index a5122e50..4063433f 100644 --- a/packages/server/tests/environment.js +++ b/packages/server/tests/environment.js @@ -1,17 +1,18 @@ process.env.DATABASE_URL = 'mongodb://chat:chat@localhost:27017/tests?authSource=admin'; -const { db } = require('../src/infra/repositories'); +const { connect } = require('../src/infra/repositories'); exports.mochaHooks = { beforeAll: async () => { - const member = (await db).collection('users').findOne({ login: 'member' }); - const admin = (await db).collection('users').findOne({ login: 'admin' }); - const channel = await (await db).collection('channels').findOne({ name: 'main' }); - const testChannel = await (await db).collection('channels').findOne({ name: 'test' }); - if (!testChannel) await (await db).collection('channels').insertOne({ name: 'test', private: false }); - await (await db).collection('messages').deleteMany({}); - await (await db).collection('badges').deleteMany({}); - await (await db).collection('messages').insertMany([ + const { db } = await connect(); + const member = db.collection('users').findOne({ login: 'member' }); + const admin = db.collection('users').findOne({ login: 'admin' }); + const channel = await db.collection('channels').findOne({ name: 'main' }); + const testChannel = await db.collection('channels').findOne({ name: 'test' }); + if (!testChannel) await db.collection('channels').insertOne({ name: 'test', private: false }); + await db.collection('messages').deleteMany({}); + await db.collection('badges').deleteMany({}); + await db.collection('messages').insertMany([ { clientId: 1 + (`${Math.random()}`).slice(2), message: { line: { text: 'Hello' } },