Skip to content

Commit

Permalink
feat: add tests for getToken and setToken
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladimir Berezovsky committed Jul 15, 2020
1 parent b3fe128 commit a98f43a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/endpoints/Oauth.js → src/endpoints/OAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Promise<TokenResponseData>>("getToken", {
api: async () => {
Expand All @@ -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;
}
});
}
Expand Down
9 changes: 9 additions & 0 deletions src/util/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
36 changes: 35 additions & 1 deletion tests/endpoints/OAuth.test.js
Original file line number Diff line number Diff line change
@@ -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] = [
Expand Down

0 comments on commit a98f43a

Please sign in to comment.