Skip to content

Commit

Permalink
faker
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwn04 committed Jan 17, 2025
1 parent 34b77ff commit 840ad5e
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 185 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"homepage": "https://github.com/acmucsd/membership-portal#readme",
"dependencies": {
"@faker-js/faker": "^9.4.0",
"@sendgrid/mail": "^8.1.4",
"amazon-s3-uri": "^0.1.1",
"async-retry": "^1.3.3",
Expand All @@ -54,9 +55,8 @@
"eslint-plugin-react": "^7.21.5",
"eslint-plugin-react-hooks": "^4",
"express": "^4.21.1",
"faker": "^5.5.3",
"jsonwebtoken": "^9.0.2",
"moment": "^2.27.0",
"moment": "2.29.4",
"moment-timezone": "^0.5.34",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
Expand Down
4 changes: 2 additions & 2 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BadRequestError, ForbiddenError, NotFoundError } from 'routing-controllers';
import { Service } from 'typedi';
import * as moment from 'moment';
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { UserAccessUpdates } from 'api/validators/AdminControllerRequests';
import {
RegExpMatcher,
Expand Down Expand Up @@ -58,7 +58,7 @@ export default class UserAccountService {
public static generateDefaultHandle(firstName: string, lastName: string): string {
const nameString = `${firstName}-${lastName}`.slice(0, 25);
// Hexadecimals look like 0x1b9Dle so we have to truncate the fixed '0x'.
const hashValue = faker.datatype.hexaDecimal(6).slice(2);
const hashValue = faker.string.hexadecimal({ length:6 }).slice(2);
const handle = `${nameString}-${hashValue}`.toLowerCase();
return handle;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/attendance.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';
import { ForbiddenError } from 'routing-controllers';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('attendance', () => {
.createEvents(event)
.write();

const attendEventRequest = { attendanceCode: faker.datatype.hexaDecimal(10) };
const attendEventRequest = { attendanceCode: faker.string.hexadecimal({ length: 10 }) };
await expect(ControllerFactory.attendance(conn).attendEvent(attendEventRequest, member))
.rejects.toThrow('Oh no! That code didn\'t work.');
});
Expand Down
6 changes: 3 additions & 3 deletions tests/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as jwt from 'jsonwebtoken';
import * as bcrypt from 'bcrypt';
import { NotFoundError } from 'routing-controllers';
import { anyString, instance, mock, verify, when } from 'ts-mockito';
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';
import { Config } from '../config';
import { UserModel } from '../models/UserModel';
Expand Down Expand Up @@ -134,8 +134,8 @@ describe('account registration', () => {
.write();

const user = UserRegistrationFactory.fake({
firstName: faker.datatype.string(40),
lastName: faker.datatype.string(40),
firstName: faker.string.alpha({ length: 40 }),
lastName: faker.string.alpha({ length: 40 }),
});

// register member
Expand Down
12 changes: 6 additions & 6 deletions tests/data/EventFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';
import { v4 as uuid } from 'uuid';
import { EventModel } from '../../models/EventModel';
Expand Down Expand Up @@ -28,12 +28,12 @@ export class EventFactory {
const fake = EventRepository.create({
uuid: uuid(),
organization: FactoryUtils.pickRandomValue(EventFactory.ORGS),
title: faker.datatype.hexaDecimal(10),
title: faker.string.hexadecimal({ length: 10 }),
description: faker.lorem.sentences(2),
location: faker.datatype.hexaDecimal(10),
location: faker.string.hexadecimal({ length: 10 }),
start,
end,
attendanceCode: faker.datatype.hexaDecimal(10),
attendanceCode: faker.string.hexadecimal({ length: 10 }),
pointValue: EventFactory.randomPointValue(),
requiresStaff: FactoryUtils.getRandomBoolean(),
staffPointBonus: EventFactory.randomPointValue(),
Expand All @@ -42,8 +42,8 @@ export class EventFactory {
deleted: false,
eventLink: faker.internet.url(),
thumbnail: FactoryUtils.getRandomImageUrl(),
discordEvent: faker.datatype.hexaDecimal(10),
googleCalendarEvent: faker.datatype.hexaDecimal(10),
discordEvent: faker.string.hexadecimal({ length: 10 }),
googleCalendarEvent: faker.string.hexadecimal({ length: 10 }),
foodItems: null,
});
return EventRepository.merge(fake, substitute);
Expand Down
6 changes: 3 additions & 3 deletions tests/data/FactoryUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';

export default class FactoryUtils {
Expand Down Expand Up @@ -38,10 +38,10 @@ export default class FactoryUtils {
}

public static randomHexString(): string {
return faker.datatype.hexaDecimal(10);
return faker.string.hexadecimal({ length: 10 });
}

public static getRandomImageUrl(): string {
return `http://i.imgur.com/${faker.random.alphaNumeric(10)}.jpeg`;
return `http://i.imgur.com/${faker.string.alphanumeric(10)}.jpeg`;
}
}
2 changes: 1 addition & 1 deletion tests/data/FeedbackFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { v4 as uuid } from 'uuid';
import { FeedbackStatus, FeedbackType } from '../../types';
import FactoryUtils from './FactoryUtils';
Expand Down
14 changes: 7 additions & 7 deletions tests/data/MerchFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';
import { v4 as uuid } from 'uuid';
import { MerchItemOptionMetadata, OrderPickupEventStatus } from '../../types';
Expand All @@ -22,7 +22,7 @@ export class MerchFactory {
public static fakeCollection(substitute?: Partial<MerchandiseCollectionModel>): MerchandiseCollectionModel {
const fake = MerchCollectionRepository.create({
uuid: uuid(),
title: faker.datatype.hexaDecimal(10),
title: faker.string.hexadecimal({ length: 10 }),
description: faker.lorem.sentences(2),
themeColorHex: faker.internet.color(),
createdAt: faker.date.recent(),
Expand Down Expand Up @@ -52,7 +52,7 @@ export class MerchFactory {
const hasVariantsEnabled = substitute?.hasVariantsEnabled ?? FactoryUtils.getRandomBoolean();
const fake = MerchItemRepository.create({
uuid: uuid(),
itemName: faker.datatype.hexaDecimal(10),
itemName: faker.string.hexadecimal({ length: 10}),
description: faker.lorem.sentences(2),
hasVariantsEnabled,
monthlyLimit: FactoryUtils.getRandomNumber(1, 5),
Expand Down Expand Up @@ -121,8 +121,8 @@ export class MerchFactory {

public static fakeOptionMetadata(substitute?: Partial<MerchItemOptionMetadata>): MerchItemOptionMetadata {
const fake = {
type: faker.datatype.hexaDecimal(10),
value: faker.datatype.hexaDecimal(10),
type: faker.string.hexadecimal({ length: 10 }),
value: faker.string.hexadecimal({ length: 10 }),
position: FactoryUtils.getRandomNumber(1, 10),
};
const sub = substitute ?? {};
Expand All @@ -136,7 +136,7 @@ export class MerchFactory {
const [start, end] = FactoryUtils.getRandomTimeInterval();
const fake = OrderPickupEventRepository.create({
uuid: uuid(),
title: faker.datatype.hexaDecimal(10),
title: faker.string.hexadecimal({ length: 10 }),
description: faker.lorem.sentences(2),
start,
end,
Expand Down Expand Up @@ -177,7 +177,7 @@ export class MerchFactory {
if (n === 1) return [MerchFactory.fakeOption()];

// create multiple options with consistent types
const type = faker.datatype.hexaDecimal(10);
const type = faker.string.hexadecimal({ length: 10 });
return FactoryUtils.create(n, () => MerchFactory.fakeOptionWithType(type));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/data/ResumeFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { v4 as uuid } from 'uuid';
import { ResumeModel } from '../../models/ResumeModel';
import FactoryUtils from './FactoryUtils';
Expand Down
8 changes: 4 additions & 4 deletions tests/data/UserFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';
import { v4 as uuid } from 'uuid';
import UserAccountService from '../../services/UserAccountService';
Expand Down Expand Up @@ -29,11 +29,11 @@ export class UserFactory {
}

public static fake(substitute?: Partial<UserModel>): UserModel {
const firstName = faker.name.firstName();
const lastName = faker.name.lastName();
const firstName = faker.person.firstName();
const lastName = faker.person.lastName();
const fake = UserRepository.create({
uuid: uuid(),
email: faker.internet.email(firstName, lastName, 'ucsd.edu'),
email: faker.internet.email({ firstName, lastName, provider: 'ucsd.edu' }),
firstName,
lastName,
hash: UserFactory.PASSWORD_HASH,
Expand Down
8 changes: 4 additions & 4 deletions tests/data/UserRegistrationFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserRegistration } from 'types';
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import FactoryUtils from './FactoryUtils';
import { UserFactory } from './UserFactory';

Expand All @@ -11,9 +11,9 @@ export class UserRegistrationFactory {
public static fake(substitute?: Partial<UserRegistration>): UserRegistration {
const fake: UserRegistration = {
email: faker.internet.email(),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
password: faker.datatype.string(10),
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
password: faker.string.alphanumeric(10),
graduationYear: UserFactory.graduationYear(),
major: UserFactory.major(),
};
Expand Down
2 changes: 1 addition & 1 deletion tests/data/UserSocialMediaFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { v4 as uuid } from 'uuid';
import { UserSocialMediaModel } from '../../models/UserSocialMediaModel';
import { SocialMediaType } from '../../types';
Expand Down
2 changes: 1 addition & 1 deletion tests/expressCheckin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { anything, instance, mock, verify, when } from 'ts-mockito';
import { DatabaseConnection, EventFactory, PortalState, UserFactory } from './data';
import { ControllerFactory } from './controllers';
Expand Down
8 changes: 4 additions & 4 deletions tests/merchOrder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import * as moment from 'moment';
import { mock, when, anything, instance, verify, anyString } from 'ts-mockito';
import { ForbiddenError, NotFoundError } from 'routing-controllers';
Expand Down Expand Up @@ -514,7 +514,7 @@ describe('merch orders', () => {
const order = placedOrderResponse.order.uuid;
const fulfillOrderParams = { uuid: order };
const orderItem = placedOrderResponse.order.items[0].uuid;
const fulfillmentUpdate = { uuid: orderItem, notes: faker.datatype.hexaDecimal(10) };
const fulfillmentUpdate = { uuid: orderItem, notes: faker.string.hexadecimal({ length: 10}) };
const itemsToFulfill = { items: [fulfillmentUpdate] };
await MerchStoreControllerWrapper.fulfillMerchOrderItems(merchController, fulfillOrderParams,
itemsToFulfill, merchDistributor, conn, pickupEvent);
Expand Down Expand Up @@ -664,7 +664,7 @@ describe('merch orders', () => {

test('order route accurately verifies monthly and lifetime limits', async () => {
const conn = await DatabaseConnection.get();
const optionMetadataType = faker.datatype.hexaDecimal(10);
const optionMetadataType = faker.string.hexadecimal({ length: 10});
const option1 = MerchFactory.fakeOptionWithType(optionMetadataType);
const option2 = MerchFactory.fakeOptionWithType(optionMetadataType);
const option3 = MerchFactory.fakeOptionWithType(optionMetadataType);
Expand Down Expand Up @@ -1044,7 +1044,7 @@ describe('merch order pickup events', () => {
.createOrderPickupEvents(pickupEvent)
.write();

const editPickupEventRequest = { pickupEvent: { title: faker.datatype.hexaDecimal(10) } };
const editPickupEventRequest = { pickupEvent: { title: faker.string.hexadecimal({ length: 10}) } };
const params = { uuid: pickupEvent.uuid };
await ControllerFactory.merchStore(conn).editPickupEvent(params, editPickupEventRequest, merchDistributor);

Expand Down
20 changes: 10 additions & 10 deletions tests/merchStore.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { ForbiddenError, NotFoundError } from 'routing-controllers';
import { zip } from 'underscore';
import { anything, instance, verify, mock, when } from 'ts-mockito';
Expand Down Expand Up @@ -176,7 +176,7 @@ describe('editing merch collections', () => {

const merchStoreController = ControllerFactory.merchStore(conn);
const params = { uuid: collection.uuid };
const editMerchCollectionRequest = { collection: { title: faker.datatype.hexaDecimal(10) } };
const editMerchCollectionRequest = { collection: { title: faker.string.hexadecimal({ length: 10}) } };

await expect(merchStoreController.editMerchCollection(params, editMerchCollectionRequest, member))
.rejects.toThrow(ForbiddenError);
Expand Down Expand Up @@ -381,7 +381,7 @@ describe('merch items with options', () => {
test('monthly and lifetime remaining values are properly set when ordering different item options', async () => {
const conn = await DatabaseConnection.get();
const member = UserFactory.fake();
const optionMetadataType = faker.datatype.hexaDecimal(10);
const optionMetadataType = faker.string.hexadecimal({ length: 10});
const option1 = MerchFactory.fakeOptionWithType(optionMetadataType);
const option2 = MerchFactory.fakeOptionWithType(optionMetadataType);
const option3 = MerchFactory.fakeOptionWithType(optionMetadataType);
Expand Down Expand Up @@ -606,7 +606,7 @@ describe('merch item edits', () => {

// update the description and increment the purchase limits
const merchItemEdits: MerchItemEdit = {
description: faker.datatype.hexaDecimal(10),
description: faker.string.hexadecimal({ length: 10}),
monthlyLimit: item.monthlyLimit + 1,
lifetimeLimit: item.lifetimeLimit + 1,
};
Expand Down Expand Up @@ -639,7 +639,7 @@ describe('merch item edits', () => {

// update the description and increment the purchase limits
const merchItemEdits: MerchItemEdit = {
description: faker.datatype.hexaDecimal(10),
description: faker.string.hexadecimal({ length: 10}),
collection: collection.uuid,
};
const editMerchItemRequest = { merchandise: merchItemEdits };
Expand Down Expand Up @@ -784,7 +784,7 @@ describe('merch item edits', () => {
.write();

// change only one option's type to a different one
item.options[0].metadata.type = faker.datatype.hexaDecimal(10);
item.options[0].metadata.type = faker.string.hexadecimal({ length: 10});

const params = { uuid: item.uuid };
const editMerchItemRequest = { merchandise: { options: item.options } };
Expand All @@ -806,7 +806,7 @@ describe('merch item edits', () => {
const params = { uuid: item.uuid };

// change every option's type to a different but consistent one
const type = faker.datatype.hexaDecimal(10);
const type = faker.string.hexadecimal({ length: 10});
const updatedOptions = item.options.map((o) => MerchItemOptionRepository.merge(o, {
metadata: {
type,
Expand Down Expand Up @@ -901,7 +901,7 @@ describe('merch item options', () => {
.createMerchItem(item)
.write();

const optionWithDifferentType = MerchFactory.fakeOptionWithType(faker.datatype.hexaDecimal(10));
const optionWithDifferentType = MerchFactory.fakeOptionWithType(faker.string.hexadecimal({ length: 10}));

const params = { uuid: item.uuid };
const createMerchOptionRequest = { option: optionWithDifferentType };
Expand Down Expand Up @@ -930,7 +930,7 @@ describe('merch item options', () => {
await merchStoreController.deleteMerchItemOption(optionParams, admin);

// add option of another type
const optionWithDifferentType = MerchFactory.fakeOptionWithType(faker.datatype.hexaDecimal(10));
const optionWithDifferentType = MerchFactory.fakeOptionWithType(faker.string.hexadecimal({ length: 10}));
const createMerchOptionRequest = { option: optionWithDifferentType };
await merchStoreController.createMerchItemOption(itemParams, createMerchOptionRequest, admin);

Expand Down Expand Up @@ -1181,7 +1181,7 @@ describe('checkout cart', () => {
.write();

const validOptionUuids = options.map((o) => o.uuid);
const invalidOptionUuid = faker.datatype.uuid();
const invalidOptionUuid = faker.string.uuid();
const params = { items: [...validOptionUuids, invalidOptionUuid] };
const merchStoreController = ControllerFactory.merchStore(conn);
await expect(merchStoreController.getCartItems(params, member))
Expand Down
4 changes: 2 additions & 2 deletions tests/user.handles.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'reflect-metadata'; // this shim is required
import { plainToClass } from 'class-transformer';
import { validate } from 'class-validator';
import * as faker from 'faker';
import { faker } from '@faker-js/faker';
import { DatabaseConnection, PortalState, UserFactory } from './data';
import { UserPatches } from '../api/validators/UserControllerRequests';
import { ControllerFactory } from './controllers';
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('Update to Invalid User Handle', () => {

test('Update handle error: too long', async () => {
const MAX_LEN = 32;
const user = UserFactory.fake({ handle: faker.datatype.hexaDecimal(2 * MAX_LEN).toLowerCase() });
const user = UserFactory.fake({ handle: faker.string.hexadecimal({ length: 2 * MAX_LEN }).toLowerCase() });

const errors = await validate(plainToClass(UserPatches, { ...user,
passwordChange }));
Expand Down
4 changes: 2 additions & 2 deletions tests/userSocialMedia.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import faker = require('faker');
import { faker } from '@faker-js/faker';
import { DataSource } from 'typeorm';
import { DatabaseConnection, PortalState, UserFactory } from './data';
import { UserModel } from '../models/UserModel';
Expand Down Expand Up @@ -116,7 +116,7 @@ describe('social media URL update', () => {

const socialMediaParams = { socialMedia: [{
url: faker.internet.url(),
uuid: faker.datatype.uuid(),
uuid: faker.string.uuid(),
}] };
const errorMessage = `Social media of UUID "${socialMediaParams.socialMedia[0].uuid}" not found`;
await expect(userController.updateSocialMediaForUser(socialMediaParams, member))
Expand Down
Loading

0 comments on commit 840ad5e

Please sign in to comment.