Merge branch 'develop' into nya

This commit is contained in:
Acid Chicken (硫酸鶏) 2019-02-08 01:12:17 +09:00 committed by GitHub
commit 936df45e06
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
408 changed files with 7038 additions and 3463 deletions

572
test/api-visibility.ts Normal file
View file

@ -0,0 +1,572 @@
/*
* Tests of API (visibility)
*
* How to run the tests:
* > mocha test/api-visibility.ts --require ts-node/register
*
* To specify test:
* > mocha test/api-visibility.ts --require ts-node/register -g 'test name'
*/
import * as http from 'http';
import * as assert from 'chai';
import { async, _signup, _request, _uploadFile, _post, _react, resetDb } from './utils';
const expect = assert.expect;
//#region process
Error.stackTraceLimit = Infinity;
// During the test the env variable is set to test
process.env.NODE_ENV = 'test';
// Display detail of unhandled promise rejection
process.on('unhandledRejection', console.dir);
//#endregion
const app = require('../built/server/api').default;
const db = require('../built/db/mongodb').default;
const server = http.createServer(app.callback());
//#region Utilities
const request = _request(server);
const signup = _signup(request);
const post = _post(request);
//#endregion
describe('API visibility', () => {
// Reset database each test
before(resetDb(db));
after(() => {
server.close();
});
describe('Note visibility', async () => {
//#region vars
/** ヒロイン */
let alice: any;
/** フォロワー */
let follower: any;
/** 非フォロワー */
let other: any;
/** 非フォロワーでもリプライやメンションをされた人 */
let target: any;
/** public-post */
let pub: any;
/** home-post */
let home: any;
/** followers-post */
let fol: any;
/** specified-post */
let spe: any;
/** private-post */
let pri: any;
/** public-reply to target's post */
let pubR: any;
/** home-reply to target's post */
let homeR: any;
/** followers-reply to target's post */
let folR: any;
/** specified-reply to target's post */
let speR: any;
/** private-reply to target's post */
let priR: any;
/** public-mention to target */
let pubM: any;
/** home-mention to target */
let homeM: any;
/** followers-mention to target */
let folM: any;
/** specified-mention to target */
let speM: any;
/** private-mention to target */
let priM: any;
/** reply target post */
let tgt: any;
//#endregion
const show = async (noteId: any, by: any) => {
return await request('/notes/show', {
noteId
}, by);
};
before(async () => {
//#region prepare
// signup
alice = await signup({ username: 'alice' });
follower = await signup({ username: 'follower' });
other = await signup({ username: 'other' });
target = await signup({ username: 'target' });
// follow alice <= follower
await request('/following/create', { userId: alice.id }, follower);
// normal posts
pub = await post(alice, { text: 'x', visibility: 'public' });
home = await post(alice, { text: 'x', visibility: 'home' });
fol = await post(alice, { text: 'x', visibility: 'followers' });
spe = await post(alice, { text: 'x', visibility: 'specified', visibleUserIds: [target.id] });
pri = await post(alice, { text: 'x', visibility: 'private' });
// replies
tgt = await post(target, { text: 'y', visibility: 'public' });
pubR = await post(alice, { text: 'x', replyId: tgt.id, visibility: 'public' });
homeR = await post(alice, { text: 'x', replyId: tgt.id, visibility: 'home' });
folR = await post(alice, { text: 'x', replyId: tgt.id, visibility: 'followers' });
speR = await post(alice, { text: 'x', replyId: tgt.id, visibility: 'specified' });
priR = await post(alice, { text: 'x', replyId: tgt.id, visibility: 'private' });
// mentions
pubM = await post(alice, { text: '@target x', replyId: tgt.id, visibility: 'public' });
homeM = await post(alice, { text: '@target x', replyId: tgt.id, visibility: 'home' });
folM = await post(alice, { text: '@target x', replyId: tgt.id, visibility: 'followers' });
speM = await post(alice, { text: '@target x', replyId: tgt.id, visibility: 'specified' });
priM = await post(alice, { text: '@target x', replyId: tgt.id, visibility: 'private' });
//#endregion
});
//#region show post
// public
it('[show] public-postを自分が見れる', async(async () => {
const res = await show(pub.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-postをフォロワーが見れる', async(async () => {
const res = await show(pub.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-postを非フォロワーが見れる', async(async () => {
const res = await show(pub.id, other);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-postを未認証が見れる', async(async () => {
const res = await show(pub.id, null);
expect(res.body).have.property('text').eql('x');
}));
// home
it('[show] home-postを自分が見れる', async(async () => {
const res = await show(home.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-postをフォロワーが見れる', async(async () => {
const res = await show(home.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-postを非フォロワーが見れる', async(async () => {
const res = await show(home.id, other);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-postを未認証が見れる', async(async () => {
const res = await show(home.id, null);
expect(res.body).have.property('text').eql('x');
}));
// followers
it('[show] followers-postを自分が見れる', async(async () => {
const res = await show(fol.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] followers-postをフォロワーが見れる', async(async () => {
const res = await show(fol.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] followers-postを非フォロワーが見れない', async(async () => {
const res = await show(fol.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] followers-postを未認証が見れない', async(async () => {
const res = await show(fol.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// specified
it('[show] specified-postを自分が見れる', async(async () => {
const res = await show(spe.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] specified-postを指定ユーザーが見れる', async(async () => {
const res = await show(spe.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] specified-postをフォロワーが見れない', async(async () => {
const res = await show(spe.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-postを非フォロワーが見れない', async(async () => {
const res = await show(spe.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-postを未認証が見れない', async(async () => {
const res = await show(spe.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// private
it('[show] private-postを自分が見れる', async(async () => {
const res = await show(pri.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] private-postをフォロワーが見れない', async(async () => {
const res = await show(pri.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-postを非フォロワーが見れない', async(async () => {
const res = await show(pri.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-postを未認証が見れない', async(async () => {
const res = await show(pri.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
//#endregion
//#region show reply
// public
it('[show] public-replyを自分が見れる', async(async () => {
const res = await show(pubR.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-replyをされた人が見れる', async(async () => {
const res = await show(pubR.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-replyをフォロワーが見れる', async(async () => {
const res = await show(pubR.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-replyを非フォロワーが見れる', async(async () => {
const res = await show(pubR.id, other);
expect(res.body).have.property('text').eql('x');
}));
it('[show] public-replyを未認証が見れる', async(async () => {
const res = await show(pubR.id, null);
expect(res.body).have.property('text').eql('x');
}));
// home
it('[show] home-replyを自分が見れる', async(async () => {
const res = await show(homeR.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-replyをされた人が見れる', async(async () => {
const res = await show(homeR.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-replyをフォロワーが見れる', async(async () => {
const res = await show(homeR.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-replyを非フォロワーが見れる', async(async () => {
const res = await show(homeR.id, other);
expect(res.body).have.property('text').eql('x');
}));
it('[show] home-replyを未認証が見れる', async(async () => {
const res = await show(homeR.id, null);
expect(res.body).have.property('text').eql('x');
}));
// followers
it('[show] followers-replyを自分が見れる', async(async () => {
const res = await show(folR.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] followers-replyを非フォロワーでもリプライされていれば見れる', async(async () => {
const res = await show(folR.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] followers-replyをフォロワーが見れる', async(async () => {
const res = await show(folR.id, follower);
expect(res.body).have.property('text').eql('x');
}));
it('[show] followers-replyを非フォロワーが見れない', async(async () => {
const res = await show(folR.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] followers-replyを未認証が見れない', async(async () => {
const res = await show(folR.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// specified
it('[show] specified-replyを自分が見れる', async(async () => {
const res = await show(speR.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] specified-replyを指定ユーザーが見れる', async(async () => {
const res = await show(speR.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] specified-replyをされた人が指定されてなくても見れる', async(async () => {
const res = await show(speR.id, target);
expect(res.body).have.property('text').eql('x');
}));
it('[show] specified-replyをフォロワーが見れない', async(async () => {
const res = await show(speR.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-replyを非フォロワーが見れない', async(async () => {
const res = await show(speR.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-replyを未認証が見れない', async(async () => {
const res = await show(speR.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// private
it('[show] private-replyを自分が見れる', async(async () => {
const res = await show(priR.id, alice);
expect(res.body).have.property('text').eql('x');
}));
it('[show] private-replyをフォロワーが見れない', async(async () => {
const res = await show(priR.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-replyを非フォロワーが見れない', async(async () => {
const res = await show(priR.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-replyを未認証が見れない', async(async () => {
const res = await show(priR.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
//#endregion
//#region show mention
// public
it('[show] public-mentionを自分が見れる', async(async () => {
const res = await show(pubM.id, alice);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] public-mentionをされた人が見れる', async(async () => {
const res = await show(pubM.id, target);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] public-mentionをフォロワーが見れる', async(async () => {
const res = await show(pubM.id, follower);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] public-mentionを非フォロワーが見れる', async(async () => {
const res = await show(pubM.id, other);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] public-mentionを未認証が見れる', async(async () => {
const res = await show(pubM.id, null);
expect(res.body).have.property('text').eql('@target x');
}));
// home
it('[show] home-mentionを自分が見れる', async(async () => {
const res = await show(homeM.id, alice);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] home-mentionをされた人が見れる', async(async () => {
const res = await show(homeM.id, target);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] home-mentionをフォロワーが見れる', async(async () => {
const res = await show(homeM.id, follower);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] home-mentionを非フォロワーが見れる', async(async () => {
const res = await show(homeM.id, other);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] home-mentionを未認証が見れる', async(async () => {
const res = await show(homeM.id, null);
expect(res.body).have.property('text').eql('@target x');
}));
// followers
it('[show] followers-mentionを自分が見れる', async(async () => {
const res = await show(folM.id, alice);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] followers-mentionを非フォロワーでもメンションされていれば見れる', async(async () => {
const res = await show(folM.id, target);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] followers-mentionをフォロワーが見れる', async(async () => {
const res = await show(folM.id, follower);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] followers-mentionを非フォロワーが見れない', async(async () => {
const res = await show(folM.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] followers-mentionを未認証が見れない', async(async () => {
const res = await show(folM.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// specified
it('[show] specified-mentionを自分が見れる', async(async () => {
const res = await show(speM.id, alice);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] specified-mentionを指定ユーザーが見れる', async(async () => {
const res = await show(speM.id, target);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] specified-mentionをされた人が指定されてなくても見れる', async(async () => {
const res = await show(speM.id, target);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] specified-mentionをフォロワーが見れない', async(async () => {
const res = await show(speM.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-mentionを非フォロワーが見れない', async(async () => {
const res = await show(speM.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] specified-mentionを未認証が見れない', async(async () => {
const res = await show(speM.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
// private
it('[show] private-mentionを自分が見れる', async(async () => {
const res = await show(priM.id, alice);
expect(res.body).have.property('text').eql('@target x');
}));
it('[show] private-mentionをフォロワーが見れない', async(async () => {
const res = await show(priM.id, follower);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-mentionを非フォロワーが見れない', async(async () => {
const res = await show(priM.id, other);
expect(res.body).have.property('isHidden').eql(true);
}));
it('[show] private-mentionを未認証が見れない', async(async () => {
const res = await show(priM.id, null);
expect(res.body).have.property('isHidden').eql(true);
}));
//#endregion
//#region HTL
it('[HTL] public-post が 自分が見れる', async(async () => {
const res = await request('/notes/timeline', { limit: 100 }, alice);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == pub.id);
expect(notes[0]).have.property('text').eql('x');
}));
it('[HTL] public-post が 非フォロワーから見れない', async(async () => {
const res = await request('/notes/timeline', { limit: 100 }, other);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == pub.id);
expect(notes).length(0);
}));
it('[HTL] followers-post が フォロワーから見れる', async(async () => {
const res = await request('/notes/timeline', { limit: 100 }, follower);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == fol.id);
expect(notes[0]).have.property('text').eql('x');
}));
//#endregion
//#region RTL
it('[replies] followers-reply が フォロワーから見れる', async(async () => {
const res = await request('/notes/replies', { noteId: tgt.id, limit: 100 }, follower);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == folR.id);
expect(notes[0]).have.property('text').eql('x');
}));
it('[replies] followers-reply が 非フォロワー (リプライ先ではない) から見れない', async(async () => {
const res = await request('/notes/replies', { noteId: tgt.id, limit: 100 }, other);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == folR.id);
expect(notes).length(0);
}));
it('[replies] followers-reply が 非フォロワー (リプライ先である) から見れる', async(async () => {
const res = await request('/notes/replies', { noteId: tgt.id, limit: 100 }, target);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == folR.id);
expect(notes[0]).have.property('text').eql('x');
}));
//#endregion
//#region MTL
it('[mentions] followers-reply が 非フォロワー (リプライ先である) から見れる', async(async () => {
const res = await request('/notes/mentions', { limit: 100 }, target);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == folR.id);
expect(notes[0]).have.property('text').eql('x');
}));
it('[mentions] followers-mention が 非フォロワー (メンション先である) から見れる', async(async () => {
const res = await request('/notes/mentions', { limit: 100 }, target);
expect(res).have.status(200);
const notes = res.body.filter((n: any) => n.id == folM.id);
expect(notes[0]).have.property('text').eql('@target x');
}));
//#endregion
});
});

View file

@ -13,7 +13,6 @@ import * as fs from 'fs';
import * as assert from 'chai';
import { async, _signup, _request, _uploadFile, _post, _react, resetDb } from './utils';
assert.use(require('chai-http'));
const expect = assert.expect;
//#region process
@ -1217,4 +1216,54 @@ describe('API', () => {
expect(res).have.status(400);
}));
});
describe('notes/replies', () => {
it('自分に閲覧権限のない投稿は含まれない', async(async () => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
const carol = await signup({ username: 'carol' });
const alicePost = await post(alice, {
text: 'foo'
});
await post(bob, {
replyId: alicePost.id,
text: 'bar',
visibility: 'specified',
visibleUserIds: [alice.id]
});
const res = await request('/notes/replies', {
noteId: alicePost.id
}, carol);
expect(res).have.status(200);
expect(res.body).be.a('array');
expect(res.body).length(0);
}));
});
describe('notes/timeline', () => {
it('フォロワー限定投稿が含まれる', async(async () => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
await request('/following/create', {
userId: alice.id
}, bob);
const alicePost = await post(alice, {
text: 'foo',
visibility: 'followers'
});
const res = await request('/notes/timeline', {}, bob);
expect(res).have.status(200);
expect(res.body).be.a('array');
expect(res.body).length(1);
expect(res.body[0].id).equals(alicePost.id);
}));
});
});

View file

@ -1,7 +1,7 @@
import * as assert from 'assert';
import extractMentions from '../src/misc/extract-mentions';
import parse from '../src/mfm/parse';
import { parse } from '../src/mfm/parse';
describe('Extract mentions', () => {
it('simple', () => {

File diff suppressed because it is too large Load diff

28
test/prelude/maybe.ts Normal file
View file

@ -0,0 +1,28 @@
/*
* Tests of Maybe
*
* How to run the tests:
* > mocha test/prelude/maybe.ts --require ts-node/register
*
* To specify test:
* > mocha test/prelude/maybe.ts --require ts-node/register -g 'test name'
*/
import * as assert from 'assert';
import { just, nothing } from '../../src/prelude/maybe';
describe('just', () => {
it('has a value', () => {
assert.deepStrictEqual(just(3).isJust(), true);
});
it('has the inverse called get', () => {
assert.deepStrictEqual(just(3).get(), 3);
});
});
describe('nothing', () => {
it('has no value', () => {
assert.deepStrictEqual(nothing().isJust(), false);
});
});

View file

@ -10,12 +10,9 @@
import * as http from 'http';
import * as WebSocket from 'ws';
import * as assert from 'chai';
import * as assert from 'assert';
import { _signup, _request, _uploadFile, _post, _react, resetDb } from './utils';
assert.use(require('chai-http'));
const expect = assert.expect;
//#region process
Error.stackTraceLimit = Infinity;
@ -35,6 +32,7 @@ const apiServer = http.createServer(app.callback());
//#region Utilities
const request = _request(apiServer);
const signup = _signup(request);
const post = _post(request);
//#endregion
describe('Streaming', () => {
@ -45,37 +43,106 @@ describe('Streaming', () => {
server.close();
});
it('投稿がタイムラインに流れる', done => {
it('投稿がタイムラインに流れる', () => new Promise(async done => {
const post = {
text: 'foo'
};
signup().then(me => {
const ws = new WebSocket(`ws://localhost/streaming?i=${me.token}`);
const me = await signup();
const ws = new WebSocket(`ws://localhost/streaming?i=${me.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'note') {
expect(msg.body.body.text).eql(post.text);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', post, me);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'note') {
assert.deepStrictEqual(msg.body.body.text, post.text);
ws.close();
done();
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: 'a',
pong: true
}
}));
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', post, me);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'homeTimeline',
id: 'a',
pong: true
}
}));
});
});
}));
it('mention event', () => new Promise(async done => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
const aliceNote = {
text: 'foo @bob bar'
};
const ws = new WebSocket(`ws://localhost/streaming?i=${bob.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'mention') {
assert.deepStrictEqual(msg.body.body.text, aliceNote.text);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', aliceNote, alice);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: 'a',
pong: true
}
}));
});
}));
it('renote event', () => new Promise(async done => {
const alice = await signup({ username: 'alice' });
const bob = await signup({ username: 'bob' });
const bobNote = await post(bob, {
text: 'foo'
});
const ws = new WebSocket(`ws://localhost/streaming?i=${bob.token}`);
ws.on('open', () => {
ws.on('message', data => {
const msg = JSON.parse(data.toString());
if (msg.type == 'channel' && msg.body.id == 'a') {
if (msg.body.type == 'renote') {
assert.deepStrictEqual(msg.body.body.renoteId, bobNote.id);
ws.close();
done();
}
} else if (msg.type == 'connected' && msg.body.id == 'a') {
request('/notes/create', {
renoteId: bobNote.id
}, alice);
}
});
ws.send(JSON.stringify({
type: 'connect',
body: {
channel: 'main',
id: 'a',
pong: true
}
}));
});
}));
});

View file

@ -1,6 +1,7 @@
import * as fs from 'fs';
import * as http from 'http';
import * as assert from 'chai';
assert.use(require('chai-http'));
export const async = (fn: Function) => (done: Function) => {
fn().then(() => {