|
| 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 | + |
1 | 7 | const { normalizeValues } = require('./helm');
|
2 | 8 | const { validatePipelineSpec, validatePipelineYaml } = require('./validation');
|
3 | 9 | const { followLogs } = require('./workflow');
|
| 10 | +const { printTableForAuthContexts } = require('./auth'); |
4 | 11 | const { sdk } = require('../../../logic');
|
5 | 12 |
|
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 | +})); |
13 | 18 |
|
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)); |
21 | 26 |
|
22 | 27 | const request = require('requestretry');
|
23 | 28 |
|
@@ -61,4 +66,127 @@ describe('helpers using sdk', () => {
|
61 | 66 | await verifyResponsesReturned([DEFAULT_RESPONSE]); // eslint-disable-line
|
62 | 67 | });
|
63 | 68 | });
|
| 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 | + }); |
64 | 192 | });
|
0 commit comments