fix(custom-widgets): address v2 merge review
This commit is contained in:
1 parent
6aceb2fd1f
commit
48ce83806d
122 files changed
+8534
-1447
No files matched your search
@@ -1,9 +1,10 @@
|
||||
import path from "path";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { MySqlContainer } from "@testcontainers/mysql";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import { migrate } from "drizzle-orm/mysql2/migrator";
|
||||
import mysql from "mysql2";
|
||||
import { describe, test } from "vitest";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
|
||||
|
||||
@@ -43,4 +44,88 @@ describe("Mysql Migration", () => {
|
||||
connection.end();
|
||||
await mysqlContainer.stop();
|
||||
}, 40_000);
|
||||
|
||||
test("preserves populated v1 custom widgets and encrypted secrets", async () => {
|
||||
const mysqlContainer = await new MySqlContainer("mysql:latest").start();
|
||||
const connection = mysql.createConnection({
|
||||
host: mysqlContainer.getHost(),
|
||||
database: mysqlContainer.getDatabase(),
|
||||
port: mysqlContainer.getPort(),
|
||||
user: mysqlContainer.getUsername(),
|
||||
password: mysqlContainer.getUserPassword(),
|
||||
multipleStatements: true,
|
||||
});
|
||||
const sql = connection.promise();
|
||||
try {
|
||||
await sql.query(`
|
||||
CREATE TABLE \`user\` (\`id\` varchar(64) NOT NULL PRIMARY KEY);
|
||||
CREATE TABLE \`custom_widget_definition\` (
|
||||
\`id\` varchar(64) NOT NULL PRIMARY KEY,
|
||||
\`name\` varchar(256) NOT NULL,
|
||||
\`description\` text,
|
||||
\`icon_url\` text,
|
||||
\`url\` text NOT NULL,
|
||||
\`auth_type\` varchar(32) NOT NULL DEFAULT 'none',
|
||||
\`header_name\` varchar(256),
|
||||
\`method\` varchar(16) NOT NULL DEFAULT 'GET',
|
||||
\`request_body\` text,
|
||||
\`display_type\` varchar(32) NOT NULL DEFAULT 'singleValue',
|
||||
\`display_config\` text NOT NULL,
|
||||
\`enabled\` boolean NOT NULL DEFAULT true,
|
||||
\`created_at\` timestamp NOT NULL,
|
||||
\`updated_at\` timestamp NOT NULL,
|
||||
\`creator_id\` varchar(64),
|
||||
CONSTRAINT \`custom_widget_definition_creator_id_user_id_fk\`
|
||||
FOREIGN KEY (\`creator_id\`) REFERENCES \`user\`(\`id\`) ON DELETE SET NULL
|
||||
);
|
||||
CREATE TABLE \`custom_widget_secret\` (
|
||||
\`kind\` varchar(64) NOT NULL,
|
||||
\`value\` text NOT NULL,
|
||||
\`updated_at\` timestamp NOT NULL,
|
||||
\`definition_id\` varchar(64) NOT NULL,
|
||||
PRIMARY KEY (\`definition_id\`, \`kind\`),
|
||||
CONSTRAINT \`cw_secret_definition_id_cw_definition_id_fk\`
|
||||
FOREIGN KEY (\`definition_id\`) REFERENCES \`custom_widget_definition\`(\`id\`) ON DELETE CASCADE
|
||||
);
|
||||
INSERT INTO \`user\` (\`id\`) VALUES ('owner');
|
||||
INSERT INTO \`custom_widget_definition\` (
|
||||
\`id\`, \`name\`, \`url\`, \`auth_type\`, \`method\`, \`display_type\`, \`display_config\`,
|
||||
\`enabled\`, \`created_at\`, \`updated_at\`, \`creator_id\`
|
||||
) VALUES (
|
||||
'legacy-weather', 'Weather', 'https://example.test/weather', 'bearer', 'GET', 'singleValue',
|
||||
'{"type":"singleValue","jsonPath":"$.temperature"}', true,
|
||||
'2023-11-14 00:00:00', '2023-11-14 00:00:01', 'owner'
|
||||
);
|
||||
INSERT INTO \`custom_widget_secret\` (\`kind\`, \`value\`, \`updated_at\`, \`definition_id\`)
|
||||
VALUES ('apiKey', 'encrypted.value', '2023-11-14 00:00:01', 'legacy-weather');
|
||||
`);
|
||||
|
||||
const migration = readFileSync(
|
||||
path.join(__dirname, "..", "migrations", "mysql", "0044_custom_widget_v2_reset.sql"),
|
||||
"utf8",
|
||||
);
|
||||
for (const statement of migration
|
||||
.split("--> statement-breakpoint")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean)) {
|
||||
await sql.query(statement);
|
||||
}
|
||||
|
||||
const [definitions] = await sql.query<mysql.RowDataPacket[]>(
|
||||
"SELECT id, name, enabled, creator_id FROM legacy_custom_widget_definition",
|
||||
);
|
||||
const [secrets] = await sql.query<mysql.RowDataPacket[]>(
|
||||
"SELECT definition_id, kind, value FROM legacy_custom_widget_secret",
|
||||
);
|
||||
expect(definitions).toEqual([
|
||||
expect.objectContaining({ id: "legacy-weather", name: "Weather", enabled: 1, creator_id: "owner" }),
|
||||
]);
|
||||
expect(secrets).toEqual([
|
||||
expect.objectContaining({ definition_id: "legacy-weather", kind: "apiKey", value: "encrypted.value" }),
|
||||
]);
|
||||
} finally {
|
||||
await sql.end();
|
||||
await mysqlContainer.stop();
|
||||
}
|
||||
}, 40_000);
|
||||
});
|
||||
@@ -1,9 +1,10 @@
|
||||
import path from "path";
|
||||
import { readFileSync } from "node:fs";
|
||||
import { PostgreSqlContainer } from "@testcontainers/postgresql";
|
||||
import { drizzle } from "drizzle-orm/node-postgres";
|
||||
import { migrate } from "drizzle-orm/node-postgres/migrator";
|
||||
import { Pool } from "pg";
|
||||
import { describe, test } from "vitest";
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
|
||||
|
||||
@@ -50,4 +51,82 @@ describe("PostgreSql Migration", () => {
|
||||
// Stop the container
|
||||
await postgreSqlContainer.stop();
|
||||
}, 40_000);
|
||||
|
||||
test("preserves populated v1 custom widgets and encrypted secrets", async () => {
|
||||
const postgreSqlContainer = await new PostgreSqlContainer("postgres:latest").start();
|
||||
const pool = new Pool({
|
||||
user: postgreSqlContainer.getUsername(),
|
||||
database: postgreSqlContainer.getDatabase(),
|
||||
password: postgreSqlContainer.getPassword(),
|
||||
port: postgreSqlContainer.getPort(),
|
||||
host: postgreSqlContainer.getHost(),
|
||||
});
|
||||
try {
|
||||
await pool.query(`
|
||||
CREATE TABLE "user" ("id" varchar(64) PRIMARY KEY NOT NULL);
|
||||
CREATE TABLE "custom_widget_definition" (
|
||||
"id" varchar(64) PRIMARY KEY NOT NULL,
|
||||
"name" varchar(256) NOT NULL,
|
||||
"description" text,
|
||||
"icon_url" text,
|
||||
"url" text NOT NULL,
|
||||
"auth_type" varchar(32) DEFAULT 'none' NOT NULL,
|
||||
"header_name" varchar(256),
|
||||
"method" varchar(16) DEFAULT 'GET' NOT NULL,
|
||||
"request_body" text,
|
||||
"display_type" varchar(32) DEFAULT 'singleValue' NOT NULL,
|
||||
"display_config" text NOT NULL,
|
||||
"enabled" boolean DEFAULT true NOT NULL,
|
||||
"created_at" timestamp NOT NULL,
|
||||
"updated_at" timestamp NOT NULL,
|
||||
"creator_id" varchar(64),
|
||||
CONSTRAINT "custom_widget_definition_creator_id_user_id_fk"
|
||||
FOREIGN KEY ("creator_id") REFERENCES "user"("id") ON DELETE SET NULL
|
||||
);
|
||||
CREATE TABLE "custom_widget_secret" (
|
||||
"kind" varchar(64) NOT NULL,
|
||||
"value" text NOT NULL,
|
||||
"updated_at" timestamp NOT NULL,
|
||||
"definition_id" varchar(64) NOT NULL,
|
||||
CONSTRAINT "custom_widget_secret_definition_id_kind_pk" PRIMARY KEY("definition_id", "kind"),
|
||||
CONSTRAINT "custom_widget_secret_definition_id_custom_widget_definition_id_fk"
|
||||
FOREIGN KEY ("definition_id") REFERENCES "custom_widget_definition"("id") ON DELETE CASCADE
|
||||
);
|
||||
INSERT INTO "user" ("id") VALUES ('owner');
|
||||
INSERT INTO "custom_widget_definition" (
|
||||
"id", "name", "url", "auth_type", "method", "display_type", "display_config", "enabled",
|
||||
"created_at", "updated_at", "creator_id"
|
||||
) VALUES (
|
||||
'legacy-weather', 'Weather', 'https://example.test/weather', 'bearer', 'GET', 'singleValue',
|
||||
'{"type":"singleValue","jsonPath":"$.temperature"}', true,
|
||||
'2023-11-14 00:00:00', '2023-11-14 00:00:01', 'owner'
|
||||
);
|
||||
INSERT INTO "custom_widget_secret" ("kind", "value", "updated_at", "definition_id")
|
||||
VALUES ('apiKey', 'encrypted.value', '2023-11-14 00:00:01', 'legacy-weather');
|
||||
`);
|
||||
|
||||
const migration = readFileSync(
|
||||
path.join(__dirname, "..", "migrations", "postgresql", "0010_custom_widget_v2_reset.sql"),
|
||||
"utf8",
|
||||
);
|
||||
for (const statement of migration
|
||||
.split("--> statement-breakpoint")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean)) {
|
||||
await pool.query(statement);
|
||||
}
|
||||
|
||||
expect(
|
||||
(await pool.query("SELECT id, name, enabled, creator_id FROM legacy_custom_widget_definition")).rows,
|
||||
).toEqual([
|
||||
expect.objectContaining({ id: "legacy-weather", name: "Weather", enabled: true, creator_id: "owner" }),
|
||||
]);
|
||||
expect((await pool.query("SELECT definition_id, kind, value FROM legacy_custom_widget_secret")).rows).toEqual([
|
||||
expect.objectContaining({ definition_id: "legacy-weather", kind: "apiKey", value: "encrypted.value" }),
|
||||
]);
|
||||
} finally {
|
||||
await pool.end();
|
||||
await postgreSqlContainer.stop();
|
||||
}
|
||||
}, 40_000);
|
||||
});
|
||||
@@ -1,8 +1,9 @@
|
||||
import path from "path";
|
||||
import { readFileSync } from "node:fs";
|
||||
import BetterSqlite3 from "better-sqlite3";
|
||||
import { drizzle } from "drizzle-orm/better-sqlite3";
|
||||
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
|
||||
import { test } from "vitest";
|
||||
import { expect, test } from "vitest";
|
||||
|
||||
import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
|
||||
|
||||
@@ -22,3 +23,68 @@ test("SQLite migrations seed the five disabled bundled custom widgets", async ()
|
||||
|
||||
connection.close();
|
||||
});
|
||||
|
||||
test("Custom JSX v2 migration preserves populated v1 definitions and secrets", () => {
|
||||
const connection = new BetterSqlite3(":memory:");
|
||||
connection.exec(`
|
||||
CREATE TABLE user (id text PRIMARY KEY NOT NULL);
|
||||
CREATE TABLE custom_widget_definition (
|
||||
id text PRIMARY KEY NOT NULL,
|
||||
name text NOT NULL,
|
||||
description text,
|
||||
icon_url text,
|
||||
url text NOT NULL,
|
||||
auth_type text DEFAULT 'none' NOT NULL,
|
||||
header_name text,
|
||||
method text DEFAULT 'GET' NOT NULL,
|
||||
request_body text,
|
||||
display_type text DEFAULT 'singleValue' NOT NULL,
|
||||
display_config text DEFAULT '{}' NOT NULL,
|
||||
enabled integer DEFAULT true NOT NULL,
|
||||
created_at integer NOT NULL,
|
||||
updated_at integer NOT NULL,
|
||||
creator_id text REFERENCES user(id) ON DELETE SET NULL
|
||||
);
|
||||
CREATE TABLE custom_widget_secret (
|
||||
kind text NOT NULL,
|
||||
value text NOT NULL,
|
||||
updated_at integer NOT NULL,
|
||||
definition_id text NOT NULL REFERENCES custom_widget_definition(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY(definition_id, kind)
|
||||
);
|
||||
INSERT INTO user (id) VALUES ('owner');
|
||||
INSERT INTO custom_widget_definition (
|
||||
id, name, url, auth_type, method, display_type, display_config, enabled, created_at, updated_at, creator_id
|
||||
) VALUES ('legacy-weather', 'Weather', 'https://example.test/weather', 'bearer', 'GET', 'singleValue',
|
||||
'{"type":"singleValue","jsonPath":"$.temperature"}', true, 1700000000, 1700000001, 'owner');
|
||||
INSERT INTO custom_widget_secret (kind, value, updated_at, definition_id)
|
||||
VALUES ('apiKey', 'encrypted.value', 1700000001, 'legacy-weather');
|
||||
`);
|
||||
|
||||
const migration = readFileSync(
|
||||
path.join(__dirname, "..", "migrations", "sqlite", "0042_custom_widget_v2_reset.sql"),
|
||||
"utf8",
|
||||
);
|
||||
for (const statement of migration
|
||||
.split("--> statement-breakpoint")
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean)) {
|
||||
connection.exec(statement);
|
||||
}
|
||||
|
||||
expect(connection.prepare("SELECT * FROM legacy_custom_widget_definition").get()).toMatchObject({
|
||||
id: "legacy-weather",
|
||||
name: "Weather",
|
||||
enabled: 1,
|
||||
creator_id: "owner",
|
||||
});
|
||||
expect(connection.prepare("SELECT * FROM legacy_custom_widget_secret").get()).toMatchObject({
|
||||
definition_id: "legacy-weather",
|
||||
kind: "apiKey",
|
||||
value: "encrypted.value",
|
||||
});
|
||||
expect(connection.prepare("SELECT count(*) AS count FROM custom_widget_definition").get()).toEqual({ count: 0 });
|
||||
expect(connection.prepare("SELECT count(*) AS count FROM custom_widget_secret").get()).toEqual({ count: 0 });
|
||||
|
||||
connection.close();
|
||||
});
|
||||
Reference in new issue
Block a user