Skip to content

Commit

Permalink
Format the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Feb 20, 2024
1 parent e25d27d commit a826f46
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 71 deletions.
30 changes: 15 additions & 15 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export interface Account {

class Sdk {
private config: SdkConfig
private pkce : PKCE
private pkce: PKCE

constructor(config: SdkConfig) {
this.config = config
if (config.redirectPath === undefined || config.redirectPath === null) {
this.config.redirectPath = "/callback";
}

if(config.scope === undefined || config.scope === null) {
if (config.scope === undefined || config.scope === null) {
this.config.scope = "profile";
}

Expand Down Expand Up @@ -120,8 +120,8 @@ class Sdk {
}

public async signin(serverUrl: string, signinPath?: string, code?: string, state?: string): Promise<Response> {
if(!code || !state) {
const params = new URLSearchParams(window.location.search);
if (!code || !state) {
const params = new URLSearchParams(window.location.search);
code = params.get("code")!;
state = params.get("state")!;
}
Expand All @@ -145,7 +145,7 @@ class Sdk {
}).then(res => res.json());
}

public isSilentSigninRequested(): boolean{
public isSilentSigninRequested(): boolean {
const params = new URLSearchParams(window.location.search);
return params.get("silentSignin") === "1";
}
Expand Down Expand Up @@ -192,10 +192,10 @@ class Sdk {

if (event.data.type === "loginSuccess") {
this.signin(serverUrl, signinPath, event.data.data.code, event.data.data.state)
.then((res: any) => {
sessionStorage.setItem("token", res.token);
window.location.reload();
});
.then((res: any) => {
sessionStorage.setItem("token", res.token);
window.location.reload();
});
popupWindow!.close();
}
};
Expand All @@ -213,12 +213,12 @@ class Sdk {

public async getUserInfo(accessToken: string): Promise<Response> {
return fetch(`${this.config.serverUrl.trim()}/api/userinfo`, {
method: "GET",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json"
},
}).then(res => res.json()
method: "GET",
headers: {
"Authorization": `Bearer ${accessToken}`,
"Content-Type": "application/json"
},
}).then(res => res.json()
);
}
}
Expand Down
112 changes: 56 additions & 56 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
import Sdk from '../src';

const sdkConfig = {
serverUrl: 'https://door.casbin.com',
clientId: '014ae4bd048734ca2dea',
appName: 'app-casnode',
organizationName: 'casbin',
redirectPath: '/callback',
serverUrl: 'https://door.casbin.com',
clientId: '014ae4bd048734ca2dea',
appName: 'app-casnode',
organizationName: 'casbin',
redirectPath: '/callback',
};

describe('sdk constructor', () => {
it('with full configs', () => {
const sdk = new Sdk(sdkConfig);
it('with full configs', () => {
const sdk = new Sdk(sdkConfig);

const instanceConfig = sdk['config'];
expect(instanceConfig.serverUrl).toEqual(sdkConfig.serverUrl);
expect(instanceConfig.clientId).toEqual(sdkConfig.clientId);
expect(instanceConfig.appName).toEqual(sdkConfig.appName);
expect(instanceConfig.organizationName).toEqual(sdkConfig.organizationName);
expect(instanceConfig.redirectPath).toEqual(sdkConfig.redirectPath);
});
const instanceConfig = sdk['config'];
expect(instanceConfig.serverUrl).toEqual(sdkConfig.serverUrl);
expect(instanceConfig.clientId).toEqual(sdkConfig.clientId);
expect(instanceConfig.appName).toEqual(sdkConfig.appName);
expect(instanceConfig.organizationName).toEqual(sdkConfig.organizationName);
expect(instanceConfig.redirectPath).toEqual(sdkConfig.redirectPath);
});

it('config withou redirectPath', () => {
let config = {
...sdkConfig,
redirectPath: undefined,
};
const sdk = new Sdk(sdkConfig);
it('config withou redirectPath', () => {
let config = {
...sdkConfig,
redirectPath: undefined,
};
const sdk = new Sdk(sdkConfig);

const instanceConfig = sdk['config'];
expect(instanceConfig.redirectPath).toEqual('/callback');
});
const instanceConfig = sdk['config'];
expect(instanceConfig.redirectPath).toEqual('/callback');
});
});

describe('getSigninUrl', () => {
beforeEach(() => {
sessionStorage.clear();
});
beforeEach(() => {
sessionStorage.clear();
});

it('redirectPath with relative path', () => {
const sdk = new Sdk(sdkConfig);
it('redirectPath with relative path', () => {
const sdk = new Sdk(sdkConfig);

expect(sdk.getSigninUrl()).toContain(
`redirect_uri=${encodeURIComponent(
window.location.origin + sdkConfig.redirectPath
)}`
);
});
expect(sdk.getSigninUrl()).toContain(
`redirect_uri=${encodeURIComponent(
window.location.origin + sdkConfig.redirectPath
)}`
);
});

it('redirectPath with fully path', () => {
const config = {
...sdkConfig,
redirectPath: 'http://localhost:6001/other-callback',
};
const sdk = new Sdk(config);
it('redirectPath with fully path', () => {
const config = {
...sdkConfig,
redirectPath: 'http://localhost:6001/other-callback',
};
const sdk = new Sdk(config);

expect(sdk.getSigninUrl()).toContain(
`redirect_uri=${encodeURIComponent(config.redirectPath)}`
);
});
expect(sdk.getSigninUrl()).toContain(
`redirect_uri=${encodeURIComponent(config.redirectPath)}`
);
});

it('with fixed state', () => {
const state = 'test-state';
const sdk = new Sdk(sdkConfig);
sessionStorage.setItem('casdoor-state', state);
expect(sdk.getSigninUrl()).toContain(`state=${state}`);
});
it('with fixed state', () => {
const state = 'test-state';
const sdk = new Sdk(sdkConfig);
sessionStorage.setItem('casdoor-state', state);
expect(sdk.getSigninUrl()).toContain(`state=${state}`);
});

it('with random state', () => {
const sdk = new Sdk(sdkConfig);
const url = sdk.getSigninUrl();
const state = sessionStorage.getItem('casdoor-state');
expect(url).toContain(`state=${state}`);
});
it('with random state', () => {
const sdk = new Sdk(sdkConfig);
const url = sdk.getSigninUrl();
const state = sessionStorage.getItem('casdoor-state');
expect(url).toContain(`state=${state}`);
});
});

0 comments on commit a826f46

Please sign in to comment.