Skip to content

Commit 18dea77

Browse files
authored
Merge pull request #257 from hashicorp/repo-sync
Repo sync
2 parents 0b414ef + a396b5d commit 18dea77

File tree

61 files changed

+178
-288
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+178
-288
lines changed

.github/workflows/build-pr-preview.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ run-name: 'Build Preview for "${{ github.event.pull_request.title }}" (#${{ gith
44
on:
55
pull_request_target:
66
types: [opened, synchronize]
7-
branches:
8-
- main
9-
- develop
10-
# paths:
117
# Hello Security 👋, we are checking to make sure forked repo PR changed paths are only in content/** inside the job security-check.
128
# We are doing this so we can also reuse this workflow for internal PRs, as pull_request_target also triggers on internal PRs. (As does pull_request)
139

@@ -220,7 +216,7 @@ jobs:
220216
echo "HASHI_ENV=unified-docs-sandbox" >> .env
221217
echo "UNIFIED_DOCS_API=${{ needs.deploy-unified-docs-api-preview.outputs.preview_url }}" >> .env
222218
echo "VERCEL_AUTOMATION_BYPASS_SECRET=${{ secrets.VERCEL_AUTOMATION_BYPASS_SECRET }}" >> .env
223-
219+
224220
- name: Build dev-portal
225221
uses: nick-fields/retry@7152eba30c6575329ac0576536151aca5a72780e # v3.0.0
226222
env:

app/api/all-docs-paths/route.test.ts

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { expect, test, vi, afterEach } from 'vitest'
77
import docsPathsMock from '__fixtures__/docsPathsAllVersionsMock.json'
88
import { GET } from './route'
99
import * as getDocsPaths from '@utils/allDocsPaths'
10+
import { mockRequest } from '@utils/mockRequest'
1011

1112
afterEach(() => {
1213
vi.restoreAllMocks()
@@ -29,12 +30,7 @@ test('GET should return a 200 response with no products', async () => {
2930
ok: true,
3031
value: Object.values(docsPathsMock).flat(),
3132
})
32-
const mockRequest = (url: string) => {
33-
return new Request(url)
34-
}
35-
const request = mockRequest(`http://localhost:8080/api/all-docs-paths`)
36-
37-
const response = await GET(request)
33+
const response = await mockRequest(GET, {})
3834

3935
expect(response.status).toBe(200)
4036
})
@@ -44,14 +40,12 @@ test('GET should return a 200 response for one product in the search params', as
4440
ok: true,
4541
value: docsPathsMock['terraform-plugin-framework']['v1.13.x'],
4642
})
47-
const mockRequest = (url: string) => {
48-
return new Request(url)
49-
}
50-
const request = mockRequest(
51-
`http://localhost:8080/api/all-docs-paths?products=terraform-plugin-framework`,
52-
)
5343

54-
const response = await GET(request)
44+
const response = await mockRequest(
45+
GET,
46+
{},
47+
'all-docs-paths?products=terraform-plugin-framework',
48+
)
5549

5650
expect(response.status).toBe(200)
5751
})
@@ -64,15 +58,12 @@ test('GET should return a 200 response for multiple products in the search param
6458
...docsPathsMock['terraform-plugin-mux']['v0.18.x'],
6559
],
6660
})
67-
const mockRequest = (url: string) => {
68-
return new Request(url)
69-
}
70-
const request = mockRequest(
71-
`http://localhost:8080/api/all-docs-paths?=terraform-plugin-framework&products=terraform-plugin-mux`,
61+
const response = await mockRequest(
62+
GET,
63+
{},
64+
'all-docs-paths?products=terraform-plugin-framework&products=terraform-plugin-mux',
7265
)
7366

74-
const response = await GET(request)
75-
7667
expect(response.status).toBe(200)
7768
})
7869

@@ -83,12 +74,7 @@ test('GET should return error if docsPaths are not found', async () => {
8374
})
8475
global.fetch = vi.fn()
8576
const mockConsole = vi.spyOn(console, 'error').mockImplementation(() => {})
86-
87-
const mockRequest = (url: string) => {
88-
return new Request(url)
89-
}
90-
const request = mockRequest(`http://localhost:8080/api/all-docs-paths`)
91-
const response = await GET(request)
77+
const response = await mockRequest(GET, {})
9278

