2019-04-07 15:50:36 +03:00
|
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
|
|
|
|
import * as assert from 'assert';
|
|
|
|
import * as childProcess from 'child_process';
|
2022-02-27 04:07:39 +02:00
|
|
|
import { Note } from '../src/models/entities/note.js';
|
2022-05-21 16:21:41 +03:00
|
|
|
import { async, signup, request, post, uploadFile, startServer, shutdownServer, initTestDb } from './utils.js';
|
2019-04-07 15:50:36 +03:00
|
|
|
|
|
|
|
describe('Note', () => {
|
|
|
|
let p: childProcess.ChildProcess;
|
|
|
|
let Notes: any;
|
|
|
|
|
|
|
|
let alice: any;
|
|
|
|
let bob: any;
|
|
|
|
|
2021-06-12 16:40:17 +03:00
|
|
|
before(async () => {
|
|
|
|
p = await startServer();
|
|
|
|
const connection = await initTestDb(true);
|
2020-01-09 07:35:04 +02:00
|
|
|
Notes = connection.getRepository(Note);
|
|
|
|
alice = await signup({ username: 'alice' });
|
|
|
|
bob = await signup({ username: 'bob' });
|
2021-06-12 16:40:17 +03:00
|
|
|
});
|
2019-04-07 15:50:36 +03:00
|
|
|
|
2021-06-05 08:54:07 +03:00
|
|
|
after(async () => {
|
|
|
|
await shutdownServer(p);
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
it('投稿できる', async(async () => {
|
|
|
|
const post = {
|
2022-05-21 16:21:41 +03:00
|
|
|
text: 'test',
|
2019-04-07 15:50:36 +03: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 uploadFile(alice);
|
|
|
|
|
|
|
|
const res = await request('/notes/create', {
|
2022-05-21 16:21:41 +03:00
|
|
|
fileIds: [file.id],
|
2019-04-07 15:50:36 +03: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 uploadFile(bob);
|
|
|
|
|
|
|
|
const res = await request('/notes/create', {
|
|
|
|
text: 'test',
|
2022-05-21 16:21:41 +03:00
|
|
|
fileIds: [file.id],
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
fileIds: ['000000000000000000000000'],
|
2019-04-07 15:50:36 +03: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 () => {
|
|
|
|
const res = await request('/notes/create', {
|
2022-05-21 16:21:41 +03:00
|
|
|
fileIds: ['kyoppie'],
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('返信できる', async(async () => {
|
|
|
|
const bobPost = await post(bob, {
|
2022-05-21 16:21:41 +03:00
|
|
|
text: 'foo',
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const alicePost = {
|
|
|
|
text: 'bar',
|
2022-05-21 16:21:41 +03:00
|
|
|
replyId: bobPost.id,
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
text: 'test',
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const alicePost = {
|
2022-05-21 16:21:41 +03:00
|
|
|
renoteId: bobPost.id,
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
text: 'test',
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
const alicePost = {
|
|
|
|
text: 'test',
|
2022-05-21 16:21:41 +03:00
|
|
|
renoteId: bobPost.id,
|
2019-04-07 15:50:36 +03: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 = {
|
2022-05-21 16:21:41 +03:00
|
|
|
text: '!'.repeat(500),
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
const res = await request('/notes/create', post, alice);
|
|
|
|
assert.strictEqual(res.status, 200);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('文字数オーバーで怒られる', async(async () => {
|
|
|
|
const post = {
|
2022-05-21 16:21:41 +03:00
|
|
|
text: '!'.repeat(501),
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
const res = await request('/notes/create', post, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('存在しないリプライ先で怒られる', async(async () => {
|
|
|
|
const post = {
|
|
|
|
text: 'test',
|
2022-05-21 16:21:41 +03:00
|
|
|
replyId: '000000000000000000000000',
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
const res = await request('/notes/create', post, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('存在しないrenote対象で怒られる', async(async () => {
|
|
|
|
const post = {
|
2022-05-21 16:21:41 +03:00
|
|
|
renoteId: '000000000000000000000000',
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
replyId: 'foo',
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
const res = await request('/notes/create', post, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('不正なrenote対象IDで怒られる', async(async () => {
|
|
|
|
const post = {
|
2022-05-21 16:21:41 +03:00
|
|
|
renoteId: 'foo',
|
2019-04-07 15:50:36 +03:00
|
|
|
};
|
|
|
|
const res = await request('/notes/create', post, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('存在しないユーザーにメンションできる', async(async () => {
|
|
|
|
const post = {
|
2022-05-21 16:21:41 +03:00
|
|
|
text: '@ghost yo',
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
text: '@bob @bob @bob yo',
|
2019-04-07 15:50:36 +03: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.findOne(res.body.createdNote.id);
|
|
|
|
assert.deepStrictEqual(noteDoc.mentions, [bob.id]);
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('notes/create', () => {
|
|
|
|
it('投票を添付できる', async(async () => {
|
|
|
|
const res = await request('/notes/create', {
|
|
|
|
text: 'test',
|
|
|
|
poll: {
|
2022-05-21 16:21:41 +03:00
|
|
|
choices: ['foo', 'bar'],
|
|
|
|
},
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
poll: {},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('投票の選択肢が無くて怒られる (空の配列)', async(async () => {
|
|
|
|
const res = await request('/notes/create', {
|
|
|
|
poll: {
|
2022-05-21 16:21:41 +03:00
|
|
|
choices: [],
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('投票の選択肢が1つで怒られる', async(async () => {
|
|
|
|
const res = await request('/notes/create', {
|
|
|
|
poll: {
|
2022-05-21 16:21:41 +03:00
|
|
|
choices: ['Strawberry Pasta'],
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('投票できる', async(async () => {
|
|
|
|
const { body } = await request('/notes/create', {
|
|
|
|
text: 'test',
|
|
|
|
poll: {
|
2022-05-21 16:21:41 +03:00
|
|
|
choices: ['sakura', 'izumi', 'ako'],
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
const res = await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 1,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
assert.strictEqual(res.status, 204);
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('複数投票できない', async(async () => {
|
|
|
|
const { body } = await request('/notes/create', {
|
|
|
|
text: 'test',
|
|
|
|
poll: {
|
2022-05-21 16:21:41 +03:00
|
|
|
choices: ['sakura', 'izumi', 'ako'],
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 0,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
const res = await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 2,
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
multiple: true,
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 0,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 1,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
const res = await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 2,
|
2019-04-07 15:50:36 +03: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 16:21:41 +03:00
|
|
|
expiredAfter: 1,
|
|
|
|
},
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
await new Promise(x => setTimeout(x, 2));
|
|
|
|
|
|
|
|
const res = await request('/notes/polls/vote', {
|
|
|
|
noteId: body.createdNote.id,
|
2022-05-21 16:21:41 +03:00
|
|
|
choice: 1,
|
2019-04-07 15:50:36 +03:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
assert.strictEqual(res.status, 400);
|
|
|
|
}));
|
|
|
|
});
|
2022-03-22 15:48:33 +02:00
|
|
|
|
|
|
|
describe('notes/delete', () => {
|
|
|
|
it('delete a reply', async(async () => {
|
|
|
|
const mainNoteRes = await request('/notes/create', {
|
|
|
|
text: 'main post',
|
|
|
|
}, alice);
|
|
|
|
const replyOneRes = await request('/notes/create', {
|
|
|
|
text: 'reply one',
|
2022-05-21 16:21:41 +03:00
|
|
|
replyId: mainNoteRes.body.createdNote.id,
|
2022-03-22 15:48:33 +02:00
|
|
|
}, alice);
|
|
|
|
const replyTwoRes = await request('/notes/create', {
|
|
|
|
text: 'reply two',
|
2022-05-21 16:21:41 +03:00
|
|
|
replyId: mainNoteRes.body.createdNote.id,
|
2022-03-22 15:48:33 +02:00
|
|
|
}, alice);
|
|
|
|
|
|
|
|
const deleteOneRes = await request('/notes/delete', {
|
|
|
|
noteId: replyOneRes.body.createdNote.id,
|
|
|
|
}, alice);
|
|
|
|
|
|
|
|
assert.strictEqual(deleteOneRes.status, 204);
|
2022-05-21 16:21:41 +03:00
|
|
|
let mainNote = await Notes.findOne({ id: mainNoteRes.body.createdNote.id });
|
2022-03-22 15:48:33 +02:00
|
|
|
assert.strictEqual(mainNote.repliesCount, 1);
|
|
|
|
|
|
|
|
const deleteTwoRes = await request('/notes/delete', {
|
|
|
|
noteId: replyTwoRes.body.createdNote.id,
|
|
|
|
}, alice);
|
|
|
|
|
|
|
|
assert.strictEqual(deleteTwoRes.status, 204);
|
2022-05-21 16:21:41 +03:00
|
|
|
mainNote = await Notes.findOne({ id: mainNoteRes.body.createdNote.id });
|
2022-03-22 15:48:33 +02:00
|
|
|
assert.strictEqual(mainNote.repliesCount, 0);
|
|
|
|
}));
|
|
|
|
});
|
2019-04-07 15:50:36 +03:00
|
|
|
});
|