Skip to content
This repository was archived by the owner on Apr 17, 2023. It is now read-only.

Commit de04589

Browse files
committed
don't set api password or port to undefined
1 parent e6da924 commit de04589

File tree

3 files changed

+84
-25
lines changed

3 files changed

+84
-25
lines changed

app/js/App.js

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ import './utils/proxy-fetch'
22
import React, { Component, PropTypes } from 'react'
33
import { bindActionCreators } from 'redux'
44
import { connect } from 'react-redux'
5-
import queryString from 'query-string'
65
import { AccountActions } from './store/account'
76
import { SettingsActions } from './store/settings'
87
import WelcomeModal from './components/WelcomeModal'
9-
import hash from 'hash-handler'
8+
import { getCoreAPIPasswordFromURL, getLogServerPortFromURL } from './utils/api-utils'
109
import log4js from 'log4js'
1110

1211
const logger = log4js.getLogger('App.js')
@@ -43,14 +42,12 @@ class App extends Component {
4342
}
4443

4544
this.closeModal = this.closeModal.bind(this)
46-
this.getCoreAPIPasswordFromURL = this.getCoreAPIPasswordFromURL.bind(this)
47-
this.getLogServerPortFromURL = this.getLogServerPortFromURL.bind(this)
4845
}
4946

5047
componentWillMount() {
5148
logger.trace('componentWillMount')
52-
const coreAPIPassword = this.getCoreAPIPasswordFromURL()
53-
const logServerPort = this.getLogServerPortFromURL()
49+
const coreAPIPassword = getCoreAPIPasswordFromURL()
50+
const logServerPort = getLogServerPortFromURL()
5451
let api = this.props.api
5552
if (coreAPIPassword !== null) {
5653
api = Object.assign({}, api, { coreAPIPassword })
@@ -71,26 +68,9 @@ class App extends Component {
7168
})
7269
}
7370

74-
getCoreAPIPasswordFromURL() {
75-
const coreAPIPassword = hash.getInstance().get('coreAPIPassword')
76-
if (typeof coreAPIPassword === undefined || coreAPIPassword === 'off') {
77-
return null
78-
}
79-
hash.getInstance().set('coreAPIPassword', 'off')
80-
return coreAPIPassword
81-
}
82-
83-
getLogServerPortFromURL() {
84-
const logServerPort = hash.getInstance().get('logServerPort')
85-
if (typeof logServerPort === undefined || logServerPort === 'off') {
86-
return null
87-
}
88-
hash.getInstance().set('logServerPort', 'off')
89-
return logServerPort
90-
}
9171

9272
closeModal() {
93-
this.setState({modalIsOpen: false})
73+
this.setState({ modalIsOpen: false })
9474
}
9575

9676
render() {
@@ -100,7 +80,8 @@ class App extends Component {
10080
accountCreated={this.state.accountCreated}
10181
storageConnected={this.state.storageConnected}
10282
coreConnected={this.state.coreConnected}
103-
closeModal={this.closeModal} />
83+
closeModal={this.closeModal}
84+
/>
10485
{this.props.children}
10586
</div>
10687
)

app/js/utils/api-utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import hash from 'hash-handler'
12
import log4js from 'log4js'
23

34
const logger = log4js.getLogger('utils/api-utils.js')
@@ -20,6 +21,23 @@ export function authorizationHeaderValue(coreAPIPassword) {
2021
return `bearer ${coreAPIPassword}`
2122
}
2223

24+
export function getCoreAPIPasswordFromURL() {
25+
const coreAPIPassword = hash.getInstance().get('coreAPIPassword')
26+
if (!coreAPIPassword || coreAPIPassword === 'off') {
27+
return null
28+
}
29+
hash.getInstance().set('coreAPIPassword', 'off')
30+
return coreAPIPassword
31+
}
32+
33+
export function getLogServerPortFromURL() {
34+
const logServerPort = hash.getInstance().get('logServerPort')
35+
if (!logServerPort || logServerPort === 'off') {
36+
return null
37+
}
38+
hash.getInstance().set('logServerPort', 'off')
39+
return logServerPort
40+
}
2341
/*
2442
export function getIdentities(address, addressLookupUrl, localIdentities, callback) {
2543
let remoteNamesDict = {},

tests/utils/api-utils.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { getCoreAPIPasswordFromURL, getLogServerPortFromURL } from '../../app/js/utils/api-utils'
2+
3+
describe('api-utils', () => {
4+
beforeEach(() => {
5+
global.origWindow = global.window
6+
global.window = { // mock window for hash-handler
7+
location: {
8+
hash: '#'
9+
},
10+
addEventListener: () => {
11+
// do nothing
12+
}
13+
}
14+
})
15+
16+
afterEach(() => {
17+
global.window = global.origWindow
18+
})
19+
describe('getCoreAPIPasswordFromURL()', () => {
20+
it('should return the password', () => {
21+
const hash = '#coreAPIPassword=abc123&logServerPort=8333'
22+
window.location.hash = hash
23+
assert.equal(window.location.hash, '#coreAPIPassword=abc123&logServerPort=8333')
24+
assert.equal(getCoreAPIPasswordFromURL(), 'abc123')
25+
})
26+
27+
it('should be null if password is off', () => {
28+
const hash = '#coreAPIPassword=off'
29+
window.location.hash = hash
30+
assert.equal(getCoreAPIPasswordFromURL(), null)
31+
})
32+
33+
it('should be null if password parameter does not exist', () => {
34+
const hash = ''
35+
window.location.hash = hash
36+
assert.equal(getCoreAPIPasswordFromURL(), null)
37+
})
38+
})
39+
40+
describe('getLogServerPortFromURL()', () => {
41+
it('should return the port', () => {
42+
const hash = '#coreAPIPassword=abc123&logServerPort=8333'
43+
window.location.hash = hash
44+
assert.equal(window.location.hash, '#coreAPIPassword=abc123&logServerPort=8333')
45+
assert.equal(getLogServerPortFromURL(), '8333')
46+
})
47+
48+
it('should be null if port is off', () => {
49+
const hash = '#logServerPort=off'
50+
window.location.hash = hash
51+
assert.equal(getLogServerPortFromURL(), null)
52+
})
53+
54+
it('should be null if port parameter does not exist', () => {
55+
const hash = ''
56+
window.location.hash = hash
57+
assert.equal(getLogServerPortFromURL(), null)
58+
})
59+
})
60+
})

0 commit comments

Comments
 (0)