|
| 1 | +/* |
| 2 | + * Copyright 2018 Adobe. All rights reserved. |
| 3 | + * This file is licensed to you under the Apache License, Version 2.0 (the "License"); |
| 4 | + * you may not use this file except in compliance with the License. You may obtain a copy |
| 5 | + * of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + * |
| 7 | + * Unless required by applicable law or agreed to in writing, software distributed under |
| 8 | + * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS |
| 9 | + * OF ANY KIND, either express or implied. See the License for the specific language |
| 10 | + * governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +/* eslint-env mocha */ |
| 14 | + |
| 15 | +const assert = require('assert'); |
| 16 | +const fs = require('fs-extra'); |
| 17 | +const nock = require('nock'); |
| 18 | +const path = require('path'); |
| 19 | +const proxyquire = require('proxyquire'); |
| 20 | +const sinon = require('sinon'); |
| 21 | +const shell = require('shelljs'); |
| 22 | +const { Logger } = require('@adobe/helix-shared'); |
| 23 | +const { createTestRoot } = require('./utils.js'); |
| 24 | + |
| 25 | +const RemotePublishCommand = require('../src/remotepublish.cmd'); |
| 26 | + |
| 27 | +describe('hlx publish --custom-vcl (check params)', () => { |
| 28 | + it('hlx publish (no custom-vcl)', () => { |
| 29 | + const remote = new RemotePublishCommand() |
| 30 | + .withCustomVCLs(); |
| 31 | + |
| 32 | + // eslint-disable-next-line no-underscore-dangle |
| 33 | + assert.equal(remote._vcl, null); |
| 34 | + }); |
| 35 | + |
| 36 | + it('hlx publish --custom-vcl fixtures/vcl/extensions.vcl', async () => { |
| 37 | + const e = path.resolve(__dirname, 'fixtures/vcl/extensions.vcl'); |
| 38 | + const remote = new RemotePublishCommand() |
| 39 | + .withCustomVCLs([e]); |
| 40 | + |
| 41 | + // eslint-disable-next-line no-underscore-dangle |
| 42 | + assert.deepEqual(remote._vcl, { |
| 43 | + extensions: (await fs.readFile(e, 'utf8')).toString(), |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + it('hlx publish --custom-vcl fixtures/vcl/extensions.vcl --custom-vcl fixtures/vcl/another.vcl', async () => { |
| 48 | + const e = path.resolve(__dirname, 'fixtures/vcl/extensions.vcl'); |
| 49 | + const a = path.resolve(__dirname, 'fixtures/vcl/another.vcl'); |
| 50 | + const remote = new RemotePublishCommand() |
| 51 | + .withCustomVCLs([e, a]); |
| 52 | + |
| 53 | + // eslint-disable-next-line no-underscore-dangle |
| 54 | + assert.deepEqual(remote._vcl, { |
| 55 | + extensions: (await fs.readFile(e, 'utf8')).toString(), |
| 56 | + another: (await fs.readFile(e, 'utf8')).toString(), |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it('hlx publish --custom-vcl fixtures/vcl/unexisting.vcl', async () => { |
| 61 | + const e = path.resolve(__dirname, 'fixtures/vcl/unexisting.vcl'); |
| 62 | + function throwsError() { |
| 63 | + new RemotePublishCommand().withCustomVCLs([e]); |
| 64 | + } |
| 65 | + assert.throws(throwsError); |
| 66 | + }); |
| 67 | +}); |
| 68 | + |
| 69 | +describe('hlx publish --custom-vcl (check requests)', () => { |
| 70 | + let ProxiedRemotePublishCommand; |
| 71 | + let writeDictItem; |
| 72 | + let purgeAll; |
| 73 | + let testRoot; |
| 74 | + let pwd; |
| 75 | + let vcl; |
| 76 | + let scope; |
| 77 | + let remote; |
| 78 | + |
| 79 | + beforeEach('Setting up Fake Server', async function bef() { |
| 80 | + this.timeout(5000); |
| 81 | + writeDictItem = sinon.fake.resolves(true); |
| 82 | + purgeAll = sinon.fake.resolves(true); |
| 83 | + |
| 84 | + ProxiedRemotePublishCommand = proxyquire('../src/remotepublish.cmd', { |
| 85 | + '@adobe/fastly-native-promises': () => ({ |
| 86 | + transact: fn => fn(3), |
| 87 | + writeDictItem, |
| 88 | + purgeAll, |
| 89 | + }), |
| 90 | + }); |
| 91 | + |
| 92 | + |
| 93 | + // ensure to reset nock to avoid conflicts with PollyJS |
| 94 | + nock.restore(); |
| 95 | + nock.cleanAll(); |
| 96 | + nock.activate(); |
| 97 | + |
| 98 | + scope = nock('https://adobeioruntime.net') |
| 99 | + .post('/api/v1/web/helix/default/publish', (body) => { |
| 100 | + ({ vcl } = body); |
| 101 | + return true; |
| 102 | + }) |
| 103 | + .reply(200, {}) |
| 104 | + .post('/api/v1/web/helix/default/addlogger') |
| 105 | + .reply(200, {}); |
| 106 | + |
| 107 | + // set up a fake git repo. |
| 108 | + testRoot = await createTestRoot(); |
| 109 | + await fs.copy(path.resolve(__dirname, 'fixtures/filtered-master.yaml'), path.resolve(testRoot, 'helix-config.yaml')); |
| 110 | + |
| 111 | + // throw a Javascript error when any shell.js command encounters an error |
| 112 | + shell.config.fatal = true; |
| 113 | + |
| 114 | + // init git repo |
| 115 | + pwd = shell.pwd(); |
| 116 | + shell.cd(testRoot); |
| 117 | + shell.exec('git init'); |
| 118 | + shell.exec('git add -A'); |
| 119 | + shell.exec('git commit -m"initial commit."'); |
| 120 | + |
| 121 | + // set up command |
| 122 | + const logger = Logger.getTestLogger(); |
| 123 | + remote = await new ProxiedRemotePublishCommand(logger) |
| 124 | + .withWskAuth('fakeauth') |
| 125 | + .withWskNamespace('fakename') |
| 126 | + .withFastlyAuth('fake_auth') |
| 127 | + .withFastlyNamespace('fake_name') |
| 128 | + .withWskHost('doesn.t.matter') |
| 129 | + .withPublishAPI('https://adobeioruntime.net/api/v1/web/helix/default/publish') |
| 130 | + .withConfigFile(path.resolve(__dirname, 'fixtures/filtered.yaml')) |
| 131 | + .withDryRun(false); |
| 132 | + }); |
| 133 | + |
| 134 | + it('hlx publish (no custom-vcl)', async () => { |
| 135 | + await remote.run(); |
| 136 | + assert.equal(vcl, null); |
| 137 | + }); |
| 138 | + |
| 139 | + it('hlx publish --custom-vcl fixtures/vcl/extensions.vcl', async () => { |
| 140 | + const e = path.resolve(__dirname, 'fixtures/vcl/extensions.vcl'); |
| 141 | + remote.withCustomVCLs([e]); |
| 142 | + await remote.run(); |
| 143 | + |
| 144 | + assert.ok(vcl); |
| 145 | + assert.deepEqual(vcl, { |
| 146 | + extensions: fs.readFileSync(e).toString(), |
| 147 | + }); |
| 148 | + }); |
| 149 | + |
| 150 | + it('hlx publish --custom-vcl fixtures/vcl/extensions.vcl --custom-vcl fixtures/vcl/another.vcl', async () => { |
| 151 | + const e = path.resolve(__dirname, 'fixtures/vcl/extensions.vcl'); |
| 152 | + const a = path.resolve(__dirname, 'fixtures/vcl/another.vcl'); |
| 153 | + remote.withCustomVCLs([e, a]); |
| 154 | + await remote.run(); |
| 155 | + |
| 156 | + assert.ok(vcl); |
| 157 | + assert.deepEqual(vcl, { |
| 158 | + extensions: fs.readFileSync(e).toString(), |
| 159 | + another: fs.readFileSync(a).toString(), |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + afterEach(async () => { |
| 164 | + scope.done(); |
| 165 | + nock.restore(); |
| 166 | + shell.cd(pwd); |
| 167 | + await fs.remove(testRoot); |
| 168 | + }); |
| 169 | +}); |
0 commit comments