immich/cli/test/e2e/login-key.e2e-spec.ts
Jonathan Jogenfors 31eb4790dc
feat(cli): dockerize (#6858)
* import dockerfile from old cli

* build works

* rename login command

* bump packages

* fix login command

* chore: remove axios dependency from CLI

* move immich script path

* can build docker

* wip

* wip

* don't externalize sdk

* can run docker

* improve entrypoint

* can save auth state between runs

* add docs

* clarify reqs

* fix lint

* bump alpine to 3.19

* add env files for api key

* remove immich cli GHA for now

* Update docs/docs/features/command-line-interface.md

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>

* remove redundant env variable check

* cleanup

* speling

---------

Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com>
2024-02-06 12:17:15 +01:00

66 lines
1.9 KiB
TypeScript

import { restoreTempFolder, testApp } from '@test-utils';
import { CLI_BASE_OPTIONS, TEST_AUTH_FILE, deleteAuthFile, setup, spyOnConsole } from 'test/cli-test-utils';
import { readFile, stat } from 'node:fs/promises';
import { LoginCommand } from '../../src/commands/login.command';
import yaml from 'yaml';
describe(`login-key (e2e)`, () => {
let apiKey: string;
let instanceUrl: string;
spyOnConsole();
beforeAll(async () => {
await testApp.create();
if (process.env.IMMICH_INSTANCE_URL) {
instanceUrl = process.env.IMMICH_INSTANCE_URL;
} else {
throw new Error('IMMICH_INSTANCE_URL environment variable not set');
}
});
afterAll(async () => {
await testApp.teardown();
await restoreTempFolder();
deleteAuthFile();
});
beforeEach(async () => {
await testApp.reset();
await restoreTempFolder();
const api = await setup();
apiKey = api.apiKey;
deleteAuthFile();
});
it('should error when providing an invalid API key', async () => {
await expect(new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, 'invalid')).rejects.toThrow(
`Failed to connect to server ${instanceUrl}: Response returned an error code`,
);
});
it('should log in when providing the correct API key', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);
});
it('should create an auth file when logging in', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);
const data: string = await readFile(TEST_AUTH_FILE, 'utf8');
const parsedConfig = yaml.parse(data);
expect(parsedConfig).toEqual(expect.objectContaining({ instanceUrl, apiKey }));
});
it('should create an auth file with chmod 600', async () => {
await new LoginCommand(CLI_BASE_OPTIONS).run(instanceUrl, apiKey);
const stats = await stat(TEST_AUTH_FILE);
const mode = (stats.mode & 0o777).toString(8);
expect(mode).toEqual('600');
});
});