Skip to content

Commit

Permalink
test: refactoring tests from old format to new
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Russak committed Dec 16, 2023
1 parent bf1fcd2 commit 6b8777d
Show file tree
Hide file tree
Showing 16 changed files with 216 additions and 255 deletions.
10 changes: 10 additions & 0 deletions packages/repo/src/repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export default class Repo<Query, Model, MongoModel> {
.then(this.serializer.deserializeInsertedId);
}

async createMany(data: Model[]): Promise<Id[]> {
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)
Expand All @@ -54,6 +58,12 @@ export default class Repo<Query, Model, MongoModel> {
.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)
Expand Down
76 changes: 52 additions & 24 deletions packages/server/src/app/actions/message/tests/pins.spec.js
Original file line number Diff line number Diff line change
@@ -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);
});
};
});
77 changes: 77 additions & 0 deletions packages/server/src/app/actions/message/tests/seeds.js
Original file line number Diff line number Diff line change
@@ -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'),
},
]);
},
};
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
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: [],
context: {
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);
Expand Down
33 changes: 33 additions & 0 deletions packages/server/src/app/commands/tests/me.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
38 changes: 38 additions & 0 deletions packages/server/src/app/commands/tests/name.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const assert = require('assert');
const api = require('../../actions/tests/api');

describe('/name <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');
});
2 changes: 1 addition & 1 deletion packages/server/src/app/common/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 0 additions & 5 deletions packages/server/tests/actions/command.spec.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/server/tests/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module.exports = (connect) => {
require('./message.spec')(connect);
require('./setupFcm.spec')(connect);
require('./typing.spec')(connect);
require('./command.spec')(connect);
});
};
9 changes: 0 additions & 9 deletions packages/server/tests/commands/ai.spec.js

This file was deleted.

9 changes: 0 additions & 9 deletions packages/server/tests/commands/index.js

This file was deleted.

Loading

0 comments on commit 6b8777d

Please sign in to comment.