Skip to content

Commit

Permalink
More remarks taken into account
Browse files Browse the repository at this point in the history
  • Loading branch information
fflorent committed Aug 6, 2024
1 parent fae34d1 commit ca8492a
Showing 1 changed file with 45 additions and 21 deletions.
66 changes: 45 additions & 21 deletions test/gen-server/lib/homedb/UsersManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ describe('UsersManager', function () {
assert.deepEqual(result, usersList.flat());
});

it('should deduplicate the results', function () {
const resources: Resource[] = [new Organization(), new Workspace()];
const usersList = makeUsersList(2, 1);
const expectedResult = usersList.flat();

const duplicateUser = new User();
duplicateUser.id = usersList[1][0].id;
usersList[0].unshift(duplicateUser);

for (const [idx, resource] of resources.entries()) {
populateResourceWithMembers(resource, usersList[idx]);
}

const result = UsersManager.getResourceUsers(resources);

assert.deepEqual(result, expectedResult);
});

it('should return users matching group names from all resources ACL', function () {
const resources: Resource[] = [new Organization(), new Workspace(), new Document()];
const usersList = makeUsersList(3, 5);
Expand Down Expand Up @@ -226,6 +244,21 @@ describe('UsersManager', function () {
return sandbox.stub(log, method);
}

/**
* Make a user profile.
* @param localPart A unique local part of the email (also used for the other fields).
*/
function makeProfile(localPart: string): UserProfile {
ensureUnique(localPart);
return {
email: makeEmail(localPart),
name: `NewUser ${localPart}`,
connectId: `ConnectId-${localPart}`,
picture: `https://mypic.com/${localPart}.png`
};
}


before(async function () {
env = new EnvironmentSnapshot();
await prepareFilesystemDirectoryForTests(tmpDir);
Expand Down Expand Up @@ -315,6 +348,10 @@ describe('UsersManager', function () {
prefs: { showGristTour: true } as any
}]);
});

it('should return undefined when the id is not found', async function () {
assert.isUndefined(await db.getUser(NON_EXISTING_USER_ID));
});
});

describe('getFullUser()', function () {
Expand Down Expand Up @@ -450,20 +487,6 @@ describe('UsersManager', function () {
userSaveSpy = sandbox.spy(User.prototype, 'save');
});

/**
* Make a user profile.
* @param localPart A unique local part of the email (also used for the other fields).
*/
function makeProfile(localPart: string): UserProfile {
ensureUnique(localPart);
return {
email: makeEmail(localPart),
name: `NewUser ${localPart}`,
connectId: `ConnectId-${localPart}`,
picture: `https://mypic.com/${localPart}.png`
};
}

async function checkUserInfo(profile: UserProfile) {
const user = await db.getExistingUserByLogin(profile.email);
assertExists(user, "the new user should be in database");
Expand Down Expand Up @@ -716,13 +739,6 @@ describe('UsersManager', function () {
});

describe('when passing information to update (using `profile`)', function () {
const makeProfile = (newEmail: string): UserProfile => ({
name: 'my user name',
email: newEmail,
picture: 'https://mypic.com/foo.png',
connectId: 'new-connect-id',
});

it('should populate the firstTimeLogin and deduce the name from the email', async function () {
sandbox.useFakeTimers(42_000);
const localPart = ensureUnique('getuserbylogin-with-profile-populates-first_time_login-and-name');
Expand All @@ -737,6 +753,7 @@ describe('UsersManager', function () {
it('should populate user with any passed information', async function () {
const localPart = ensureUnique('getuserbylogin-with-profile-populates-user-with-passed-info_OLD');
await db.getUserByLogin(makeEmail(localPart));
const originalNormalizedLoginEmail = makeEmail(localPart.toLowerCase());

const profile = makeProfile(makeEmail('getuserbylogin-with-profile-populates-user-with-passed-info_NEW'));
const userOptions: UserOptions = {authSubject: 'my-auth-subject'};
Expand All @@ -749,6 +766,7 @@ describe('UsersManager', function () {
});
assert.deepInclude(updatedUser.logins[0], {
displayEmail: profile.email,
email: originalNormalizedLoginEmail,
});
assert.deepInclude(updatedUser.options, {
authSubject: userOptions.authSubject,
Expand Down Expand Up @@ -817,12 +835,17 @@ describe('UsersManager', function () {
return manager.exists('group_users', { where: {user_id: userId} });
}

async function assertUserStillExistsInDb(userId: number) {
assert.exists(await db.getUser(userId));
}

it('should refuse to delete the account of someone else', async function () {
const userToDelete = await createUniqueUser('deleteuser-refuses-for-someone-else');

const promise = db.deleteUser({userId: 2}, userToDelete.id);

await assert.isRejected(promise, 'not permitted to delete this user');
await assertUserStillExistsInDb(userToDelete.id);
});

it('should refuse to delete a non existing account', async function () {
Expand All @@ -848,6 +871,7 @@ describe('UsersManager', function () {

await assert.isRejected(promise);
await promise.catch(e => assert.match(e.message, /user name did not match/));
await assertUserStillExistsInDb(userToDelete.id);
});

it('should remove the user and cleanup their info and personal organization', async function () {
Expand Down

0 comments on commit ca8492a

Please sign in to comment.