From 99da4f9839e49c339624e16390a487ed102e96c2 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 16 Oct 2018 08:54:36 +0900 Subject: [PATCH] Add some tests and some fixes --- src/server/api/endpoints/i/update.ts | 2 +- src/server/api/private/signup.ts | 10 ++++- test/api.ts | 67 +++++++++++++++++++++++++--- 3 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/server/api/endpoints/i/update.ts b/src/server/api/endpoints/i/update.ts index 77825a4aa..7b8431f0e 100644 --- a/src/server/api/endpoints/i/update.ts +++ b/src/server/api/endpoints/i/update.ts @@ -101,7 +101,7 @@ export const meta = { export default async (params: any, user: ILocalUser, app: IApp) => new Promise(async (res, rej) => { const [ps, psErr] = getParams(meta, params); - if (psErr) throw psErr; + if (psErr) return rej(psErr); const isSecure = user != null && app == null; diff --git a/src/server/api/private/signup.ts b/src/server/api/private/signup.ts index e3e8f044b..13ca16eb9 100644 --- a/src/server/api/private/signup.ts +++ b/src/server/api/private/signup.ts @@ -132,6 +132,12 @@ export default async (ctx: Koa.Context) => { updateUserStats(account, true); - // Response - ctx.body = await pack(account); + const res = await pack(account, account, { + detail: true, + includeSecrets: true + }); + + res.token = secret; + + ctx.body = res; }; diff --git a/test/api.ts b/test/api.ts index cc7c8c529..1d6298669 100644 --- a/test/api.ts +++ b/test/api.ts @@ -75,7 +75,7 @@ describe('API', () => { username: 'test.', password: 'test' }); - expect(res).to.have.status(400); + expect(res).have.status(400); })); it('空のパスワードでアカウントが作成できない', async(async () => { @@ -83,7 +83,7 @@ describe('API', () => { username: 'test', password: '' }); - expect(res).to.have.status(400); + expect(res).have.status(400); })); it('正しくアカウントが作成できる', async(async () => { @@ -92,7 +92,7 @@ describe('API', () => { password: 'test' }; const res = await request('/signup', me); - expect(res).to.have.status(200); + expect(res).have.status(200); expect(res.body).be.a('object'); expect(res.body).have.property('username').eql(me.username); })); @@ -105,7 +105,7 @@ describe('API', () => { username: 'test', password: 'test' }); - expect(res).to.have.status(400); + expect(res).have.status(400); })); }); @@ -119,7 +119,7 @@ describe('API', () => { username: 'test', password: 'bar' }); - expect(res).to.have.status(403); + expect(res).have.status(403); })); it('クエリをインジェクションできない', async(async () => { @@ -132,7 +132,7 @@ describe('API', () => { $gt: '' } }); - expect(res).to.have.status(400); + expect(res).have.status(400); })); it('正しい情報でサインインできる', async(async () => { @@ -144,7 +144,60 @@ describe('API', () => { username: 'test', password: 'foo' }); - expect(res).to.have.status(204); + expect(res).have.status(204); + })); + }); + + describe('i/update', () => { + it('アカウント設定を更新できる', async(async () => { + const me = await signup(); + + const myName = '大室櫻子'; + const myLocation = '七森中'; + const myBirthday = '2000-09-07'; + + const res = await request('/i/update', { + name: myName, + location: myLocation, + birthday: myBirthday + }, me); + + expect(res).have.status(200); + expect(res.body).be.a('object'); + expect(res.body).have.property('name').eql(myName); + expect(res.body).have.nested.property('profile').a('object'); + expect(res.body).have.nested.property('profile.location').eql(myLocation); + expect(res.body).have.nested.property('profile.birthday').eql(myBirthday); + })); + + it('名前を空白にできない', async(async () => { + const me = await signup(); + const res = await request('/i/update', { + name: ' ' + }, me); + expect(res).have.status(400); + })); + + it('誕生日の設定を削除できる', async(async () => { + const me = await signup(); + await request('/i/update', { + birthday: '2000-09-07' + }, me); + const res = await request('/i/update', { + birthday: null + }, me); + expect(res).have.status(200); + expect(res.body).be.a('object'); + expect(res.body).have.nested.property('profile').a('object'); + expect(res.body).have.nested.property('profile.birthday').eql(null); + })); + + it('不正な誕生日の形式で怒られる', async(async () => { + const me = await signup(); + const res = await request('/i/update', { + birthday: '2000/09/07' + }, me); + expect(res).have.status(400); })); }); });