Skip to content

Commit

Permalink
feat: adjust redirectPath with full path (#44)
Browse files Browse the repository at this point in the history
* feat: adjust redirectPath with full path

* to: add test case

* Create sdk.test.ts

---------

Co-authored-by: hsluoyz <[email protected]>
  • Loading branch information
jump2cn and hsluoyz authored Apr 23, 2023
1 parent 0ff27b1 commit 432c626
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"maxConcurrency": 1,
"maxWorkers": 1,
"testTimeout": 30000,
"testEnvironment": "node",
"testEnvironment": "jsdom",
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
Expand Down
2 changes: 1 addition & 1 deletion src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Sdk {
}

public getSigninUrl(): string {
const redirectUri = `${window.location.origin}${this.config.redirectPath}`;
const redirectUri = this.config.redirectPath && this.config.redirectPath.includes('://') ? this.config.redirectPath : `${window.location.origin}${this.config.redirectPath}`;
const scope = "read";
const state = this.getOrSaveState();
return `${this.config.serverUrl.trim()}/login/oauth/authorize?client_id=${this.config.clientId}&response_type=code&redirect_uri=${encodeURIComponent(redirectUri)}&scope=${scope}&state=${state}`;
Expand Down
83 changes: 73 additions & 10 deletions test/sdk.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,75 @@
import Sdk from '../src';

test('new sdk', () => {
const sdkConfig = {
serverUrl: "https://door.casbin.com",
clientId: "014ae4bd048734ca2dea",
appName: "app-casnode",
organizationName: "casbin",
redirectPath: "/callback",
}
const sdk = new Sdk(sdkConfig)
})
const sdkConfig = {
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);

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);

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

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

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

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);

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 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 432c626

Please sign in to comment.