Skip to content

Commit dc50140

Browse files
committed
add codecov & add more test & update README Labels
1 parent 8b0a79a commit dc50140

File tree

16 files changed

+456
-35
lines changed

16 files changed

+456
-35
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
<a href="https://circleci.com/gh/IndexXuan/v2ex-api-bridge/tree/master">
1111
<img src="https://circleci.com/gh/IndexXuan/v2ex-api-bridge/tree/master.svg?style=shield" alt="Build Status">
1212
</a>
13+
<a href="https://codecov.io/github/IndexXuan/v2ex-api-bridge?branch=master">
14+
<img src="https://codecov.io/github/IndexXuan/v2ex-api-bridge/coverage.svg?branch=master" alt="Code Coverage">
15+
</a>
1316
<img src="https://img.shields.io/badge/Vim-Best%20Editor-green.svg" alt="Vim Best Editor" />
1417
<br>
1518
<br>
@@ -18,7 +21,6 @@
1821
</a>
1922
</p>
2023

21-
2224
## Intro
2325

2426
`V2EX API Bridge`: 提供论坛公开API的封装和私有API的挖掘实现,目前已支持 `登录,签到,发帖,回帖` 等非官方API功能.  

app/controller/auth.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ module.exports = app => {
2121
async login (ctx) {
2222
const { username, password } = ctx.query
2323
if (username == null) {
24-
throw new Error('请传入用户名')
24+
throw new Error('请传入用户名')
2525
}
2626
if (password == null) {
27-
throw new Error('请传入密码')
27+
throw new Error('请传入密码')
2828
}
29-
ctx.body = await ctx.service.auth.login(ctx.query)
29+
ctx.body = await ctx.service.auth.login({username, password})
3030
}
3131

3232
/**

app/extend/context.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @Extend Context
3+
* ---------------------------------------------
4+
* Author : IndexXuan(https://github.com/IndexXuan)
5+
* Mail : indexxuan@gmail.com
6+
* Date : Fri 24 Mar 2017 11:15:04 AM CST
7+
*/
8+
9+
'use strict'
10+
11+
// sessionid cookie name
12+
const sessionCookieName = 'PB3_SESSION'
13+
14+
// token cookie name
15+
const tokenCookieName = 'A2'
16+
17+
/* istanbul ignore next */
18+
module.exports = {
19+
get sessionCookieName () {
20+
return sessionCookieName
21+
},
22+
get tokenCookieName () {
23+
return tokenCookieName
24+
},
25+
get sessionid () {
26+
return `${sessionCookieName}=${this.cookies.get(sessionCookieName)}`
27+
},
28+
get token () {
29+
return `${tokenCookieName}=${this.cookies.get(tokenCookieName)}`
30+
},
31+
// 公共请求头
32+
get commonHeaders () {
33+
return {
34+
"Accept": "text/html,application/xhtml+xml,application/xml",
35+
"Origin": "https://www.v2ex.com",
36+
"Referer": "https://www.v2ex.com",
37+
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
38+
}
39+
},
40+
get commonCookies () {
41+
return {
42+
// 千万注意大小写,V2EX对这些cookie校验很严格...
43+
tab: 'V2EX_TAB="2|1:0|10:1489746039|8:V2EX_TAB|8:dGVjaA==|b72b63fabe0f8faeff147ac38e26299655d713ad1880feef6679b56d8d1e9f47"',
44+
others: 'V2EX_LANG=zhcn; _ga=GA1.2.1254455933.1474272858; _gat=1'
45+
}
46+
}
47+
} // /.exports
48+

app/router.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ module.exports = app => {
4343
// @repies
4444
app.get('v2ex', `${prefix}/replies/:topic_id`, 'replies.show')
4545
// auth required
46-
app.post('v2ex', `${prefix}/replies/:topic_id`, 'replies.create')
47-
// app.get('v2ex', `${prefix}/replies/:topic_id/new`, 'replies.create') // for test
46+
app.post('v2ex', `${prefix}/replies/:topic_id/new`, 'replies.create')
47+
app.get('v2ex', `${prefix}/replies/:topic_id/new`, 'replies.create') // for test
4848

4949
/**
5050
* @examples

app/service/auth.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,14 @@ module.exports = app => {
116116
* login获取签名主方法
117117
*
118118
* @param {Object} params - 请求参数
119+
* - {String} username - 用户名
120+
* - {String} password - 密码
119121
*/
120-
async login (params) {
121-
// @step1 获取提交的用户名密码
122-
const { username, password } = params
123-
124-
// @step2 进入登录页,获取页面隐藏登录域以及once的值
122+
async login ({username, password}) {
123+
// @step1 进入登录页,获取页面隐藏登录域以及once的值
125124
await this.enterLoginPage()
126125

127-
// @step3 设置请求参数
126+
// @step2 设置请求参数
128127
const opts = {
129128
method: 'POST',
130129
headers: Object.assign({}, this.ctx.commonHeaders, { Cookie: this.sessionCookieStr }),
@@ -135,16 +134,16 @@ module.exports = app => {
135134
}
136135
}
137136

138-
// @step4 发起请求
137+
// @step3 发起请求
139138
const result = await this.request(this.loginUrl, opts)
140139

141-
// @step5 更新session并设置在客户端
140+
// @step4 更新session并设置在客户端
142141
await this.enterHomePage()
143142

144-
// @step6 解析获取到的cookies
143+
// @step5 解析获取到的cookies
145144
const cs = setCookieParser(result)
146145

147-
// @step7 判断是否登录成功并种下客户端cookies
146+
// @step6 判断是否登录成功并种下客户端cookies
148147
let success = false
149148
cs.forEach(c => {
150149
// 查看是否有令牌项的cookie,有就说明登录成功了
@@ -157,7 +156,7 @@ module.exports = app => {
157156
})
158157
})
159158

160-
// @step8 设置API返回结果
159+
// @step7 设置API返回结果
161160
return {
162161
result: success,
163162
msg: success ? 'ok' : '登录失败,请确认用户名密码无误!',

app/service/replies.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ module.exports = app => {
2020
constructor (ctx) {
2121
super(ctx)
2222
this.root = `${app.config.root}/replies`
23+
// 默认有权限,后面会判断
24+
this.auth = true
2325
// 请求需要的一次性签名
2426
this.once = ''
2527
}
@@ -81,7 +83,11 @@ module.exports = app => {
8183
// @step1 获取准备数据
8284
const session = this.ctx.sessionid
8385
const token = this.ctx.token
84-
const headers = Object.assign(this.ctx.commonHeaders, { Cookie: `${session}; ${token}` })
86+
const headers = Object.assign({}, this.ctx.commonHeaders, { Cookie: `${session}; ${token}` })
87+
88+
if (session.includes('undefined')) {
89+
this.auth = false
90+
}
8591

8692
// @step2 进入创建页,获取once
8793
const url = `https://www.v2ex.com/t/${params.topic_id}`
@@ -92,6 +98,7 @@ module.exports = app => {
9298
dataType: 'text'
9399
})
94100

101+
95102
// @step3 获取once
96103
this.getOnce(r.data)
97104

@@ -108,9 +115,10 @@ module.exports = app => {
108115

109116
// @step6 设置API返回值
110117
const success = result && result.res && result.res.requestUrls && result.res.requestUrls[0]
118+
const msg = !this.auth ? '请先登录再回帖' : success ? 'ok' : '回帖未知错误'
111119
return {
112-
result: !!success,
113-
msg: !!success ? 'ok' : 'error',
120+
result: !!success && this.auth,
121+
msg: msg,
114122
url: success
115123
}
116124
}

app/service/topics.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module.exports = app => {
1919
constructor (ctx) {
2020
super(ctx)
2121
this.root = `${app.config.root}/topics`
22+
// 默认有权限,后面会判断
23+
this.auth = true
2224
// 请求需要的一次性签名
2325
this.once = ''
2426
}
@@ -123,7 +125,11 @@ module.exports = app => {
123125
// step1 获取准备数据
124126
const session = this.ctx.sessionid
125127
const token = this.ctx.token
126-
const headers = Object.assign(this.ctx.commonHeaders, { Cookie: `${session}; ${token}` })
128+
const headers = Object.assign({}, this.ctx.commonHeaders, { Cookie: `${session}; ${token};` })
129+
130+
if (session.includes('undefined')) {
131+
this.auth = false
132+
}
127133

128134
// @step2 进入创建页,获取once
129135
const url = 'https://www.v2ex.com/new'
@@ -137,7 +143,7 @@ module.exports = app => {
137143
// @step3 解析得到once
138144
this.getOnce(r.data)
139145

140-
// @step4 设置请求参数
146+
// @step4 设置请求参数, 模仿真实的传送两遍content
141147
const data = Object.assign(params, { once: this.once }, { content: params.content })
142148

143149
// @step5 发起请求
@@ -149,10 +155,11 @@ module.exports = app => {
149155
})
150156

151157
// @step6 设置API返回值
152-
const success = result && result.res && result.res.requestUrls && result.res.requestUrls[0]
158+
const success = this.auth && result && result.res && result.res.requestUrls && result.res.requestUrls[0]
159+
const msg = !this.auth ? '请先登录再发帖' : success ? 'ok' : '发帖未知错误'
153160
return {
154-
result: !!success,
155-
msg: !!success ? 'ok' : 'error',
161+
result: !!success && this.auth,
162+
msg: msg,
156163
url: success
157164
}
158165
}

ci.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
# copy from vue project
3+
4+
set -e
5+
npm test
6+
7+
# report coverage stats for non-PRs
8+
if [[ -z $CI_PULL_REQUEST ]]; then
9+
cat ./coverage/lcov.info | ./node_modules/.bin/codecov
10+
fi

circle.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ machine:
44

55
test:
66
override:
7-
- npm run test
7+
- bash ci.sh

codecov.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
coverage:
2+
parsers:
3+
javascript:
4+
enable_partials: no

0 commit comments

Comments
 (0)