diff --git a/src/core/plugins/asteroid_os/api.js b/src/core/plugins/asteroid_os/api.js index 3cf8aabb..16f41ed6 100644 --- a/src/core/plugins/asteroid_os/api.js +++ b/src/core/plugins/asteroid_os/api.js @@ -49,8 +49,7 @@ const getImages = (channel, device) => ) .catch(error => { if (error.response.status === 404) throw new Error("404"); - else if (error.response) throw new Error("no network"); - else throw error; + else throw new Error("no network"); }); /** diff --git a/src/core/plugins/asteroid_os/api.spec.js b/src/core/plugins/asteroid_os/api.spec.js index 1fcfb86f..8df0d673 100644 --- a/src/core/plugins/asteroid_os/api.spec.js +++ b/src/core/plugins/asteroid_os/api.spec.js @@ -1,7 +1,58 @@ process.argv = [null, null, "-vv"]; const { ipcMain } = require("electron"); jest.mock("electron"); - +const axios = require("axios"); +jest.mock("axios"); +axios.create.mockReturnValue(axios); const api = require("./api.js"); -it("should construct", () => expect(require("./api.js")).toBeDefined()); +describe("asteroid_os api", () => { + describe("getImages", () => { + it("should resolve images", () => { + axios.get.mockResolvedValueOnce({ data: "123 a\n456 b\n" }); + return api.getImages("1.0", "lenok").then(r => + expect(r).toEqual([ + { + checksum: { algorithm: "md5", sum: "123" }, + url: "https://release.asteroidos.org/1.0/lenok/a" + }, + { + checksum: { algorithm: "md5", sum: "456" }, + url: "https://release.asteroidos.org/1.0/lenok/b" + } + ]) + ); + }); + it("should reject on 404", done => { + axios.get.mockRejectedValueOnce({ response: { status: 404 } }); + return api.getImages("1.0", "lenok").catch(e => { + expect(e.message).toEqual("404"); + done(); + }); + }); + it("should reject on network error", done => { + axios.get.mockRejectedValueOnce({ response: {} }); + return api.getImages("1.0", "lenok").catch(e => { + expect(e.message).toEqual("no network"); + done(); + }); + }); + }); + describe("getChannels", () => { + it("should resolve channels", () => { + axios.get.mockResolvedValue({ data: "123 a\n456 b\n" }); + axios.get.mockRejectedValueOnce({ response: { status: 404 } }); + return api + .getChannels("lenok") + .then(r => expect(r).toEqual(["nightlies", "1.0-alpha"])); + }); + it("should reject on error", done => { + axios.get.mockResolvedValue({ data: "123 a\n456 b\n" }); + axios.get.mockRejectedValueOnce({ response: {} }); + return api.getChannels("lenok").catch(r => { + expect(r.message).toEqual("no network"); + done(); + }); + }); + }); +}); diff --git a/src/core/plugins/systemimage/api.js b/src/core/plugins/systemimage/api.js index e8f27c76..fe13b532 100644 --- a/src/core/plugins/systemimage/api.js +++ b/src/core/plugins/systemimage/api.js @@ -79,8 +79,7 @@ const getImages = (channel, device, wipe, enable = [], disable = []) => })) .catch(error => { if (error.response.status === 404) throw new Error("404"); - else if (error.response) throw new Error("no network"); - else throw error; + else throw new Error("no network"); }); /** diff --git a/src/core/plugins/systemimage/api.spec.js b/src/core/plugins/systemimage/api.spec.js index 1fcfb86f..aa3a87db 100644 --- a/src/core/plugins/systemimage/api.spec.js +++ b/src/core/plugins/systemimage/api.spec.js @@ -1,7 +1,128 @@ process.argv = [null, null, "-vv"]; const { ipcMain } = require("electron"); jest.mock("electron"); - +const axios = require("axios"); +jest.mock("axios"); +axios.create.mockReturnValue(axios); const api = require("./api.js"); -it("should construct", () => expect(require("./api.js")).toBeDefined()); +const files = [ + { + checksum: { algorithm: "sha256", sum: "1337" }, + url: "https://system-image.ubports.com/asdf.img" + }, + { url: "https://system-image.ubports.com/asdf.img.asc" }, + { + url: "https://system-image.ubports.com/gpg/image-signing.tar.xz" + }, + { + url: "https://system-image.ubports.com/gpg/image-signing.tar.xz.asc" + }, + { + url: "https://system-image.ubports.com/gpg/image-master.tar.xz" + }, + { + url: "https://system-image.ubports.com/gpg/image-master.tar.xz.asc" + } +]; + +describe("systemimage api", () => { + describe("getImages", () => { + it("should resolve images", () => { + axios.get.mockResolvedValueOnce({ + data: { + images: [ + { + type: "full", + files: [ + { + checksum: "1337", + path: "asdf.img", + signature: "asdf.img.asc" + } + ] + } + ] + } + }); + return api.getImages("16.04/stable", "bacon").then(r => + expect(r).toEqual({ + commands: + "format system\n\ +load_keyring image-master.tar.xz image-master.tar.xz.asc\n\ +load_keyring image-signing.tar.xz image-signing.tar.xz.asc\n\ +mount system\n\n\ +update asdf.img asdf.img.asc\n\ +unmount system", + files + }) + ); + }); + describe("getImages", () => { + it("should resolve images", () => { + axios.get.mockResolvedValueOnce({ + data: { + images: [ + { + type: "full", + files: [ + { + checksum: "1337", + path: "asdf.img", + signature: "asdf.img.asc" + } + ] + } + ] + } + }); + return api + .getImages("16.04/stable", "bacon", true, ["developer_mode"], ["mtp"]) + .then(r => + expect(r).toEqual({ + commands: + "format system\n\ +load_keyring image-master.tar.xz image-master.tar.xz.asc\n\ +load_keyring image-signing.tar.xz image-signing.tar.xz.asc\n\ +mount system\n\ +format data\n\ +update asdf.img asdf.img.asc\n\ +enable developer_mode\n\ +disable mtp\n\ +unmount system", + files + }) + ); + }); + it("should reject on 404", done => { + axios.get.mockRejectedValueOnce({ response: { status: 404 } }); + return api.getImages("1.0", "lenok").catch(e => { + expect(e.message).toEqual("404"); + done(); + }); + }); + it("should reject on network error", done => { + axios.get.mockRejectedValueOnce({ response: {} }); + return api.getImages("1.0", "lenok").catch(e => { + expect(e.message).toEqual("no network"); + done(); + }); + }); + }); + describe("getChannels", () => { + it("should resolve channels", () => { + axios.get.mockResolvedValue({ + data: { + "16.04/stable": { devices: { bacon: "a" } }, + "17.04/stable": { devices: { bacon: "a" }, hidden: true }, + "18.04/stable": { devices: { bacon: "a" }, redirect: "asdf" }, + "19.04/stable": { devices: { hamburger: "a" } } + } + }); + return api + .getChannels("bacon") + .then(r => expect(r).toEqual(["16.04/stable"])); + }); + }); + }); +});