diff --git a/src/commands/pull-languages/actions.test.ts b/src/commands/pull-languages/actions.test.ts new file mode 100644 index 0000000..34a97b6 --- /dev/null +++ b/src/commands/pull-languages/actions.test.ts @@ -0,0 +1,93 @@ +import { http, HttpResponse } from 'msw' +import { setupServer } from 'msw/node' +import chalk from 'chalk' +import { vol } from 'memfs' +import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest' +import { pullLanguages, saveLanguagesToFile } from './actions' + +const handlers = [ + http.get('https://api.storyblok.com/v1/spaces/12345', async ({ request }) => { + const token = request.headers.get('Authorization') + if (token === 'valid-token') { + return HttpResponse.json({ + space: { + default_lang_name: 'en', + languages: [ + { + code: 'ca', + name: 'Catalan', + }, + { + code: 'fr', + name: 'French', + }, + ], + }, + }) + } + return new HttpResponse('Unauthorized', { status: 401 }) + }), +] + +const server = setupServer(...handlers) + +beforeAll(() => server.listen({ onUnhandledRequest: 'error' })) + +afterEach(() => server.resetHandlers()) +afterAll(() => server.close()) + +vi.mock('node:fs') +vi.mock('node:fs/promises') + +describe('pull languages actions', () => { + beforeEach(() => { + vi.clearAllMocks() + vol.reset() + }) + + describe('pullLanguages', () => { + it('should pull languages successfully with a valid token', async () => { + const mockResponse = { + default_lang_name: 'en', + languages: [ + { + code: 'ca', + name: 'Catalan', + }, + { + code: 'fr', + name: 'French', + }, + ], + } + const result = await pullLanguages('12345', 'valid-token', 'eu') + expect(result).toEqual(mockResponse) + }) + }) + it('should throw an masked error for invalid token', async () => { + await expect(pullLanguages('12345', 'invalid-token', 'eu')).rejects.toThrow( + new Error(`The user is not authorized to access the API`), + ) + }) + + describe('saveLanguagesToFile', () => { + it('should save languages to a json file with space number', async () => { + const mockResponse = { + default_lang_name: 'en', + languages: [ + { + code: 'ca', + name: 'Catalan', + }, + { + code: 'fr', + name: 'French', + }, + ], + } + await saveLanguagesToFile('12345', mockResponse, '/temp') + const content = vol.readFileSync('/temp/languages.12345.json', 'utf8') + expect(content).toBe(JSON.stringify(mockResponse, null, 2)) + }) + }) +}) diff --git a/src/commands/pull-languages/actions.ts b/src/commands/pull-languages/actions.ts index 5312292..7f4ebd1 100644 --- a/src/commands/pull-languages/actions.ts +++ b/src/commands/pull-languages/actions.ts @@ -4,8 +4,6 @@ import { join, resolve } from 'node:path' import { handleAPIError, handleFileSystemError } from '../../utils' import { ofetch } from 'ofetch' import { regionsDomain } from '../../constants' -import { session } from '../../session' -import { apiClient } from '../../api' export interface SpaceInternationalizationOptions { languages: SpaceLanguage[] @@ -16,17 +14,11 @@ export interface SpaceLanguage { name: string } -export const pullLanguages = async (space: string): Promise => { +export const pullLanguages = async (space: string, token: string, region: string): Promise => { try { - const { client } = apiClient() - if (!client) { - throw new Error('Client not initialized') - } - - const { state } = session() - const response = await ofetch(`https://${regionsDomain[state.region]}/v1/spaces/${space}`, { + const response = await ofetch(`https://${regionsDomain[region]}/v1/spaces/${space}`, { headers: { - Authorization: state.password, + Authorization: token, }, }) return { diff --git a/src/commands/pull-languages/index.ts b/src/commands/pull-languages/index.ts index 32e1bde..2256070 100644 --- a/src/commands/pull-languages/index.ts +++ b/src/commands/pull-languages/index.ts @@ -22,7 +22,7 @@ export const pullLanguagesCommand = program const { state, initializeSession } = session() await initializeSession() - if (!state.isLoggedIn) { + if (!state.isLoggedIn || !state.password || !state.region) { handleError(new CommandError(`You are currently not logged in. Please login first to get your user info.`), verbose) return } @@ -33,7 +33,7 @@ export const pullLanguagesCommand = program } try { - const internationalization = await pullLanguages(space) + const internationalization = await pullLanguages(space, state.password, state.region) if (!internationalization || !internationalization.languages) { konsola.warn(`No languages found in the space ${space}`)