Skip to content

Commit

Permalink
adapt also network code and adapt working tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jreidinger committed Apr 9, 2024
1 parent ae3e779 commit 45ebc48
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 28 deletions.
12 changes: 6 additions & 6 deletions web/src/client/l10n.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class L10nClient {
async getUILocale() {
const response = await this.client.get("/l10n/config");
if (!response.ok) {
console.log("Failed to get localizaation config: ", response);
console.error("Failed to get localization config: ", response);
return "";
}
const config = await response.json();
Expand All @@ -89,7 +89,7 @@ class L10nClient {
async getUIKeymap() {
const response = await this.client.get("/l10n/config");
if (!response.ok) {
console.log("Failed to get localizaation config: ", response);
console.error("Failed to get localizaation config: ", response);
return "";
}
const config = await response.json();
Expand All @@ -116,7 +116,7 @@ class L10nClient {
async timezones() {
const response = await this.client.get("/l10n/timezones");
if (!response.ok) {
console.log("Failed to get localizaation config: ", response);
console.error("Failed to get localizaation config: ", response);
return [];
}
const timezones = await response.json();
Expand Down Expand Up @@ -153,7 +153,7 @@ class L10nClient {
async locales() {
const response = await this.client.get("/l10n/locales");
if (!response.ok) {
console.log("Failed to get localizaation config: ", response);
console.error("Failed to get localizaation config: ", response);
return [];
}
const locales = await response.json();
Expand Down Expand Up @@ -191,7 +191,7 @@ class L10nClient {
async keymaps() {
const response = await this.client.get("/l10n/keymaps");
if (!response.ok) {
console.log("Failed to get localizaation config: ", response);
console.error("Failed to get localizaation config: ", response);
return [];
}
const keymaps = await response.json();
Expand Down Expand Up @@ -269,7 +269,7 @@ class L10nClient {
async getConfig() {
const response = await this.client.get("/l10n/config");
if (!response.ok) {
console.log("Failed to get localization config: ", response);
console.error("Failed to get localization config: ", response);
return {};
}
return await response.json();
Expand Down
6 changes: 3 additions & 3 deletions web/src/client/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class ManagerBaseClient {
async canInstall() {
const response = await this.client.get("/manager/installer");
if (!response.ok) {
console.log("Failed to get installer config: ", response);
console.error("Failed to get installer config: ", response);
return false;
}
const installer = await response.json();
Expand All @@ -97,7 +97,7 @@ class ManagerBaseClient {
async getPhase() {
const response = await this.client.get("/manager/installer");
if (!response.ok) {
console.log("Failed to get installer config: ", response);
console.error("Failed to get installer config: ", response);
return 0;
}
const installer = await response.json();
Expand Down Expand Up @@ -133,7 +133,7 @@ class ManagerBaseClient {
async useIguana() {
const response = await this.client.get("/manager/installer");
if (!response.ok) {
console.log("Failed to get installer config: ", response);
console.error("Failed to get installer config: ", response);
return false;
}
const installer = await response.json();
Expand Down
17 changes: 12 additions & 5 deletions web/src/client/network.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ const mockSettings = {
networking_enabled: true
}

const mockJsonFn = jest.fn();

const mockGetFn = jest.fn().mockImplementation(() => {
return {
ok: true,
json: mockJsonFn,
};
});

jest.mock("./http", () => {
return {
HTTPClient: jest.fn().mockImplementation(() => {
Expand All @@ -78,14 +87,12 @@ jest.mock("./http", () => {
};
});

const mockGetFn = jest.fn();

describe("NetworkClient", () => {
describe("#connections", () => {
it("returns the list of active connections from the adapter", async () => {
const http = new HTTPClient(new URL(ADDRESS));
const client = new NetworkClient(http);
mockGetFn.mockResolvedValue([mockWiredConnection, mockWirelessConnection]);
mockJsonFn.mockResolvedValue([mockWiredConnection, mockWirelessConnection]);
const connections = await client.connections();
const eth0 = connections.find(c => c.id === "eth0");
expect(eth0).toEqual(mockConnection);
Expand All @@ -96,7 +103,7 @@ describe("NetworkClient", () => {
it("returns the list of addresses", async () => {
const http = new HTTPClient(new URL(ADDRESS));
const client = new NetworkClient(http);
mockGetFn.mockResolvedValue([mockWiredConnection, mockWirelessConnection]);
mockJsonFn.mockResolvedValue([mockWiredConnection, mockWirelessConnection]);
const addresses = await client.addresses();
expect(addresses).toEqual([{ address: "192.168.122.100", prefix: 24 }]);
});
Expand All @@ -107,7 +114,7 @@ describe("NetworkClient", () => {
it("returns network general settings", async () => {
const http = new HTTPClient(new URL(ADDRESS));
const client = new NetworkClient(http);
mockGetFn.mockResolvedValue(mockSettings);
mockJsonFn.mockResolvedValue(mockSettings);
const settings = await client.settings();
expect(settings.hostname).toEqual("localhost.localdomain");
});
Expand Down
41 changes: 31 additions & 10 deletions web/src/client/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,13 @@ class NetworkClient {
* @return {Promise<Connection[]>}
*/
async connections() {
const connections = await this.client.get("/network/connections");
const response = await this.client.get("/network/connections");
if (!response.ok) {
console.error("Failed to get list of connections", response);
return [];
}

const connections = await response.json();
return connections.map(this.fromApiConnection);
}

Expand Down Expand Up @@ -141,9 +146,14 @@ class NetworkClient {
* @return {Promise<AccessPoint[]>}
*/
async accessPoints() {
const access_points = await this.client.get("/network/wifi");

return access_points.map(ap => {
const response = await this.client.get("/network/wifi");
if (!response.ok) {
console.error("Failed to get list of APs", response);
return [];
}
const access_points = await response.json();

return access_points.map((ap) => {
return createAccessPoint({
ssid: ap.ssid,
hwAddress: ap.hw_address,
Expand All @@ -169,7 +179,7 @@ class NetworkClient {
* Apply network changes
*/
async apply() {
return this.client.put("/network/system/apply");
return this.client.put("/network/system/apply", {});
}

/**
Expand Down Expand Up @@ -205,7 +215,13 @@ class NetworkClient {
* @return {Promise<Connection>} the added connection
*/
async addConnection(connection) {
return this.client.post("/network/connections", this.toApiConnection(connection));
const response = await this.client.post("/network/connections", this.toApiConnection(connection));
if (!response.ok) {
console.error("Failed to post list of connections", response);
return null;
}

return response.json();
}

/**
Expand All @@ -232,7 +248,7 @@ class NetworkClient {
async updateConnection(connection) {
const conn = this.toApiConnection(connection);
await this.client.put(`/network/connections/${conn.id}`, conn);
return this.apply();
return (await this.apply()).ok;
}

/**
Expand All @@ -246,7 +262,7 @@ class NetworkClient {
*/
async deleteConnection(id) {
await this.client.delete(`/network/connections/${id}`);
return this.apply();
return (await this.apply()).ok;
}

/*
Expand All @@ -266,8 +282,13 @@ class NetworkClient {
*
* @return {Promise<NetworkSettings>}
*/
settings() {
return this.client.get("/network/state");
async settings() {
const response = await this.client.get("/network/settings");
if (!response.ok) {
console.error("Failed to get settings", response);
return {};
}
return response.json();
}
}

Expand Down
15 changes: 11 additions & 4 deletions web/src/client/software.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ import DBusClient from "./dbus";
import { HTTPClient } from "./http";
import { ProductClient, SoftwareClient } from "./software";

const mockJsonFn = jest.fn();

const mockGetFn = jest.fn().mockImplementation(() => {
return {
ok: true,
json: mockJsonFn,
};
});

jest.mock("./http", () => {
return {
HTTPClient: jest.fn().mockImplementation(() => {
Expand All @@ -35,8 +44,6 @@ jest.mock("./http", () => {
};
});

const mockGetFn = jest.fn();

jest.mock("./dbus");

const PRODUCT_IFACE = "org.opensuse.Agama.Software1.Product";
Expand Down Expand Up @@ -82,7 +89,7 @@ describe("ProductClient", () => {
it.only("returns the list of available products", async () => {
const http = new HTTPClient(new URL("http://localhost"));
const client = new ProductClient(http);
mockGetFn.mockResolvedValue([tumbleweed, microos]);
mockJsonFn.mockResolvedValue([tumbleweed, microos]);
const products = await client.getAll();
expect(products).toEqual([
{ id: "Tumbleweed", name: "openSUSE Tumbleweed", description: "Tumbleweed is..." },
Expand All @@ -95,7 +102,7 @@ describe("ProductClient", () => {
it.only("returns the selected product", async () => {
const http = new HTTPClient(new URL("http://localhost"));
const client = new ProductClient(http);
mockGetFn.mockResolvedValue({ product: "microos" });
mockJsonFn.mockResolvedValue({ product: "microos" });
const selected = await client.getSelected();
expect(selected).toEqual("microos");
});
Expand Down

0 comments on commit 45ebc48

Please sign in to comment.