-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adjust redirectPath with full path (#44)
* feat: adjust redirectPath with full path * to: add test case * Create sdk.test.ts --------- Co-authored-by: hsluoyz <[email protected]>
- Loading branch information
Showing
3 changed files
with
75 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
}); | ||
}); |