diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2019-01-23 12:15:27 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2019-01-23 12:15:27 +0900 |
| commit | 931f17c58932d4a9b11fdad5d338e3c91fa749a3 (patch) | |
| tree | 54f38fe57b612053b6d60990853b407eb45548eb /test/utils.ts | |
| parent | Update tslint.json (diff) | |
| download | sharkey-931f17c58932d4a9b11fdad5d338e3c91fa749a3.tar.gz sharkey-931f17c58932d4a9b11fdad5d338e3c91fa749a3.tar.bz2 sharkey-931f17c58932d4a9b11fdad5d338e3c91fa749a3.zip | |
Refactor: Separate some test files
Diffstat (limited to 'test/utils.ts')
| -rw-r--r-- | test/utils.ts | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/test/utils.ts b/test/utils.ts new file mode 100644 index 0000000000..06fc1e4051 --- /dev/null +++ b/test/utils.ts @@ -0,0 +1,82 @@ +import * as fs from 'fs'; +import * as http from 'http'; +import * as assert from 'chai'; + +export const async = (fn: Function) => (done: Function) => { + fn().then(() => { + done(); + }, (err: Error) => { + done(err); + }); +}; + +export const _request = (server: http.Server) => async (endpoint: string, params: any, me?: any): Promise<ChaiHttp.Response> => { + const auth = me ? { + i: me.token + } : {}; + + const res = await assert.request(server) + .post(endpoint) + .send(Object.assign(auth, params)); + + return res; +}; + +export const _signup = (request: ReturnType<typeof _request>) => async (params?: any): Promise<any> => { + const q = Object.assign({ + username: 'test', + password: 'test' + }, params); + + const res = await request('/signup', q); + + return res.body; +}; + +export const _post = (request: ReturnType<typeof _request>) => async (user: any, params?: any): Promise<any> => { + const q = Object.assign({ + text: 'test' + }, params); + + const res = await request('/notes/create', q, user); + + return res.body.createdNote; +}; + +export const _react = (request: ReturnType<typeof _request>) => async (user: any, note: any, reaction: string): Promise<any> => { + await request('/notes/reactions/create', { + noteId: note.id, + reaction: reaction + }, user); +}; + +export const _uploadFile = (server: http.Server) => async (user: any): Promise<any> => { + const res = await assert.request(server) + .post('/drive/files/create') + .field('i', user.token) + .attach('file', fs.readFileSync(__dirname + '/resources/Lenna.png'), 'Lenna.png'); + + return res.body; +}; + +export const resetDb = (db: any) => () => new Promise(res => { + // APIがなにかレスポンスを返した後に、後処理を行う場合があり、 + // レスポンスを受け取ってすぐデータベースをリセットすると + // その後処理と競合し(テスト自体は合格するものの)エラーがコンソールに出力され + // 見た目的に気持ち悪くなるので、後処理が終るのを待つために500msくらい待ってから + // データベースをリセットするようにする + setTimeout(async () => { + await Promise.all([ + db.get('users').drop(), + db.get('notes').drop(), + db.get('driveFiles.files').drop(), + db.get('driveFiles.chunks').drop(), + db.get('driveFolders').drop(), + db.get('apps').drop(), + db.get('accessTokens').drop(), + db.get('authSessions').drop() + ]); + + res(); + }, 500); +}); |