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

Commit 9f5ec4b

Browse files
committed
Move login/logout actions to GithubTabController
1 parent c506ed9 commit 9f5ec4b

File tree

2 files changed

+73
-61
lines changed

2 files changed

+73
-61
lines changed

lib/controllers/github-tab-controller.js

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,30 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33

44
import {
5-
GithubLoginModelPropType, RefHolderPropType, RemoteSetPropType, BranchSetPropType, RefresherPropType,
5+
GithubLoginModelPropType, TokenPropType, RefHolderPropType,
6+
RemoteSetPropType, RemotePropType, BranchSetPropType, BranchPropType,
7+
RefresherPropType,
68
} from '../prop-types';
79
import GitHubTabView from '../views/github-tab-view';
10+
import {incrementCounter} from '../reporter-proxy';
811

912
export default class GitHubTabController extends React.Component {
1013
static propTypes = {
1114
workspace: PropTypes.object.isRequired,
1215
refresher: RefresherPropType.isRequired,
1316
loginModel: GithubLoginModelPropType.isRequired,
17+
token: TokenPropType,
1418
rootHolder: RefHolderPropType.isRequired,
1519

1620
workingDirectory: PropTypes.string,
1721
repository: PropTypes.object.isRequired,
1822
allRemotes: RemoteSetPropType.isRequired,
23+
githubRemotes: RemoteSetPropType.isRequired,
24+
currentRemote: RemotePropType.isRequired,
1925
branches: BranchSetPropType.isRequired,
20-
selectedRemoteName: PropTypes.string,
21-
aheadCount: PropTypes.number,
26+
currentBranch: BranchPropType.isRequired,
27+
aheadCount: PropTypes.number.isRequired,
28+
manyRemotesAvailable: PropTypes.bool.isRequired,
2229
pushInProgress: PropTypes.bool.isRequired,
2330
isLoading: PropTypes.bool.isRequired,
2431
currentWorkDir: PropTypes.string,
@@ -35,21 +42,11 @@ export default class GitHubTabController extends React.Component {
3542
}
3643

3744
render() {
38-
const gitHubRemotes = this.props.allRemotes.filter(remote => remote.isGithubRepo());
39-
const currentBranch = this.props.branches.getHeadBranch();
40-
41-
let currentRemote = gitHubRemotes.withName(this.props.selectedRemoteName);
42-
let manyRemotesAvailable = false;
43-
if (!currentRemote.isPresent() && gitHubRemotes.size() === 1) {
44-
currentRemote = Array.from(gitHubRemotes)[0];
45-
} else if (!currentRemote.isPresent() && gitHubRemotes.size() > 1) {
46-
manyRemotesAvailable = true;
47-
}
48-
4945
return (
5046
<GitHubTabView
5147
// Connection
52-
loginModel={this.props.loginModel}
48+
endpoint={this.currentEndpoint()}
49+
token={this.props.token}
5350

5451
workspace={this.props.workspace}
5552
refresher={this.props.refresher}
@@ -59,14 +56,17 @@ export default class GitHubTabController extends React.Component {
5956
contextLocked={this.props.contextLocked}
6057
repository={this.props.repository}
6158
branches={this.props.branches}
62-
currentBranch={currentBranch}
63-
remotes={gitHubRemotes}
64-
currentRemote={currentRemote}
65-
manyRemotesAvailable={manyRemotesAvailable}
59+
currentBranch={this.props.currentBranch}
60+
remotes={this.props.githubRemotes}
61+
currentRemote={this.props.currentRemote}
62+
manyRemotesAvailable={this.props.manyRemotesAvailable}
6663
aheadCount={this.props.aheadCount}
6764
pushInProgress={this.props.pushInProgress}
6865
isLoading={this.props.isLoading}
6966

67+
handleLogin={this.handleLogin}
68+
handleLogout={this.handleLogout}
69+
handleTokenRetry={this.handleTokenRetry}
7070
handlePushBranch={this.handlePushBranch}
7171
handleRemoteSelect={this.handleRemoteSelect}
7272
changeWorkingDirectory={this.props.changeWorkingDirectory}
@@ -94,4 +94,20 @@ export default class GitHubTabController extends React.Component {
9494
}
9595

9696
openBoundPublishDialog = () => this.props.openPublishDialog(this.props.repository);
97+
98+
handleLogin = token => {
99+
incrementCounter('github-login');
100+
this.props.loginModel.setToken(this.currentEndpoint().getLoginAccount(), token);
101+
}
102+
103+
handleLogout = () => {
104+
incrementCounter('github-logout');
105+
this.props.loginModel.removeToken(this.currentEndpoint().getLoginAccount());
106+
}
107+
108+
handleTokenRetry = () => this.props.loginModel.didUpdate();
109+
110+
currentEndpoint() {
111+
return this.props.currentRemote.getEndpointOrDotcom();
112+
}
97113
}

test/controllers/github-tab-controller.test.js

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import Repository from '../../lib/models/repository';
66
import BranchSet from '../../lib/models/branch-set';
77
import Branch, {nullBranch} from '../../lib/models/branch';
88
import RemoteSet from '../../lib/models/remote-set';
9-
import Remote from '../../lib/models/remote';
10-
import {InMemoryStrategy} from '../../lib/shared/keytar-strategy';
9+
import Remote, {nullRemote} from '../../lib/models/remote';
10+
import {DOTCOM} from '../../lib/models/endpoint';
11+
import {InMemoryStrategy, UNAUTHENTICATED} from '../../lib/shared/keytar-strategy';
1112
import GithubLoginModel from '../../lib/models/github-login-model';
1213
import RefHolder from '../../lib/models/ref-holder';
1314
import Refresher from '../../lib/models/refresher';
15+
import * as reporterProxy from '../../lib/reporter-proxy';
1416

1517
import {buildRepository, cloneRepository} from '../helpers';
1618

@@ -34,12 +36,18 @@ describe('GitHubTabController', function() {
3436
workspace={atomEnv.workspace}
3537
refresher={new Refresher()}
3638
loginModel={new GithubLoginModel(InMemoryStrategy)}
39+
token="1234"
3740
rootHolder={new RefHolder()}
3841

3942
workingDirectory={repo.getWorkingDirectoryPath()}
4043
repository={repo}
4144
allRemotes={new RemoteSet()}
45+
githubRemotes={new RemoteSet()}
46+
currentRemote={nullRemote}
4247
branches={new BranchSet()}
48+
currentBranch={nullBranch}
49+
aheadCount={0}
50+
manyRemotesAvailable={false}
4351
pushInProgress={false}
4452
isLoading={false}
4553
currentWorkDir={repo.getWorkingDirectoryPath()}
@@ -60,48 +68,15 @@ describe('GitHubTabController', function() {
6068
}
6169

6270
describe('derived view props', function() {
63-
const dotcom0 = new Remote('yes0', '[email protected]:aaa/bbb.git');
64-
const dotcom1 = new Remote('yes1', 'https://github.com/ccc/ddd.git');
65-
const nonDotcom = new Remote('no0', '[email protected]:eee/fff.git');
66-
67-
it('passes the current branch', function() {
68-
const currentBranch = new Branch('aaa', nullBranch, nullBranch, true);
69-
const otherBranch = new Branch('bbb');
70-
const branches = new BranchSet([currentBranch, otherBranch]);
71-
const wrapper = shallow(buildApp({branches}));
72-
73-
assert.strictEqual(wrapper.find('GitHubTabView').prop('currentBranch'), currentBranch);
74-
});
75-
76-
it('passes remotes hosted on GitHub', function() {
77-
const allRemotes = new RemoteSet([dotcom0, dotcom1, nonDotcom]);
78-
const wrapper = shallow(buildApp({allRemotes}));
79-
80-
const passed = wrapper.find('GitHubTabView').prop('remotes');
81-
assert.isTrue(passed.withName('yes0').isPresent());
82-
assert.isTrue(passed.withName('yes1').isPresent());
83-
assert.isFalse(passed.withName('no0').isPresent());
84-
});
85-
86-
it('detects an explicitly specified current remote', function() {
87-
const allRemotes = new RemoteSet([dotcom0, dotcom1, nonDotcom]);
88-
const wrapper = shallow(buildApp({allRemotes, selectedRemoteName: 'yes1'}));
89-
assert.strictEqual(wrapper.find('GitHubTabView').prop('currentRemote'), dotcom1);
90-
assert.isFalse(wrapper.find('GitHubTabView').prop('manyRemotesAvailable'));
71+
it('passes the endpoint from the current GitHub remote when one exists', function() {
72+
const remote = new Remote('hub', '[email protected]:some/repo.git');
73+
const wrapper = shallow(buildApp({currentRemote: remote}));
74+
assert.strictEqual(wrapper.find('GitHubTabView').prop('endpoint'), remote.getEndpoint());
9175
});
9276

93-
it('uses a single GitHub-hosted remote', function() {
94-
const allRemotes = new RemoteSet([dotcom0, nonDotcom]);
95-
const wrapper = shallow(buildApp({allRemotes}));
96-
assert.strictEqual(wrapper.find('GitHubTabView').prop('currentRemote'), dotcom0);
97-
assert.isFalse(wrapper.find('GitHubTabView').prop('manyRemotesAvailable'));
98-
});
99-
100-
it('indicates when multiple remotes are available', function() {
101-
const allRemotes = new RemoteSet([dotcom0, dotcom1]);
102-
const wrapper = shallow(buildApp({allRemotes}));
103-
assert.isFalse(wrapper.find('GitHubTabView').prop('currentRemote').isPresent());
104-
assert.isTrue(wrapper.find('GitHubTabView').prop('manyRemotesAvailable'));
77+
it('defaults the endpoint to dotcom', function() {
78+
const wrapper = shallow(buildApp({currentRemote: nullRemote}));
79+
assert.strictEqual(wrapper.find('GitHubTabView').prop('endpoint'), DOTCOM);
10580
});
10681
});
10782

@@ -139,5 +114,26 @@ describe('GitHubTabController', function() {
139114
wrapper.find('GitHubTabView').prop('openBoundPublishDialog')();
140115
assert.isTrue(openPublishDialog.calledWith(someRepo));
141116
});
117+
118+
it('handles and instruments a login', async function() {
119+
sinon.stub(reporterProxy, 'incrementCounter');
120+
const loginModel = new GithubLoginModel(InMemoryStrategy);
121+
122+
const wrapper = shallow(buildApp({loginModel}));
123+
await wrapper.find('GitHubTabView').prop('handleLogin')('good-token');
124+
assert.strictEqual(await loginModel.getToken(DOTCOM.getLoginAccount()), 'good-token');
125+
assert.isTrue(reporterProxy.incrementCounter.calledWith('github-login'));
126+
});
127+
128+
it('handles and instruments a logout', async function() {
129+
sinon.stub(reporterProxy, 'incrementCounter');
130+
const loginModel = new GithubLoginModel(InMemoryStrategy);
131+
await loginModel.setToken(DOTCOM.getLoginAccount(), 'good-token');
132+
133+
const wrapper = shallow(buildApp({loginModel}));
134+
await wrapper.find('GitHubTabView').prop('handleLogout')();
135+
assert.strictEqual(await loginModel.getToken(DOTCOM.getLoginAccount()), UNAUTHENTICATED);
136+
assert.isTrue(reporterProxy.incrementCounter.calledWith('github-logout'));
137+
});
142138
});
143139
});

0 commit comments

Comments
 (0)