appstore-cli
Version:
A command-line interface (CLI) to interact with the Apple App Store Connect API.
174 lines (134 loc) • 5.21 kB
text/typescript
import { saveConfig, loadConfig, getFastlaneToken, getUsername } from './config';
import fs from 'fs';
import os from 'os';
import path from 'path';
import { storePrivateKey, retrievePrivateKey } from './secure-storage';
// Mock the secure storage functions
jest.mock('./secure-storage', () => ({
storePrivateKey: jest.fn(),
retrievePrivateKey: jest.fn(),
}));
describe('config', () => {
const configDir = path.join(os.homedir(), '.appstore-cli');
const configFile = path.join(configDir, 'config.json');
beforeEach(() => {
// Clear all mocks before each test
jest.clearAllMocks();
// Clear environment variables
delete process.env.APPSTORE_KEY_ID;
delete process.env.APPSTORE_ISSUER_ID;
delete process.env.APPSTORE_PRIVATE_KEY;
delete process.env.APPSTORE_USERNAME;
delete process.env.FASTLANE_TOKEN;
});
describe('saveConfig', () => {
it('should save API key configuration', async () => {
const config = {
keyId: 'test-key-id',
issuerId: 'test-issuer-id',
privateKey: 'test-private-key',
};
await saveConfig(config);
expect(storePrivateKey).toHaveBeenCalledWith('test-key-id', 'test-private-key');
expect(fs.writeFileSync).toHaveBeenCalledWith(
configFile,
JSON.stringify({
keyId: 'test-key-id',
issuerId: 'test-issuer-id',
}, null, 2)
);
});
it('should save Fastlane token configuration', async () => {
const config = {
fastlaneToken: 'test-fastlane-token',
};
await saveConfig(config);
expect(storePrivateKey).toHaveBeenCalledWith('fastlane-token', 'test-fastlane-token');
expect(fs.writeFileSync).toHaveBeenCalledWith(
configFile,
JSON.stringify({}, null, 2)
);
});
it('should save username configuration', async () => {
const config = {
username: 'test-user',
};
await saveConfig(config);
expect(fs.writeFileSync).toHaveBeenCalledWith(
configFile,
JSON.stringify({
username: 'test-user',
}, null, 2)
);
});
});
describe('loadConfig', () => {
it('should load configuration from file', () => {
const mockConfig = {
keyId: 'test-key-id',
issuerId: 'test-issuer-id',
username: 'test-user',
};
(fs.existsSync as jest.Mock).mockReturnValue(true);
(fs.readFileSync as jest.Mock).mockReturnValue(JSON.stringify(mockConfig));
const config = loadConfig();
expect(config).toEqual(mockConfig);
});
it('should load configuration from environment variables (API keys)', () => {
process.env.APPSTORE_KEY_ID = 'env-key-id';
process.env.APPSTORE_ISSUER_ID = 'env-issuer-id';
process.env.APPSTORE_USERNAME = 'env-user';
const config = loadConfig();
expect(config).toEqual({
keyId: 'env-key-id',
issuerId: 'env-issuer-id',
username: 'env-user',
});
});
it('should load configuration from environment variables (Fastlane token)', () => {
process.env.FASTLANE_TOKEN = 'env-fastlane-token';
process.env.APPSTORE_USERNAME = 'env-user';
const config = loadConfig();
expect(config).toEqual({
fastlaneToken: 'env-fastlane-token',
username: 'env-user',
});
});
it('should throw error when config file does not exist and no environment variables', () => {
(fs.existsSync as jest.Mock).mockReturnValue(false);
expect(() => loadConfig()).toThrow('Configuration file not found');
});
});
describe('getFastlaneToken', () => {
it('should return Fastlane token from environment variable', async () => {
process.env.FASTLANE_TOKEN = 'env-fastlane-token';
const token = await getFastlaneToken({});
expect(token).toBe('env-fastlane-token');
});
it('should return Fastlane token from secure storage', async () => {
(retrievePrivateKey as jest.Mock).mockResolvedValue('stored-fastlane-token');
const token = await getFastlaneToken({});
expect(token).toBe('stored-fastlane-token');
expect(retrievePrivateKey).toHaveBeenCalledWith('fastlane-token');
});
it('should throw error when Fastlane token is not found', async () => {
(retrievePrivateKey as jest.Mock).mockResolvedValue(null);
await expect(getFastlaneToken({})).rejects.toThrow('Fastlane token not found');
});
});
describe('getUsername', () => {
it('should return username from environment variable', () => {
process.env.APPSTORE_USERNAME = 'env-user';
const username = getUsername({});
expect(username).toBe('env-user');
});
it('should return username from config', () => {
const config = { username: 'config-user' };
const username = getUsername(config);
expect(username).toBe('config-user');
});
it('should throw error when username is not found', () => {
expect(() => getUsername({})).toThrow('Username not found');
});
});
});