Skip to content

Commit

Permalink
fixed tests and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-dange committed Aug 20, 2023
1 parent e42c68b commit a7f3833
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 15 deletions.
16 changes: 9 additions & 7 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InjectManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
import * as moment from 'moment';
import * as faker from 'faker';
import { UserAccessUpdates } from 'api/validators/AdminControllerRequests';
import Repositories, { TransactionsManager } from '../repositories';
import {
Uuid,
Expand All @@ -18,7 +19,6 @@ import {
} from '../types';
import { UserRepository } from '../repositories/UserRepository';
import { UserModel } from '../models/UserModel';
import { UserAccessUpdates } from 'api/validators/AdminControllerRequests';

@Service()
export default class UserAccountService {
Expand Down Expand Up @@ -195,7 +195,6 @@ export default class UserAccountService {
});
}


public async checkDuplicateEmails(emails: string[]) {
const emailSet = emails.reduce((set, email) => {
if (set.has(email)) {
Expand Down Expand Up @@ -238,15 +237,15 @@ export default class UserAccountService {
return map;
}, {});

console.log("emailToUserMap: ", emailToUserMap);
// console.log('emailToUserMap: ', emailToUserMap);

const updatedUsers = await Promise.all(accessUpdates.map(async (accessUpdate, index) => {
const { user, accessType } = accessUpdate;

const matchingAccess = Object.keys(UserAccessType).find((access) => access === accessType);
const updatingAccess: UserAccessType = UserAccessType[matchingAccess];

//const currUser = await userRepository.findByEmail(user);
// const currUser = await userRepository.findByEmail(user);

const currUser = emailToUserMap[user];
const oldAccess = currUser.accessType;
Expand All @@ -263,12 +262,15 @@ export default class UserAccountService {
const updatedUser = await userRepository.upsertUser(currUser, { accessType: updatingAccess });
// log the activity of changing a user's access type
const activity = {
currentUser,
user: currentUser,
type: ActivityType.ACCOUNT_UPDATE_INFO,
description: `${currentUser.email} changed ${updatedUser.email}'s access level from ${oldAccess} to ${accessType}`
description: `${currentUser.email} changed ${updatedUser.email}'s
access level from ${oldAccess} to ${accessType}`,
};

console.log("activity: ", activity.description);
await Repositories
.activity(txn)
.logActivity(activity);

return updatedUser;
}));
Expand Down
57 changes: 49 additions & 8 deletions tests/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { BadRequestError, ForbiddenError } from 'routing-controllers';
import { In } from 'typeorm';
import { ActivityScope, ActivityType, SubmitAttendanceForUsersRequest, UserAccessType } from '../types';
import { ControllerFactory } from './controllers';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';
import { UserModel } from '../models/UserModel';

beforeAll(async () => {
await DatabaseConnection.connect();
Expand Down Expand Up @@ -190,8 +192,6 @@ describe('updating user access level', () => {
const admin = UserFactory.fake({ accessType: UserAccessType.ADMIN });
const users = UserFactory.create(4);

console.log('original access: ', users[0].accessType);

users[0].accessType = UserAccessType.STAFF;
users[1].accessType = UserAccessType.STANDARD;
users[2].accessType = UserAccessType.MARKETING;
Expand All @@ -212,20 +212,26 @@ describe('updating user access level', () => {
],
}, admin);


console.log("User after update: ", users[0]);
const repository = conn.getRepository(UserModel);
const updatedUsers = await repository.find({
email: In([users[0].email, users[1].email, users[2].email, users[3].email]),
});

// expect statements
expect(accessLevelResponse.updatedUsers[0].email).toEqual(users[0].email);
expect(updatedUsers[0].email).toEqual(users[0].email);
expect(updatedUsers[0].accessType).toEqual(UserAccessType.MERCH_STORE_MANAGER);
expect(accessLevelResponse.updatedUsers[0].accessType).toEqual(UserAccessType.MERCH_STORE_MANAGER);

expect(accessLevelResponse.updatedUsers[1].email).toEqual(users[1].email);
expect(updatedUsers[1].email).toEqual(users[1].email);
expect(updatedUsers[1].accessType).toEqual(UserAccessType.MARKETING);
expect(accessLevelResponse.updatedUsers[1].accessType).toEqual(UserAccessType.MARKETING);

expect(accessLevelResponse.updatedUsers[2].email).toEqual(users[2].email);
expect(updatedUsers[2].email).toEqual(users[2].email);
expect(updatedUsers[2].accessType).toEqual(UserAccessType.MERCH_STORE_DISTRIBUTOR);
expect(accessLevelResponse.updatedUsers[2].accessType).toEqual(UserAccessType.MERCH_STORE_DISTRIBUTOR);

expect(accessLevelResponse.updatedUsers[3].email).toEqual(users[3].email);
expect(updatedUsers[3].email).toEqual(users[3].email);
expect(updatedUsers[3].accessType).toEqual(UserAccessType.STAFF);
expect(accessLevelResponse.updatedUsers[3].accessType).toEqual(UserAccessType.STAFF);
});

Expand Down Expand Up @@ -335,4 +341,39 @@ describe('updating user access level', () => {
}, admin);
}).rejects.toThrow(ForbiddenError);
});

test("ensure that the updating user's access level is not changed", async () => {
const conn = await DatabaseConnection.get();
const admin = UserFactory.fake({ accessType: UserAccessType.ADMIN });
const users = UserFactory.create(4);

users[0].accessType = UserAccessType.STAFF;
users[1].accessType = UserAccessType.STANDARD;
users[2].accessType = UserAccessType.MARKETING;
users[3].accessType = UserAccessType.MERCH_STORE_DISTRIBUTOR;

await new PortalState()
.createUsers(users[0], users[1], users[2], users[3], admin)
.write();

const adminController = ControllerFactory.admin(conn);

await adminController.updateUserAccessLevel({
accessUpdates: [
{ user: users[0].email, accessType: 'MERCH_STORE_MANAGER' },
{ user: users[1].email, accessType: 'MARKETING' },
{ user: users[2].email, accessType: 'MERCH_STORE_DISTRIBUTOR' },
{ user: users[3].email, accessType: 'STAFF' },
],
}, admin);

const repository = conn.getRepository(UserModel);
const existingAdmin = await repository.find({
email: admin.email,
});

// expect statements
expect(existingAdmin[0].email).toEqual(admin.email);
expect(existingAdmin[0].accessType).toEqual(UserAccessType.ADMIN);
});
});

0 comments on commit a7f3833

Please sign in to comment.