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

Commit b75a24d

Browse files
committed
Remove login handling from RemoteContainer
1 parent 95ab2d9 commit b75a24d

File tree

2 files changed

+24
-146
lines changed

2 files changed

+24
-146
lines changed

lib/containers/remote-container.js

Lines changed: 13 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,20 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import {QueryRenderer, graphql} from 'react-relay';
44

5-
import {incrementCounter} from '../reporter-proxy';
6-
import {RemotePropType, RemoteSetPropType, BranchSetPropType, RefresherPropType, EndpointPropType} from '../prop-types';
5+
import {
6+
RemotePropType, RemoteSetPropType, BranchSetPropType, RefresherPropType,
7+
EndpointPropType, TokenPropType,
8+
} from '../prop-types';
79
import RelayNetworkLayerManager from '../relay-network-layer-manager';
8-
import {UNAUTHENTICATED, INSUFFICIENT} from '../shared/keytar-strategy';
910
import RemoteController from '../controllers/remote-controller';
10-
import ObserveModel from '../views/observe-model';
1111
import LoadingView from '../views/loading-view';
1212
import QueryErrorView from '../views/query-error-view';
13-
import GithubLoginView from '../views/github-login-view';
1413

1514
export default class RemoteContainer extends React.Component {
1615
static propTypes = {
1716
// Connection
18-
loginModel: PropTypes.object.isRequired,
1917
endpoint: EndpointPropType.isRequired,
18+
token: TokenPropType.isRequired,
2019

2120
// Repository attributes
2221
refresher: RefresherPropType.isRequired,
@@ -29,52 +28,13 @@ export default class RemoteContainer extends React.Component {
2928
aheadCount: PropTypes.number,
3029

3130
// Action methods
31+
handleLogin: PropTypes.func.isRequired,
32+
handleLogout: PropTypes.func.isRequired,
3233
onPushBranch: PropTypes.func.isRequired,
3334
}
3435

35-
fetchToken = loginModel => {
36-
return loginModel.getToken(this.props.endpoint.getLoginAccount());
37-
}
38-
3936
render() {
40-
return (
41-
<ObserveModel model={this.props.loginModel} fetchData={this.fetchToken}>
42-
{this.renderWithToken}
43-
</ObserveModel>
44-
);
45-
}
46-
47-
renderWithToken = token => {
48-
if (token === null) {
49-
return <LoadingView />;
50-
}
51-
52-
if (token instanceof Error) {
53-
return (
54-
<QueryErrorView
55-
error={token}
56-
retry={this.handleTokenRetry}
57-
login={this.handleLogin}
58-
logout={this.handleLogout}
59-
/>
60-
);
61-
}
62-
63-
if (token === UNAUTHENTICATED) {
64-
return <GithubLoginView onLogin={this.handleLogin} />;
65-
}
66-
67-
if (token === INSUFFICIENT) {
68-
return (
69-
<GithubLoginView onLogin={this.handleLogin}>
70-
<p>
71-
Your token no longer has sufficient authorizations. Please re-authenticate and generate a new one.
72-
</p>
73-
</GithubLoginView>
74-
);
75-
}
76-
77-
const environment = RelayNetworkLayerManager.getEnvironmentForHost(this.props.endpoint, token);
37+
const environment = RelayNetworkLayerManager.getEnvironmentForHost(this.props.endpoint, this.props.token);
7838
const query = graphql`
7939
query remoteContainerQuery($owner: String!, $name: String!) {
8040
repository(owner: $owner, name: $name) {
@@ -96,21 +56,21 @@ export default class RemoteContainer extends React.Component {
9656
environment={environment}
9757
variables={variables}
9858
query={query}
99-
render={result => this.renderWithResult(result, token)}
59+
render={this.renderWithResult}
10060
/>
10161
);
10262
}
10363

104-
renderWithResult({error, props, retry}, token) {
64+
renderWithResult = ({error, props, retry}) => {
10565
this.props.refresher.setRetryCallback(this, retry);
10666

10767
if (error) {
10868
return (
10969
<QueryErrorView
11070
error={error}
111-
login={this.handleLogin}
71+
login={this.props.handleLogin}
72+
logout={this.props.handleLogout}
11273
retry={retry}
113-
logout={this.handleLogout}
11474
/>
11575
);
11676
}
@@ -122,7 +82,7 @@ export default class RemoteContainer extends React.Component {
12282
return (
12383
<RemoteController
12484
endpoint={this.props.endpoint}
125-
token={token}
85+
token={this.props.token}
12686

12787
repository={props.repository}
12888

@@ -139,16 +99,4 @@ export default class RemoteContainer extends React.Component {
13999
/>
140100
);
141101
}
142-
143-
handleLogin = token => {
144-
incrementCounter('github-login');
145-
this.props.loginModel.setToken(this.props.endpoint.getLoginAccount(), token);
146-
}
147-
148-
handleLogout = () => {
149-
incrementCounter('github-logout');
150-
this.props.loginModel.removeToken(this.props.endpoint.getLoginAccount());
151-
}
152-
153-
handleTokenRetry = () => this.props.loginModel.didUpdate();
154102
}

test/containers/remote-container.test.js

Lines changed: 11 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,21 @@ import {shallow} from 'enzyme';
33
import {QueryRenderer} from 'react-relay';
44

55
import RemoteContainer from '../../lib/containers/remote-container';
6-
import * as reporterProxy from '../../lib/reporter-proxy';
76
import {queryBuilder} from '../builder/graphql/query';
87
import Remote from '../../lib/models/remote';
98
import RemoteSet from '../../lib/models/remote-set';
109
import Branch, {nullBranch} from '../../lib/models/branch';
1110
import BranchSet from '../../lib/models/branch-set';
12-
import GithubLoginModel from '../../lib/models/github-login-model';
13-
import {getEndpoint} from '../../lib/models/endpoint';
11+
import {DOTCOM} from '../../lib/models/endpoint';
1412
import Refresher from '../../lib/models/refresher';
15-
import {InMemoryStrategy, INSUFFICIENT, UNAUTHENTICATED} from '../../lib/shared/keytar-strategy';
1613

1714
import remoteQuery from '../../lib/containers/__generated__/remoteContainerQuery.graphql';
1815

1916
describe('RemoteContainer', function() {
20-
let atomEnv, model;
17+
let atomEnv;
2118

2219
beforeEach(function() {
2320
atomEnv = global.buildAtomEnvironment();
24-
model = new GithubLoginModel(InMemoryStrategy);
2521
});
2622

2723
afterEach(function() {
@@ -36,45 +32,30 @@ describe('RemoteContainer', function() {
3632

3733
return (
3834
<RemoteContainer
39-
loginModel={model}
40-
endpoint={getEndpoint('github.com')}
35+
endpoint={DOTCOM}
36+
token="1234"
4137

4238
refresher={new Refresher()}
39+
pushInProgress={false}
4340
workingDirectory={__dirname}
44-
notifications={atomEnv.notifications}
4541
workspace={atomEnv.workspace}
4642
remote={origin}
4743
remotes={remotes}
4844
branches={branches}
49-
5045
aheadCount={0}
51-
pushInProgress={false}
5246

47+
handleLogin={() => {}}
48+
handleLogout={() => {}}
5349
onPushBranch={() => {}}
5450

5551
{...overrideProps}
5652
/>
5753
);
5854
}
5955

60-
it('renders a loading spinner while the token is being fetched', function() {
56+
it('renders a loading spinner while the GraphQL query is being performed', function() {
6157
const wrapper = shallow(buildApp());
62-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(null);
63-
assert.isTrue(tokenWrapper.exists('LoadingView'));
64-
});
65-
66-
it('renders a loading spinner while the GraphQL query is being performed', async function() {
67-
model.setToken('https://api.github.com', '1234');
68-
69-
sinon.spy(model, 'getToken');
70-
sinon.stub(model, 'getScopes').resolves(GithubLoginModel.REQUIRED_SCOPES);
71-
72-
const wrapper = shallow(buildApp());
73-
74-
assert.strictEqual(await wrapper.find('ObserveModel').prop('fetchData')(model), '1234');
75-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
76-
77-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({
58+
const resultWrapper = wrapper.find(QueryRenderer).renderProp('render')({
7859
error: null,
7960
props: null,
8061
retry: () => {},
@@ -83,68 +64,18 @@ describe('RemoteContainer', function() {
8364
assert.isTrue(resultWrapper.exists('LoadingView'));
8465
});
8566

86-
it('renders a login prompt if no token is found', function() {
87-
const wrapper = shallow(buildApp());
88-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(UNAUTHENTICATED);
89-
assert.isTrue(tokenWrapper.exists('GithubLoginView'));
90-
});
91-
92-
it('renders a login prompt if the token has insufficient OAuth scopes', function() {
93-
const wrapper = shallow(buildApp());
94-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(INSUFFICIENT);
95-
96-
assert.match(tokenWrapper.find('GithubLoginView').find('p').text(), /sufficient/);
97-
});
98-
99-
it('renders an offline view if the user is offline', function() {
100-
sinon.spy(model, 'didUpdate');
101-
102-
const wrapper = shallow(buildApp());
103-
const e = new Error('oh no');
104-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(e);
105-
assert.isTrue(tokenWrapper.exists('QueryErrorView'));
106-
107-
tokenWrapper.find('QueryErrorView').prop('retry')();
108-
assert.isTrue(model.didUpdate.called);
109-
});
110-
11167
it('renders an error message if the GraphQL query fails', function() {
11268
const wrapper = shallow(buildApp());
113-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
11469

11570
const error = new Error('oh shit!');
11671
error.rawStack = error.stack;
117-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({error, props: null, retry: () => {}});
72+
const resultWrapper = wrapper.find(QueryRenderer).renderProp('render')({error, props: null, retry: () => {}});
11873

11974
assert.strictEqual(resultWrapper.find('QueryErrorView').prop('error'), error);
12075
});
12176

122-
it('increments a counter on login', function() {
123-
const incrementCounterStub = sinon.stub(reporterProxy, 'incrementCounter');
124-
125-
const wrapper = shallow(buildApp());
126-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')(UNAUTHENTICATED);
127-
128-
tokenWrapper.find('GithubLoginView').prop('onLogin')();
129-
assert.isTrue(incrementCounterStub.calledOnceWith('github-login'));
130-
});
131-
132-
it('increments a counter on logout', function() {
133-
const wrapper = shallow(buildApp());
134-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
135-
136-
const error = new Error('just show the logout button');
137-
error.rawStack = error.stack;
138-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({error, props: null, retry: () => {}});
139-
140-
const incrementCounterStub = sinon.stub(reporterProxy, 'incrementCounter');
141-
resultWrapper.find('QueryErrorView').prop('logout')();
142-
assert.isTrue(incrementCounterStub.calledOnceWith('github-logout'));
143-
});
144-
14577
it('renders the controller once results have arrived', function() {
14678
const wrapper = shallow(buildApp());
147-
const tokenWrapper = wrapper.find('ObserveModel').renderProp('children')('1234');
14879

14980
const props = queryBuilder(remoteQuery)
15081
.repository(r => {
@@ -155,10 +86,9 @@ describe('RemoteContainer', function() {
15586
});
15687
})
15788
.build();
158-
const resultWrapper = tokenWrapper.find(QueryRenderer).renderProp('render')({error: null, props, retry: () => {}});
89+
const resultWrapper = wrapper.find(QueryRenderer).renderProp('render')({error: null, props, retry: () => {}});
15990

16091
const controller = resultWrapper.find('RemoteController');
161-
assert.strictEqual(controller.prop('token'), '1234');
16292
assert.deepEqual(controller.prop('repository'), {
16393
id: 'the-repo',
16494
defaultBranchRef: {

0 commit comments

Comments
 (0)