|
| 1 | +import assert from 'assert'; |
| 2 | +import sinon from 'sinon'; |
| 3 | +import auth from '../../../../Auth.js'; |
| 4 | +import { cli } from '../../../../cli/cli.js'; |
| 5 | +import { CommandInfo } from '../../../../cli/CommandInfo.js'; |
| 6 | +import { Logger } from '../../../../cli/Logger.js'; |
| 7 | +import { CommandError } from '../../../../Command.js'; |
| 8 | +import request from '../../../../request.js'; |
| 9 | +import { telemetry } from '../../../../telemetry.js'; |
| 10 | +import { odata } from '../../../../utils/odata.js'; |
| 11 | +import { pid } from '../../../../utils/pid.js'; |
| 12 | +import { session } from '../../../../utils/session.js'; |
| 13 | +import { sinonUtil } from '../../../../utils/sinonUtil.js'; |
| 14 | +import { z } from 'zod'; |
| 15 | +import commands from '../../commands.js'; |
| 16 | +import command from './brandcenter-colors-list.js'; |
| 17 | + |
| 18 | +describe(commands.BRANDCENTER_COLORS_LIST, () => { |
| 19 | + let log: any[]; |
| 20 | + let logger: Logger; |
| 21 | + let loggerLogSpy: sinon.SinonSpy; |
| 22 | + let commandInfo: CommandInfo; |
| 23 | + let commandOptionsSchema: z.ZodTypeAny; |
| 24 | + |
| 25 | + const configurationResponseWithColors = { |
| 26 | + "BrandColorsListId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", |
| 27 | + "BrandColorsListUrl": { |
| 28 | + "DecodedUrl": "https://contoso.sharepoint.com/sites/BrandGuide/_catalogs/brandcolors" |
| 29 | + }, |
| 30 | + "BrandFontLibraryId": "23af51de-856c-4d00-aa11-0d03af0e46e3", |
| 31 | + "BrandFontLibraryUrl": { |
| 32 | + "DecodedUrl": "https://contoso.sharepoint.com/sites/BrandGuide/Fonts" |
| 33 | + }, |
| 34 | + "IsBrandCenterSiteFeatureEnabled": true, |
| 35 | + "IsPublicCdnEnabled": true, |
| 36 | + "SiteId": "52b46e48-9c0c-40cb-a955-13eb6c717ff3", |
| 37 | + "SiteUrl": "https://contoso.sharepoint.com/sites/BrandGuide" |
| 38 | + }; |
| 39 | + |
| 40 | + const configurationResponseWithoutColors = { |
| 41 | + "BrandColorsListId": "00000000-0000-0000-0000-000000000000", |
| 42 | + "BrandColorsListUrl": null, |
| 43 | + "BrandFontLibraryId": "23af51de-856c-4d00-aa11-0d03af0e46e3", |
| 44 | + "BrandFontLibraryUrl": { |
| 45 | + "DecodedUrl": "https://contoso.sharepoint.com/sites/BrandGuide/Fonts" |
| 46 | + }, |
| 47 | + "IsBrandCenterSiteFeatureEnabled": true, |
| 48 | + "IsPublicCdnEnabled": true, |
| 49 | + "SiteId": "52b46e48-9c0c-40cb-a955-13eb6c717ff3", |
| 50 | + "SiteUrl": "https://contoso.sharepoint.com/sites/BrandGuide" |
| 51 | + }; |
| 52 | + |
| 53 | + const brandColorsListItems = [ |
| 54 | + { |
| 55 | + "OData__SPColorTitle": "Primary", |
| 56 | + "OData__SPColorCode": "#0078D4", |
| 57 | + "OData__SPColorVisible": true |
| 58 | + }, |
| 59 | + { |
| 60 | + "OData__SPColorTitle": "Secondary", |
| 61 | + "OData__SPColorCode": "#FF4500", |
| 62 | + "OData__SPColorVisible": false |
| 63 | + } |
| 64 | + ]; |
| 65 | + |
| 66 | + const mappedBrandColors = [ |
| 67 | + { |
| 68 | + "Title": "Primary", |
| 69 | + "ColorCode": "#0078D4", |
| 70 | + "IsVisible": true |
| 71 | + }, |
| 72 | + { |
| 73 | + "Title": "Secondary", |
| 74 | + "ColorCode": "#FF4500", |
| 75 | + "IsVisible": false |
| 76 | + } |
| 77 | + ]; |
| 78 | + |
| 79 | + before(() => { |
| 80 | + sinon.stub(auth, 'restoreAuth').resolves(); |
| 81 | + sinon.stub(telemetry, 'trackEvent').resolves(); |
| 82 | + sinon.stub(pid, 'getProcessName').returns(''); |
| 83 | + sinon.stub(session, 'getId').returns(''); |
| 84 | + |
| 85 | + auth.connection.active = true; |
| 86 | + auth.connection.spoUrl = 'https://contoso.sharepoint.com'; |
| 87 | + commandInfo = cli.getCommandInfo(command); |
| 88 | + commandOptionsSchema = commandInfo.command.getSchemaToParse()!; |
| 89 | + }); |
| 90 | + |
| 91 | + beforeEach(() => { |
| 92 | + log = []; |
| 93 | + logger = { |
| 94 | + log: async (msg: string) => { |
| 95 | + log.push(msg); |
| 96 | + }, |
| 97 | + logRaw: async (msg: string) => { |
| 98 | + log.push(msg); |
| 99 | + }, |
| 100 | + logToStderr: async (msg: string) => { |
| 101 | + log.push(msg); |
| 102 | + } |
| 103 | + }; |
| 104 | + loggerLogSpy = sinon.spy(logger, 'log'); |
| 105 | + }); |
| 106 | + |
| 107 | + afterEach(() => { |
| 108 | + sinonUtil.restore([ |
| 109 | + request.get, |
| 110 | + odata.getAllItems |
| 111 | + ]); |
| 112 | + }); |
| 113 | + |
| 114 | + after(() => { |
| 115 | + sinon.restore(); |
| 116 | + auth.connection.active = false; |
| 117 | + auth.connection.spoUrl = undefined; |
| 118 | + }); |
| 119 | + |
| 120 | + it('has correct name', () => { |
| 121 | + assert.strictEqual(command.name, commands.BRANDCENTER_COLORS_LIST); |
| 122 | + }); |
| 123 | + |
| 124 | + it('has a description', () => { |
| 125 | + assert.notStrictEqual(command.description, null); |
| 126 | + }); |
| 127 | + |
| 128 | + it('defines correct default properties', () => { |
| 129 | + assert.deepStrictEqual(command.defaultProperties(), ['ColorCode', 'Title', 'IsVisible']); |
| 130 | + }); |
| 131 | + |
| 132 | + it('passes validation with no options', () => { |
| 133 | + const actual = commandOptionsSchema.safeParse({}); |
| 134 | + assert.strictEqual(actual.success, true); |
| 135 | + }); |
| 136 | + |
| 137 | + it('fails validation with unknown options', () => { |
| 138 | + const actual = commandOptionsSchema.safeParse({ option: "value" }); |
| 139 | + assert.strictEqual(actual.success, false); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns empty array when brand colors list does not exist', async () => { |
| 143 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 144 | + if (opts.url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration') { |
| 145 | + return configurationResponseWithoutColors; |
| 146 | + } |
| 147 | + |
| 148 | + throw 'Invalid request'; |
| 149 | + }); |
| 150 | + |
| 151 | + await command.action(logger, { options: {} }); |
| 152 | + assert(loggerLogSpy.calledOnceWithExactly([])); |
| 153 | + }); |
| 154 | + |
| 155 | + it('returns empty array when brand colors list does not exist (verbose)', async () => { |
| 156 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 157 | + if (opts.url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration') { |
| 158 | + return configurationResponseWithoutColors; |
| 159 | + } |
| 160 | + |
| 161 | + throw 'Invalid request'; |
| 162 | + }); |
| 163 | + |
| 164 | + await command.action(logger, { options: { verbose: true } }); |
| 165 | + assert(loggerLogSpy.calledOnceWithExactly([])); |
| 166 | + }); |
| 167 | + |
| 168 | + it('successfully lists brand center colors', async () => { |
| 169 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 170 | + if (opts.url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration') { |
| 171 | + return configurationResponseWithColors; |
| 172 | + } |
| 173 | + |
| 174 | + throw 'Invalid request'; |
| 175 | + }); |
| 176 | + |
| 177 | + sinon.stub(odata, 'getAllItems').callsFake(async (url) => { |
| 178 | + if (url === `https://contoso.sharepoint.com/sites/BrandGuide/_api/web/lists(guid'a1b2c3d4-e5f6-7890-abcd-ef1234567890')/items?$select=OData__SPColorTitle,OData__SPColorCode,OData__SPColorVisible`) { |
| 179 | + return brandColorsListItems; |
| 180 | + } |
| 181 | + |
| 182 | + throw 'Invalid request'; |
| 183 | + }); |
| 184 | + |
| 185 | + await command.action(logger, { options: {} }); |
| 186 | + assert(loggerLogSpy.calledOnceWithExactly(mappedBrandColors)); |
| 187 | + }); |
| 188 | + |
| 189 | + it('successfully lists brand center colors (verbose)', async () => { |
| 190 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 191 | + if (opts.url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration') { |
| 192 | + return configurationResponseWithColors; |
| 193 | + } |
| 194 | + |
| 195 | + throw 'Invalid request'; |
| 196 | + }); |
| 197 | + |
| 198 | + sinon.stub(odata, 'getAllItems').callsFake(async (url) => { |
| 199 | + if (url === `https://contoso.sharepoint.com/sites/BrandGuide/_api/web/lists(guid'a1b2c3d4-e5f6-7890-abcd-ef1234567890')/items?$select=OData__SPColorTitle,OData__SPColorCode,OData__SPColorVisible`) { |
| 200 | + return brandColorsListItems; |
| 201 | + } |
| 202 | + |
| 203 | + throw 'Invalid request'; |
| 204 | + }); |
| 205 | + |
| 206 | + await command.action(logger, { options: { verbose: true } }); |
| 207 | + assert(loggerLogSpy.calledOnceWithExactly(mappedBrandColors)); |
| 208 | + }); |
| 209 | + |
| 210 | + it('correctly handles error when retrieving configuration', async () => { |
| 211 | + sinon.stub(request, 'get').rejects({ |
| 212 | + "error": { |
| 213 | + "code": "accessDenied", |
| 214 | + "message": "Access denied" |
| 215 | + } |
| 216 | + }); |
| 217 | + |
| 218 | + await assert.rejects(command.action(logger, { options: {} }), |
| 219 | + new CommandError('Access denied')); |
| 220 | + }); |
| 221 | + |
| 222 | + it('correctly handles error when retrieving list items', async () => { |
| 223 | + sinon.stub(request, 'get').callsFake(async (opts) => { |
| 224 | + if (opts.url === 'https://contoso.sharepoint.com/_api/Brandcenter/Configuration') { |
| 225 | + return configurationResponseWithColors; |
| 226 | + } |
| 227 | + |
| 228 | + throw 'Invalid request'; |
| 229 | + }); |
| 230 | + |
| 231 | + sinon.stub(odata, 'getAllItems').rejects({ |
| 232 | + "error": { |
| 233 | + "code": "itemNotFound", |
| 234 | + "message": "The specified list was not found" |
| 235 | + } |
| 236 | + }); |
| 237 | + |
| 238 | + await assert.rejects(command.action(logger, { options: {} }), |
| 239 | + new CommandError('The specified list was not found')); |
| 240 | + }); |
| 241 | +}); |
0 commit comments