Skip to content

Commit 1ae9e37

Browse files
committed
refactor to use oas flag
1 parent cf39219 commit 1ae9e37

File tree

5 files changed

+474
-89
lines changed

5 files changed

+474
-89
lines changed

src/download/exchangeDirectoryParser.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
77

8-
import { extractFiles } from "./exchangeDirectoryParser";
8+
import { extractFiles, extractFile } from "./exchangeDirectoryParser";
99

1010
import { expect, default as chai } from "chai";
1111
import chaiAsPromised from "chai-as-promised";
@@ -33,6 +33,33 @@ before(() => {
3333
chai.use(chaiAsPromised);
3434
});
3535

36+
describe("extractFile", () => {
37+
it("should reject with an error message when trying to extract an invalid zip file", async () => {
38+
const directory = tmp.dirSync();
39+
const invalidZipPath = path.join(directory.name, "invalid.zip");
40+
41+
// Create a file that looks like a zip but isn't valid
42+
fs.writeFileSync(invalidZipPath, "This is not a valid zip file");
43+
44+
await expect(extractFile(invalidZipPath)).to.be.rejectedWith(
45+
`Failed to extract ${invalidZipPath}, probably not a zip file`
46+
);
47+
});
48+
49+
it("should successfully extract a valid zip file", async () => {
50+
const directory = tmp.dirSync();
51+
const zipPath = path.join(directory.name, "api1.zip");
52+
53+
// Create a valid zip file
54+
await createZipFile(directory, "api1.zip");
55+
56+
const extractedPath = await extractFile(zipPath);
57+
58+
expect(fs.existsSync(extractedPath)).to.be.true;
59+
expect(fs.existsSync(path.join(extractedPath, "exchange.json"))).to.be.true;
60+
});
61+
});
62+
3663
describe("extractFiles", () => {
3764
it("should extract a zip file into the specified directory", async () => {
3865
const directory = tmp.dirSync();

src/download/exchangeDownloader.test.ts

Lines changed: 93 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
downloadRestApi,
99
downloadRestApis,
1010
searchExchange,
11-
getLatestCleanApiVersions,
11+
getVersionByDeployment,
1212
getSpecificApi,
1313
getAsset,
1414
runFetch,
@@ -34,10 +34,10 @@ const assetSearchResults = require("../../testResources/download/resources/asset
3434
const getAssetWithVersion = require("../../testResources/download/resources/getAssetWithVersion");
3535

3636
// eslint-disable-next-line @typescript-eslint/no-var-requires
37-
const getAssetWithoutVersion = require("../../testResources/download/resources/getAsset");
37+
const getAssetWithVersionV2 = require("../../testResources/download/resources/getAssetWithVersionV2");
3838

3939
// eslint-disable-next-line @typescript-eslint/no-var-requires
40-
const getAssetWithMultipleVersionGroups = require("../../testResources/download/resources/getAssetWithMultipleVersionGroups.json");
40+
const getAssetWithoutVersion = require("../../testResources/download/resources/getAsset");
4141

4242
const REST_API: RestApi = {
4343
id: "8888888/test-api/1.0.0",
@@ -234,56 +234,50 @@ describe("exchangeDownloader", () => {
234234
});
235235
});
236236

237-
describe("getLatestCleanApiVersions", () => {
237+
describe("getVersionByDeployment", () => {
238238
const scope = nock("https://anypoint.mulesoft.com/exchange/api/v1/assets");
239239

240-
it("should return the latest version", async () => {
240+
it("should return a version if no deployment is specified", async () => {
241241
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
242242

243243
return expect(
244-
getLatestCleanApiVersions("AUTH_TOKEN", REST_API)
245-
).to.eventually.deep.equal(["0.1.1"]);
244+
getVersionByDeployment("AUTH_TOKEN", REST_API)
245+
).to.eventually.equal("0.0.7");
246246
});
247247

248-
it("should return undefined if the asset does not exist", async () => {
249-
scope.get("/8888888/test-api").reply(404, "Not Found");
248+
it("should return a version if a deployment exists", async () => {
249+
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
250250

251-
return expect(getLatestCleanApiVersions("AUTH_TOKEN", REST_API)).to
252-
.eventually.be.undefined;
251+
return expect(
252+
getVersionByDeployment("AUTH_TOKEN", REST_API, /production/i)
253+
).to.eventually.equal("0.0.7");
253254
});
254255

255-
it("should return undefined if the asset does not have a version groups", async () => {
256-
const assetWithoutVersion = _.cloneDeep(getAssetWithoutVersion);
257-
delete assetWithoutVersion.versionGroups;
258-
259-
scope.get("/8888888/test-api").reply(200, assetWithoutVersion);
256+
it("should return the base version if the deployment does not exist", async () => {
257+
scope.get("/8888888/test-api").reply(200, getAssetWithoutVersion);
260258

261-
return expect(getLatestCleanApiVersions("AUTH_TOKEN", REST_API)).to
262-
.eventually.be.undefined;
259+
return expect(
260+
getVersionByDeployment("AUTH_TOKEN", REST_API, /NOT AVAILABLE/i)
261+
).to.eventually.equal(getAssetWithoutVersion.version);
263262
});
264263

265-
it("should return latest versions from all the version groups", async () => {
266-
scope
267-
.get("/8888888/test-api")
268-
.reply(200, getAssetWithMultipleVersionGroups);
264+
it("should return undefined if the asset does not exist", async () => {
265+
scope.get("/8888888/test-api").reply(404, "Not Found");
269266

270267
return expect(
271-
getLatestCleanApiVersions("AUTH_TOKEN", REST_API)
272-
).to.eventually.deep.equal(["2.0.10", "1.8.19"]);
268+
getVersionByDeployment("AUTH_TOKEN", REST_API, /NOT AVAILABLE/i)
269+
).to.eventually.be.undefined;
273270
});
274271

275-
it("should return undefined if the version groups does not have a version", async () => {
276-
const assetWithoutVersion = _.cloneDeep(
277-
getAssetWithMultipleVersionGroups
278-
);
279-
delete assetWithoutVersion.versionGroups[0].versions;
280-
delete assetWithoutVersion.versionGroups[1].versions;
272+
it("should return undefined if the asset does not have a version", async () => {
273+
const assetWithoutVersion = _.cloneDeep(getAssetWithoutVersion);
274+
delete assetWithoutVersion.version;
281275

282276
scope.get("/8888888/test-api").reply(200, assetWithoutVersion);
283277

284278
return expect(
285-
getLatestCleanApiVersions("AUTH_TOKEN", REST_API)
286-
).to.eventually.deep.equal([]);
279+
getVersionByDeployment("AUTH_TOKEN", REST_API, /NOT AVAILABLE/i)
280+
).to.eventually.be.undefined;
287281
});
288282
});
289283

@@ -345,11 +339,11 @@ describe("exchangeDownloader", () => {
345339
.reply(200, [assetSearchResults[0]]);
346340
});
347341

348-
it("searches Exchange and filters by latest version", () => {
342+
it("searches Exchange and filters by deployment", () => {
349343
scope
350344
.get("/shop-products-categories-api-v1")
351345
.reply(200, getAssetWithVersion)
352-
.get("/shop-products-categories-api-v1/0.1.1")
346+
.get("/shop-products-categories-api-v1/0.0.1")
353347
.reply(200, getAssetWithVersion);
354348

355349
return expect(search("searchString")).to.eventually.deep.equal([
@@ -360,22 +354,76 @@ describe("exchangeDownloader", () => {
360354
it("works when an asset does not exist", () => {
361355
scope.get("/shop-products-categories-api-v1").reply(404, "Not Found");
362356

363-
return expect(search("searchString")).to.eventually.deep.equal([]);
357+
return expect(search("searchString")).to.eventually.deep.equal([
358+
{
359+
id: null,
360+
name: "Shopper Products",
361+
description:
362+
"Enable developers to add functionality that shows product details in shopping apps.",
363+
updatedDate: null,
364+
groupId: "893f605e-10e2-423a-bdb4-f952f56eb6d8",
365+
assetId: "shop-products-categories-api-v1",
366+
version: null,
367+
categories: {
368+
"API layer": ["Process"],
369+
"CC API Family": ["Product"],
370+
"CC Version Status": ["Beta"],
371+
"CC API Visibility": ["External"],
372+
},
373+
fatRaml: {
374+
classifier: "fat-raml",
375+
packaging: "zip",
376+
createdDate: null,
377+
md5: null,
378+
sha1: null,
379+
mainFile: "shop-products-categories-api-v1.raml",
380+
},
381+
fatOas: null,
382+
},
383+
]);
364384
});
365385

366-
it("searches Exchange and returns multiple version groups", () => {
386+
it("works when there are no matches for the specified deployment", () => {
387+
const asset = _.cloneDeep(shopperCustomersAsset);
388+
asset.id = "893f605e-10e2-423a-bdb4-f952f56eb6d8/shopper-customers/0.0.7";
389+
asset.version = "0.0.7";
390+
asset.fatRaml = {
391+
classifier: "fat-raml",
392+
packaging: "zip",
393+
externalLink: "https://short.url/test",
394+
createdDate: "2020-02-05T21:26:01.199Z",
395+
md5: "87b3ad2b2aa17639b52f0cc83c5a8d40",
396+
sha1: "f2b9b2de50b7250616e2eea8843735b57235c22b",
397+
mainFile: "shopper-customers.raml",
398+
};
399+
367400
scope
368401
.get("/shop-products-categories-api-v1")
369-
.reply(200, getAssetWithMultipleVersionGroups)
370-
.get("/shop-products-categories-api-v1/1.8.19")
371-
.reply(200, getAssetWithVersion)
372-
.get("/shop-products-categories-api-v1/2.0.10")
373-
.reply(200, getAssetWithVersion);
402+
.reply(200, getAssetWithoutVersion)
403+
.get("/shop-products-categories-api-v1/0.0.7")
404+
.reply(200, getAssetWithoutVersion);
374405

375-
return expect(search("searchString")).to.eventually.deep.equal([
376-
shopperCustomersAsset,
377-
shopperCustomersAsset,
378-
]);
406+
return expect(search("searchString")).to.eventually.deep.equal([asset]);
407+
});
408+
409+
/**
410+
* Returns root asset version when production tag is not found
411+
* on V2 API response. The actual deployed version is available
412+
* under otherVersions attributes, that has no external link to download
413+
* and no environmentName attribute to match
414+
*/
415+
it("returns the root asset version without production tag on V2 response", () => {
416+
const asset = _.cloneDeep(shopperCustomersAsset);
417+
asset.id = "893f605e-10e2-423a-bdb4-f952f56eb6d8/shopper-customers/0.5.0";
418+
asset.version = "0.5.0";
419+
asset.fatRaml.externalLink = "https://somewhere/fatraml.zip";
420+
scope
421+
.get("/shop-products-categories-api-v1")
422+
.reply(200, getAssetWithVersionV2)
423+
.get("/shop-products-categories-api-v1/0.5.0")
424+
.reply(200, getAssetWithVersionV2);
425+
426+
return expect(search("searchString")).to.eventually.deep.equal([asset]);
379427
});
380428
});
381429

0 commit comments

Comments
 (0)