Skip to content

Commit

Permalink
feat: allow api key in config (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
stepan662 authored Jan 20, 2025
1 parent bef01d2 commit c291bfc
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"description": "The url of Tolgee API.",
"type": "string"
},
"apiKey": {
"description": "Api key to Tolgee Platform.\n\nWARNING: Make sure you don't leak your API key\nUse `apiKey` only if you are loading it from an environment or other secured source (supported in .js or .yml files) or your config is not public.\n\nIn most cases, it's better to use a one-time `login` command or set it via the `TOLGEE_API_KEY` environment variable.",
"type": "string"
},
"format": {
"$ref": "#/$defs/format"
},
Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ async function run() {
program.addOption(VERBOSE);
program.addOption(CONFIG_OPT);
program.addOption(API_URL_OPT.default(config.apiUrl ?? DEFAULT_API_URL));
program.addOption(API_KEY_OPT);
program.addOption(API_KEY_OPT.default(config.apiKey));
program.addOption(PROJECT_ID_OPT.default(config.projectId ?? -1));
program.addOption(FORMAT_OPT.default(config.format ?? 'JSON_TOLGEE'));
program.addOption(EXTRACTOR.default(config.extractor));
Expand Down
9 changes: 9 additions & 0 deletions src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ export interface Schema {
* The url of Tolgee API.
*/
apiUrl?: string;
/**
* Api key to Tolgee Platform.
*
* WARNING: Make sure you don't leak your API key
* Use `apiKey` only if you are loading it from an environment or other secured source (supported in .js or .yml files) or your config is not public.
*
* In most cases, it's better to use a one-time `login` command or set it via the `TOLGEE_API_KEY` environment variable.
*/
apiKey?: string;
format?: Format;
/**
* A path to a custom extractor to use instead of the default one.
Expand Down
45 changes: 44 additions & 1 deletion test/e2e/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { tmpdir } from 'os';
import { join } from 'path';
import { rm, readFile } from 'fs/promises';
import { rm, readFile, mkdtemp, writeFile } from 'fs/promises';
import { run } from './utils/run.js';
import { TolgeeClient } from '#cli/client/TolgeeClient.js';
import { PROJECT_1 } from './utils/api/project1.js';
Expand All @@ -10,6 +10,7 @@ import {
createProjectWithClient,
deleteProject,
} from './utils/api/common.js';
import { Schema } from '#cli/schema.js';

const AUTH_FILE_PATH = join(tmpdir(), '.tolgee-e2e', 'authentication.json');

Expand Down Expand Up @@ -86,4 +87,46 @@ describe('Project 1', () => {
const authFile = await readFile(AUTH_FILE_PATH, 'utf8');
expect(authFile).not.toContain(pat);
});

it('allows to specify api key through config', async () => {
const pak = await createPak(client);
const { tempFolder, configFile } = await createTmpFolderWithConfig({
apiKey: pak,
pull: {
path: './data',
},
});
const englishFile = join(tempFolder, 'data', 'en.json');

const out = await run(['-c', configFile, 'pull']);

expect(out.code).toBe(0);
expect(out.stderr).toBe('');

const enFile = JSON.parse((await readFile(englishFile)).toString());
expect(enFile.controller).toEqual('Controller');
});

it('ignores login, when apiKey defined in config', async () => {
const pak = await createPak(client);
const pat = await createPat(client);
const loginOut = await run(['login', pat]);
expect(loginOut.code).toBe(0);

const { configFile } = await createTmpFolderWithConfig({
apiKey: pak,
pull: {
path: './data',
},
});
const out = await run(['-c', configFile, 'pull']);
expect(out.code).toBe(0);
});

async function createTmpFolderWithConfig(config: Schema) {
const tempFolder = await mkdtemp(join(tmpdir(), 'cli-project-'));
const configFile = join(tempFolder, '.tolgeerc.json');
await writeFile(configFile, JSON.stringify(config, null, 2));
return { tempFolder, configFile };
}
});

0 comments on commit c291bfc

Please sign in to comment.