diff --git a/packages/repo/src/repo.ts b/packages/repo/src/repo.ts index c31d1c4e..ca430db3 100644 --- a/packages/repo/src/repo.ts +++ b/packages/repo/src/repo.ts @@ -30,6 +30,10 @@ export default class Repo { .then(this.serializer.deserializeInsertedId); } + async createMany(data: Model[]): Promise { + return Promise.all(data.map((d) => this.create(d))); + } + async update(query: Query, data: Model, type = 'set') { const { db } = await connect(); return db.collection(this.tableName) @@ -54,6 +58,12 @@ export default class Repo { .deleteOne(this.serializer.serializeQuery(query)); } + async removeMany(query: Query) { + const { db } = await connect(); + return db.collection(this.tableName) + .deleteMany(this.serializer.serializeQuery(query)); + } + async count(query: Query) { const { db } = await connect(); return db.collection(this.tableName) 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 fc5ed24f..0b26bd73 100644 --- a/packages/server/src/app/actions/message/tests/pins.spec.js +++ b/packages/server/src/app/actions/message/tests/pins.spec.js @@ -1,31 +1,59 @@ const assert = require('assert'); const api = require('../../tests/api'); -const seeds = require('../../../../../tests/actions/seeds'); +const seeds = require('./seeds'); -module.exports = (connect) => { - describe('messages:pins', () => { - let channel; - before(async () => { - await seeds.run(); - channel = await api.repo.channel.get({ name: 'main' }); - }); +describe('messages:pins', () => { + let channel; + let user; + before(async () => { + await seeds.run(); + channel = await api.repo.channel.get({ name: 'main' }); + user = await api.repo.user.get({ login: 'admin' }); + }); after(async () => { await api.repo.close(); }); - it('should return last added messsage', async () => { - const ws = await connect(); - const [msg, ret] = await ws.send({ - type: 'messages:pins', - channelId: channel._id.toHexString(), - limit: 1, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - assert.equal(ret.count, 1); - assert.equal(msg.flat, 'Hello pinned'); - ws.close(); - }); - it('should return pins with before filter'); - it('should return pins with after filter'); + + it('should return last added messsage', async () => { + const { res, data: [msg] } = await api.sendMessage({ + type: 'messages:pins', + channelId: channel.id, + limit: 1, + }, { userId: user.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); + assert.equal(res.count, 1); + assert.equal(msg.flat, 'Hello pinned'); + }); + + it('should have no access to someones private channel', async () => { + const deniedChannel = await api.repo.channel.get({ name: 'denied' }); + const { res } = await api.sendMessage({ + type: 'messages:pins', + channelId: deniedChannel.id, + limit: 1, + }, { userId: user.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'error'); + assert.equal(res.message, 'ACCESS_DENIED'); + }); + + it('should ', async () => { + const { res, data: [msg1, msg2] } = await api.sendMessage({ + type: 'messages:pins', + channelId: channel.id, + limit: 2, + }, { userId: user.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); + const { res: res2, data: [msg] } = await api.sendMessage({ + type: 'messages:pins', + channelId: channel.id, + after: msg1.id, + limit: 1, + }, { userId: user.id }); + assert.equal(res2.type, 'response'); + assert.equal(res2.status, 'ok'); + assert.equal(msg.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 new file mode 100644 index 00000000..1b282a96 --- /dev/null +++ b/packages/server/src/app/actions/message/tests/seeds.js @@ -0,0 +1,77 @@ +const api = require('../../tests/api'); + +module.exports = { + run: async () => { + const member = await api.repo.user.get({ login: 'member' }); + const admin = await api.repo.user.get({ login: 'admin' }); + const channel = await api.repo.channel.get({ name: 'main' }); + let deniedChannel = await api.repo.channel.get({ name: 'denied' }); + if (!deniedChannel) { + const id = await api.repo.channel.create({ name: 'denied', private: true, users: [] }); + deniedChannel = await api.repo.channel.get({ id }); + } + 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' }); + await api.repo.badge.removeMany({}); + await api.repo.message.createMany([ + { + clientId: 1, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: channel.id, + flat: 'Hello', + userId: member.id, + createdAt: new Date('2022-01-01'), + }, + { + clientId: 2, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: channel.id, + flat: 'Hello', + pinned: true, + userId: admin.id, + createdAt: new Date('2022-01-02'), + }, + { + clientId: 3, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: channel.id, + flat: 'Hello', + pinned: true, + userId: member.id, + createdAt: new Date('2022-01-03'), + }, + { + clientId: 4, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: channel.id, + flat: 'Hello', + userId: admin.id, + createdAt: new Date('2022-01-04'), + }, + { + clientId: 5, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: channel.id, + flat: 'Hello', + userId: member.id, + createdAt: new Date('2022-01-05'), + }, + { + clientId: 6, + message: { line: { text: 'Hello' } }, + channel: 'main', + channelId: deniedChannel.id, + pinned: true, + flat: 'Hello', + userId: member.id, + createdAt: new Date('2022-01-05'), + }, + ]); + }, +}; diff --git a/packages/server/tests/commands/help.spec.js b/packages/server/src/app/commands/tests/help.spec.js similarity index 65% rename from packages/server/tests/commands/help.spec.js rename to packages/server/src/app/commands/tests/help.spec.js index 314dbc1e..f63e7145 100644 --- a/packages/server/tests/commands/help.spec.js +++ b/packages/server/src/app/commands/tests/help.spec.js @@ -1,12 +1,12 @@ const assert = require('assert'); -const repo = require('../../src/infra/repositories'); +const api = require('../../actions/tests/api'); module.exports = (connect) => { describe('/help', () => { it('should return help message', async () => { const ws = await connect(); - const channel = await repo.channel.get({ name: 'main' }); - const [help, ret] = await ws.send({ + const channel = await api.repo.channel.get({ name: 'main' }); + const { res, data: [help] } = await api.sendMessage({ type: 'command:execute', name: 'help', args: [], @@ -14,8 +14,8 @@ module.exports = (connect) => { channelId: channel.id, }, }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); assert.equal(help.type, 'message'); assert.equal(help.id.slice(0, 3), 'sys'); assert.equal(help.priv, true); diff --git a/packages/server/src/app/commands/tests/me.spec.js b/packages/server/src/app/commands/tests/me.spec.js new file mode 100644 index 00000000..ba65d6e8 --- /dev/null +++ b/packages/server/src/app/commands/tests/me.spec.js @@ -0,0 +1,33 @@ +const assert = require('assert'); +const api = require('../../actions/tests/api'); + +describe('/me', () => { + const NAME = 'Admin'; + let channel; + let admin; + + before(async () => { + admin = await api.repo.user.get({ login: 'admin' }); + channel = await api.repo.channel.get({ name: 'main' }); + await api.repo.user.update({ login: 'admin' }, { name: 'Johny' }); + }); + + after(async () => { + await api.repo.user.update({ login: 'admin' }, { name: 'Admin' }); + }); + + it('should return message with imformation about user', async () => { + const { res, data: [user] } = await api.sendMessage({ + type: 'command:execute', + name: 'me', + args: [], + context: { + channelId: channel.id, + }, + }, { userId: admin.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); + + assert.equal(user.type, 'message'); + }); +}); diff --git a/packages/server/src/app/commands/tests/name.spec.js b/packages/server/src/app/commands/tests/name.spec.js new file mode 100644 index 00000000..932bf239 --- /dev/null +++ b/packages/server/src/app/commands/tests/name.spec.js @@ -0,0 +1,38 @@ +const assert = require('assert'); +const api = require('../../actions/tests/api'); + +describe('/name ', () => { + const NAME = 'Admin'; + let channel; + let admin; + + before(async () => { + admin = await api.repo.user.get({ login: 'admin' }); + channel = await api.repo.channel.get({ name: 'main' }); + await api.repo.user.update({ login: 'admin' }, { name: 'Johny' }); + }); + + after(async () => { + await api.repo.user.update({ login: 'admin' }, { name: 'Admin' }); + }); + + it('should change users name', async () => { + const { res, data: [user] } = await api.sendMessage({ + type: 'command:execute', + name: 'name', + args: [NAME], + context: { + channelId: channel.id, + }, + }, { userId: admin.id }); + assert.equal(res.type, 'response'); + assert.equal(res.status, 'ok'); + + const state = await api.repo.user.get({ login: 'admin' }); + assert.equal(state.name, NAME); + assert.equal(user.type, 'user'); + assert.equal(user.name, NAME); + }); + + it('should inform others about change'); +}); diff --git a/packages/server/src/app/common/channel.js b/packages/server/src/app/common/channel.js index 51af0326..b3ae2a7b 100644 --- a/packages/server/src/app/common/channel.js +++ b/packages/server/src/app/common/channel.js @@ -5,7 +5,7 @@ module.exports = { const channel = await repo.channel.get({ id }); if (channel?.private - && !channel.users.includes(userId)) { + && !channel.users?.includes(userId)) { return false; } return true; diff --git a/packages/server/tests/actions/command.spec.js b/packages/server/tests/actions/command.spec.js deleted file mode 100644 index bad93a4b..00000000 --- a/packages/server/tests/actions/command.spec.js +++ /dev/null @@ -1,5 +0,0 @@ -module.exports = (connect) => { - describe('command:execute', () => { - require('../commands')(connect); - }); -}; diff --git a/packages/server/tests/actions/index.js b/packages/server/tests/actions/index.js index c6471d65..7ea1c76d 100644 --- a/packages/server/tests/actions/index.js +++ b/packages/server/tests/actions/index.js @@ -3,6 +3,5 @@ module.exports = (connect) => { require('./message.spec')(connect); require('./setupFcm.spec')(connect); require('./typing.spec')(connect); - require('./command.spec')(connect); }); }; diff --git a/packages/server/tests/commands/ai.spec.js b/packages/server/tests/commands/ai.spec.js deleted file mode 100644 index d08dd4a4..00000000 --- a/packages/server/tests/commands/ai.spec.js +++ /dev/null @@ -1,9 +0,0 @@ -/* eslint-disable no-unused-vars */ -const assert = require('assert'); -const { db } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - describe('/ai ', () => { - it('should work'); - }); -}; diff --git a/packages/server/tests/commands/index.js b/packages/server/tests/commands/index.js deleted file mode 100644 index 1e59b8e4..00000000 --- a/packages/server/tests/commands/index.js +++ /dev/null @@ -1,9 +0,0 @@ -module.exports = (connect) => { - require('./help.spec')(connect); - // require('./join.spec')(connect); - // require('./leave.spec')(connect); - require('./name.spec')(connect); - require('./ai.spec')(connect); - require('./prompt.spec')(connect); - require('./me.spec')(connect); -}; diff --git a/packages/server/tests/commands/join.spec.js b/packages/server/tests/commands/join.spec.js deleted file mode 100644 index b9c39d78..00000000 --- a/packages/server/tests/commands/join.spec.js +++ /dev/null @@ -1,83 +0,0 @@ -const assert = require('assert'); -const { db } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - const CHANNEL = 'join-channel'; - - describe.skip('/join', () => { - before(async () => { - await (await db) - .collection('channels') - .deleteOne({ cid: CHANNEL }); - }); - afterEach(async () => { - await (await db) - .collection('channels') - .updateOne({ cid: CHANNEL }, { $set: { users: [] } }); - }); - - it('should add user to a channel', async () => { - const ws = await connect('admin'); - const [chan, ret] = await ws.send({ - type: 'command:execute', - name: 'join', - args: [], - context: { - channel: CHANNEL, - }, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - - assert.equal(chan.type, 'channel'); - assert.equal(chan.cid, CHANNEL); - assert.equal(chan.name, CHANNEL); - assert(chan.users.includes(ws.userId)); - - const channel = await (await db) - .collection('channels') - .findOne({ cid: CHANNEL }); - assert(channel.users.map((u) => u.toHexString()).includes(ws.userId)); - ws.close(); - }); - - it('should add user to a existing channel', async () => { - const ws = await connect('admin'); - const [, ret] = await ws.send({ - type: 'command:execute', - name: 'join', - args: [], - context: { - channel: CHANNEL, - }, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - const channel = await (await db) - .collection('channels') - .findOne({ cid: CHANNEL }); - assert(channel.users.map((u) => u.toHexString()).includes(ws.userId)); - ws.close(); - }); - - it('should not permit adding to private channel', async () => { - await (await db) - .collection('channels') - .updateOne({ cid: CHANNEL }, { $set: { private: true } }); - - const ws = await connect('admin'); - const [ret] = await ws.send({ - type: 'command:execute', - name: 'join', - args: [], - context: { - channel: CHANNEL, - }, - }).catch((e) => e); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'error'); - assert.equal(ret.message, 'ACCESS_DENIED'); - ws.close(); - }); - }); -}; diff --git a/packages/server/tests/commands/leave.spec.js b/packages/server/tests/commands/leave.spec.js deleted file mode 100644 index 2d848675..00000000 --- a/packages/server/tests/commands/leave.spec.js +++ /dev/null @@ -1,53 +0,0 @@ -const assert = require('assert'); -const { db, ObjectId } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - const CHANNEL = 'leave-channel'; - - describe('/leave', () => { - let ws; - let channelId; - before(async () => { - ws = await connect('admin'); - await (await db).collection('channels').deleteOne({ cid: CHANNEL }); - ({ insertedId: channelId } = await (await db) - .collection('channels') - .insertOne({ - cid: CHANNEL, - name: CHANNEL, - users: [ObjectId(ws.userId)], - })); - }); - - after(async () => { - ws.close(); - }); - - it('should remove user from a channel', async () => { - const [removeChannel, info, ret] = await ws.send({ - type: 'command:execute', - name: 'leave', - args: [], - context: { - channelId: channelId.toHexString(), - }, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - - assert.equal(info.type, 'message'); - assert.equal(info.priv, true); - assert.equal(info.userId, 'system'); - assert.ok(info.createdAt); - assert.ok(info.message); - - assert.equal(removeChannel.type, 'removeChannel'); - assert.equal(removeChannel.channelId, channelId.toHexString()); - - const channel = await (await db) - .collection('channels') - .findOne({ cid: CHANNEL }); - assert(!channel.users.map((u) => u.toHexString()).includes(ws.userId)); - }); - }); -}; diff --git a/packages/server/tests/commands/me.spec.js b/packages/server/tests/commands/me.spec.js deleted file mode 100644 index 8c6d05bf..00000000 --- a/packages/server/tests/commands/me.spec.js +++ /dev/null @@ -1,9 +0,0 @@ -/* eslint-disable no-unused-vars */ -const assert = require('assert'); -const { db } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - describe('/me', () => { - it('should display information about user'); - }); -}; diff --git a/packages/server/tests/commands/name.spec.js b/packages/server/tests/commands/name.spec.js deleted file mode 100644 index 69bfa25a..00000000 --- a/packages/server/tests/commands/name.spec.js +++ /dev/null @@ -1,46 +0,0 @@ -const assert = require('assert'); -const { db } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - describe('/name ', () => { - const NAME = 'Admin'; - - let ws; - before(async () => { - ws = await connect('admin'); - await (await db) - .collection('users') - .updateOne({ login: 'admin' }, { $set: { name: 'Johny' } }); - }); - - after(async () => { - await (await db).collection('users').updateOne({ login: 'admin' }, { $set: { name: 'Admin' } }); - ws.close(); - }); - - it('should change users name and infor about change', async () => { - const channel = await (await db).collection('channels').findOne({ name: 'main' }); - const [user, ret] = await ws.send({ - type: 'command:execute', - name: 'name', - args: [NAME], - context: { - channelId: channel._id.toHexString(), - }, - }); - assert.equal(ret.type, 'response'); - assert.equal(ret.status, 'ok'); - - const state = await (await db) - .collection('users') - .findOne({ login: 'admin' }); - assert.equal(state.name, NAME); - - assert.equal(user.type, 'user'); - assert.equal(user.name, NAME); - assert.equal(user.id, ws.userId); - }); - - it('should inform others about change'); - }); -}; diff --git a/packages/server/tests/commands/prompt.spec.js b/packages/server/tests/commands/prompt.spec.js deleted file mode 100644 index 0d8a47e2..00000000 --- a/packages/server/tests/commands/prompt.spec.js +++ /dev/null @@ -1,10 +0,0 @@ -/* eslint-disable no-unused-vars */ -const assert = require('assert'); -const { db } = require('../../src/infra/repositories'); - -module.exports = (connect) => { - describe('/prompt ', () => { - it('should work'); - it('crashing application because openai userId is string which cant be converted to ObjectId'); - }); -};