9379
expect(mockConsole).toHaveBeenCalledOnce()
9480
expect(mockConsole).toHaveBeenLastCalledWith(

app/api/assets/[productSlug]/[version]/[...assetPath]/route.test.ts

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { expect, test, vi } from 'vitest'
77
import { GET } from './route'
88
import { getAssetData } from '@utils/file'
99
import { getProductVersion } from '@utils/contentVersions'
10+
import { mockRequest } from '@utils/mockRequest'
1011

1112
vi.mock('@utils/file')
1213
vi.mock('@utils/contentVersions')
@@ -23,21 +24,16 @@ vi.mock('@api/versionMetadata.json', () => {
2324
})
2425

2526
test("Return 404 if `product` doesn't exist", async () => {
26-
const mockRequest = (url: string) => {
27-
return new Request(url)
28-
}
29-
3027
// eat error message
3128
vi.spyOn(console, 'error').mockImplementation(() => {})
3229

3330
const productSlug = 'fake product'
3431
const version = 'v1.1.x'
3532
const assetPath = ['test.png']
36-
const request = mockRequest(
37-
`http://localhost:8080/api/assets/${productSlug}/${version}/${assetPath.join('/')}`,
38-
)
39-
const response = await GET(request, {
40-
params: { productSlug, version, assetPath },
33+
const response = await mockRequest(GET, {
34+
productSlug,
35+
version,
36+
assetPath,
4137
})
4238

4339
expect(response.status).toBe(404)
@@ -46,22 +42,13 @@ test("Return 404 if `product` doesn't exist", async () => {
4642
})
4743

4844
test("Return 404 if `version` doesn't exist for `productSlug`", async () => {
49-
const mockRequest = (url: string) => {
50-
return new Request(url)
51-
}
52-
5345
const productSlug = 'terraform'
5446
const version = 'fake_version'
5547
const assetPath = ['test.png']
56-
const request = mockRequest(
57-
`http://localhost:8080/api/assets/${productSlug}/${version}/${assetPath.join('/')}`,
58-
)
5948

6049
vi.mocked(getProductVersion).mockReturnValueOnce({ ok: false, value: '' })
6150

62-
const response = await GET(request, {
63-
params: { productSlug, version, assetPath },
64-
})
51+
const response = await mockRequest(GET, { productSlug, version, assetPath })
6552

6653
expect(response.status).toBe(404)
6754
const text = await response.text()
@@ -75,8 +62,6 @@ test('Return 200 and an image for a valid `product`, `version`, and `assetPath`'
7562
assetPath: ['test.png'],
7663
}
7764

78-
const request = new Request('http://localhost:8080')
79-
8065
const assetData: {
8166
ok: true
8267
value: { buffer: Buffer; contentType: string }
@@ -95,7 +80,7 @@ test('Return 200 and an image for a valid `product`, `version`, and `assetPath`'
9580

9681
vi.mocked(getAssetData).mockResolvedValueOnce(assetData)
9782

98-
const response = await GET(request, { params })
83+
const response = await mockRequest(GET, params)
9984

10085
expect(response.status).toBe(200)
10186
const buffer = Buffer.from(await response.arrayBuffer())
@@ -109,8 +94,6 @@ test('Return 200 and an image for the `version` being `latest` and the rest of t
10994
assetPath: ['test.png'],
11095
}
11196

112-
const request = new Request('http://localhost:8080')
113-
11497
const assetData: {
11598
ok: true
11699
value: { buffer: Buffer; contentType: string }
@@ -129,7 +112,7 @@ test('Return 200 and an image for the `version` being `latest` and the rest of t
129112

130113
vi.mocked(getAssetData).mockResolvedValueOnce(assetData)
131114

132-
const response = await GET(request, { params })
115+
const response = await mockRequest(GET, params)
133116

134117
expect(response.status).toBe(200)
135118
const buffer = Buffer.from(await response.arrayBuffer())

app/api/assets/[productSlug]/[version]/[...assetPath]/route.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,9 @@ import { getAssetData } from '@utils/file'
77
import { getProductVersion } from '@utils/contentVersions'
88
import { errorResultToString } from '@utils/result'
99
import { PRODUCT_CONFIG } from '@utils/productConfig.mjs'
10+
import { VersionedProduct } from '@api/types'
1011

11-
export type GetParams = {
12-
/**
13-
* The product that docs are being requested for (i.e "terraform")
14-
*/
15-
productSlug: string
16-
17-
/**
18-
* Can be a semver version (i.e: `v10.0.x`) or a ptfe version (i.e: `v202206-01`)
19-
*/
20-
version: string
21-
12+
export type GetParams = VersionedProduct & {
2213
/**
2314
* Full path to the asset in the production build, i.e. `terraform/v1.9.x/img/docs/plan-comments.png`
2415
*/

app/api/content-versions/route.test.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import { expect, test, vi, beforeEach, afterEach } from 'vitest'
77
import { GET } from './route'
8+
import { mockRequest } from '@utils/mockRequest'
89

910
import { vol } from 'memfs'
1011

@@ -40,13 +41,11 @@ afterEach(() => {
4041
})
4142

4243
test('should return 400 if `product` query parameter is missing', async () => {
43-
const mockRequest = (url: string) => {
44-
return new Request(url)
45-
}
46-
const request = mockRequest(
47-
'http://localhost:8080/api/content-versions?fullPath=doc#docs/internals',
44+
const response = await mockRequest(
45+
GET,
46+
{},
47+
'content-versions?fullPath=doc%23cdktf%2Fapi-reference%2Fpython',
4848
)
49-
const response = await GET(request)
5049
expect(response.status).toBe(400)
5150
const text = await response.text()
5251
expect(text).toBe(
@@ -55,13 +54,7 @@ test('should return 400 if `product` query parameter is missing', async () => {
5554
})
5655

5756
test('should return 400 if `fullPath` query parameter is missing', async () => {
58-
const mockRequest = (url: string) => {
59-
return new Request(url)
60-
}
61-
const request = mockRequest(
62-
'http://localhost:8080/api/content-versions?product=vault',
63-
)
64-
const response = await GET(request)
57+
const response = await mockRequest(GET, {}, 'content-versions?product=vault')
6558
expect(response.status).toBe(400)
6659
const text = await response.text()
6760
expect(text).toBe(
@@ -72,13 +65,11 @@ test('should return 400 if `fullPath` query parameter is missing', async () => {
7265
test('should return 404 if the product is invalid', async () => {
7366
vol.fromJSON({})
7467

75-
const mockRequest = (url: string) => {
76-
return new Request(url)
77-
}
78-
const request = mockRequest(
79-
'http://localhost:8080/api/content-versions?product=nonexistent&fullPath=doc#docs/internals',
68+
const response = await mockRequest(
69+
GET,
70+
{},
71+
'content-versions?product=nonexistent&fullPath=doc%23cdktf%2Fapi-reference%2Fpython',
8072
)
81-
const response = await GET(request)
8273
expect(response.status).toBe(404)
8374
const text = await response.text()
8475
expect(text).toBe('Not found')
@@ -95,13 +86,11 @@ test('should return 200 and array of strings on valid params', async () => {
9586
const mockedResponse = {
9687
versions: ['v0.20.x', 'v0.21.x'],
9788
}
98-
const mockRequest = (url: string) => {
99-
return new Request(url)
100-
}
101-
const request = mockRequest(
102-
`http://localhost:8080/api/content-versions?product=terraform-cdk&fullPath=doc%23cdktf%2Fapi-reference%2Fpython`,
89+
const response = await mockRequest(
90+
GET,
91+
{},
92+
'content-versions?product=terraform-cdk&fullPath=doc%23cdktf%2Fapi-reference%2Fpython',
10393
)
104-
const response = await GET(request)
10594
expect(response.status).toBe(200)
10695
const json = await response.json()
10796
expect(json).toEqual(mockedResponse)

app/api/content/[productSlug]/doc/[version]/[...docsPath]/route.test.ts

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ import {
1212
afterAll,
1313
MockInstance,
1414
} from 'vitest'
15-
import { GET, GetParams } from './route'
15+
import { GET } from './route'
1616
import { Err, Ok } from '@utils/result'
1717
import { getProductVersion } from '@utils/contentVersions'
1818
import { PRODUCT_CONFIG } from '__fixtures__/productConfig.mjs'
1919
import { readFile, parseMarkdownFrontMatter } from '@utils/file'
20+
import { mockRequest } from '@utils/mockRequest'
2021

2122
vi.mock(import('@utils/contentVersions'), async (importOriginal: any) => {
2223
const mod = await importOriginal()
@@ -63,19 +64,8 @@ vi.mock('@utils/productConfig.mjs', () => {
6364
})
6465

6566
describe('GET /[productSlug]/[version]/[...docsPath]', () => {
66-
let mockRequest: (params: GetParams) => ReturnType<typeof GET>
6767
let consoleMock: MockInstance<Console['error']>
6868
beforeEach(() => {
69-
mockRequest = (params: GetParams) => {
70-
const { productSlug, version, docsPath } = params
71-
// The URL doesn't actually matter in testing, but for completeness
72-
// it's nice to have it match the real URL being used
73-
const url = new URL(
74-
`http://localhost:8000/api/content/${productSlug}/doc/${version}/${docsPath.join('/')}`,
75-
)
76-
const req = new Request(url)
77-
return GET(req, { params })
78-
}
7969
// spy on console.error so that we can examine it's calls
8070
consoleMock = vi.spyOn(console, 'error').mockImplementation(() => {})
8171
})
@@ -85,7 +75,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
8575

8676
it('returns a 404 for nonexistent products', async () => {
8777
const fakeProductSlug = 'fake product'
88-
const response = await mockRequest({
78+
const response = await mockRequest(GET, {
8979
docsPath: [''],
9080
productSlug: fakeProductSlug,
9181
version: '',
@@ -107,7 +97,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
10797
vi.mocked(getProductVersion).mockReturnValue(
10898
Err(`Product, ${productSlug}, has no "${version}" version`),
10999
)
110-
const response = await mockRequest({
100+
const response = await mockRequest(GET, {
111101
docsPath: [''],
112102
productSlug,
113103
version,
@@ -135,7 +125,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
135125
return Err(`Failed to read file at path: ${filePath.join('/')}`)
136126
})
137127

138-
const response = await mockRequest({
128+
const response = await mockRequest(GET, {
139129
docsPath: [''],
140130
productSlug,
141131
version,
@@ -166,7 +156,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
166156
return Err('Failed to parse Markdown front-matter')
167157
})
168158

169-
const response = await mockRequest({
159+
const response = await mockRequest(GET, {
170160
docsPath: [''],
171161
productSlug,
172162
version,
@@ -202,7 +192,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
202192
return Ok({ markdownSource, metadata: {} })
203193
})
204194

205-
const response = await mockRequest({
195+
const response = await mockRequest(GET, {
206196
docsPath: ['plugin', 'framework', 'internals', 'rpcs'],
207197
productSlug,
208198
version,
@@ -243,7 +233,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
243233
return Ok({ markdownSource, metadata: {} })
244234
})
245235

246-
const response = await mockRequest({
236+
const response = await mockRequest(GET, {
247237
docsPath: ['plugin', 'framework', 'internals', 'rpcs.mdx'],
248238
productSlug,
249239
version,
@@ -279,7 +269,7 @@ describe('GET /[productSlug]/[version]/[...docsPath]', () => {
279269
Ok({ markdownSource, metadata: {} }),
280270
)
281271

282-
const response = await mockRequest({
272+
const response = await mockRequest(GET, {
283273
docsPath: ['docs', 'example'],
284274
productSlug,
285275
version,

0 commit comments

Comments
 (0)