Skip to content

Commit 6335e63

Browse files
committed
add test case for validator & update sth
1 parent dddfcf3 commit 6335e63

File tree

10 files changed

+69
-8
lines changed

10 files changed

+69
-8
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<a href="https://www.v2ex.com/">
2+
<a href="https://www.v2ex.com/" target="_blank">
33
<img src="https://v2ex.assets.uxengine.net/site/logo@2x.png?m=1346064962" alt="V2EX" title="V2EX" />
44
</a>
55
</p>
@@ -19,7 +19,7 @@ Node.js >= 7.6.0 required(async & await native supported).
1919
## Features
2020

2121
- ✔︎ Full APIs Wrapped
22-
- ✔︎ Restful Style
22+
- ✔︎ RESTful Style
2323
- ✔︎ 100% Unit Test
2424

2525
### Unit Test

app/controller/members.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = app => {
1313
async show (ctx) {
1414
ctx.validate({
1515
username: { type: 'string', required: false },
16-
id: { type: 'string', required: false }
16+
id: { type: 'id', required: false }
1717
}, ctx.params)
1818

1919
ctx.body = await ctx.service.members.show(ctx.params)

app/controller/nodes.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ module.exports = app => {
1515
}
1616

1717
async show (ctx) {
18+
// validate
19+
ctx.validate({
20+
id: { type: 'id', required: false },
21+
name: { type: 'string', required: false }
22+
}, ctx.params)
23+
1824
ctx.body = await ctx.service.nodes.show(ctx.params)
1925
}
2026

app/controller/replies.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports = app => {
1212

1313
async show (ctx) {
1414
ctx.validate({
15-
topic_id: { type: 'string', required: true }
15+
topic_id: { type: 'id', required: true }
1616
}, ctx.params)
1717

1818
ctx.params = Object.assign(ctx.params, ctx.query)

app/controller/topics.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ module.exports = app => {
2626
}
2727

2828
async show (ctx) {
29+
// validate
30+
ctx.validate({
31+
id: { type: 'id', required: true }
32+
}, ctx.params)
33+
2934
ctx.body = await ctx.service.topics.show(ctx.params)
3035
}
3136

3237
async getAllByType (ctx) {
3338
// validate the params
3439
ctx.validate({
35-
username: { type: 'string', required: false },
36-
node_name: { type: 'string', required: false },
37-
node_id: { type: 'string', required: false }
40+
type: { type: 'enum', values: [ 'username', 'node_name', 'node_id' ], required: true },
41+
value: { type: 'string', required: true },
42+
page: { type: 'id', required: false }
3843
}, ctx.params)
3944

4045
// set default page in controller

app/service/replies.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ module.exports = app => {
1818

1919
async request (query, opts) {
2020
const url = `${this.root}/${query}.json`
21-
console.log(url)
2221
opts = Object.assign({
2322
timeout: [ '30s', '30s' ],
2423
dataType: 'json'

test/app/controller/members.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,15 @@ describe('test/app/controller/members.test.js', () => {
4848
assert(item.username === 'IndexXuan')
4949
})
5050

51+
it('3 should validation failed for id', async () => {
52+
const r = await request(app.callback())
53+
.get('/api/v2/members/id/abcdef')
54+
.expect(422)
55+
56+
const b = r.body
57+
assert(b.error === 'Validation Failed')
58+
assert(b.detail[0].field === 'id')
59+
})
60+
5161
}) // /.describe
62+

test/app/controller/nodes.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,15 @@ describe('test/app/controller/nodes.test.js', () => {
6060
assert(vim.url.includes('v2ex.com/go/vim'))
6161
})
6262

63+
it('4 should validation failed for id', async () => {
64+
const r = await request(app.callback())
65+
.get('/api/v2/nodes/id/abc')
66+
.expect(422)
67+
68+
const b = r.body
69+
assert(b.error === 'Validation Failed')
70+
assert(b.detail[0].field === 'id')
71+
})
72+
6373
})
6474

test/app/controller/replies.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,15 @@ describe('test/app/controller/replies.test.js', () => {
3535
assert(item.content.includes('站长辛苦了'))
3636
})
3737

38+
it('2 should validation failed for topic_id', async () => {
39+
const r = await request(app.callback())
40+
.get('/api/v2/replies/abcdef')
41+
.expect(422)
42+
43+
const b = r.body
44+
assert(b.error === 'Validation Failed')
45+
assert(b.detail[0].field === 'topic_id')
46+
})
47+
3848
}) // /.describe
3949

test/app/controller/topics.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,25 @@ describe('test/app/controller/topics.test.js', () => {
104104
}
105105
})
106106

107+
it('6 should return all vim\'s topics by node_id', async () => {
108+
const r = await request(app.callback())
109+
.get('/api/v2/topics/all/node_id/249')
110+
.expect(200)
111+
112+
assert(Array.isArray(r.body))
113+
const item = r.body.filter(item => item.id === 344868)[0]
114+
})
115+
116+
it('7 should validation failed for enum type when get all topics', async () => {
117+
const r = await request(app.callback())
118+
.get('/api/v2/topics/all/not_node_name/vim')
119+
.expect(422)
120+
121+
const b = r.body
122+
assert(b.error === 'Validation Failed')
123+
assert(b.detail[0].message.includes('one of'))
124+
assert(b.detail[0].field === 'type')
125+
})
126+
107127
}) // /.describe
108128

0 commit comments

Comments
 (0)