Navigation Menu

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

MLIBZ-1626: Add unit tests for user logout #110

Merged
merged 3 commits into from Feb 23, 2017
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 93 additions & 1 deletion test/unit/entity/user.test.js
Expand Up @@ -2,11 +2,13 @@ import { User } from 'src/entity';
import { randomString } from 'src/utils';
import { ActiveUserError, InvalidCredentialsError, KinveyError } from 'src/errors';
import { CacheRequest } from 'src/request';
import { TestUser } from '../mocks';
import { CacheStore, SyncStore } from 'src/datastore';
import Query from 'src/query';
import expect from 'expect';
import nock from 'nock';
import assign from 'lodash/assign';
import localStorage from 'local-storage';
import { TestUser } from '../mocks';
const rpcNamespace = process.env.KINVEY_RPC_NAMESPACE || 'rpc';

describe('User', function() {
Expand Down Expand Up @@ -211,6 +213,96 @@ describe('User', function() {
});
});

describe('logout()', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@thomasconner the logout() tests should assert more that just the active user API. Please add some asserts for the cache being cleaned, sync queue emptied and checks for any other local storage items we remove (e.g. device tokens, SQL tables etc)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tejasranade I have added some checks for these items.

beforeEach(function() {
return TestUser.login(randomString(), randomString());
});

beforeEach(function() {
const entity1 = {
_id: randomString(),
title: 'Opela',
author: 'Maria Crawford',
isbn: '887420007-2',
summary: 'Quisque id justo sit amet sapien dignissim vestibulum. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Nulla dapibus dolor vel est. Donec odio justo, sollicitudin ut, suscipit a, feugiat et, eros.\n\nVestibulum ac est lacinia nisi venenatis tristique. Fusce congue, diam id ornare imperdiet, sapien urna pretium nisl, ut volutpat sapien arcu sed augue. Aliquam erat volutpat.',
_acl: {
creator: randomString()
},
_kmd: {
lmt: '2016-08-17T15:32:01.741Z',
ect: '2016-08-17T15:32:01.741Z'
}
};
const entity2 = {
_id: randomString(),
title: 'Treeflex',
author: 'Harry Larson',
isbn: '809087960-8',
summary: 'Aenean fermentum. Donec ut mauris eget massa tempor convallis. Nulla neque libero, convallis eget, eleifend luctus, ultricies eu, nibh.',
_acl: {
creator: randomString()
},
_kmd: {
lmt: '2016-08-17T15:32:01.744Z',
ect: '2016-08-17T15:32:01.744Z'
}
};

// Kinvey API response
nock(this.client.apiHostname, { encodedQueryParams: true })
.get(`/appdata/${this.client.appKey}/foo`)
.reply(200, [entity1, entity2], {
'content-type': 'application/json'
});

// Pull data into cache
const store = new CacheStore('foo');
return store.pull()
.then((entities) => {
expect(entities).toEqual([entity1, entity2]);
});
});

afterEach(function() {
const store = new SyncStore('foo');
return store.find().toPromise()
.then((entities) => {
expect(entities).toEqual([]);
});
});

afterEach(function() {
const store = new SyncStore('kinvey_sync');
return store.find().toPromise()
.then((entities) => {
expect(entities).toEqual([]);
});
});

afterEach(function() {
const user = localStorage.get(`${this.client.appKey}kinvey_user`);
expect(user).toEqual(null);
});

it('should logout the active user', function() {
return TestUser.logout()
.then(() => {
expect(TestUser.getActiveUser()).toEqual(null);
});
});

it('should logout when there is not an active user', function() {
return TestUser.logout()
.then(() => {
expect(TestUser.getActiveUser()).toEqual(null);
})
.then(() => TestUser.logout())
.then(() => {
expect(TestUser.getActiveUser()).toEqual(null);
});
});
});

describe('verifyEmail()', function() {
it('should throw an error if a username is not provided', async function() {
try {
Expand Down
13 changes: 9 additions & 4 deletions test/unit/mocks/src/user.js
Expand Up @@ -12,7 +12,7 @@ export default class TestUser extends User {
return new TestUser(activeUser.data);
}

return Promise.resolve(null);
return null;
}

login(username, password, options) {
Expand Down Expand Up @@ -117,8 +117,13 @@ export default class TestUser extends User {
return super.logout(options);
}

static logout(options) {
const user = new TestUser({}, options);
return user.logout(options);
static logout(options = {}) {
const user = TestUser.getActiveUser(options.client);

if (user) {
return user.logout(options);
}

return Promise.resolve(null);
}
}