Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sign out and i18n #700

Merged
merged 8 commits into from
Apr 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 18 additions & 71 deletions packages/aws-amplify/__tests__/Auth/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1319,21 +1319,14 @@ describe('auth unit test', () => {
});

describe('signOut', () => {
test('happy case for all', async () => {
test('happy case', async () => {
const auth = new Auth(authOptions);
auth['credentials'] = new CognitoIdentityCredentials({
IdentityPoolId: 'identityPoolId'
});

const user = new CognitoUser({
Username: 'username',
Pool: userPool
});


const spyonAuth = jest.spyOn(Auth.prototype, "currentUserCredentials")
.mockImplementationOnce(() => {
return new Promise((resolve, reject) => { resolve(); });
});
const spyon = jest.spyOn(CognitoIdentityCredentials.prototype, "clearCachedId");
const spyon2 = jest.spyOn(Cache, 'removeItem');
const spyon3 = jest.spyOn(CognitoUserPool.prototype, "getCurrentUser")
Expand All @@ -1347,87 +1340,41 @@ describe('auth unit test', () => {
expect(spyon).toBeCalled();
expect(spyon2).toBeCalledWith('federatedInfo');

spyonAuth.mockClear();
spyon.mockClear();
spyon2.mockClear();
spyon3.mockClear();
});

test('happy case for source userpool', async () => {
const auth = new Auth(authOptions);
const user = new CognitoUser({
Username: 'username',
Pool: userPool
});
auth['credentials_source'] = 'aws';
auth['credentials'] = new CognitoIdentityCredentials({
IdentityPoolId: 'identityPoolId'
});

const spyonAuth = jest.spyOn(Auth.prototype, "currentUserCredentials")
.mockImplementationOnce(() => {
return new Promise((resolve, reject) => { resolve(); });
});
const spyon = jest.spyOn(CognitoUserPool.prototype, "getCurrentUser")
.mockImplementationOnce(() => {
return user;
});
const spyon2 = jest.spyOn(CognitoUser.prototype, "signOut");
// @ts-ignore

await auth.signOut();

expect.assertions(1);
expect(spyon2).toBeCalled();

spyonAuth.mockClear();
spyon.mockClear();
spyon2.mockClear();
});

test('no UserPool', async () => {
test('happy case for no userpool', async () => {
const auth = new Auth(authOptionsWithNoUserPoolId);
auth['credentials_source'] = 'aws';
auth['credentials'] = new CognitoIdentityCredentials({
IdentityPoolId: 'identityPoolId'
});

const spyonAuth = jest.spyOn(Auth.prototype, "currentUserCredentials")
.mockImplementationOnce(() => {
return new Promise((resolve, reject) => { resolve(); });
});
expect.assertions(1);
try {
await auth.signOut();
} catch (e) {
expect(e).toBe('No userPool');
}

spyonAuth.mockClear();
expect(await auth.signOut()).toBeUndefined();
});

test('no User in userpool', async () => {
const auth = new Auth(authOptions);
auth['credentials_source'] = 'aws';
auth['credentials'] = new CognitoIdentityCredentials({
IdentityPoolId: 'identityPoolId'
});

const spyonAuth = jest.spyOn(Auth.prototype, "currentUserCredentials")
.mockImplementationOnce(() => {
return new Promise((resolve, reject) => { resolve(); });
});
const spyon = jest.spyOn(CognitoUserPool.prototype, 'getCurrentUser')
.mockImplementationOnce(() => {
return null;
});
expect(await auth.signOut()).toBeUndefined();

spyon.mockClear();
});

expect.assertions(1);
expect(await auth.signOut()).toBeUndefined();
test('get guest credentials failed', async() => {
const auth = new Auth(authOptionsWithNoUserPoolId);

spyonAuth.mockClear();
spyon.mockClear();
const cognitoCredentialSpyon = jest.spyOn(CognitoIdentityCredentials.prototype, 'getPromise').mockImplementation(() => {
return new Promise((res, rej) => {
rej('err');
});
});

expect(await auth.signOut()).toBeUndefined();

cognitoCredentialSpyon.mockClear();
});
});

Expand Down
96 changes: 96 additions & 0 deletions packages/aws-amplify/__tests__/Common/I18n-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { I18n } from '../../src/Common/I18n/I18n';

describe('I18n test', () => {
describe('setLanguage', () => {
test('happy case', () => {
const i18n = new I18n(null);

i18n.setLanguage('en');
});
});

describe('get test', () => {
test('no language', () => {
const i18n = new I18n(null);

i18n._lang = null;
expect(i18n.get('key')).toBe('key');
expect(i18n.get('key', 'defVal')).toBe('defVal');
});

test('has language', () => {
const i18n = new I18n(null);

i18n._lang = 'en';
const spyon = jest.spyOn(I18n.prototype, 'getByLanguage').mockReturnValueOnce('val');

expect(i18n.get('key')).toBe('val');

spyon.mockClear();
});

test('has language 2', () => {
const i18n = new I18n(null);

i18n._lang = 'en-val';
const spyon = jest.spyOn(I18n.prototype, 'getByLanguage').mockImplementationOnce((key, lang) => {
if (lang ==='en-val') return null;
else if (lang === 'en') return 'val';
});

expect(i18n.get('key')).toBe('key');

spyon.mockClear();
});

test('has language 2', () => {
const i18n = new I18n(null);

i18n._lang = 'other';
const spyon = jest.spyOn(I18n.prototype, 'getByLanguage').mockReturnValueOnce(null);

expect(i18n.get('key')).toBe('key');

spyon.mockClear();
});
});

describe('getByLangurage test', () => {
test('happy case', () => {
const i18n = new I18n(null);

expect(i18n.getByLanguage('key', undefined)).toBeNull();
});

test('has dict', () => {
const i18n = new I18n(null);
i18n._dict = {en: {
key: 'val'
}};

expect(i18n.getByLanguage('key', 'en', 'defval')).toBe('val');
});

test('no dict', () => {
const i18n = new I18n(null);
i18n._dict = {};

expect(i18n.getByLanguage('key', 'en', 'val')).toBe('val');
});
});

describe('putVocabulariesForLanguage test', () => {
test('happy case', () => {
const i18n = new I18n(null);

i18n.putVocabulariesForLanguage('cn', {
'hello': '你好',
'exciting': '+1s'
});

i18n._dict = {
cn: {}
}
});
});
});
34 changes: 17 additions & 17 deletions packages/aws-amplify/src/Auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -923,30 +923,30 @@ export default class AuthClass {
logger.debug('failed to clear cached items');
}

const source = this.credentials_source;
if (source === 'aws' || source === 'userPool') {
if (!this.userPool) { return Promise.reject('No userPool'); }
if (this.userPool) {
const user = this.userPool.getCurrentUser();
if (!user) { return Promise.resolve(); }
logger.debug('user sign out', user);
user.signOut();
if (this._cognitoAuthClient) {
this._cognitoAuthClient.signOut();
if (user) {
logger.debug('user sign out', user);
user.signOut();
if (this._cognitoAuthClient) {
this._cognitoAuthClient.signOut();
}
}
} else {
logger.debug('no Congito User pool');
}

const that = this;
return new Promise((resolve, reject) => {
that._setCredentialsForGuest().then((cred) => {
dispatchAuthEvent('signOut', that.user);
that.user = null;
resolve();
}).catch((e) => {
logger.debug('cannot load guest credentials for unauthenticated user');
return new Promise(async (resolve, reject) => {
try {
await that._setCredentialsForGuest();
} catch (e) {
logger.debug('cannot load guest credentials for unauthenticated user', e);
} finally {
dispatchAuthEvent('signOut', that.user);
that.user = null;
resolve();
});
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/

import { I18nOptions } from './types';
import { ConsoleLogger as Logger } from '../Common';
import { ConsoleLogger as Logger } from '../Logger';

const logger = new Logger('I18n');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import { I18n as I18nClass } from './I18n';

import { ConsoleLogger as Logger } from '../Common/Logger';
import { ConsoleLogger as Logger } from '../Logger';

const logger = new Logger('I18n');

Expand Down
1 change: 1 addition & 0 deletions packages/aws-amplify/src/Common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export { default as ClientDevice } from './ClientDevice';
export * from './Logger';
export * from './Errors';
export { default as Hub } from './Hub';
export { default as I18n } from './I18n';
export { default as JS } from './JS';
export { default as Signer } from './Signer';
export { default as Parser } from './Parser';
Expand Down
4 changes: 2 additions & 2 deletions packages/aws-amplify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ import Auth, { AuthClass } from './Auth';
import Storage, { StorageClass } from './Storage';
import API, { APIClass, graphqlOperation } from './API';
import PubSub from './PubSub';
import I18n from './I18n';
import Cache from './Cache';
import {
ConsoleLogger as Logger,
Hub,
JS,
ClientDevice,
Signer
Signer,
I18n
} from './Common';

const logger = new Logger('Amplify');
Expand Down