Skip to content

Commit

Permalink
feature: Add verify method to domains
Browse files Browse the repository at this point in the history
  • Loading branch information
Omazepa committed Apr 8, 2022
1 parent f498f31 commit dd65f7b
Show file tree
Hide file tree
Showing 7 changed files with 14,290 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ results.xml
build

./test.js
.coverage/
1 change: 1 addition & 0 deletions dist/domains.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default class DomainClient {
list(query?: DomainsQuery): Promise<Domain[]>;
get(domain: string): Promise<Domain>;
create(data: DomainInfo): Promise<Domain>;
verify(domain: string): Promise<Domain>;
destroy(domain: string): Promise<MessageResponse>;
getConnection(domain: string): Promise<ConnectionSettings>;
updateConnection(domain: string, data: ConnectionSettings): Promise<UpdatedConnectionSettings>;
Expand Down
10,601 changes: 10,598 additions & 3 deletions dist/mailgun.node.js

Large diffs are not rendered by default.

3,528 changes: 3,525 additions & 3 deletions dist/mailgun.web.js

Large diffs are not rendered by default.

15 changes: 13 additions & 2 deletions lib/domains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export default class DomainClient {
.then((res : APIResponse) => this._parseDomain(res as DomainResponseData));
}

verify(domain: string): Promise<Domain> {
return this.request.put(`/v3/domains/${domain}/verify`, {})
.then((res : APIResponse) => this._parseDomain(res as DomainResponseData));
}

destroy(domain: string): Promise<MessageResponse> {
return this.request.delete(`/v3/domains/${domain}`)
.then((res : APIResponse) => this._parseMessage(res as DestroyedDomainResponse));
Expand Down Expand Up @@ -169,7 +174,7 @@ export default class DomainClient {
data: OpenTrackingInfo | ClickTrackingInfo | UnsubscribeTrackingInfo
): Promise<UpdatedOpenTracking> {
if (typeof data?.active === 'boolean') {
throw new APIError({ status: 400, statusText: '', body: { message: 'Property "active" must contain string value.' } } as APIErrorOptions);
throw new APIError({ status: 400, statusText: 'Received boolean value for active property', body: { message: 'Property "active" must contain string value.' } } as APIErrorOptions);
}
return this.request.putWithFD(urljoin('/v3/domains', domain, 'tracking', type), data)
.then((res : APIResponse) => this._parseTrackingUpdate(res as UpdateDomainTrackingResponse));
Expand Down Expand Up @@ -197,7 +202,13 @@ export default class DomainClient {
unlinkIpPoll(domain: string, replacement: ReplacementForPool): Promise<APIResponse> {
let searchParams = '';
if (replacement.pool_id && replacement.ip) {
throw new APIError({ status: 400, statusText: '', body: { message: 'Please specify either pool_id or ip (not both)' } } as APIErrorOptions);
throw new APIError(
{
status: 400,
statusText: 'Too much data for replacement',
body: { message: 'Please specify either pool_id or ip (not both)' }
} as APIErrorOptions
);
} else if (replacement.pool_id) {
searchParams = `?pool_id=${replacement.pool_id}`;
} else if (replacement.ip) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build:release": "webpack --config ./webpack/webpack.release.config.js --progress --color && npm run build:prepublish",
"build:prepublish": "node ./SetupPackage.js",
"start": "webpack --watch --config ./webpack/webpack.dev.config.js --progress --color",
"test": "multi='dot=- xunit=./results.xml' nyc mocha -t 10000 -R mocha-multi -r ts-node/register test/*.test.ts",
"test": "multi='dot=- xunit=./results.xml' nyc --reporter=text-summary --reporter=lcov --report-dir=.coverage mocha -t 10000 -R mocha-multi -r ts-node/register test/*.test.ts",
"test-watch": "mocha -r ts-node/register -w -R dot test/*.test.ts",
"docs": "typedoc --tsconfig ./tsconfig.json",
"lint": "eslint . && eslint . --ext .ts",
Expand Down
151 changes: 151 additions & 0 deletions test/domains.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from '../lib/interfaces/Domains';
import DomainTemplatesClient from '../lib/domainsTemplates';
import DomainTagsClient from '../lib/domainsTags';
import APIResponse from '../lib/interfaces/ApiResponse';

// TODO: fix types
describe('DomainClient', function () {
Expand Down Expand Up @@ -162,6 +163,78 @@ describe('DomainClient', function () {
});
});

describe('verify', function () {
it('verifies a domain', function () {
const domainData = {
created_at: 'Sun, 19 Oct 2014 18:49:36 GMT',
name: 'test.example.com',
smtp_login: '[email protected]',
smtp_password: 'password',
spam_action: 'disabled',
state: 'active',
type: 'custom',
wildcard: true,
skip_verification: true,
require_tls: true
};

api.put('/v3/domains/test.example.com/verify').reply(200, {
domain: domainData,
receiving_dns_records: [
{
cached: ['test-value1', 'test-value2'],
priority: '10',
record_type: 'MX',
valid: 'valid',
value: 'test-value'
},
],
sending_dns_records: [
{
cached: ['test-value'],
name: 'test name',
record_type: 'TXT',
valid: 'valid',
value: 'test-value'
},
]
});

return client.verify('test.example.com').then(function (domain: Domain) {
domain.should.eql({
created_at: 'Sun, 19 Oct 2014 18:49:36 GMT',
name: 'test.example.com',
receiving_dns_records: [
{
cached: ['test-value1', 'test-value2'],
priority: '10',
record_type: 'MX',
valid: 'valid',
value: 'test-value'
}
],
require_tls: true,
sending_dns_records: [
{
cached: ['test-value'],
name: 'test name',
record_type: 'TXT',
valid: 'valid',
value: 'test-value'
}
],
skip_verification: true,
smtp_login: '[email protected]',
smtp_password: 'password',
spam_action: 'disabled',
state: 'active',
type: 'custom',
wildcard: true
});
});
});
});

describe('destroy', function () {
it('deletes a domain', function () {
api.delete('/v3/domains/test.example.com').reply(200, {
Expand Down Expand Up @@ -246,6 +319,20 @@ describe('DomainClient', function () {
}
});
});

it('checks input parameter', async function () {
try {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await client.updateTracking('domain.com', 'open', { active: true });
} catch (error) {
expect(error).to.include({
status: 400,
details: 'Property "active" must contain string value.',
message: 'Received boolean value for active property'
});
}
});
});

describe('getIps', () => {
Expand All @@ -259,6 +346,70 @@ describe('DomainClient', function () {
});
});

describe('assignIp', () => {
it('should assign Ip', () => {
const ip = '127.0.0.1';
api.post('/v3/domains/domain.com/ips').reply(200, { message: 'success' });

return client.assignIp('domain.com', ip).then((response: APIResponse) => {
response.should.eql({ status: 200, body: { message: 'success' } });
});
});
});

describe('deleteIp', () => {
it('should delete assigned Ip', () => {
const ip = '127.0.0.1';
api.delete('/v3/domains/domain.com/ips/127.0.0.1').reply(200, { message: 'success' });

return client.deleteIp('domain.com', ip).then((response: APIResponse) => {
response.should.eql({ status: 200, body: { message: 'success' } });
});
});
});

describe('linkIpPool', () => {
it('should link ip pool', () => {
const ipPool = '121212121212121212121212';
api.post('/v3/domains/domain.com/ips').reply(200, { message: 'success' });

return client.linkIpPool('domain.com', ipPool).then((response: APIResponse) => {
response.should.eql({ status: 200, body: { message: 'success' } });
});
});
});

describe('unlinkIpPoll', () => {
it('should unlink ip pool with new ip pool', () => {
const ipPool = '121212121212121212121212';
api.delete(`/v3/domains/domain.com/ips/ip_pool?pool_id=${ipPool}`).reply(200, { message: 'success' });

return client.unlinkIpPoll('domain.com', { pool_id: ipPool }).then((response: APIResponse) => {
response.should.eql({ status: 200, body: { message: 'success' } });
});
});

it('should unlink ip pool with ip', () => {
const ip = '127.0.0.1';
api.delete(`/v3/domains/domain.com/ips/ip_pool?ip=${ip}`).reply(200, { message: 'success' });

return client.unlinkIpPoll('domain.com', { ip }).then((response: APIResponse) => {
response.should.eql({ status: 200, body: { message: 'success' } });
});
});

it('should check replacement data', async () => {
try {
await client.unlinkIpPoll('domain.com', { ip: 'test', pool_id: 'test' });
} catch (error) {
expect(error).to.include({
status: 400,
details: 'Please specify either pool_id or ip (not both)',
message: 'Too much data for replacement'
});
}
});
});
describe('updateDKIMAuthority', () => {
it('changes the DKIM authority for a domain.', () => {
const expectedRes = {
Expand Down

0 comments on commit dd65f7b

Please sign in to comment.