-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate edge dns zone creation to api v4 (#2093)
- Loading branch information
1 parent
8398553
commit eb71aa4
Showing
16 changed files
with
161 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/services/edge-dns-services/v4/create-edge-dns-zones-service.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { AxiosHttpClientAdapter } from '@/services/axios/AxiosHttpClientAdapter' | ||
import * as Errors from '@/services/axios/errors' | ||
import { makeEdgeDNSBaseUrl } from './make-edge-dns-base-url' | ||
import { extractApiError } from '@/helpers/extract-api-error' | ||
|
||
export const createEdgeDNSZonesService = async (payload) => { | ||
const adaptPayload = adapt(payload) | ||
|
||
let httpResponse = await AxiosHttpClientAdapter.request({ | ||
url: `${makeEdgeDNSBaseUrl()}/zones`, | ||
method: 'POST', | ||
body: adaptPayload | ||
}) | ||
|
||
return parseHttpResponse(httpResponse) | ||
} | ||
|
||
const adapt = (payload) => { | ||
return { | ||
name: payload.name, | ||
domain: payload.domain, | ||
active: payload.isActive | ||
} | ||
} | ||
|
||
/** | ||
* @param {Object} httpResponse - The HTTP response object. | ||
* @param {Object} httpResponse.body - The response body. | ||
* @returns {string} The formatted error message. | ||
*/ | ||
const parseHttpResponse = (httpResponse) => { | ||
switch (httpResponse.statusCode) { | ||
case 202: | ||
return { | ||
feedback: 'Your Edge DNS Zone has been created', | ||
urlToEditView: `/edge-dns/edit/${httpResponse.body.data.id}` | ||
} | ||
case 500: | ||
throw new Errors.InternalServerError().message | ||
default: | ||
throw new Error(extractApiError(httpResponse)).message | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
src/tests/services/edge-dns-services/v4/create-edge-dns-zones-service.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { AxiosHttpClientAdapter } from '@/services/axios/AxiosHttpClientAdapter' | ||
import * as Errors from '@/services/axios/errors' | ||
import { createEdgeDNSZonesService } from '@/services/edge-dns-services/v4/create-edge-dns-zones-service' | ||
import { describe, expect, it, vi } from 'vitest' | ||
|
||
const fixtures = { | ||
edgeDNSZoneMock: { | ||
name: 'mockName', | ||
domain: 'example.com', | ||
isActive: true | ||
} | ||
} | ||
|
||
const makeSut = () => { | ||
const sut = createEdgeDNSZonesService | ||
|
||
return { sut } | ||
} | ||
|
||
describe('EdgeDNSZonesService', () => { | ||
it('should call API with correct params', async () => { | ||
const requestSpy = vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({ | ||
statusCode: 202, | ||
body: { | ||
data: { | ||
id: '123456' | ||
} | ||
} | ||
}) | ||
const { sut } = makeSut() | ||
|
||
await sut(fixtures.edgeDNSZoneMock) | ||
|
||
expect(requestSpy).toHaveBeenCalledWith({ | ||
url: 'v4/edge_dns/zones', | ||
method: 'POST', | ||
body: { | ||
name: fixtures.edgeDNSZoneMock.name, | ||
domain: fixtures.edgeDNSZoneMock.domain, | ||
active: fixtures.edgeDNSZoneMock.isActive | ||
} | ||
}) | ||
}) | ||
|
||
it('should return feedback and urlToEditView when successfully create an Edge DNS Zone', async () => { | ||
const zoneId = '123456' | ||
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({ | ||
statusCode: 202, | ||
body: { | ||
data: { | ||
id: zoneId | ||
} | ||
} | ||
}) | ||
const { sut } = makeSut() | ||
|
||
const result = await sut(fixtures.edgeDNSZoneMock) | ||
|
||
expect(result).toEqual({ | ||
feedback: 'Your Edge DNS Zone has been created', | ||
urlToEditView: `/edge-dns/edit/${zoneId}` | ||
}) | ||
}) | ||
|
||
it('should throw internal server error when request fails with 500 status code', async () => { | ||
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({ | ||
statusCode: 500, | ||
body: { | ||
detail: 'Internal Server Error' | ||
} | ||
}) | ||
|
||
const { sut } = makeSut() | ||
|
||
const apiErrorResponse = sut(fixtures.edgeDNSZoneMock) | ||
const expectedError = new Errors.InternalServerError().message | ||
|
||
expect(apiErrorResponse).rejects.toBe(expectedError) | ||
}) | ||
|
||
it('should throw parsing api error when request fails with non-500 status code', async () => { | ||
const apiErrorMock = { | ||
detail: 'api error message' | ||
} | ||
|
||
vi.spyOn(AxiosHttpClientAdapter, 'request').mockResolvedValueOnce({ | ||
statusCode: 400, | ||
body: apiErrorMock | ||
}) | ||
|
||
const { sut } = makeSut() | ||
|
||
const apiErrorResponse = sut(fixtures.edgeDNSZoneMock) | ||
|
||
expect(apiErrorResponse).rejects.toBe('api error message') | ||
}) | ||
}) |