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

Add AdminController Test Suite #187

Merged
merged 26 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
cba950b
Add tests for "/bonus" and "/emails" routes
StormFireFox1 May 16, 2021
11c362c
nit: "proxyUser" -> "adminUser" for admin tests
StormFireFox1 May 27, 2021
fdbbd08
Adjust array equality checks on admin tests
StormFireFox1 May 27, 2021
3a27e7f
Remove test for non-present emails
StormFireFox1 May 27, 2021
5fc6a9a
Inline functions and variables in admin tests
StormFireFox1 May 27, 2021
5fe3084
Modify test to check bonus points for all users
StormFireFox1 May 27, 2021
1e37dd2
Combine both bonus points tests into one
StormFireFox1 May 27, 2021
5ad4381
Rearrange asserts to suffix response calls
StormFireFox1 May 27, 2021
df1a901
Rename adminUser to admin in admin test suite
StormFireFox1 Jun 9, 2021
aca6574
Adjust order of assertions in admin test
StormFireFox1 Jun 9, 2021
2afef57
Adjust name of extraneous user in admin test
StormFireFox1 Jun 9, 2021
acdf94a
Make variable for UserController in admin test
StormFireFox1 Jun 9, 2021
aa286ee
Merge branch 'master' into add-admin-test-suite
StormFireFox1 Jun 20, 2021
2c591a5
Fix nits and check attendance response details
StormFireFox1 Jul 3, 2021
6688e90
Check for correct activities in admin test suite
StormFireFox1 Jul 3, 2021
da3210a
Add points field of ActivityModel in test suite
StormFireFox1 Jul 3, 2021
747b44d
Port EventFactory code from #181
Jul 16, 2021
a9834d4
Adjust test suite to account for factory bug
Jul 16, 2021
94e1113
Revert event factory code changes
StormFireFox1 Jul 22, 2021
b5f03dc
Adjust transaction order when saving factory state
StormFireFox1 Jul 22, 2021
f5832f5
Merge branch 'master' into add-admin-test-suite
StormFireFox1 Jul 22, 2021
54c65b8
Revert transaction order adjustment
StormFireFox1 Jul 22, 2021
fe8f65b
Guarantee correct activity order in failed tests
StormFireFox1 Jul 22, 2021
b41e0da
Merge user creation methods to have optional args
StormFireFox1 Jul 22, 2021
4c3f0fc
Adjust user creation method to have set timestamp
StormFireFox1 Jul 22, 2021
b7ab295
Fix argument call for creating users in test suite
StormFireFox1 Jul 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 86 additions & 21 deletions tests/admin.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActivityScope, ActivityType, SubmitAttendanceForUsersRequest, UserAccessType } from '../types';
import { ControllerFactory } from './controllers';
import { DatabaseConnection, EventFactory, UserFactory, PortalState } from './data';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';

