UNPKG

@jesseditson/dnsimple

Version:

A Node.JS client for the DNSimple API.

110 lines (109 loc) 4.89 kB
"use strict"; 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 util_1 = require("./util"); const dnsimple = (0, util_1.createTestClient)(); describe("oauth", () => { const clientId = "super-client"; const clientSecret = "super-secret"; const code = "super-code"; const redirectUri = "https://great-app.com/oauth"; const state = "mysecretstate"; describe("#exchangeAuthorizationForToken", () => { it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () { const scope = nock("https://api.dnsimple.com") .post("/v2/oauth/access_token", { client_id: clientId, client_secret: clientSecret, code, grant_type: "authorization_code", redirect_uri: redirectUri, state, }) .reply((0, util_1.readFixtureAt)("oauthAccessToken/success.http")); yield dnsimple.oauth.exchangeAuthorizationForToken({ code, clientId, clientSecret, redirectUri, state, }); expect(scope.isDone()).toBeTruthy(); })); it("returns the oauth token", () => __awaiter(void 0, void 0, void 0, function* () { nock("https://api.dnsimple.com") .post("/v2/oauth/access_token", { client_id: clientId, client_secret: clientSecret, code, grant_type: "authorization_code", redirect_uri: redirectUri, state, }) .reply((0, util_1.readFixtureAt)("oauthAccessToken/success.http")); const response = yield dnsimple.oauth.exchangeAuthorizationForToken({ code, clientId, clientSecret, redirectUri, state, }); expect(response.access_token).toBe("zKQ7OLqF5N1gylcJweA9WodA000BUNJD"); expect(response.token_type).toBe("Bearer"); expect(response.account_id).toBe(1); })); describe("when state and redirect_uri are provided", () => { const state = "super-state"; const redirectUri = "super-redirect-uri"; it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () { const scope = nock("https://api.dnsimple.com") .post("/v2/oauth/access_token", { client_id: clientId, client_secret: clientSecret, code, grant_type: "authorization_code", state, redirect_uri: redirectUri, }) .reply((0, util_1.readFixtureAt)("oauthAccessToken/success.http")); yield dnsimple.oauth.exchangeAuthorizationForToken({ code, clientId, clientSecret, state, redirectUri, }); expect(scope.isDone()).toBeTruthy(); })); }); }); describe("#authorizeUrl", () => { it("builds the correct url", () => { const authorizeUrl = new URL(dnsimple.oauth.authorizeUrl({ clientId: "great-app", redirectUri, state, })); const expectedUrl = new URL("https://dnsimple.com/oauth/authorize?client_id=great-app&redirect_uri=https://great-app.com/oauth&response_type=code&state=mysecretstate"); const searchParamsToObj = (params) => { const obj = {}; params.forEach((val, key) => { obj[key] = val; }); return obj; }; expect(authorizeUrl.protocol).toBe(expectedUrl.protocol); expect(authorizeUrl.host).toBe(expectedUrl.host); expect(searchParamsToObj(authorizeUrl.searchParams)).toEqual(searchParamsToObj(expectedUrl.searchParams)); }); }); });