Skip to content

Commit 39183c1

Browse files
authored
test: add test for staging extension api (#6141)
* fix: use consts and write tests for staging endpoint extensions * fix: implement feedback
1 parent 61c3b00 commit 39183c1

File tree

5 files changed

+69
-32
lines changed

5 files changed

+69
-32
lines changed

packages/config/src/api/site_info.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,14 @@ const getIntegrations = async function ({
135135
return []
136136
}
137137
const sendBuildBotTokenToJigsaw = featureFlags?.send_build_bot_token_to_jigsaw
138-
const { host } = testOpts
139-
const baseUrl = new URL(host ? `http://${host}` : extensionApiBaseUrl)
138+
const { host, setBaseUrl } = testOpts
139+
140+
// We only use this for testing
141+
if (host && setBaseUrl) {
142+
setBaseUrl(extensionApiBaseUrl)
143+
}
140144

145+
const baseUrl = new URL(host ? `http://${host}` : extensionApiBaseUrl)
141146
// if accountId isn't present, use safe v1 endpoint
142147
const url = accountId
143148
? `${baseUrl}team/${accountId}/integrations/installations/meta/${siteId}`

packages/config/src/integrations.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import { IntegrationResponse } from './types/api.js'
33
import { Integration } from './types/integrations.js'
44
import { TestOptions } from './types/options.js'
55

6+
export const NETLIFY_API_STAGING_BASE_URL = 'api-staging.netlify.com'
7+
export const EXTENSION_API_BASE_URL = 'https://api.netlifysdk.com'
8+
export const EXTENSION_API_STAGING_BASE_URL = 'https://api-staging.netlifysdk.com'
9+
610
type MergeIntegrationsOpts = {
711
configIntegrations?: { name: string; dev?: { path: string; force_run_in_build?: boolean } }[]
812
apiIntegrations: IntegrationResponse[]
@@ -60,11 +64,7 @@ export const mergeIntegrations = async function ({
6064
return undefined
6165
}
6266

63-
return {
64-
slug: integration.slug,
65-
version: integration.hostSiteUrl,
66-
has_build: !!integration.hasBuild,
67-
}
67+
return { slug: integration.slug, version: integration.hostSiteUrl, has_build: !!integration.hasBuild }
6868
})
6969
.filter((i): i is IntegrationResponse => typeof i !== 'undefined')
7070

packages/config/src/main.ts

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import { getEnv } from './env/main.js'
99
import { resolveConfigPaths } from './files.js'
1010
import { getHeadersPath, addHeaders } from './headers.js'
1111
import { getInlineConfig } from './inline_config.js'
12-
import { mergeIntegrations } from './integrations.js'
12+
import {
13+
EXTENSION_API_BASE_URL,
14+
EXTENSION_API_STAGING_BASE_URL,
15+
mergeIntegrations,
16+
NETLIFY_API_STAGING_BASE_URL,
17+
} from './integrations.js'
1318
import { logResult } from './log/main.js'
1419
import { mergeConfigs } from './merge.js'
1520
import { normalizeBeforeConfigMerge, normalizeAfterConfigMerge } from './merge_normalize.js'
@@ -51,7 +56,7 @@ export const resolveConfig = async function (opts) {
5156

5257
// TODO(kh): remove this mapping and get the extensionApiHost from the opts
5358
const extensionApiBaseUrl =
54-
host === 'api-staging.netlify.com' ? 'https://api-staging.netlifysdk.com' : 'https://api.netlifysdk.com'
59+
host === NETLIFY_API_STAGING_BASE_URL ? EXTENSION_API_STAGING_BASE_URL : EXTENSION_API_BASE_URL
5560

5661
const {
5762
config: configOpt,
@@ -189,13 +194,7 @@ const addLegacyFunctionsDirectory = (config) => {
189194
return config
190195
}
191196

192-
return {
193-
...config,
194-
build: {
195-
...config.build,
196-
functions: config.functionsDirectory,
197-
},
198-
}
197+
return { ...config, build: { ...config.build, functions: config.functionsDirectory } }
199198
}
200199

201200
/**
@@ -289,24 +288,10 @@ const getFullConfig = async function ({
289288
logs,
290289
featureFlags,
291290
}) {
292-
const configPath = await getConfigPath({
293-
configOpt,
294-
cwd,
295-
repositoryRoot,
296-
packagePath,
297-
configBase,
298-
})
291+
const configPath = await getConfigPath({ configOpt, cwd, repositoryRoot, packagePath, configBase })
299292
try {
300293
const config = await parseConfig(configPath)
301-
const configA = mergeAndNormalizeConfig({
302-
config,
303-
defaultConfig,
304-
inlineConfig,
305-
context,
306-
branch,
307-
logs,
308-
packagePath,
309-
})
294+
const configA = mergeAndNormalizeConfig({ config, defaultConfig, inlineConfig, context, branch, logs, packagePath })
310295
const {
311296
config: configB,
312297
buildDir,

packages/config/src/types/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ export type ModeOption = 'buildbot' | 'cli' | 'require'
33
export type TestOptions = {
44
env?: boolean
55
host?: string
6+
setBaseUrl?: (url: string) => void
67
}

packages/config/tests/api/tests.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import { fileURLToPath } from 'url'
44
import { Fixture, normalizeOutput } from '@netlify/testing'
55
import test from 'ava'
66

7+
import {
8+
EXTENSION_API_STAGING_BASE_URL,
9+
NETLIFY_API_STAGING_BASE_URL,
10+
EXTENSION_API_BASE_URL,
11+
} from '../../lib/integrations.js'
12+
713
const SITE_INFO_PATH = '/api/v1/sites/test'
814
const SITE_INFO_DATA = {
915
path: SITE_INFO_PATH,
@@ -517,3 +523,43 @@ test('It fetches site info if cachedConfig is provided, use_cached_site_info is
517523

518524
t.assert(requests.length === 0)
519525
})
526+
527+
test('We call the staging extension API when the apiHost is not api.netlify.com', async (t) => {
528+
let baseUrl = ''
529+
const setBaseUrl = (url) => {
530+
baseUrl = url
531+
}
532+
533+
await new Fixture('./fixtures/base')
534+
.withFlags({
535+
siteId: 'test',
536+
mode: 'dev',
537+
token: 'test',
538+
accountId: 'account1',
539+
testOpts: { host: undefined, setBaseUrl },
540+
host: NETLIFY_API_STAGING_BASE_URL,
541+
})
542+
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
543+
544+
t.assert(baseUrl === EXTENSION_API_STAGING_BASE_URL)
545+
})
546+
547+
test('We call the production extension API when the apiHost is api.netlify.com', async (t) => {
548+
let baseUrl = ''
549+
const setBaseUrl = (url) => {
550+
baseUrl = url
551+
}
552+
553+
await new Fixture('./fixtures/base')
554+
.withFlags({
555+
siteId: 'test',
556+
mode: 'dev',
557+
token: 'test',
558+
accountId: 'account1',
559+
testOpts: { host: undefined, setBaseUrl },
560+
host: 'api.netlify.com',
561+
})
562+
.runConfigServer([SITE_INFO_DATA, TEAM_INSTALLATIONS_META_RESPONSE, FETCH_INTEGRATIONS_EMPTY_RESPONSE])
563+
564+
t.assert(baseUrl === EXTENSION_API_BASE_URL)
565+
})

0 commit comments

Comments
 (0)