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

HSB-431 fix: email case sensitive in email provider #4042

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ describe('AdminService', () => {
NOT: {
inviteeEmail: {
in: [dbAdminUsers[0].email],
mode: 'insensitive',
},
},
},
Expand Down Expand Up @@ -229,7 +230,10 @@ describe('AdminService', () => {

expect(mockPrisma.invitedUsers.deleteMany).toHaveBeenCalledWith({
where: {
inviteeEmail: { in: [invitedUsers[0].inviteeEmail] },
inviteeEmail: {
in: [invitedUsers[0].inviteeEmail],
mode: 'insensitive',
},
},
});
expect(result).toEqualRight(true);
Expand Down
12 changes: 9 additions & 3 deletions packages/hoppscotch-backend/src/admin/admin.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,17 @@ export class AdminService {
adminEmail: string,
inviteeEmail: string,
) {
if (inviteeEmail == adminEmail) return E.left(DUPLICATE_EMAIL);
if (inviteeEmail.toLowerCase() == adminEmail.toLowerCase()) {
return E.left(DUPLICATE_EMAIL);
}
if (!validateEmail(inviteeEmail)) return E.left(INVALID_EMAIL);

const alreadyInvitedUser = await this.prisma.invitedUsers.findFirst({
where: {
inviteeEmail: inviteeEmail,
inviteeEmail: {
equals: inviteeEmail,
mode: 'insensitive',
},
},
});
if (alreadyInvitedUser != null) return E.left(USER_ALREADY_INVITED);
Expand Down Expand Up @@ -159,7 +164,7 @@ export class AdminService {
try {
await this.prisma.invitedUsers.deleteMany({
where: {
inviteeEmail: { in: inviteeEmails },
inviteeEmail: { in: inviteeEmails, mode: 'insensitive' },
},
});
return E.right(true);
Expand Down Expand Up @@ -189,6 +194,7 @@ export class AdminService {
NOT: {
inviteeEmail: {
in: userEmailObjs.map((user) => user.email),
mode: 'insensitive',
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ export class ShortcodeService implements UserDataHandler, OnModuleInit {
where: userEmail
? {
User: {
email: userEmail,
email: {
equals: userEmail,
mode: 'insensitive',
},
},
}
: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ export class TeamInvitationService {
if (!isEmailValid) return E.left(INVALID_EMAIL);

try {
const teamInvite = await this.prisma.teamInvitation.findUniqueOrThrow({
const teamInvite = await this.prisma.teamInvitation.findFirstOrThrow({
where: {
teamID_inviteeEmail: {
inviteeEmail: inviteeEmail,
teamID: teamID,
inviteeEmail: {
equals: inviteeEmail,
mode: 'insensitive',
},
teamID,
},
});

Expand Down
4 changes: 2 additions & 2 deletions packages/hoppscotch-backend/src/user/user.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ beforeEach(() => {
describe('UserService', () => {
describe('findUserByEmail', () => {
test('should successfully return a valid user given a valid email', async () => {
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
mockPrisma.user.findFirst.mockResolvedValueOnce(user);
mirarifhasan marked this conversation as resolved.
Show resolved Hide resolved

const result = await userService.findUserByEmail(
'dwight@dundermifflin.com',
Expand All @@ -158,7 +158,7 @@ describe('UserService', () => {
});

test('should return a null user given a invalid email', async () => {
mockPrisma.user.findUniqueOrThrow.mockRejectedValueOnce('NotFoundError');
mockPrisma.user.findFirst.mockResolvedValueOnce(null);

const result = await userService.findUserByEmail('jim@dundermifflin.com');
expect(result).resolves.toBeNone;
Expand Down
18 changes: 9 additions & 9 deletions packages/hoppscotch-backend/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@ export class UserService {
* @returns Option of found User
*/
async findUserByEmail(email: string): Promise<O.None | O.Some<AuthUser>> {
try {
const user = await this.prisma.user.findUniqueOrThrow({
where: {
email: email,
const user = await this.prisma.user.findFirst({
where: {
email: {
equals: email,
mode: 'insensitive',
},
});
return O.some(user);
} catch (error) {
return O.none;
}
},
});
if (!user) return O.none;
return O.some(user);
}

/**
Expand Down