3 var assert = require('assert');
4 var Promise = require('bluebird');
5 var fixtureLoader = require('gitter-web-test-utils/lib/test-fixtures');
6 var mongoUtils = require('gitter-web-persistence-utils/lib/mongo-utils');
7 var userService = require('../lib/user-service');
8 var persistence = require('gitter-web-persistence');
10 describe('User Service', function() {
11 var fixture2 = fixtureLoader.setup({
12 user1: { username: true },
17 describe('duplicate account creation', function() {
18 fixtureLoader.ensureIntegrationEnvironment('GITTER_INTEGRATION_USER_SCOPE_TOKEN');
20 it('should allow two users with the same githubId to be created at the same moment, but only create a single account', function() {
21 var githubId = fixture2.generateGithubId();
24 userService.findOrCreateUserForGithubId({
26 username: fixture2.generateUsername(),
27 githubToken: fixture2.GITTER_INTEGRATION_USER_SCOPE_TOKEN
29 userService.findOrCreateUserForGithubId({
31 username: fixture2.generateUsername(),
32 githubToken: fixture2.GITTER_INTEGRATION_USER_SCOPE_TOKEN
35 .spread(function(user1, user2) {
36 assert.strictEqual(user1.id, user2.id);
38 .catch(mongoUtils.mongoErrorWithCode(11000), function() {
39 // It looks like mongo is just incapable of guaranteeing this. Up to
40 // 50% of the time this test runs it throws this error.
41 console.log('Duplicate user.'); // eslint-disable-line no-console
46 it('should create new users', function() {
47 return persistence.User.findOneAndRemove({ githubId: -1 })
50 return userService.findOrCreateUserForGithubId({
52 username: '__test__gitter_007'
57 it('should destroy tokens for users', function() {
59 .findOrCreateUserForGithubId({
60 githubId: -Date.now(),
61 username: fixture2.generateUsername(),
64 githubScopes: { 'user:email': 1 }
66 .then(function(user) {
67 assert(user.githubToken);
68 assert(user.githubUserToken);
69 assert(user.githubScopes['user:email']);
71 return [user, userService.destroyTokensForUserId(user.id)];
73 .then(function([user]) {
74 return userService.findById(user.id);
76 .then(function(user) {
77 assert(!user.githubToken);
78 assert(!user.githubUserToken);
79 assert.deepEqual(user.githubScopes, {});
83 it('should allow timezone information to be updated', function() {
85 .updateTzInfo(fixture2.user1.id, { offset: 60, abbr: 'CST', iana: 'Europe/Paris' })
87 return userService.findById(fixture2.user1.id);
89 .then(function(user) {
92 assert.strictEqual(tz.offset, 60);
93 assert.strictEqual(tz.abbr, 'CST');
94 assert.strictEqual(tz.iana, 'Europe/Paris');
96 return userService.updateTzInfo(fixture2.user1.id, {});
99 return userService.findById(fixture2.user1.id);
101 .then(function(user) {
104 assert(!tz || !tz.offset);
105 assert(!tz || !tz.abbr);
106 assert(!tz || !tz.iana);
110 describe('findAllByEmail', () => {
111 const findAllByEmailFixture = fixtureLoader.setup({
113 emails: ['test@gitter.im']
116 emails: ['test@gitter.im']
121 providerKey: fixtureLoader.generateGithubId(),
122 email: 'test@gitter.im'
126 it('find all users matching the email', async () => {
127 const users = await userService.findAllByEmail('test@gitter.im');
128 const userIds = users.map(user => user.id);
130 // The order that the users are returned is not consistent
131 // so we can't use an easy `assert.deepEqual`
135 `Expected ${JSON.stringify([
136 findAllByEmailFixture.user1.id,
137 findAllByEmailFixture.userGitlab1.id
138 ])} but found ${JSON.stringify(userIds)}`
141 userIds.includes(findAllByEmailFixture.user1.id),
142 `Expected ${findAllByEmailFixture.user1.id} in users=${JSON.stringify(userIds)}`
145 userIds.includes(findAllByEmailFixture.userGitlab1.id),
146 `Expected ${findAllByEmailFixture.userGitlab1.id} in users=${JSON.stringify(userIds)}`
151 describe('#unremoveUser', () => {
152 const rmFixture = fixtureLoader.setup({
158 it('should clear the status of removed user', async () => {
159 assert.strictEqual(rmFixture.user.state, 'REMOVED');
160 await userService.unremoveUser(rmFixture.user);
161 const user = await persistence.User.findOne({ _id: rmFixture.user._id });
162 assert.strictEqual(user.state, undefined);