beforeAll(async () => {
await DatabaseConnection.connect();
Expand All @@ -20,31 +20,31 @@ describe('retroactive attendance submission', () => {
const conn = await DatabaseConnection.get();
const users = UserFactory.create(3);
const emails = users.map((user) => user.email);
const [proxyUser] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [admin] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [event] = EventFactory.create(1);

await new PortalState()
.createUsers([...users, proxyUser])
.createUsers([...users, admin])
.createEvents([event])
.write();

const userController = ControllerFactory.user(conn);
const adminController = ControllerFactory.admin(conn);
const attendanceController = ControllerFactory.attendance(conn);

await adminController.submitAttendanceForUsers({ users: emails, event: event.uuid }, proxyUser);
await adminController.submitAttendanceForUsers({ users: emails, event: event.uuid }, admin);

for (let u = 0; u < users.length; u += 1) {
const user = users[u];
const userResponse = await userController.getUser({ uuid: user.uuid }, proxyUser);
const userResponse = await userController.getUser({ uuid: user.uuid }, admin);

expect(userResponse.user.points).toEqual(user.points + event.pointValue);

const attendanceResponse = await attendanceController.getAttendancesForCurrentUser(user);
expect(attendanceResponse.attendances).toHaveLength(1);
expect(attendanceResponse.attendances[0].event).toStrictEqual(event.getPublicEvent());

const activityResponse = await userController.getUserActivityStream({ uuid: user.uuid }, proxyUser);
const activityResponse = await userController.getUserActivityStream({ uuid: user.uuid }, admin);

expect(activityResponse.activity).toHaveLength(2);
expect(activityResponse.activity[1].pointsEarned).toEqual(event.pointValue);
Expand All @@ -56,11 +56,14 @@ describe('retroactive attendance submission', () => {
test('does not log activity, attendance, and points for users who already attended', async () => {
const conn = await DatabaseConnection.get();
const [user] = UserFactory.create(1);
const [proxyUser] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [admin] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [event] = EventFactory.create(1);

await new PortalState()
.createUsers([user, proxyUser])
// Create users at beginning of time to ensure that account creation time does not
// end up after event attendance time, causing the order guarantee for activities
// below to fail.
.createUsers([user, admin])
.createEvents([event])
.attendEvents([user], [event])
.write();
Expand All @@ -71,28 +74,33 @@ describe('retroactive attendance submission', () => {

await adminController.submitAttendanceForUsers(
{ users: [user.email], event: event.uuid },
proxyUser,
admin,
);

const userResponse = await userController.getUser({ uuid: user.uuid }, proxyUser);
const attendanceResponse = await attendanceController.getAttendancesForCurrentUser(user);
const activityResponse = await userController.getCurrentUserActivityStream(user);

const userResponse = await userController.getUser({ uuid: user.uuid }, admin);
expect(userResponse.user.points).toEqual(user.points);

const attendanceResponse = await attendanceController.getAttendancesForCurrentUser(user);
StormFireFox1 marked this conversation as resolved.
Show resolved Hide resolved
expect(attendanceResponse.attendances).toHaveLength(1);
expect(attendanceResponse.attendances[0].event.uuid).toEqual(event.uuid);
expect(attendanceResponse.attendances[0].asStaff).toEqual(false);
StormFireFox1 marked this conversation as resolved.
Show resolved Hide resolved

const activityResponse = await userController.getCurrentUserActivityStream(user);
StormFireFox1 marked this conversation as resolved.
Show resolved Hide resolved
expect(activityResponse.activity).toHaveLength(2);
expect(activityResponse.activity[1].description).toBeNull();
expect(activityResponse.activity[1].type).toEqual(ActivityType.ATTEND_EVENT);
expect(activityResponse.activity[1].pointsEarned).toEqual(event.pointValue);
});

test('logs proper activity and point rewards for staff attendance', async () => {
const conn = await DatabaseConnection.get();
const [user] = UserFactory.create(1);
const [staffUser] = UserFactory.with({ accessType: UserAccessType.STAFF });
const [proxyUser] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [admin] = UserFactory.with({ accessType: UserAccessType.ADMIN });
const [event] = EventFactory.with({ requiresStaff: true, staffPointBonus: 10 });

await new PortalState()
.createUsers([user, staffUser, proxyUser])
.createUsers([user, staffUser, admin])
.createEvents([event])
.write();

Expand All @@ -104,16 +112,73 @@ describe('retroactive attendance submission', () => {
asStaff: true,
};

await adminController.submitAttendanceForUsers(request, proxyUser);

const userResponse = await userController.getUser({ uuid: user.uuid }, proxyUser);
const staffUserResponse = await userController.getUser({ uuid: staffUser.uuid }, proxyUser);
const activityResponse = await userController.getCurrentUserActivityStream(user);
const staffActivityResponse = await userController.getCurrentUserActivityStream(staffUser);
await adminController.submitAttendanceForUsers(request, admin);

const userResponse = await userController.getUser({ uuid: user.uuid }, admin);
expect(userResponse.user.points).toEqual(event.pointValue);

const staffUserResponse = await userController.getUser({ uuid: staffUser.uuid }, admin);
expect(staffUserResponse.user.points).toEqual(event.pointValue + event.staffPointBonus);

const activityResponse = await userController.getCurrentUserActivityStream(user);
expect(activityResponse.activity[1].type).toEqual(ActivityType.ATTEND_EVENT);

const staffActivityResponse = await userController.getCurrentUserActivityStream(staffUser);
expect(staffActivityResponse.activity[1].type).toEqual(ActivityType.ATTEND_EVENT_AS_STAFF);
});
});

describe('email retrieval', () => {
test('gets all the emails of stored users', async () => {
const conn = await DatabaseConnection.get();
const users = UserFactory.create(5);
const emails = users.map((user) => user.email.toLowerCase());
const [admin] = UserFactory.with({ accessType: UserAccessType.ADMIN });

await new PortalState()
.createUsers([...users, admin])
.write();

const response = await ControllerFactory.admin(conn).getAllEmails(admin);
expect(expect.arrayContaining(response.emails)).toEqual([...emails, admin.email]);
});
});

describe('bonus points submission', () => {
test('updates points and activity to the users in the bonus request', async () => {
const conn = await DatabaseConnection.get();
const [userNotGettingBonus, ...users] = UserFactory.create(5);
const emails = users.map((user) => user.email.toLowerCase());
const [admin] = UserFactory.with({ accessType: UserAccessType.ADMIN });

await new PortalState()
.createUsers([...users, userNotGettingBonus, admin])
.write();

const bonus = {
description: 'Test addition of bonus points',
users: emails,
points: 200,
};

const createBonusResponse = await ControllerFactory.admin(conn).addBonus({ bonus }, admin);
expect(createBonusResponse.emails).toEqual(expect.arrayContaining(emails));

const userController = ControllerFactory.user(conn);

for (let u = 0; u < users.length; u += 1) {
const user = users[u];
const getUserResponse = await userController.getUser({ uuid: user.uuid }, admin);
expect(getUserResponse.user.points).toEqual(200);
StormFireFox1 marked this conversation as resolved.
Show resolved Hide resolved

const activityResponse = await userController.getCurrentUserActivityStream(user);
expect(activityResponse.activity).toHaveLength(2);
expect(activityResponse.activity[1].description).toEqual(bonus.description);
expect(activityResponse.activity[1].type).toEqual(ActivityType.BONUS_POINTS);
expect(activityResponse.activity[1].pointsEarned).toEqual(bonus.points);
}

const getNoBonusUserResponse = await userController.getUser({ uuid: userNotGettingBonus.uuid }, admin);
expect(getNoBonusUserResponse.user.points).toEqual(0);
});
});
3 changes: 3 additions & 0 deletions tests/data/PortalState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as rfdc from 'rfdc';
import { flatten } from 'underscore';
import * as moment from 'moment';
import { AttendanceModel } from '../../models/AttendanceModel';
import { EventModel } from '../../models/EventModel';
import { MerchandiseCollectionModel } from '../../models/MerchandiseCollectionModel';
Expand Down Expand Up @@ -61,6 +62,7 @@ export class PortalState {
user,
type: ActivityType.ACCOUNT_CREATE,
scope: ActivityScope.PUBLIC,
timestamp: moment().subtract(1, 'months'),
}));
}
return this;
Expand Down Expand Up @@ -97,6 +99,7 @@ export class PortalState {
type: asStaff ? ActivityType.ATTEND_EVENT_AS_STAFF : ActivityType.ATTEND_EVENT,
timestamp,
scope: ActivityScope.PUBLIC,
pointsEarned,
}));
}
}
Expand Down