Skip to content

Commit

Permalink
Add tests for the new UsersManager methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Nov 22, 2024
1 parent 324e6cb commit c3b291b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
9 changes: 5 additions & 4 deletions app/gen-server/lib/homedb/UsersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,11 @@ export class UsersManager {
});
}

public async getUsers() {
return await User.find({relations: ["logins"]});
}


/**
* ==================================
*
Expand Down Expand Up @@ -706,10 +711,6 @@ export class UsersManager {
return [this.getSupportUserId(), this.getAnonymousUserId(), this.getEveryoneUserId()];
}

public async getUsers() {
return await User.find({relations: ["logins"]});
}

/**
* Returns a Promise for an array of User entites for the given userIds.
*/
Expand Down
63 changes: 63 additions & 0 deletions test/gen-server/lib/homedb/UsersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { assert } from 'chai';
import Sinon, { SinonSandbox, SinonSpy } from 'sinon';
import { EntityManager } from 'typeorm';
import winston from 'winston';
import omit from 'lodash/omit';

import {delay} from 'app/common/delay';

Expand Down Expand Up @@ -982,6 +983,68 @@ describe('UsersManager', function () {
}
});
});

describe('overrideUser()', function () {
it('should reject when user is not found', async function () {
disableLoggingLevel('debug');

const promise = db.overrideUser(NON_EXISTING_USER_ID, {
email: '[email protected]',
name: 'whatever',
});

await assert.isRejected(promise, 'unable to find user to update');
});

it('should update user information', async function () {
const localPart = 'overrideuser-updates-user-info';
const newLocalPart = 'overrideuser-updates-user-info-new';
const user = await createUniqueUser(localPart);
const newInfo: UserProfile = {
name: 'new name',
email: makeEmail(newLocalPart).toUpperCase(),
picture: 'https://mypic.com/me.png',
locale: 'fr-FR',
};

await db.overrideUser(user.id, newInfo);

const updatedUser = await getOrCreateUser(newLocalPart);
assert.deepInclude(updatedUser, {
id: user.id,
name: newInfo.name,
picture: newInfo.picture,
options: {locale: newInfo.locale},
});
assert.deepInclude(updatedUser.logins[0], {
email: newInfo.email.toLowerCase(),
displayEmail: newInfo.email,
});
});
});

describe('getUsers()', function () {
it('should return all users with their logins', async function () {
const localPart = 'getUsers-user';
const existingUser = await createUniqueUser(localPart);
const users = await db.getUsers();
assert.isAbove(users.length, 2);
const mapUsersById = new Map(users.map(user => [user.id, user]));

// Check that we retrieve the existing user in the result with all their property
// except the personalOrg
const existingUserInResult = mapUsersById.get(existingUser.id);
assertExists(existingUserInResult);
assertExists(existingUserInResult.logins);
assert.lengthOf(existingUserInResult.logins, 1);
assert.deepEqual(existingUserInResult, omit(existingUser, 'personalOrg'));

// Check that we retrieve special accounts among the result
assert.exists(mapUsersById.get(db.getSupportUserId()));
assert.exists(mapUsersById.get(db.getEveryoneUserId()));
assert.exists(mapUsersById.get(db.getAnonymousUserId()));
});
});
});

describe('class method without db setup', function () {
Expand Down

0 comments on commit c3b291b

Please sign in to comment.