Skip to content

Commit

Permalink
setting the stage for more tests and future enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
machellerogden committed Feb 28, 2017
1 parent a4574a2 commit 82d530c
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 26 deletions.
6 changes: 4 additions & 2 deletions drivers/bitbucket-server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ const axios = require('axios');
const Promise = require('bluebird');
const inquirer = require('inquirer');

module.exports = bitbucketSourcePromise;
module.exports = {
getConfiguredRepos
};

function bitbucketSourcePromise(config) {
function getConfiguredRepos(config) {
const bitbucketConfig = _.find(config.drivers, { type: 'bitbucket-server' });
if (bitbucketConfig && bitbucketConfig.host) {
return inquirer.prompt([
Expand Down
34 changes: 18 additions & 16 deletions drivers/github/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,24 @@ const _ = require('lodash');
const axios = require('axios');
const Promise = require('bluebird');

module.exports = githubSourcePromise;
module.exports = {
getConfiguredRepos
};

function getConfiguredRepos(config) {
const githubConfig = _.find(config.drivers, { type: 'github' });
let resultPromises = [];
if (githubConfig && githubConfig.accessToken) {
resultPromises.push(getOwnRepos(githubConfig));
}
if (githubConfig && githubConfig.usernames) {
resultPromises = resultPromises.concat(getReposFromUsernames(githubConfig));
}
if (!resultPromises.length) {
return Promise.resolve([]);
}
return Promise.all(resultPromises);
}

function parseLinkHeader(header) {
if (header.length === 0) throw new Error("input must not be of zero length");
Expand Down Expand Up @@ -60,18 +77,3 @@ function getReposFromUsernames(config) {
return sources;
}, []);
}

function githubSourcePromise(config) {
const githubConfig = _.find(config.drivers, { type: 'github' });
let resultPromises = [];
if (githubConfig && githubConfig.accessToken) {
resultPromises.push(getOwnRepos(githubConfig));
}
if (githubConfig && githubConfig.usernames) {
resultPromises = resultPromises.concat(getReposFromUsernames(githubConfig));
}
if (!resultPromises.length) {
return Promise.resolve([]);
}
return Promise.all(resultPromises);
}
2 changes: 1 addition & 1 deletion lib/getSourceData.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function getSourceData(forceRefresh = false) {
} else {
output.log('Loading cache...');
const selected = _.pick(drivers, _.map(rc.drivers, (v) => v.type));
return Promise.props(_.mapValues(selected, (driver) => driver(rc)))
return Promise.props(_.mapValues(selected, (driver) => driver.getConfiguredRepos(rc)))
.then((sourceData) => {
sourceData = _.mapValues(sourceData, _.flow(_.flattenDeep, _.compact));
sourceData = _.mapValues(sourceData, (entries) => _.uniqBy(entries, 'name'));
Expand Down
36 changes: 29 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ describe('the bitcar cli', () => {
});
sandbox.stub(browser, 'open', mocks.open);
sandbox.stub(fs, 'commit', (cb) => cb());
sandbox.stub(config, 'get', () => mocks.config);
sandbox.stub(inquirer, 'prompt', (options) => {
const setupPromptValidation = schemas.setupPrompt.validate(options);
if (!setupPromptValidation.error) return Promise.resolve(mocks.setupAnswers);
Expand Down Expand Up @@ -233,40 +232,63 @@ describe('the bitcar cli', () => {
sandbox.stub(output, 'log');
});
it('should call each configured driver', () => {
sandbox.stub(drivers, 'bitbucket-server', () => Promise.resolve([]));
sandbox.stub(drivers, 'github', () => Promise.resolve([]));
sandbox.stub(drivers['bitbucket-server'], 'getConfiguredRepos', () => Promise.resolve([]));
sandbox.stub(drivers.github, 'getConfiguredRepos', () => Promise.resolve([]));
return cli({ _: [ ], refresh: true })
.then(() => {
expect(drivers['bitbucket-server']).to.have.been.called;
expect(drivers.github).to.have.been.called;
expect(drivers['bitbucket-server'].getConfiguredRepos).to.have.been.called;
expect(drivers.github.getConfiguredRepos).to.have.been.called;
});
});
});
describe('bitbucket-server driver', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
sandbox.stub(axios, 'request', () => Promise.resolve(mocks.bitbucketServerResponse));
sandbox.stub(drivers, 'github', () => Promise.resolve([]));
sandbox.stub(drivers.github, 'getConfiguredRepos', () => Promise.resolve([]));
});
it('should call axios request', () => {
sandbox.stub(config, 'get', () => mocks.config);
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.request).to.have.been.called;
});
});
it('shouldn\'t make any requests if there is no config', () => {
sandbox.stub(config, 'get', () => mocks.configWithoutBitbucketServer);
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.request).not.to.have.been.called;
});
});
});
describe('github driver', () => {
beforeEach(() => {
sandbox.stub(output, 'log');
sandbox.stub(axios, 'get', () => Promise.resolve(mocks.githubResponse));
sandbox.stub(drivers, 'bitbucket-server', () => Promise.resolve([]));
sandbox.stub(drivers['bitbucket-server'], 'getConfiguredRepos', () => Promise.resolve([]));
});
it('should call axios request', () => {
sandbox.stub(config, 'get', () => mocks.config);
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.get).to.have.been.called;
});
});
it('should make requests for any usernames given in config', () => {
sandbox.stub(config, 'get', () => mocks.configWithUsernames);
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.get).to.have.been.called;
});
});
it('shouldn\'t make any requests if there is no config', () => {
sandbox.stub(config, 'get', () => mocks.configWithoutGithub);
return cli({ _: [ ], refresh: true })
.then(() => {
expect(axios.get).not.to.have.been.called;
});
});
});
});

37 changes: 37 additions & 0 deletions test/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,43 @@ exports.config = {
]
};

exports.configWithUsernames = {
alias: "bit",
drivers: [
{
type: "github",
host: "github.com",
accessToken: "9eccdc79e394f713624486c0272f44fe67267b97",
usernames: [ 'google' ]
},
{
type: "bitbucket-server",
host: "git.cars.com"
}
]
};

exports.configWithoutGithub = {
alias: "bit",
drivers: [
{
type: "bitbucket-server",
host: "git.cars.com"
}
]
};

exports.configWithoutBitbucketServer = {
alias: "bit",
drivers: [
{
type: "github",
host: "github.com",
accessToken: "9eccdc79e394f713624486c0272f44fe67267b97"
}
]
};

exports.open = function (url, cb) {
cb(null);
};

0 comments on commit 82d530c

Please sign in to comment.