From a98f43ae81e56dc9ce07f029f2af9523c3f39424 Mon Sep 17 00:00:00 2001 From: Vladimir Berezovsky Date: Wed, 15 Jul 2020 16:43:23 +0300 Subject: [PATCH] feat: add tests for getToken and setToken --- src/endpoints/{Oauth.js => OAuth.js} | 12 +++++----- src/util/testing.js | 9 +++++++ tests/endpoints/OAuth.test.js | 36 +++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 7 deletions(-) rename src/endpoints/{Oauth.js => OAuth.js} (92%) diff --git a/src/endpoints/Oauth.js b/src/endpoints/OAuth.js similarity index 92% rename from src/endpoints/Oauth.js rename to src/endpoints/OAuth.js index b99320ea..9ffcc9b1 100644 --- a/src/endpoints/Oauth.js +++ b/src/endpoints/OAuth.js @@ -19,11 +19,15 @@ export default class OAuth extends Endpoint { const body = new URLSearchParams(); + // $FlowFixMe body.append("client_id", clientId); + // $FlowFixMe body.append("client_secret", clientSecret); + // $FlowFixMe + body.append("redirect_uri", redirectUri); + body.append("code", authorizationCode); body.append("grant_type", "authorization_code"); - body.append("redirect_uri", redirectUri); return this.configureRequest>("getToken", { api: async () => { @@ -41,11 +45,7 @@ export default class OAuth extends Endpoint { } ); - if (!!response.error) { - throw new BaseError(`Error: ${response.error}`); - } else { - return response.access_token; - } + return response.access_token; } }); } diff --git a/src/util/testing.js b/src/util/testing.js index e8ee473f..d3ed58f2 100644 --- a/src/util/testing.js +++ b/src/util/testing.js @@ -71,6 +71,15 @@ export function mockAPI( (nock("http://apiurl"): any)[method](url).reply(code, response); } +export function mockAuth( + url: string, + response: Object, + code: number = 200, + method: string = "get" +) { + (nock("https://auth.goabstract.com"): any)[method](url).reply(code, response); +} + export function mockPreviewAPI( url: string, response: Object, diff --git a/tests/endpoints/OAuth.test.js b/tests/endpoints/OAuth.test.js index 4de417b9..bec72500 100644 --- a/tests/endpoints/OAuth.test.js +++ b/tests/endpoints/OAuth.test.js @@ -1,6 +1,40 @@ -import { API_CLIENT } from "../../src/util/testing"; +import { mockAuth, API_CLIENT } from "../../src/util/testing"; describe("oauth", () => { + describe("getToken", () => { + test("api - with data", async () => { + mockAuth( + "/auth/tokens", + { + access_token: "access_token", + client_id: "client_id", + created_at: "created_at", + id: "id", + scope: "scope", + user_id: "user_id" + }, + 200, + "post" + ); + + const response = await API_CLIENT.oauth.getToken({ + client_id: "client_id", + client_secret: "client_secret", + redirect_uri: "redirect_uri", + authorization_code: "authorization_code" + }); + + expect(response).toEqual("access_token"); + }); + }); + + describe("setToken", () => { + test("set Client with new accessToken", () => { + const client = API_CLIENT.oauth.setToken("accessToken"); + expect(client).toEqual(API_CLIENT); + }); + }); + describe("generateAuthUrl", () => { test("options are passed", () => { const [clientId, redirectUri, state] = [