-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #205 from creative-commoners/pulls/4.0/disable-option
NEW Support displaying and setting a default registered method in the member settings fields
- Loading branch information
Showing
23 changed files
with
593 additions
and
82 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
client/src/components/FormField/RegisteredMFAMethodListField/MethodListItem/SetDefault.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import registeredMethodShape from 'types/registeredMethod'; | ||
import Config from 'lib/Config'; // eslint-disable-line | ||
import { connect } from 'react-redux'; | ||
import { setDefaultMethod } from 'state/mfaAdministration/actions'; | ||
|
||
const fallbacks = require('../../../../../lang/src/en.json'); | ||
|
||
/** | ||
* An action to set the current method as the default registered method for a user | ||
*/ | ||
class SetDefault extends Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
this.handleSetDefault = this.handleSetDefault.bind(this); | ||
} | ||
|
||
handleSetDefault() { | ||
const { method, onSetDefaultMethod } = this.props; | ||
const { endpoints: { setDefault } } = this.context; | ||
|
||
const token = Config.get('SecurityID'); | ||
const endpoint = `${setDefault.replace('{urlSegment}', method.urlSegment)}?SecurityID=${token}`; | ||
|
||
fetch(endpoint, { | ||
method: 'PUT', | ||
}).then(response => response.json().then(json => { | ||
if (response.status === 200) { | ||
onSetDefaultMethod(method.urlSegment); | ||
return; | ||
} | ||
|
||
const message = (json.errors && ` Errors: \n - ${json.errors.join('\n -')}`) || ''; | ||
throw Error(`Could not set default method. Error code ${response.status}.${message}`); | ||
})); | ||
} | ||
|
||
render() { | ||
const { ss: { i18n } } = window; | ||
|
||
return ( | ||
<button | ||
className="registered-method-list-item__control" | ||
type="button" | ||
onClick={this.handleSetDefault} | ||
> | ||
{i18n._t( | ||
'MultiFactorAuthentication.SET_AS_DEFAULT', | ||
fallbacks['MultiFactorAuthentication.SET_AS_DEFAULT'] | ||
)} | ||
</button> | ||
); | ||
} | ||
} | ||
|
||
SetDefault.propTypes = { | ||
method: registeredMethodShape.isRequired, | ||
}; | ||
|
||
SetDefault.contextTypes = { | ||
endpoints: PropTypes.shape({ | ||
setDefault: PropTypes.string | ||
}), | ||
}; | ||
|
||
const mapDispatchToProps = dispatch => ({ | ||
onSetDefaultMethod: urlSegment => dispatch(setDefaultMethod(urlSegment)), | ||
}); | ||
|
||
export { SetDefault as Component }; | ||
|
||
export default connect(null, mapDispatchToProps)(SetDefault); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
client/src/components/FormField/RegisteredMFAMethodListField/tests/MethodListItem-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* global jest, describe, it, expect */ | ||
import React from 'react'; | ||
import Enzyme, { shallow } from 'enzyme'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import MethodListItem from '../MethodListItem'; | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); | ||
|
||
window.ss = { | ||
i18n: { | ||
_t: (key, string) => string, | ||
detectLocale: () => 'en_NZ', | ||
inject: (message) => message, // not a great mock... | ||
}, | ||
}; | ||
|
||
describe('MethodListitem', () => { | ||
describe('getStatusMessage()', () => { | ||
it('identifies default methods', () => { | ||
const wrapper = shallow( | ||
<MethodListItem | ||
method={{ urlSegment: 'foo', }} | ||
isDefaultMethod | ||
/> | ||
); | ||
|
||
expect(wrapper.instance().getStatusMessage()).toContain('(default)'); | ||
}); | ||
|
||
it('identifies backup methods', () => { | ||
const wrapper = shallow( | ||
<MethodListItem | ||
method={{ urlSegment: 'foo', }} | ||
isBackupMethod | ||
/> | ||
); | ||
|
||
expect(wrapper.instance().getStatusMessage()).toContain('Created'); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.