Skip to content
This repository was archived by the owner on Nov 28, 2022. It is now read-only.

Commit 3744e03

Browse files
committed
Add support for basic auth from user object
1 parent b4891d9 commit 3744e03

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

packages/api-explorer/__tests__/SecurityInput.test.jsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,29 @@ describe('basic', () => {
107107
securityInput.find('input[name="password"]').instance().value = 'pass';
108108
securityInput.find('input[name="password"]').simulate('change');
109109

110-
expect(onChange.mock.calls[1][0]).toEqual({
110+
// TODO this should eventually be removed
111+
// see comment above componentDidMount in Basic.jsx
112+
expect(onChange.mock.calls[0][0]).toEqual({
113+
'test-basic': {
114+
user: '',
115+
password: '',
116+
},
117+
});
118+
119+
expect(onChange.mock.calls[2][0]).toEqual({
111120
'test-basic': {
112121
user: 'user',
113122
password: 'pass',
114123
},
115124
});
116125
});
126+
127+
test('should display user/pass if set', () => {
128+
const user = 'user';
129+
const pass = 'pass';
130+
const securityInput = mount(<SecurityInput {...props} {...baseProps} oauth user={{ user, pass }} />);
131+
132+
expect(securityInput.find('input[name="user"]').prop('value')).toBe(user)
133+
expect(securityInput.find('input[name="password"]').prop('value')).toBe(pass)
134+
});
117135
});

packages/api-explorer/src/SecurityInput.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ function SecurityInput(props) {
1616
return <Oauth2 {...props} apiKey={getAuth(props.user, props.scheme)} change={change} />;
1717
case 'http':
1818
// TODO support other schemes? https://github.com/readmeio/api-explorer/issues/15
19-
return <Basic {...props} change={change} />;
19+
const { user, pass } = getAuth(props.user, props.scheme);
20+
return <Basic {...props} change={change} user={user} pass={pass} />;
2021
case 'apiKey':
2122
return <ApiKey {...props} change={change} />;
2223
default:

packages/api-explorer/src/security-input-types/Basic.jsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,22 @@ const PropTypes = require('prop-types');
44
class Basic extends React.Component {
55
constructor(props) {
66
super(props);
7-
this.state = { user: '', password: '' };
7+
this.state = { user: props.user || '', password: props.pass || '' };
88
this.inputChange = this.inputChange.bind(this);
99
}
10+
// TODO refactor this
11+
// This is not ideal... we're having to update the state
12+
// here so that the code sample updates with the base64
13+
// encoded user/pass on first render. This is a sign of
14+
// bad prop passing somewhere and is quite un-reacty.
15+
// Maybe we should be calling getAuth from the top level
16+
// so the value is correct on the first pass through to
17+
// the CodeSample component. Let me mull this over a little more.
18+
componentDidMount() {
19+
this.props.change({ user: this.state.user, password: this.state.password });
20+
}
1021
componentDidUpdate(prevProps, prevState) {
22+
// Without this if block the code spirals into an infinite loop
1123
if (prevState.user !== this.state.user || prevState.password !== this.state.password)
1224
this.props.change(this.state);
1325
}
@@ -26,6 +38,7 @@ class Basic extends React.Component {
2638
type="text"
2739
onChange={e => this.inputChange(e.currentTarget.name, e.currentTarget.value)}
2840
name="user"
41+
value={this.state.user}
2942
/>
3043
</div>
3144
<div className="col-xs-6">
@@ -34,6 +47,7 @@ class Basic extends React.Component {
3447
type="text"
3548
onChange={e => this.inputChange(e.currentTarget.name, e.currentTarget.value)}
3649
name="password"
50+
value={this.state.password}
3751
/>
3852
</div>
3953
</div>

0 commit comments

Comments
 (0)