@jesseditson/dnsimple
Version:
A Node.JS client for the DNSimple API.
309 lines (308 loc) • 16.6 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock = require("nock");
const main_1 = require("../lib/main");
const util_1 = require("./util");
const dnsimple = (0, util_1.createTestClient)();
describe("templates", () => {
describe("#listTemplates", () => {
const accountId = 1010;
it("supports pagination", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates?page=1")
.reply((0, util_1.readFixtureAt)("listTemplates/success.http"));
yield dnsimple.templates.listTemplates(accountId, { page: 1 });
expect(scope.isDone()).toBeTruthy();
}));
it("supports extra request options", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates?foo=bar")
.reply((0, util_1.readFixtureAt)("listTemplates/success.http"));
yield dnsimple.templates.listTemplates(accountId, { foo: "bar" });
expect(scope.isDone()).toBeTruthy();
}));
it("supports sorting", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates?sort=name%3Aasc")
.reply((0, util_1.readFixtureAt)("listTemplates/success.http"));
yield dnsimple.templates.listTemplates(accountId, { sort: "name:asc" });
expect(scope.isDone()).toBeTruthy();
}));
it("produces a template list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates")
.reply((0, util_1.readFixtureAt)("listTemplates/success.http"));
const response = yield dnsimple.templates.listTemplates(accountId);
const templates = response.data;
expect(templates.length).toBe(2);
expect(templates[0].name).toBe("Alpha");
expect(templates[0].account_id).toBe(1010);
}));
it("exposes the pagination info", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates")
.reply((0, util_1.readFixtureAt)("listTemplates/success.http"));
const response = yield dnsimple.templates.listTemplates(accountId);
const pagination = response.pagination;
expect(pagination).not.toBe(null);
expect(pagination.current_page).toBe(1);
}));
});
describe("#listTemplates.collectAll", () => {
const accountId = 1010;
it("produces a complete list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates?page=1")
.reply((0, util_1.readFixtureAt)("pages-1of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/templates?page=2")
.reply((0, util_1.readFixtureAt)("pages-2of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/templates?page=3")
.reply((0, util_1.readFixtureAt)("pages-3of3.http"));
const items = yield dnsimple.templates.listTemplates.collectAll(accountId);
expect(items.length).toBe(5);
expect(items[0].id).toBe(1);
expect(items[4].id).toBe(5);
}));
});
describe("#getTemplate", () => {
const accountId = 1010;
const templateId = "name";
it("produces a template", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/name")
.reply((0, util_1.readFixtureAt)("getTemplate/success.http"));
const response = yield dnsimple.templates.getTemplate(accountId, templateId);
const template = response.data;
expect(template.id).toBe(1);
expect(template.account_id).toBe(1010);
expect(template.name).toBe("Alpha");
expect(template.sid).toBe("alpha");
expect(template.description).toBe("An alpha template.");
expect(template.created_at).toBe("2016-03-22T11:08:58Z");
expect(template.updated_at).toBe("2016-03-22T11:08:58Z");
}));
describe("when the template does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/name")
.reply((0, util_1.readFixtureAt)("notfound-template.http"));
yield expect(dnsimple.templates.getTemplate(accountId, templateId)).rejects.toThrow(main_1.NotFoundError);
}));
});
});
describe("#createTemplate", () => {
const accountId = 1010;
const attributes = { name: "Beta" };
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.post("/v2/1010/templates", { name: "Beta" })
.reply((0, util_1.readFixtureAt)("createTemplate/created.http"));
yield dnsimple.templates.createTemplate(accountId, attributes);
expect(scope.isDone()).toBeTruthy();
}));
it("produces a template", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.post("/v2/1010/templates")
.reply((0, util_1.readFixtureAt)("createTemplate/created.http"));
const response = yield dnsimple.templates.createTemplate(accountId, attributes);
expect(response.data.id).toBe(1);
}));
});
describe("#updateTemplate", () => {
const accountId = 1010;
const templateId = 1;
const attributes = { name: "Alpha" };
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.patch("/v2/1010/templates/1", { name: "Alpha" })
.reply((0, util_1.readFixtureAt)("updateTemplate/success.http"));
yield dnsimple.templates.updateTemplate(accountId, templateId, attributes);
expect(scope.isDone()).toBeTruthy();
}));
it("produces a template", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.patch("/v2/1010/templates/1")
.reply((0, util_1.readFixtureAt)("updateTemplate/success.http"));
const response = yield dnsimple.templates.updateTemplate(accountId, templateId, attributes);
expect(response.data.id).toBe(1);
}));
describe("when the template does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.patch("/v2/1010/templates/0")
.reply((0, util_1.readFixtureAt)("notfound-template.http"));
yield expect(dnsimple.templates.updateTemplate(accountId, 0, attributes)).rejects.toThrow(main_1.NotFoundError);
}));
});
});
describe("#deleteTemplate", () => {
const accountId = 1010;
const templateId = 1;
it("produces nothing", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.delete("/v2/1010/templates/1")
.reply((0, util_1.readFixtureAt)("deleteTemplate/success.http"));
const response = yield dnsimple.templates.deleteTemplate(accountId, templateId);
expect(response).toEqual({});
}));
});
describe("#applyTemplate", () => {
const accountId = 1010;
const domainId = "example.com";
const templateId = 1;
it("produces nothing", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.post("/v2/1010/domains/example.com/templates/1")
.reply((0, util_1.readFixtureAt)("applyTemplate/success.http"));
const response = yield dnsimple.templates.applyTemplate(accountId, domainId, templateId);
expect(response).toEqual({});
}));
});
});
describe("template records", () => {
describe("#listTemplateRecords", () => {
const accountId = 1010;
const templateId = "1";
it("supports pagination", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?page=1")
.reply((0, util_1.readFixtureAt)("listTemplateRecords/success.http"));
yield dnsimple.templates.listTemplateRecords(accountId, templateId, {
page: 1,
});
expect(scope.isDone()).toBeTruthy();
}));
it("supports extra request options", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?foo=bar")
.reply((0, util_1.readFixtureAt)("listTemplateRecords/success.http"));
yield dnsimple.templates.listTemplateRecords(accountId, templateId, {
foo: "bar",
});
expect(scope.isDone()).toBeTruthy();
}));
it("supports sorting", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?sort=name%3Aasc")
.reply((0, util_1.readFixtureAt)("listTemplateRecords/success.http"));
yield dnsimple.templates.listTemplateRecords(accountId, templateId, {
sort: "name:asc",
});
expect(scope.isDone()).toBeTruthy();
}));
it("produces a template list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records")
.reply((0, util_1.readFixtureAt)("listTemplateRecords/success.http"));
const response = yield dnsimple.templates.listTemplateRecords(accountId, templateId);
expect(response.data.length).toBe(2);
}));
it("exposes the pagination info", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records")
.reply((0, util_1.readFixtureAt)("listTemplateRecords/success.http"));
const response = yield dnsimple.templates.listTemplateRecords(accountId, templateId);
const pagination = response.pagination;
expect(pagination).not.toBe(null);
expect(pagination.current_page).toBe(1);
}));
});
describe("#listTemplateRecords.collectAll", () => {
const accountId = 1010;
const templateId = 1;
it("produces a complete list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?page=1")
.reply((0, util_1.readFixtureAt)("pages-1of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?page=2")
.reply((0, util_1.readFixtureAt)("pages-2of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/1/records?page=3")
.reply((0, util_1.readFixtureAt)("pages-3of3.http"));
const items = yield dnsimple.templates.listTemplateRecords.collectAll(accountId, templateId);
expect(items.length).toBe(5);
expect(items[0].id).toBe(1);
expect(items[4].id).toBe(5);
}));
});
describe("#getTemplateRecord", () => {
const accountId = 1010;
const templateId = "name";
const recordId = 1;
it("produces a template", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/name/records/1")
.reply((0, util_1.readFixtureAt)("getTemplateRecord/success.http"));
const response = yield dnsimple.templates.getTemplateRecord(accountId, templateId, recordId);
const record = response.data;
expect(record.id).toBe(301);
expect(record.template_id).toBe(268);
expect(record.name).toBe("");
expect(record.content).toBe("mx.example.com");
expect(record.ttl).toBe(600);
expect(record.priority).toBe(10);
expect(record.type).toBe("MX");
expect(record.created_at).toBe("2016-05-03T08:03:26Z");
expect(record.updated_at).toBe("2016-05-03T08:03:26Z");
}));
describe("when the template does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/0/records/1")
.reply((0, util_1.readFixtureAt)("notfound-template.http"));
yield expect(dnsimple.templates.getTemplateRecord(accountId, 0, recordId)).rejects.toThrow(main_1.NotFoundError);
}));
});
describe("when the template record does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/templates/name/records/0")
.reply((0, util_1.readFixtureAt)("notfound-record.http"));
yield expect(dnsimple.templates.getTemplateRecord(accountId, templateId, 0)).rejects.toThrow(main_1.NotFoundError);
}));
});
});
describe("#createTemplateRecord", () => {
const accountId = 1010;
const templateId = 1;
const attributes = { content: "mx.example.com" };
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.post("/v2/1010/templates/1/records", { content: "mx.example.com" })
.reply((0, util_1.readFixtureAt)("createTemplateRecord/created.http"));
yield dnsimple.templates.createTemplateRecord(accountId, templateId, attributes);
expect(scope.isDone()).toBeTruthy();
}));
it("produces a record", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.post("/v2/1010/templates/1/records")
.reply((0, util_1.readFixtureAt)("createTemplateRecord/created.http"));
const response = yield dnsimple.templates.createTemplateRecord(accountId, templateId, attributes);
expect(response.data.id).toBe(300);
}));
});
describe("#deleteTemplateRecord", () => {
const accountId = 1010;
const templateId = 1;
const recordId = 2;
it("produces nothing", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.delete("/v2/1010/templates/1/records/2")
.reply((0, util_1.readFixtureAt)("deleteTemplateRecord/success.http"));
const response = yield dnsimple.templates.deleteTemplateRecord(accountId, templateId, recordId);
expect(response).toEqual({});
}));
});
});
;