Skip to content

Commit

Permalink
fix: no recalculate expired time at setData (#225)
Browse files Browse the repository at this point in the history
* fix: no recalcute expired time

* chore: comment unstable tests

* chore: add lock file

* chore: use jwt for default tests

* chore: add tests

* chore: add tests

* misc: use typeof checking
  • Loading branch information
embbnux committed Oct 12, 2023
1 parent 9acd8b6 commit 0fe73e5
Show file tree
Hide file tree
Showing 10 changed files with 22,893 additions and 56 deletions.
647 changes: 647 additions & 0 deletions api-test/package-lock.json

Large diffs are not rendered by default.

871 changes: 871 additions & 0 deletions demo/package-lock.json

Large diffs are not rendered by default.

18,785 changes: 18,785 additions & 0 deletions react-demo/package-lock.json

Large diffs are not rendered by default.

1,283 changes: 1,283 additions & 0 deletions react/package-lock.json

Large diffs are not rendered by default.

1,196 changes: 1,196 additions & 0 deletions redux/package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions sdk/README.md
Expand Up @@ -323,8 +323,8 @@ rcsdk.platform().auth().accessTokenValid(); // returns Promise<boolean>
You can retrieve save and set back the auth information:

```js
var authData = rcsdk.platform().auth().data();
rcsdk.platform().auth().setData(authData);
var authData = await rcsdk.platform().auth().data();
await rcsdk.platform().auth().setData(authData);
```

It can be useful on the server if SDK instances are created and disposed for every HTTP request.
Expand Down
67 changes: 34 additions & 33 deletions sdk/src/SDK-spec.ts
@@ -1,39 +1,40 @@
import {expect, SDK, spy} from './test/test';

describe('RingCentral.SDK', () => {
it('connects to sandbox', async function theTest() {
this.timeout(20000);
const server = SDK.server.sandbox;
const sdk = new SDK({
server,
clientId: '',
clientSecret: '',
});

// sandbox's /restapi/v1.0/status triggers service overloaded very easily, but /restapi/v1.0 works fine
const res = await sdk.platform().get('/restapi/v1.0', null, {skipAuthCheck: true});

await sdk.cache().clean();

expect(res.status).to.equal(200);
});

it('connects to production', async function theTest() {
this.timeout(20000);
const server = SDK.server.production;
const sdk = new SDK({
server,
clientId: '',
clientSecret: '',
});

// production's /restapi/v1.0/status triggers service overloaded very easily, but /restapi/v1.0 works fine
const res = await sdk.platform().get('/restapi/v1.0', null, {skipAuthCheck: true});

await sdk.cache().clean();

expect(res.status).to.equal(200);
});
// TODO: gzip content is not supported by Karma. Need to migrate karma (deprecated) to other framework
// it('connects to sandbox', async function theTest() {
// this.timeout(20000);
// const server = SDK.server.sandbox;
// const sdk = new SDK({
// server,
// clientId: '',
// clientSecret: '',
// });

// // sandbox's /restapi/v1.0/status triggers service overloaded very easily, but /restapi/v1.0 works fine
// const res = await sdk.platform().get('/restapi/v1.0', null, {skipAuthCheck: true});

// await sdk.cache().clean();

// expect(res.status).to.equal(200);
// });

// it('connects to production', async function theTest() {
// this.timeout(20000);
// const server = SDK.server.production;
// const sdk = new SDK({
// server,
// clientId: '',
// clientSecret: '',
// });

// // production's /restapi/v1.0/status triggers service overloaded very easily, but /restapi/v1.0 works fine
// const res = await sdk.platform().get('/restapi/v1.0', null, {skipAuthCheck: true});

// await sdk.cache().clean();

// expect(res.status).to.equal(200);
// });

it('sets rate limit', async function rateLimitTest() {
const sdk = new SDK({handleRateLimit: 60});
Expand Down
17 changes: 9 additions & 8 deletions sdk/src/platform/Auth.ts
Expand Up @@ -41,16 +41,17 @@ export default class Auth {

public async setData(newData: AuthData = {}) {
const data = await this.data();
let refreshTokenExpireTime = data.refresh_token_expire_time;
if (newData.refresh_token_expires_in) {
refreshTokenExpireTime = Date.now() + parseInt(newData.refresh_token_expires_in, 10) * 1000;
}
await this._cache.setItem(this._cacheId, {
const savedData: AuthData = {
...data,
...newData,
expire_time: Date.now() + parseInt(newData.expires_in, 10) * 1000,
refresh_token_expire_time: refreshTokenExpireTime,
});
};
if (typeof newData.expires_in !== 'undefined' && !newData.expire_time) {
savedData.expire_time = Date.now() + parseInt(newData.expires_in, 10) * 1000;
}
if (typeof newData.refresh_token_expires_in !== 'undefined' && !newData.refresh_token_expire_time) {
savedData.refresh_token_expire_time = Date.now() + parseInt(newData.refresh_token_expires_in, 10) * 1000;
}
await this._cache.setItem(this._cacheId, savedData);
}

/**
Expand Down
66 changes: 60 additions & 6 deletions sdk/src/platform/Platform-spec.ts
@@ -1,20 +1,19 @@
import {version} from '../core/Constants';
import {
apiCall,
asyncTest,
authentication,
cleanFetchMock,
createSdk,
expect,
expectThrows,
getExternalDiscoveryMockData,
getInitialDiscoveryMockData,
logout,
spy,
tokenRefresh,
createSdk,
cleanFetchMock,
getInitialDiscoveryMockData,
getExternalDiscoveryMockData,
} from '../test/test';

import {version} from '../core/Constants';

const globalAny: any = global;
const windowAny: any = typeof window !== 'undefined' ? window : global;

Expand Down Expand Up @@ -193,6 +192,12 @@ describe('RingCentral.platform.Platform', () => {
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
expect(await platform.auth().accessTokenValid()).to.equal(true);
expect(await platform.auth().refreshTokenValid()).to.equal(true);
expect((await platform.auth().data()).expire_time > Date.now() + 30 * 60 * 1000).to.equal(true);
expect(
(await platform.auth().data()).refresh_token_expire_time > Date.now() + 6 * 24 * 60 * 60 * 1000,
).to.equal(true);
}),
);

Expand All @@ -210,6 +215,7 @@ describe('RingCentral.platform.Platform', () => {
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
expect(await platform.auth().accessTokenValid()).to.equal(true);
}),
);

Expand All @@ -229,6 +235,26 @@ describe('RingCentral.platform.Platform', () => {
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
expect(await platform.auth().accessTokenValid()).to.equal(true);
}),
);

it(
'login with username/password without extension',
asyncTest(async sdk => {
const platform = sdk.platform();

await platform.auth().cancelAccessToken();

authentication();

await platform.login({
username: 'foo',
password: 'foo',
});

expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN');
expect(await platform.auth().accessTokenValid()).to.equal(true);
}),
);

Expand All @@ -249,6 +275,7 @@ describe('RingCentral.platform.Platform', () => {
});
const authData = await platform.auth().data();
expect(authData.access_token).to.equal('ACCESS_TOKEN');
expect(await platform.auth().accessTokenValid()).to.equal(true);
expect(authData.code_verifier).not.to.be.empty;
},
{
Expand All @@ -266,6 +293,7 @@ describe('RingCentral.platform.Platform', () => {
await platform.login({code: 'test', code_verifier: 'test_code_verifier'});
const authData = await platform.auth().data();
expect(authData.code_verifier).to.equal('test_code_verifier');
expect(await platform.auth().accessTokenValid()).to.equal(true);
},
{
clientSecret: '',
Expand All @@ -288,6 +316,7 @@ describe('RingCentral.platform.Platform', () => {
const authData = await platform.auth().data();
expect(authData.access_token).to.equal('ACCESS_TOKEN');
expect(authData.code_verifier).to.equal('test_code_verifier');
expect(await platform.auth().accessTokenValid()).to.equal(true);
}),
);

Expand Down Expand Up @@ -606,6 +635,8 @@ describe('RingCentral.platform.Platform', () => {
await platform.refresh();
expect(request.headers.get('authorization')).not.to.equal(null);
expect((await platform.auth().data()).access_token).to.equal('ACCESS_TOKEN_FROM_REFRESH');
expect(await platform.auth().accessTokenValid()).to.equal(true);
expect(await platform.auth().refreshTokenValid()).to.equal(true);
}),
);

Expand Down Expand Up @@ -1023,6 +1054,8 @@ describe('RingCentral.platform.Platform', () => {
await platform.auth().setData({
code_verifier: '1212121',
});
expect(await platform.auth().accessTokenValid()).to.equal(true);
expect(await platform.auth().refreshTokenValid()).to.equal(true);
let request;
client.on(client.events.requestSuccess, (_, r) => {
request = r;
Expand Down Expand Up @@ -1584,4 +1617,25 @@ describe('RingCentral.platform.Platform', () => {
expect(request.headers.get('discovery-tag')).to.equal(discoveryTag);
});
});

describe('Auth setData', () => {
it('should set auth data successfully', async () => {
const sdk = createSdk({clientId: 'xxx', clientSecret: 'yyy', server: ''});
const platform = sdk.platform();
const oldToken = {
access_token: 'xxx',
refresh_token: 'yyy',
expire_time: Date.now() + 100000,
refresh_token_expire_time: Date.now() + 900000,
};
await platform.auth().setData(oldToken);
expect(await platform.auth().accessTokenValid()).to.equal(true);
expect(await platform.auth().refreshTokenValid()).to.equal(true);
const authData = await platform.auth().data();
expect(authData.access_token).to.equal(oldToken.access_token);
expect(authData.refresh_token).to.equal(oldToken.refresh_token);
expect(authData.expire_time).to.equal(oldToken.expire_time);
expect(authData.refresh_token_expire_time).to.equal(oldToken.refresh_token_expire_time);
});
});
});
13 changes: 6 additions & 7 deletions sdk/src/test/test.ts
@@ -1,4 +1,5 @@
import {expect, spy, fetchMock} from '@ringcentral/sdk-utils/test';
import {expect, fetchMock, spy} from '@ringcentral/sdk-utils/test';

import {SDK, SDKOptions} from '../SDK';

fetchMock.config.fallbackToNetwork = true;
Expand Down Expand Up @@ -30,9 +31,8 @@ export function authentication(status = 200) {
token_type: 'bearer',
expires_in: 3600,
refresh_token: 'REFRESH_TOKEN',
refresh_token_expires_in: 60480,
refresh_token_expires_in: 604800,
scope: 'SMS RCM Foo Boo',
expireTime: new Date().getTime() + 3600000,
},
status,
);
Expand All @@ -49,7 +49,7 @@ export function tokenRefresh(failure = false) {
token_type: 'bearer',
expires_in: 3600,
refresh_token: 'REFRESH_TOKEN_FROM_REFRESH',
refresh_token_expires_in: 60480,
refresh_token_expires_in: 604800,
scope: 'SMS RCM Foo Boo',
});
} else {
Expand Down Expand Up @@ -99,8 +99,7 @@ export function asyncTest(fn: (sdk: SDK) => any, sdkOption: SDKOptions = {}) {
const platofrm = sdk.platform();

await platofrm.login({
username: 'whatever',
password: 'whatever',
jwt: 'jwt_string',
});

await fn(sdk);
Expand Down Expand Up @@ -176,4 +175,4 @@ export function getExternalDiscoveryMockData() {
};
}

export {spy, SDK, expect};
export {expect, SDK, spy};

0 comments on commit 0fe73e5

Please sign in to comment.