Skip to content

Commit 77426fb

Browse files
fix print context table + tests (#308)
1 parent 9c3e86b commit 77426fb

File tree

2 files changed

+152
-30
lines changed

2 files changed

+152
-30
lines changed

lib/interface/cli/helpers/auth.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,62 @@
11
const _ = require('lodash');
22
const columnify = require('columnify');
3+
const chalk = require('chalk');
34
const kefir = require('kefir');
45
const debug = require('debug')('codefresh:cli:helpers:auth');
56
const { Config } = require('codefresh-sdk');
67
const { sdk } = require('../../../logic');
78

8-
const printTableForAuthContexts = ({ filter = 'all' }) => {
9+
const printTableForAuthContexts = ({ filter = 'all' } = {}) => {
910
const configManager = Config.manager();
1011
const currentContext = configManager.getCurrentContext();
1112
const sdkContext = sdk.config && sdk.config.context;
12-
if (!currentContext && !sdkContext) {
13+
if (!currentContext && (!sdkContext || sdkContext.isNoAuth)) {
1314
console.log('No authentication contexts. Please create an authentication context (see codefresh auth create-context --help)');
14-
return;
15+
return Promise.resolve();
1516
}
1617

1718
let contexts;
18-
if (currentContext.name !== sdkContext.name) {
19+
if (!currentContext || currentContext.name !== sdkContext.name) {
1920
console.log('Using authentication context created from CF_API_KEY env variable:');
2021
sdkContext.current = true;
2122
contexts = [sdkContext];
2223
} else if (filter === 'all') {
2324
contexts = _.values(configManager.getAllContexts());
2425
} else if (filter === 'current') {
25-
const ctx = configManager.getCurrentContext();
26-
contexts = ctx ? [ctx] : [];
26+
contexts = currentContext ? [currentContext] : [];
2727
}
2828

2929

30-
const res = [];
3130
let mergedKeys = [];
32-
let contextsInfo = {};
3331
return kefir.sequentially(0, contexts)
3432
.flatMap((context) => {
35-
// let context = configManager.getContextByName(contextName);
3633
debug(`context to check ${context.name}`);
3734
return kefir.fromPromise(context.addAccountInfo()
3835
.then(() => {
3936
mergedKeys = _.union(mergedKeys, context.defaultColumns);
40-
let ret = _.chain(context)
37+
return _.chain(context)
4138
.pick(context.defaultColumns)
4239
.mapValues((value, key) => {
43-
if (key === 'current') return '*'.green;
40+
if (key === 'current') return chalk.green('*');
4441
return _.isString(value) ? value : JSON.stringify(value);
4542
})
4643
.value();
47-
48-
return ret;
4944
}));
5045
})
5146
.scan((prev, context) => {
5247
prev.push(context);
5348
return prev;
5449
}, [])
5550
.flatMap((info) => {
56-
info = _.sortBy(info, ['name']);
57-
output = columnify(info, { columns: mergedKeys });
51+
info = _.sortBy(info, ['name']); // eslint-disable-line
52+
const output = columnify(info, { columns: mergedKeys });
5853
return kefir.constant(output);
5954
})
6055
.ignoreErrors()
6156
.toPromise()
6257
.then((output) => {
6358
console.log(output);
6459
});
65-
6660
};
6761

6862
module.exports = { printTableForAuthContexts };

lib/interface/cli/helpers/helpers.sdk.spec.js

Lines changed: 142 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
1+
const _ = require('lodash');
2+
const columnify = require('columnify');
3+
const kefir = require('kefir');
4+
const chalk = require('chalk');
5+
const { Config } = require('codefresh-sdk');
6+
17
const { normalizeValues } = require('./helm');
28
const { validatePipelineSpec, validatePipelineYaml } = require('./validation');
39
const { followLogs } = require('./workflow');
10+
const { printTableForAuthContexts } = require('./auth');
411
const { sdk } = require('../../../logic');
512

6-
jest.mock('../../../logic/entities/Context', () => {
7-
return {
8-
fromResponse: () => ({
9-
getType: () => 'yaml',
10-
}),
11-
};
12-
});
13+
jest.mock('../../../logic/entities/Context', () => ({
14+
fromResponse: () => ({
15+
getType: () => 'yaml',
16+
}),
17+
}));
1318

14-
jest.mock('../../../logic/entities/Workflow', () => {
15-
return {
16-
fromResponse: () => ({
17-
getStatus: () => 0,
18-
}),
19-
};
20-
});
19+
jest.mock('../../../logic/entities/Workflow', () => ({
20+
fromResponse: () => ({
21+
getStatus: () => 0,
22+
}),
23+
}));
24+
25+
jest.mock('columnify', () => jest.fn(data => data));
2126

2227
const request = require('requestretry');
2328

@@ -61,4 +66,127 @@ describe('helpers using sdk', () => {
6166
await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
6267
});
6368
});
69+
70+
describe('auth', () => {
71+
describe('printTableForAuthContexts', () => {
72+
beforeEach(() => {
73+
jest.spyOn(console, 'log');
74+
jest.spyOn(kefir, 'sequentially');
75+
columnify.mockClear();
76+
const addAccountInfo = jest.fn(() => Promise.resolve());
77+
const defaultColumns = ['current', 'name'];
78+
const contexts = {
79+
current: {
80+
addAccountInfo,
81+
defaultColumns,
82+
current: true,
83+
name: 'current',
84+
},
85+
nonCurrent: {
86+
addAccountInfo,
87+
defaultColumns,
88+
name: 'nonCurrent',
89+
},
90+
};
91+
const managerMock = {
92+
getCurrentContext: jest.fn(() => contexts.current),
93+
getAllContexts: jest.fn(() => contexts),
94+
};
95+
Config.manager = () => managerMock;
96+
Config.MOCK_CONTEXTS = contexts;
97+
Config.ADD_ACCOUNT_INFO_MOCK = addAccountInfo;
98+
});
99+
100+
it('should log fallback message when no current context and no sdk context', async () => {
101+
Config.manager().getCurrentContext = jest.fn(() => undefined);
102+
sdk.config.context = undefined;
103+
104+
await printTableForAuthContexts();
105+
106+
expect(console.log).toBeCalledWith('No authentication contexts. Please create an authentication context (see codefresh auth create-context --help)'); // eslint-disable-line
107+
expect(Config.manager().getCurrentContext).toBeCalled();
108+
expect(kefir.sequentially).not.toBeCalled();
109+
});
110+
111+
it('should log fallback message when no current context and when sdk context isNoAuth', async () => {
112+
Config.manager().getCurrentContext = jest.fn(() => undefined);
113+
sdk.config.context = { isNoAuth: true };
114+
115+
await printTableForAuthContexts();
116+
117+
expect(console.log).toBeCalledWith('No authentication contexts. Please create an authentication context (see codefresh auth create-context --help)'); // eslint-disable-line
118+
expect(Config.manager().getCurrentContext).toBeCalled();
119+
expect(kefir.sequentially).not.toBeCalled();
120+
});
121+
122+
it('should log environment context when no current context but sdk context exists', async () => {
123+
const mockContext = Config.manager().getCurrentContext();
124+
const verifyContext = _.merge(_.pick(mockContext, mockContext.defaultColumns), { current: chalk.green('*') });
125+
sdk.config.context = mockContext;
126+
Config.manager().getCurrentContext = jest.fn(() => undefined);
127+
128+
await printTableForAuthContexts();
129+
130+
expect(console.log).toBeCalledWith('Using authentication context created from CF_API_KEY env variable:');
131+
expect(console.log).lastCalledWith([verifyContext]);
132+
expect(Config.manager().getCurrentContext).toBeCalled();
133+
expect(Config.manager().getAllContexts).not.toBeCalled();
134+
expect(kefir.sequentially).toBeCalled();
135+
});
136+
137+
it('should log environment context when there is current context but sdk context differs', async () => {
138+
const mockContext = _.clone(Config.manager().getCurrentContext());
139+
mockContext.name = 'sdk context';
140+
const verifyContext = _.merge(_.pick(mockContext, mockContext.defaultColumns), { current: chalk.green('*') });
141+
sdk.config.context = mockContext;
142+
143+
await printTableForAuthContexts();
144+
145+
expect(console.log).toBeCalledWith('Using authentication context created from CF_API_KEY env variable:');
146+
expect(console.log).lastCalledWith([verifyContext]);
147+
expect(Config.manager().getCurrentContext).toBeCalled();
148+
expect(Config.manager().getAllContexts).not.toBeCalled();
149+
expect(kefir.sequentially).toBeCalled();
150+
});
151+
152+
it('should log all contexts when filter is "all"', async () => {
153+
sdk.config.context = Config.manager().getCurrentContext();
154+
const verifyContexts = _.map(_.values(Config.MOCK_CONTEXTS), (context) => {
155+
return _.mapValues(_.pick(context, context.defaultColumns), (val, key) => {
156+
if (key === 'current') return chalk.green('*');
157+
return val;
158+
});
159+
});
160+
161+
await printTableForAuthContexts();
162+
163+
expect(console.log).toBeCalledWith(verifyContexts);
164+
expect(Config.manager().getCurrentContext).toBeCalled();
165+
expect(Config.manager().getAllContexts).toBeCalled();
166+
expect(kefir.sequentially).toBeCalled();
167+
});
168+
169+
it('should log current context when filter is "current"', async () => {
170+
const mockContext = Config.manager().getCurrentContext();
171+
const verifyContext = _.merge(_.pick(mockContext, mockContext.defaultColumns), { current: chalk.green('*') });
172+
sdk.config.context = mockContext;
173+
174+
await printTableForAuthContexts({ filter: 'current' });
175+
176+
expect(console.log).lastCalledWith([verifyContext]);
177+
expect(Config.manager().getCurrentContext).toBeCalled();
178+
expect(Config.manager().getAllContexts).not.toBeCalled();
179+
expect(kefir.sequentially).toBeCalled();
180+
});
181+
182+
it('should add account info for each context', async () => {
183+
sdk.config.context = Config.manager().getCurrentContext();
184+
185+
await printTableForAuthContexts();
186+
187+
expect(kefir.sequentially).toBeCalled();
188+
expect(Config.ADD_ACCOUNT_INFO_MOCK).toBeCalledTimes(_.keys(Config.MOCK_CONTEXTS).length);
189+
});
190+
});
191+
});
64192
});

0 commit comments

Comments
 (0)