提交 c89c4589 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

add error emit for blocked account message (#872)

上级 617c1971
......@@ -17,8 +17,9 @@
* limitations under the License.
*
*/
// tslint:disable:no-shadowed-variable
import * as test from 'blue-tape'
// tslint:disable:no-shadowed-variable
// import * as sinon from 'sinon'
// const sinonTest = require('sinon-test')(sinon)
......@@ -33,3 +34,66 @@ test('PuppetWebBridge', async t => {
t.ok(bridge, 'Bridge instnace')
await bridge.quit()
})
test('testBlockedMessage()', async t => {
const BLOCKED_XML_ZH = `
<error>
<ret>1203</ret>
<message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
</error>
`
const BLOCKED_TEXT_ZH = [
'当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。',
'你可以通过手机客户端或者windows微信登录。',
].join('')
// tslint:disable:max-line-length
const BLOCKED_XML_EN = `
<error>
<ret>1203</ret>
<message>For account security, newly registered WeChat accounts are unable to log in to Web WeChat. To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com</message>
</error>
`
const BLOCKED_TEXT_EN = [
'For account security, newly registered WeChat accounts are unable to log in to Web WeChat.',
' To use WeChat on a computer, use Windows WeChat or Mac WeChat at http://wechat.com',
].join('')
test('not blocked', async t => {
const profile = new Profile()
const bridge = new Bridge({ profile })
try {
await bridge.testBlockedMessage('this is not xml')
t.pass('should not throw when no block message')
} catch (e) {
t.fail('should throw when no block message')
}
})
test('zh', async t => {
const profile = new Profile()
const bridge = new Bridge({ profile })
try {
await bridge.testBlockedMessage(BLOCKED_XML_ZH)
t.fail('should throw exception')
} catch (e) {
t.equal(e.message, BLOCKED_TEXT_ZH, 'should get zh blocked message')
}
})
test('en', async t => {
const profile = new Profile()
const bridge = new Bridge({ profile })
try {
await bridge.testBlockedMessage(BLOCKED_XML_EN)
t.fail('should throw exception')
} catch (e) {
t.equal(e.message, BLOCKED_TEXT_EN, 'should get en blocked message')
}
})
})
......@@ -593,39 +593,38 @@ export class Bridge extends EventEmitter {
}
/**
* <error>
* <ret>1203</ret>
* <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
* </error>
* Throw if there's a blocked message
*/
public async blockedMessageBody(): Promise<string | null> {
log.silly('PuppetWebBridge', 'blockedMessageBody()')
const text = await this.page.evaluate('return document.body.innerText')
public async testBlockedMessage(text: string): Promise<void> {
log.silly('PuppetWebBridge', 'testBlockedMessage(%s)', text)
return new Promise<string | null>((resolve, reject) => {
return new Promise<void>((resolve, reject) => {
parseString(text, { explicitArray: false }, (err, obj) => {
if (err) {
return resolve(null)
return resolve()
}
if (!obj.error) {
return resolve(null)
return resolve()
}
const code = obj.error.code
const message = obj.error.message
const message = obj.error.message as string
const e = new Error(message)
if (code === 1203) {
// <error>
// <ret>1203</ret>
// <message>当前登录环境异常。为了你的帐号安全,暂时不能登录web微信。你可以通过手机客户端或者windows微信登录。</message>
// </error>
return resolve(message)
return reject(e)
}
return resolve(message) // other error message
log.warn('PuppetWebBridge', 'testBlockedMessage() code: %s type: %s', code, typeof code)
return reject(e) // other error message
})
})
}
public async clickSwitchAccount(): Promise<boolean> {
log.verbose('PuppetWebBrowser', 'clickSwitchAccount()')
log.verbose('PuppetWebBridge', 'clickSwitchAccount()')
const SELECTOR = `//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']`
try {
......@@ -633,10 +632,10 @@ export class Bridge extends EventEmitter {
await button.click()
// const button = await this.driver.driver.findElement(By.xpath(
// "//div[contains(@class,'association') and contains(@class,'show')]/a[@ng-click='qrcodeLogin()']"))
log.silly('PuppetWebBrowser', 'clickSwitchAccount() clicked!')
log.silly('PuppetWebBridge', 'clickSwitchAccount() clicked!')
return true
} catch (e) {
log.silly('PuppetWebBrowser', 'clickSwitchAccount() button not found')
log.silly('PuppetWebBridge', 'clickSwitchAccount() button not found')
return false
}
}
......
......@@ -258,18 +258,15 @@ export class PuppetWeb extends Puppet {
try {
await this.bridge.init()
} catch (e) {
// FIXME
// Enable the following code again
/*
const blockedMessage = await this.bridge.blockedMessageBody()
if (blockedMessage) {
const error = new Error(blockedMessage)
this.emit('error', error)
const text = await this.bridge.evaluate('document.body.innerHTML')
try {
await this.bridge.testBlockedMessage(text)
log.error('PuppetWeb', 'initBridge() exception: %s', e.message)
Raven.captureException(e)
this.emit('error', e)
} catch (blockedError) {
this.emit('error', blockedError)
}
*/
log.error('PuppetWeb', 'initBridge() exception: %s', e.message)
Raven.captureException(e)
throw e
}
return this.bridge
......
......@@ -54,7 +54,6 @@
"no-duplicate-variable": true,
"no-empty": true,
"no-eval": true,
"no-inferrable-types": false,
"no-internal-module": true,
"no-require-imports": false,
"no-shadowed-variable": true,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册