Skip to content

Commit

Permalink
test: adding tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pawel Psztyc <jarrodek@gmail.com>
  • Loading branch information
jarrodek committed Nov 8, 2022
1 parent b8743c9 commit 188e963
Show file tree
Hide file tree
Showing 19 changed files with 1,310 additions and 155 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "API console extension",
"version": "0.1.0",
"version": "0.1.3",
"description": "API Console extension to proxy HTTP requests to the documented API.",
"author": "Pawel Psztyc <pawel.psztyc@mulesoft.com>",
"content_scripts": [
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@advanced-rest-client/api-console-extension",
"version": "0.1.2",
"version": "0.1.3",
"description": "API Console extension to proxy HTTP requests to the documented API.",
"type": "module",
"main": "index.js",
Expand Down
42 changes: 17 additions & 25 deletions src/ApiConsoleAppProxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,51 +234,40 @@ export class ApiConsoleAppProxy extends EventTarget {
detail: {
message: "No response has been recorded.",
code: "no_response",
error: true,
},
})
);
return;
}
const typedError = /** @type IApiConsoleProxyError */ (data);
if (typedError.error) {
const message =
typeof typedError.message === "string"
? typedError.message
: "No response has been recorded.";
const code =
typeof typedError.code === "string" ? typedError.code : "unknown_error";
const message = typeof typedError.message === "string" ? typedError.message : "No response has been recorded.";
const code = typeof typedError.code === "string" ? typedError.code : "unknown_error";
const state = typeof typedError.state === "string" ? typedError.state : undefined;
this.eventTarget.dispatchEvent(
new CustomEvent("oauth2-error", {
bubbles: true,
composed: true,
detail: {
message,
code,
state,
error: true,
},
})
);
return;
}
const typedToken = /** @type ITokenInfo */ (data);
const state =
typeof typedToken.state === "string" ? typedToken.state : undefined;
const accessToken =
typedToken.accessToken && typeof typedToken.accessToken === "string"
? typedToken.accessToken
: undefined;
const tokenType =
typedToken.tokenType && typeof typedToken.tokenType === "string"
? typedToken.tokenType
: undefined;
const expiresIn =
typedToken.expiresIn &&
(typeof typedToken.expiresIn === "number" ||
typeof typedToken.expiresIn === "string")
? Number(typedToken.expiresIn)
: undefined;
const scope = Array.isArray(typedToken.scope)
? typedToken.scope
: undefined;
const state = typeof typedToken.state === "string" ? typedToken.state : undefined;
const accessToken = typedToken.accessToken && typeof typedToken.accessToken === "string" ? typedToken.accessToken : undefined;
const refreshToken = typedToken.refreshToken && typeof typedToken.refreshToken === "string" ? typedToken.refreshToken : undefined;
const tokenType = typedToken.tokenType && typeof typedToken.tokenType === "string" ? typedToken.tokenType : undefined;
const expiresIn = typedToken.expiresIn && (typeof typedToken.expiresIn === "number" || typeof typedToken.expiresIn === "string") ? Number(typedToken.expiresIn): undefined;
const expiresAt = typedToken.expiresAt && (typeof typedToken.expiresAt === "number" || typeof typedToken.expiresAt === "string") ? Number(typedToken.expiresAt): undefined;
const expiresAssumed = typeof typedToken.expiresAssumed === "boolean" ? typedToken.expiresAssumed : undefined;
const scope = Array.isArray(typedToken.scope) ? typedToken.scope : undefined;
this.eventTarget.dispatchEvent(
new CustomEvent("oauth2-token-response", {
bubbles: true,
Expand All @@ -289,6 +278,9 @@ export class ApiConsoleAppProxy extends EventTarget {
tokenType,
expiresIn,
scope,
refreshToken,
expiresAt,
expiresAssumed,
},
})
);
Expand Down
4 changes: 3 additions & 1 deletion src/proxy/OAuth2Proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ export class OAuth2Proxy {
id: id,
tab: tab
};
this._addTabHandlers();
} catch (e) {
throw new AuthorizationError(
e.message,
Expand Down Expand Up @@ -343,7 +344,7 @@ export class OAuth2Proxy {
/** @type string */
let raw;
try {
const raw = this._authDataFromUrl(url);
raw = this._authDataFromUrl(url);
if (!raw) {
throw new Error('');
}
Expand Down Expand Up @@ -437,6 +438,7 @@ export class OAuth2Proxy {
let tokenInfo;
try {
tokenInfo = await this.exchangeCode(code);
tokenInfo.state = state;
} catch (e) {
this._handleTokenCodeError(/** @type Error */(e));
return;
Expand Down
6 changes: 3 additions & 3 deletions src/proxy/OAuthUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function sanityCheck(settings) {
*/
export function randomString() {
const array = new Uint32Array(28);
window.crypto.getRandomValues(array);
globalThis.crypto.getRandomValues(array);
return Array.from(array, (dec) => `0${dec.toString(16)}`.substr(-2)).join("");
}

Expand Down Expand Up @@ -93,7 +93,7 @@ export function camel(name) {
export async function sha256(value) {
const encoder = new TextEncoder();
const data = encoder.encode(value);
return window.crypto.subtle.digest("SHA-256", data);
return globalThis.crypto.subtle.digest("SHA-256", data);
}

/**
Expand Down Expand Up @@ -126,7 +126,7 @@ export async function generateCodeChallenge(verifier) {
export function nonceGenerator(size = 20) {
const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let array = new Uint8Array(size);
window.crypto.getRandomValues(array);
globalThis.crypto.getRandomValues(array);
array = array.map(x => validChars.charCodeAt(x % validChars.length));
return String.fromCharCode.apply(null, array);
}
5 changes: 3 additions & 2 deletions src/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ class ApiConsoleService {
} catch (e) {
this.sendResponse({
'message': e.message || 'The request is invalid.',
'code': 'invalid_request',
'error': true
'code': e.code || 'invalid_request',
'error': true,
'state': e.state,
});
}
}
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export interface IApiConsoleProxyError {
error: true;
code?: string;
message: string;
state?: string;
}

export interface IApiConsoleHttpResponseStats {
Expand Down
Loading

0 comments on commit 188e963

Please sign in to comment.