Files
Homarr/packages/integrations/test/home-assistant.spec.ts
Manuel 2535192b2c feat: downloads widget (#844)
Usenet and Torrent downloads in 1 widget.
sabNZBd, NzbGet, Deluge, qBitTorrent, and transmission support.
Columns can be reordered in Edit mode.
Sorting enabled.
Time uses Dayjs with auto translation.
Can pause/resume single items, clients, or all.
Can delete items (With option to delete assossiated files).
Clients list and details.
Include all filtering and processing for ratio from oldmarr torrent widget.
Invalidation of old data (older than 30 seconds) to show an integration is not responding anymore.

Misc (So many miscs):
Fixed validation error with multiText.
Fixed translation application for multiSelect to behave the same as select.
Added background to gitignore (I needed to add a background to visually test opacity, probably will in the future too)
Added setOptions to frontend components so potential updates made from the Dashboard can be saved.
Extracted background and border color to use in widgets.
humanFileSize function based on the si format (powers of 1024, not 1000).
Improved integrationCreatorByKind by @Meierschlumpf.
Changed integrationCreatorByKind to integrationCreator so it functions directly from the integration.
Added integrationCreatorFromSecrets to directly work with secrets from db.
Added getIntegrationKindsByCategory to get a list of integrations sharing categories.
Added IntegrationKindByCategory type to get the types possible for a category (Great to cast on integration.kind that isn't already properly limited/typed but for which we know the limitation)
Added a common AtLeastOneOf type. Applied to TKind and IntegrationSecretKind[] where it was already being used and Added to the getIntegrationKindsByCategory's output to be more freely used.
Added the Modify type, instead of omiting to then add again just to change a parameters type, use the modify instead. Applied code wide already.
Hook to get list of integration depending on permission level of user. (By @Meierschlumpf)
2024-09-11 17:30:21 +02:00

83 lines
2.9 KiB
TypeScript

import { join } from "path";
import type { StartedTestContainer } from "testcontainers";
import { GenericContainer, getContainerRuntimeClient, ImageName, Wait } from "testcontainers";
import { beforeAll, describe, expect, test } from "vitest";
import { HomeAssistantIntegration, IntegrationTestConnectionError } from "../src";
const DEFAULT_API_KEY =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJkNjQwY2VjNDFjOGU0NGM5YmRlNWQ4ZmFjMjUzYWViZiIsImlhdCI6MTcxODQ3MTE1MSwiZXhwIjoyMDMzODMxMTUxfQ.uQCZ5FZTokipa6N27DtFhLHkwYEXU1LZr0fsVTryL2Q";
const IMAGE_NAME = "ghcr.io/home-assistant/home-assistant:stable";
describe("Home Assistant integration", () => {
beforeAll(async () => {
const containerRuntimeClient = await getContainerRuntimeClient();
await containerRuntimeClient.image.pull(ImageName.fromString(IMAGE_NAME));
}, 100_000);
test("Test connection should work", async () => {
// Arrange
const startedContainer = await prepareHomeAssistantContainerAsync();
const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer);
// Act
const actAsync = async () => await homeAssistantIntegration.testConnectionAsync();
// Assert
await expect(actAsync()).resolves.not.toThrow();
// Cleanup
await startedContainer.stop();
}, 30_000); // Timeout of 30 seconds
test("Test connection should fail with wrong credentials", async () => {
// Arrange
const startedContainer = await prepareHomeAssistantContainerAsync();
const homeAssistantIntegration = createHomeAssistantIntegration(startedContainer, "wrong-api-key");
// Act
const actAsync = async () => await homeAssistantIntegration.testConnectionAsync();
// Assert
await expect(actAsync()).rejects.toThrow(IntegrationTestConnectionError);
// Cleanup
await startedContainer.stop();
}, 30_000); // Timeout of 30 seconds
});
const prepareHomeAssistantContainerAsync = async () => {
const homeAssistantContainer = createHomeAssistantContainer();
const startedContainer = await homeAssistantContainer.start();
await startedContainer.exec(["unzip", "-o", "/tmp/config.zip", "-d", "/config"]);
await startedContainer.restart();
return startedContainer;
};
const createHomeAssistantContainer = () => {
return new GenericContainer(IMAGE_NAME)
.withCopyFilesToContainer([
{
source: join(__dirname, "/volumes/home-assistant-config.zip"),
target: "/tmp/config.zip",
},
])
.withPrivilegedMode()
.withExposedPorts(8123)
.withWaitStrategy(Wait.forHttp("/", 8123));
};
const createHomeAssistantIntegration = (container: StartedTestContainer, apiKeyOverride?: string) => {
return new HomeAssistantIntegration({
id: "1",
decryptedSecrets: [
{
kind: "apiKey",
value: apiKeyOverride ?? DEFAULT_API_KEY,
},
],
name: "Home assistant",
url: `http://${container.getHost()}:${container.getMappedPort(8123)}`,
});
};