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

Commit

Permalink
don't set api password or port to undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
larrysalibra committed Apr 19, 2017
1 parent e6da924 commit de04589
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 25 deletions.
31 changes: 6 additions & 25 deletions app/js/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import './utils/proxy-fetch'
import React, { Component, PropTypes } from 'react'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
import queryString from 'query-string'
import { AccountActions } from './store/account'
import { SettingsActions } from './store/settings'
import WelcomeModal from './components/WelcomeModal'
import hash from 'hash-handler'
import { getCoreAPIPasswordFromURL, getLogServerPortFromURL } from './utils/api-utils'
import log4js from 'log4js'

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

this.closeModal = this.closeModal.bind(this)
this.getCoreAPIPasswordFromURL = this.getCoreAPIPasswordFromURL.bind(this)
this.getLogServerPortFromURL = this.getLogServerPortFromURL.bind(this)
}

componentWillMount() {
logger.trace('componentWillMount')
const coreAPIPassword = this.getCoreAPIPasswordFromURL()
const logServerPort = this.getLogServerPortFromURL()
const coreAPIPassword = getCoreAPIPasswordFromURL()
const logServerPort = getLogServerPortFromURL()
let api = this.props.api
if (coreAPIPassword !== null) {
api = Object.assign({}, api, { coreAPIPassword })
Expand All @@ -71,26 +68,9 @@ class App extends Component {
})
}

getCoreAPIPasswordFromURL() {
const coreAPIPassword = hash.getInstance().get('coreAPIPassword')
if (typeof coreAPIPassword === undefined || coreAPIPassword === 'off') {
return null
}
hash.getInstance().set('coreAPIPassword', 'off')
return coreAPIPassword
}

getLogServerPortFromURL() {
const logServerPort = hash.getInstance().get('logServerPort')
if (typeof logServerPort === undefined || logServerPort === 'off') {
return null
}
hash.getInstance().set('logServerPort', 'off')
return logServerPort
}

closeModal() {
this.setState({modalIsOpen: false})
this.setState({ modalIsOpen: false })
}

render() {
Expand All @@ -100,7 +80,8 @@ class App extends Component {
accountCreated={this.state.accountCreated}
storageConnected={this.state.storageConnected}
coreConnected={this.state.coreConnected}
closeModal={this.closeModal} />
closeModal={this.closeModal}
/>
{this.props.children}
</div>
)
Expand Down
18 changes: 18 additions & 0 deletions app/js/utils/api-utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import hash from 'hash-handler'
import log4js from 'log4js'

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

export function getCoreAPIPasswordFromURL() {
const coreAPIPassword = hash.getInstance().get('coreAPIPassword')
if (!coreAPIPassword || coreAPIPassword === 'off') {
return null
}
hash.getInstance().set('coreAPIPassword', 'off')
return coreAPIPassword
}

export function getLogServerPortFromURL() {
const logServerPort = hash.getInstance().get('logServerPort')
if (!logServerPort || logServerPort === 'off') {
return null
}
hash.getInstance().set('logServerPort', 'off')
return logServerPort
}
/*
export function getIdentities(address, addressLookupUrl, localIdentities, callback) {
let remoteNamesDict = {},
Expand Down
60 changes: 60 additions & 0 deletions tests/utils/api-utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { getCoreAPIPasswordFromURL, getLogServerPortFromURL } from '../../app/js/utils/api-utils'

describe('api-utils', () => {
beforeEach(() => {
global.origWindow = global.window
global.window = { // mock window for hash-handler
location: {
hash: '#'
},
addEventListener: () => {
// do nothing
}
}
})

afterEach(() => {
global.window = global.origWindow
})
describe('getCoreAPIPasswordFromURL()', () => {
it('should return the password', () => {
const hash = '#coreAPIPassword=abc123&logServerPort=8333'
window.location.hash = hash
assert.equal(window.location.hash, '#coreAPIPassword=abc123&logServerPort=8333')
assert.equal(getCoreAPIPasswordFromURL(), 'abc123')
})

it('should be null if password is off', () => {
const hash = '#coreAPIPassword=off'
window.location.hash = hash
assert.equal(getCoreAPIPasswordFromURL(), null)
})

it('should be null if password parameter does not exist', () => {
const hash = ''
window.location.hash = hash
assert.equal(getCoreAPIPasswordFromURL(), null)
})
})

describe('getLogServerPortFromURL()', () => {
it('should return the port', () => {
const hash = '#coreAPIPassword=abc123&logServerPort=8333'
window.location.hash = hash
assert.equal(window.location.hash, '#coreAPIPassword=abc123&logServerPort=8333')
assert.equal(getLogServerPortFromURL(), '8333')
})

it('should be null if port is off', () => {
const hash = '#logServerPort=off'
window.location.hash = hash
assert.equal(getLogServerPortFromURL(), null)
})

it('should be null if port parameter does not exist', () => {
const hash = ''
window.location.hash = hash
assert.equal(getLogServerPortFromURL(), null)
})
})
})

0 comments on commit de04589

Please sign in to comment.