Files
misskey/packages/backend/test/note.ts
T

371 lines
10 KiB
TypeScript
Raw Normal View History

2019-04-07 21:50:36 +09:00
process.env.NODE_ENV = 'test';
import * as assert from 'assert';
import * as childProcess from 'child_process';
2022-02-27 11:07:39 +09:00
import { Note } from '../src/models/entities/note.js';
import { async, signup, request, post, uploadUrl, startServer, shutdownServer, initTestDb, api } from './utils.js';
2019-04-07 21:50:36 +09:00
describe('Note', () => {
let p: childProcess.ChildProcess;
let Notes: any;
let alice: any;
let bob: any;
before(async () => {
p = await startServer();
const connection = await initTestDb(true);
2020-01-09 14:35:04 +09:00
Notes = connection.getRepository(Note);
alice = await signup({ username: 'alice' });
bob = await signup({ username: 'bob' });
});
2019-04-07 21:50:36 +09:00
after(async () => {
await shutdownServer(p);
2019-04-07 21:50:36 +09:00
});
it('投稿できる', async(async () => {
const post = {
2022-05-21 22:21:41 +09:00
text: 'test',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.text, post.text);
}));
it('ファイルを添付できる', async(async () => {
const file = await uploadUrl(alice, 'https://raw.githubusercontent.com/misskey-dev/misskey/develop/packages/backend/test/resources/Lenna.jpg');
2019-04-07 21:50:36 +09:00
const res = await request('/notes/create', {
2022-05-21 22:21:41 +09:00
fileIds: [file.id],
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.deepStrictEqual(res.body.createdNote.fileIds, [file.id]);
}));
it('他人のファイルは無視', async(async () => {
const file = await uploadUrl(bob, 'https://raw.githubusercontent.com/misskey-dev/misskey/develop/packages/backend/test/resources/Lenna.jpg');
2019-04-07 21:50:36 +09:00
const res = await request('/notes/create', {
text: 'test',
2022-05-21 22:21:41 +09:00
fileIds: [file.id],
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.deepStrictEqual(res.body.createdNote.fileIds, []);
}));
it('存在しないファイルは無視', async(async () => {
const res = await request('/notes/create', {
text: 'test',
2022-05-21 22:21:41 +09:00
fileIds: ['000000000000000000000000'],
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.deepStrictEqual(res.body.createdNote.fileIds, []);
}));
it('不正なファイルIDは無視', async(async () => {
2019-04-07 21:50:36 +09:00
const res = await request('/notes/create', {
2022-05-21 22:21:41 +09:00
fileIds: ['kyoppie'],
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.deepStrictEqual(res.body.createdNote.fileIds, []);
2019-04-07 21:50:36 +09:00
}));
it('返信できる', async(async () => {
const bobPost = await post(bob, {
2022-05-21 22:21:41 +09:00
text: 'foo',
2019-04-07 21:50:36 +09:00
});
const alicePost = {
text: 'bar',
2022-05-21 22:21:41 +09:00
replyId: bobPost.id,
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', alicePost, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.text, alicePost.text);
assert.strictEqual(res.body.createdNote.replyId, alicePost.replyId);
assert.strictEqual(res.body.createdNote.reply.text, bobPost.text);
}));
it('renoteできる', async(async () => {
const bobPost = await post(bob, {
2022-05-21 22:21:41 +09:00
text: 'test',
2019-04-07 21:50:36 +09:00
});
const alicePost = {
2022-05-21 22:21:41 +09:00
renoteId: bobPost.id,
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', alicePost, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.renoteId, alicePost.renoteId);
assert.strictEqual(res.body.createdNote.renote.text, bobPost.text);
}));
it('引用renoteできる', async(async () => {
const bobPost = await post(bob, {
2022-05-21 22:21:41 +09:00
text: 'test',
2019-04-07 21:50:36 +09:00
});
const alicePost = {
text: 'test',
2022-05-21 22:21:41 +09:00
renoteId: bobPost.id,
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', alicePost, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.text, alicePost.text);
assert.strictEqual(res.body.createdNote.renoteId, alicePost.renoteId);
assert.strictEqual(res.body.createdNote.renote.text, bobPost.text);
}));
it('文字数ぎりぎりで怒られない', async(async () => {
const post = {
text: '!'.repeat(3000),
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 200);
}));
it('文字数オーバーで怒られる', async(async () => {
const post = {
text: '!'.repeat(3001),
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 400);
}));
it('存在しないリプライ先で怒られる', async(async () => {
const post = {
text: 'test',
2022-05-21 22:21:41 +09:00
replyId: '000000000000000000000000',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 400);
}));
it('存在しないrenote対象で怒られる', async(async () => {
const post = {
2022-05-21 22:21:41 +09:00
renoteId: '000000000000000000000000',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 400);
}));
it('不正なリプライ先IDで怒られる', async(async () => {
const post = {
text: 'test',
2022-05-21 22:21:41 +09:00
replyId: 'foo',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 400);
}));
it('不正なrenote対象IDで怒られる', async(async () => {
const post = {
2022-05-21 22:21:41 +09:00
renoteId: 'foo',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 400);
}));
it('存在しないユーザーにメンションできる', async(async () => {
const post = {
2022-05-21 22:21:41 +09:00
text: '@ghost yo',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.text, post.text);
}));
it('同じユーザーに複数メンションしても内部的にまとめられる', async(async () => {
const post = {
2022-05-21 22:21:41 +09:00
text: '@bob @bob @bob yo',
2019-04-07 21:50:36 +09:00
};
const res = await request('/notes/create', post, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.text, post.text);
const noteDoc = await Notes.findOneBy({ id: res.body.createdNote.id });
2019-04-07 21:50:36 +09:00
assert.deepStrictEqual(noteDoc.mentions, [bob.id]);
}));
describe('notes/create', () => {
it('投票を添付できる', async(async () => {
const res = await request('/notes/create', {
text: 'test',
poll: {
2022-05-21 22:21:41 +09:00
choices: ['foo', 'bar'],
},
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 200);
assert.strictEqual(typeof res.body === 'object' && !Array.isArray(res.body), true);
assert.strictEqual(res.body.createdNote.poll != null, true);
}));
it('投票の選択肢が無くて怒られる', async(async () => {
const res = await request('/notes/create', {
2022-05-21 22:21:41 +09:00
poll: {},
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 400);
}));
it('投票の選択肢が無くて怒られる (空の配列)', async(async () => {
const res = await request('/notes/create', {
poll: {
2022-05-21 22:21:41 +09:00
choices: [],
},
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 400);
}));
it('投票の選択肢が1つで怒られる', async(async () => {
const res = await request('/notes/create', {
poll: {
2022-05-21 22:21:41 +09:00
choices: ['Strawberry Pasta'],
},
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 400);
}));
it('投票できる', async(async () => {
const { body } = await request('/notes/create', {
text: 'test',
poll: {
2022-05-21 22:21:41 +09:00
choices: ['sakura', 'izumi', 'ako'],
},
2019-04-07 21:50:36 +09:00
}, alice);
const res = await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 1,
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 204);
}));
it('複数投票できない', async(async () => {
const { body } = await request('/notes/create', {
text: 'test',
poll: {
2022-05-21 22:21:41 +09:00
choices: ['sakura', 'izumi', 'ako'],
},
2019-04-07 21:50:36 +09:00
}, alice);
await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 0,
2019-04-07 21:50:36 +09:00
}, alice);
const res = await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 2,
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 400);
}));
it('許可されている場合は複数投票できる', async(async () => {
const { body } = await request('/notes/create', {
text: 'test',
poll: {
choices: ['sakura', 'izumi', 'ako'],
2022-05-21 22:21:41 +09:00
multiple: true,
},
2019-04-07 21:50:36 +09:00
}, alice);
await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 0,
2019-04-07 21:50:36 +09:00
}, alice);
await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 1,
2019-04-07 21:50:36 +09:00
}, alice);
const res = await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 2,
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 204);
}));
it('締め切られている場合は投票できない', async(async () => {
const { body } = await request('/notes/create', {
text: 'test',
poll: {
choices: ['sakura', 'izumi', 'ako'],
2022-05-21 22:21:41 +09:00
expiredAfter: 1,
},
2019-04-07 21:50:36 +09:00
}, alice);
await new Promise(x => setTimeout(x, 2));
const res = await request('/notes/polls/vote', {
noteId: body.createdNote.id,
2022-05-21 22:21:41 +09:00
choice: 1,
2019-04-07 21:50:36 +09:00
}, alice);
assert.strictEqual(res.status, 400);
}));
});
2022-03-22 14:48:33 +01:00
describe('notes/delete', () => {
it('delete a reply', async(async () => {
const mainNoteRes = await api('notes/create', {
2022-03-22 14:48:33 +01:00
text: 'main post',
}, alice);
const replyOneRes = await api('notes/create', {
2022-03-22 14:48:33 +01:00
text: 'reply one',
2022-05-21 22:21:41 +09:00
replyId: mainNoteRes.body.createdNote.id,
2022-03-22 14:48:33 +01:00
}, alice);
const replyTwoRes = await api('notes/create', {
2022-03-22 14:48:33 +01:00
text: 'reply two',
2022-05-21 22:21:41 +09:00
replyId: mainNoteRes.body.createdNote.id,
2022-03-22 14:48:33 +01:00
}, alice);
const deleteOneRes = await api('notes/delete', {
2022-03-22 14:48:33 +01:00
noteId: replyOneRes.body.createdNote.id,
}, alice);
assert.strictEqual(deleteOneRes.status, 204);
let mainNote = await Notes.findOneBy({ id: mainNoteRes.body.createdNote.id });
2022-03-22 14:48:33 +01:00
assert.strictEqual(mainNote.repliesCount, 1);
const deleteTwoRes = await api('notes/delete', {
2022-03-22 14:48:33 +01:00
noteId: replyTwoRes.body.createdNote.id,
}, alice);
assert.strictEqual(deleteTwoRes.status, 204);
mainNote = await Notes.findOneBy({ id: mainNoteRes.body.createdNote.id });
2022-03-22 14:48:33 +01:00
assert.strictEqual(mainNote.repliesCount, 0);
}));
});
2019-04-07 21:50:36 +09:00
});