Skip to content

Commit

Permalink
tests: updateDnsRecords
Browse files Browse the repository at this point in the history
  • Loading branch information
proohit committed Apr 30, 2022
1 parent 36f0a8f commit e7e97d1
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 0 deletions.
48 changes: 48 additions & 0 deletions test/api.spec.ts
Expand Up @@ -3,13 +3,15 @@ import {
ApiResponse,
InfoDNSRecordsResponse,
InfoDNSZoneResponse,
UpdateDNSRecordsResponse,
} from '../src/@types/Responses';
import NetcupRestApi from '../src/api';
import {
createEmptyApiResponse,
createEmptyInfoDnsRecordsResponse,
createEmptyInfoDnsZoneResponse,
createEmptyLoginResponse,
createEmptyUpdateDnsRecordsResponse,
} from './testUtils';

describe('Api functions', () => {
Expand Down Expand Up @@ -89,6 +91,52 @@ describe('Api functions', () => {
});
expect(res).toEqual(givenResponse);
});

it('updateDnsRecords', async () => {
const api = new NetcupRestApi();
const emptyResponse = createEmptyUpdateDnsRecordsResponse();
const givenResponse: UpdateDNSRecordsResponse = {
...emptyResponse,
statuscode: 2000,
responsedata: {
...emptyResponse.responsedata,
dnsrecords: [
{
id: '1',
hostname: 'www',
type: 'A',
state: 'yes',
priority: '',
deleterecord: false,
destination: '@',
},
],
},
};
jest
.spyOn(api.axios, 'post')
.mockReturnValue(Promise.resolve({ data: givenResponse }));
const res = await api.updateDnsRecords({
domainname: 'test.com',
customernumber: '',
apikey: '',
apisessionid: '',
dnsrecordset: {
dnsrecords: [
{
id: '1',
hostname: 'www',
type: 'A',
state: 'yes',
priority: '',
deleterecord: false,
destination: '@',
},
],
},
});
expect(res).toEqual(givenResponse);
});
});

describe('format tests', () => {
Expand Down
48 changes: 48 additions & 0 deletions test/index.spec.ts
Expand Up @@ -3,11 +3,13 @@ import { InitParams } from '../src/@types/InitParams';
import {
InfoDNSRecordsResponse,
InfoDNSZoneResponse,
UpdateDNSRecordsResponse,
} from '../src/@types/Responses';
import { INVALID_FORMAT_ERROR, NOT_INITIALIZED_ERROR } from '../src/constants';
import {
createEmptyInfoDnsRecordsResponse,
createEmptyInfoDnsZoneResponse,
createEmptyUpdateDnsRecordsResponse,
givenSessionId,
mockLoginResponse,
} from './testUtils';
Expand Down Expand Up @@ -116,4 +118,50 @@ describe('exported functions', () => {
).rejects.toThrow(NOT_INITIALIZED_ERROR);
});
});

describe('updateDnsRecords tests', () => {
it('should correctly update dns records', async () => {
const netcupApi = new NetcupApi();
mockLoginResponse(netcupApi.restApi);
await netcupApi.init(givenAuthData);
const givenDnsRecordsUpdate: UpdateDNSRecordsResponse = {
...createEmptyUpdateDnsRecordsResponse(),
responsedata: {
dnsrecords: [
{
id: '',
deleterecord: false,
hostname: 'test',
priority: '',
state: 'yes',
type: 'A',
destination: '@',
},
],
},
};

jest
.spyOn(netcupApi.restApi, 'updateDnsRecords')
.mockReturnValue(Promise.resolve(givenDnsRecordsUpdate));
const updateDnsRecordsResult = await netcupApi.updateDnsRecords({
dnsrecordset: {
dnsrecords: [],
},
domainname: 'test.domain.com',
});
expect(updateDnsRecordsResult).toEqual(givenDnsRecordsUpdate);
});

it('should throw error on update dns records without auth', async () => {
await expect(() =>
new NetcupApi().updateDnsRecords({
dnsrecordset: {
dnsrecords: [],
},
domainname: '',
}),
).rejects.toThrow(NOT_INITIALIZED_ERROR);
});
});
});
10 changes: 10 additions & 0 deletions test/testUtils.ts
Expand Up @@ -5,6 +5,7 @@ import {
InfoDNSRecordsResponse,
InfoDNSZoneResponse,
LoginResponse,
UpdateDNSRecordsResponse,
} from '../src/@types/Responses';
import NetcupRestApi from '../src/api';

Expand Down Expand Up @@ -67,3 +68,12 @@ export function mockLoginResponse(api: NetcupRestApi) {
};
jest.spyOn(api, 'login').mockReturnValue(Promise.resolve(givenLoginResponse));
}

export const createEmptyUpdateDnsRecordsResponse =
(): UpdateDNSRecordsResponse => ({
...createEmptyApiResponse<UpdateDNSRecordsResponse>(),
action: Actions.updateDnsRecords,
responsedata: {
dnsrecords: [],
},
});

0 comments on commit e7e97d1

Please sign in to comment.