Skip to content

Commit

Permalink
Test asteroid_os and systemimage APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoTheThird committed Mar 4, 2021
1 parent 456a6e1 commit 921649e
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 8 deletions.
3 changes: 1 addition & 2 deletions src/core/plugins/asteroid_os/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

/**
Expand Down
55 changes: 53 additions & 2 deletions src/core/plugins/asteroid_os/api.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
});
3 changes: 1 addition & 2 deletions src/core/plugins/systemimage/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});

/**
Expand Down
125 changes: 123 additions & 2 deletions src/core/plugins/systemimage/api.spec.js
Original file line number Diff line number Diff line change
@@ -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"]));
});
});
});
});

0 comments on commit 921649e

Please sign in to comment.