Files
hoppscotch/packages/hoppscotch-backend/src/user/user.service.spec.ts
Ankit Sridhar f78354a377 feat: Introducing Admin Module to Backend (HBE-83) (#21)
* feat: introducing admin module, resolvers and service files as a module

* feat: adding admin module in the app module

* feat: introducing admin guard and decorator for allowing admin operations

* feat: invited user model

* chore: added user invitation mail description to mailer service

* chore: added admin and user related error

* feat: added invited users as a new model in prisma

* chore: added admin related topics to pubsub

* chore: added service method to fetch all users from user table

* chore: added user deletion base implementation

* Revert "chore: added user deletion base implementation"

This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb.

* feat: adding team related operations to admin

* chore: adding admin related service methods to teams module service

* chore: adding admin related service methods to team coll invitations requests envs

* chore: added more module error messages

* chore: added admin check service method

* chore: added find individual user by UID in admin

* HBE-106 feat: introduced code to handle first time admin login setup (#23)

* test: wrote test cases for verifyAdmin route service method

* chore: added comments to verifyAdmin service method

* chore: deleted the prisma migration file

* chore: added find admin users

* feat: added user deletion into admin module

* chore: admin user related errors

* chore: fixed registry pattern in the shortcodes and teams to handle user deletion

* chore: add subscription topic for user deletion

* chore: updated user type in data handler

* feat: implement and fix user deletion

* feat: added make user admin mutation

* chore: added unit tests for admin specific service methods in admin module

* chore: added invitation not found error

* chore: added admin specific operation test cases in specific modules

* chore: added tests related to user deletion and admin related operation in user module

* chore: updated to error constant when invitations not found

* chore: fix rebase overwritten methods

* feat: implement remove user as admin

* chore: add new line

* feat: introducing basic metrics into the self-hosted admin module (HBE-104) (#43)

* feat: introducing admin module, resolvers and service files as a module

* feat: adding admin module in the app module

* feat: introducing admin guard and decorator for allowing admin operations

* feat: invited user model

* chore: added user invitation mail description to mailer service

* chore: added admin and user related error

* feat: added invited users as a new model in prisma

* chore: added admin related topics to pubsub

* chore: added service method to fetch all users from user table

* chore: added user deletion base implementation

* Revert "chore: added user deletion base implementation"

This reverts commit d1615ad83db2bae946e2d366a903d2f95051dabb.

* feat: adding team related operations to admin

* chore: adding admin related service methods to teams module service

* chore: adding admin related service methods to team coll invitations requests envs

* chore: added more module error messages

* chore: added admin check service method

* chore: added find individual user by UID in admin

* HBE-106 feat: introduced code to handle first time admin login setup (#23)

* test: wrote test cases for verifyAdmin route service method

* chore: added comments to verifyAdmin service method

* chore: deleted the prisma migration file

* chore: added find admin users

* feat: added user deletion into admin module

* chore: admin user related errors

* chore: fixed registry pattern in the shortcodes and teams to handle user deletion

* chore: add subscription topic for user deletion

* chore: updated user type in data handler

* feat: implement and fix user deletion

* feat: added make user admin mutation

* chore: added unit tests for admin specific service methods in admin module

* chore: added invitation not found error

* chore: added admin specific operation test cases in specific modules

* chore: added tests related to user deletion and admin related operation in user module

* chore: updated to error constant when invitations not found

* chore: fix rebase overwritten methods

* feat: implement remove user as admin

* chore: add new line

* chore: created new GQL return type for admin module

* chore: created resolver and service method for method to fetch org metrics

* chore: removed all entities relevant to seperate query for fetching admin metrics

* chore: created all resolvers for metrics

* feat: completed adding field resolves to query org metrics

* test: wrote tests for all metrics related methods in admin module

* test: added test cases for get count functions in multiple modules

* chore: removed prisma migration folder

* Delete backend-schema.gql

* chore: resolved merge conflicts in team test file

---------

Co-authored-by: ankitsridhar16 <ankit.sridhar16@gmail.com>

* refactor: update mailer service to stop using postmark (#38)

* refactor: update mailer service to stop using postmark

* chore: remove postmark as a dep and move out postmark code

* chore: remove postmark variables from .env.example

* chore: add formal errors for mailer initialization errors

* chore: add and update jsdoc comments in mailer service methods

* chore: added user invitation mail description to mailer service

* chore: updated with review changes requested for admin module

* feat: adding admin resolver to gql schema

* feat: adding input args for admin resolvers

* chore: invited user renamed

* chore: updated mailer service to be compatible with new mailer

* chore: updated team service with review changes

* chore: updated team collection service with review changes

* chore: updated team environments service with review changes

* chore: updated team requests service with review changes

* chore: updated user service with review changes

* refactor: invited user model

* chore: review changes implemented

* chore: implemented the review changes for admin, user and teams module

* chore: removed error handling and implemented review changes

* refactor: naming change for IsAdmin

---------

Co-authored-by: Balu Babu <balub997@gmail.com>
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
2023-03-21 16:42:30 +05:30

560 lines
17 KiB
TypeScript

import { JSON_INVALID, USER_NOT_FOUND } from 'src/errors';
import { mockDeep, mockReset } from 'jest-mock-extended';
import { PrismaService } from 'src/prisma/prisma.service';
import { AuthUser } from 'src/types/AuthUser';
import { User } from './user.model';
import { UserService } from './user.service';
import { PubSubService } from 'src/pubsub/pubsub.service';
import * as TO from 'fp-ts/TaskOption';
import * as T from 'fp-ts/Task';
const mockPrisma = mockDeep<PrismaService>();
const mockPubSub = mockDeep<PubSubService>();
let service: UserService;
const handler1 = {
canAllowUserDeletion: jest.fn(),
onUserDelete: jest.fn(),
};
const handler2 = {
canAllowUserDeletion: jest.fn(),
onUserDelete: jest.fn(),
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const userService = new UserService(mockPrisma, mockPubSub as any);
const currentTime = new Date();
const user: AuthUser = {
uid: '123344',
email: 'dwight@dundermifflin.com',
displayName: 'Dwight Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: false,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
};
const adminUser: AuthUser = {
uid: '123344',
email: 'dwight@dundermifflin.com',
displayName: 'Dwight Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: true,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
};
const users: AuthUser[] = [
{
uid: '123344',
email: 'dwight@dundermifflin.com',
displayName: 'Dwight Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: false,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
{
uid: '5555',
email: 'abc@dundermifflin.com',
displayName: 'abc Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: false,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
{
uid: '6666',
email: 'def@dundermifflin.com',
displayName: 'def Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: false,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
];
const adminUsers: AuthUser[] = [
{
uid: '123344',
email: 'dwight@dundermifflin.com',
displayName: 'Dwight Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: true,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
{
uid: '5555',
email: 'abc@dundermifflin.com',
displayName: 'abc Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: true,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
{
uid: '6666',
email: 'def@dundermifflin.com',
displayName: 'def Schrute',
photoURL: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
isAdmin: true,
currentRESTSession: {},
currentGQLSession: {},
refreshToken: 'hbfvdkhjbvkdvdfjvbnkhjb',
createdOn: currentTime,
},
];
const exampleSSOProfileData = {
id: '123rfedvd',
emails: [{ value: 'dwight@dundermifflin.com' }],
displayName: 'Dwight Schrute',
provider: 'google',
photos: 'https://en.wikipedia.org/wiki/Dwight_Schrute',
};
beforeEach(() => {
mockReset(mockPrisma);
mockPubSub.publish.mockClear();
service = new UserService(mockPrisma, mockPubSub as any);
service.registerUserDataHandler(handler1);
service.registerUserDataHandler(handler2);
});
describe('UserService', () => {
describe('findUserByEmail', () => {
test('should successfully return a valid user given a valid email', async () => {
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
const result = await userService.findUserByEmail(
'dwight@dundermifflin.com',
);
expect(result).toEqualSome(user);
});
test('should return a null user given a invalid email', async () => {
mockPrisma.user.findUniqueOrThrow.mockRejectedValueOnce('NotFoundError');
const result = await userService.findUserByEmail('jim@dundermifflin.com');
expect(result).resolves.toBeNone;
});
});
describe('findUserById', () => {
test('should successfully return a valid user given a valid user uid', async () => {
mockPrisma.user.findUniqueOrThrow.mockResolvedValueOnce(user);
const result = await userService.findUserById('123344');
expect(result).toEqualSome(user);
});
test('should return a null user given a invalid user uid', async () => {
mockPrisma.user.findUniqueOrThrow.mockRejectedValueOnce('NotFoundError');
const result = await userService.findUserById('sdcvbdbr');
expect(result).resolves.toBeNone;
});
});
describe('createUserViaMagicLink', () => {
test('should successfully create user and account for magic-link given valid inputs', async () => {
mockPrisma.user.create.mockResolvedValueOnce(user);
const result = await userService.createUserViaMagicLink(
'dwight@dundermifflin.com',
);
expect(result).toEqual(user);
});
});
describe('createUserSSO', () => {
test('should successfully create user and account for SSO provider given valid inputs ', async () => {
mockPrisma.user.create.mockResolvedValueOnce(user);
const result = await userService.createUserSSO(
'sdcsdcsdc',
'dscsdc',
exampleSSOProfileData,
);
expect(result).toEqual(user);
});
test('should successfully create user and account for SSO provider given no displayName ', async () => {
mockPrisma.user.create.mockResolvedValueOnce({
...user,
displayName: null,
});
const result = await userService.createUserSSO('sdcsdcsdc', 'dscsdc', {
...exampleSSOProfileData,
displayName: null,
});
expect(result).toEqual({
...user,
displayName: null,
});
});
test('should successfully create user and account for SSO provider given no photoURL ', async () => {
mockPrisma.user.create.mockResolvedValueOnce({
...user,
photoURL: null,
});
const result = await userService.createUserSSO('sdcsdcsdc', 'dscsdc', {
...exampleSSOProfileData,
photoURL: null,
});
expect(result).toEqual({
...user,
photoURL: null,
});
});
});
describe('createProviderAccount', () => {
test('should successfully create ProviderAccount for user given valid inputs ', async () => {
mockPrisma.account.create.mockResolvedValueOnce({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: 'dscsdc',
providerAccessToken: 'sdcsdcsdc',
providerScope: 'user.email',
loggedIn: currentTime,
});
const result = await userService.createProviderAccount(
user,
'sdcsdcsdc',
'dscsdc',
exampleSSOProfileData,
);
expect(result).toEqual({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: 'dscsdc',
providerAccessToken: 'sdcsdcsdc',
providerScope: 'user.email',
loggedIn: currentTime,
});
});
test('should successfully create ProviderAccount for user given no accessToken ', async () => {
mockPrisma.account.create.mockResolvedValueOnce({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: 'dscsdc',
providerAccessToken: null,
providerScope: 'user.email',
loggedIn: currentTime,
});
const result = await userService.createProviderAccount(
user,
'sdcsdcsdc',
'dscsdc',
exampleSSOProfileData,
);
expect(result).toEqual({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: 'dscsdc',
providerAccessToken: null,
providerScope: 'user.email',
loggedIn: currentTime,
});
});
test('should successfully create ProviderAccount for user given no refreshToken', async () => {
mockPrisma.account.create.mockResolvedValueOnce({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: null,
providerAccessToken: 'sdcsdcsdc',
providerScope: 'user.email',
loggedIn: currentTime,
});
const result = await userService.createProviderAccount(
user,
'sdcsdcsdc',
'dscsdc',
exampleSSOProfileData,
);
expect(result).toEqual({
id: '123dcdc',
userId: user.uid,
provider: exampleSSOProfileData.provider,
providerAccountId: exampleSSOProfileData.id,
providerRefreshToken: null,
providerAccessToken: 'sdcsdcsdc',
providerScope: 'user.email',
loggedIn: currentTime,
});
});
});
describe('updateUserSessions', () => {
test('Should resolve right and update users GQL session', async () => {
const sessionData = user.currentGQLSession;
mockPrisma.user.update.mockResolvedValue({
...user,
currentGQLSession: sessionData,
currentRESTSession: null,
});
const result = await userService.updateUserSessions(
user,
JSON.stringify(sessionData),
'GQL',
);
expect(result).toEqualRight({
...user,
currentGQLSession: JSON.stringify(sessionData),
currentRESTSession: null,
});
});
test('Should resolve right and update users REST session', async () => {
const sessionData = user.currentGQLSession;
mockPrisma.user.update.mockResolvedValue({
...user,
currentGQLSession: null,
currentRESTSession: sessionData,
});
const result = await userService.updateUserSessions(
user,
JSON.stringify(sessionData),
'REST',
);
expect(result).toEqualRight({
...user,
currentGQLSession: null,
currentRESTSession: JSON.stringify(sessionData),
});
});
test('Should reject left and update user for invalid GQL session', async () => {
const sessionData = 'invalid json';
const result = await userService.updateUserSessions(
user,
sessionData,
'GQL',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should reject left and update user for invalid REST session', async () => {
const sessionData = 'invalid json';
const result = await userService.updateUserSessions(
user,
sessionData,
'REST',
);
expect(result).toEqualLeft(JSON_INVALID);
});
test('Should publish pubsub message on user update sessions', async () => {
mockPrisma.user.update.mockResolvedValue({
...user,
});
await userService.updateUserSessions(
user,
JSON.stringify(user.currentGQLSession),
'GQL',
);
expect(mockPubSub.publish).toHaveBeenCalledTimes(1);
expect(mockPubSub.publish).toHaveBeenCalledWith(
`user/${user.uid}/updated`,
{
...user,
currentGQLSession: JSON.stringify(user.currentGQLSession),
currentRESTSession: JSON.stringify(user.currentRESTSession),
},
);
});
});
describe('fetchAllUsers', () => {
test('should resolve right and return 20 users when cursor is null', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(users);
const result = await userService.fetchAllUsers(null, 20);
expect(result).toEqual(users);
});
test('should resolve right and return next 20 users when cursor is provided', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(users);
const result = await userService.fetchAllUsers('123344', 20);
expect(result).toEqual(users);
});
test('should resolve left and return an error when users not found', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce([]);
const result = await userService.fetchAllUsers(null, 20);
expect(result).toEqual([]);
});
});
describe('fetchAdminUsers', () => {
test('should return a list of admin users', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(adminUsers);
const result = await userService.fetchAdminUsers();
expect(mockPrisma.user.findMany).toHaveBeenCalledWith({
where: {
isAdmin: true,
},
});
expect(result).toEqual(adminUsers);
});
test('should return null when no admin users found', async () => {
mockPrisma.user.findMany.mockResolvedValueOnce(null);
const result = await userService.fetchAdminUsers();
expect(mockPrisma.user.findMany).toHaveBeenCalledWith({
where: {
isAdmin: true,
},
});
expect(result).toEqual(null);
});
});
describe('makeAdmin', () => {
test('should resolve right and return a user object after making a user admin', async () => {
mockPrisma.user.update.mockResolvedValueOnce(adminUser);
const result = await userService.makeAdmin(user.uid);
expect(mockPrisma.user.update).toHaveBeenCalledWith({
where: {
uid: user.uid,
},
data: {
isAdmin: true,
},
});
expect(result).toEqualRight(adminUser);
});
test('should resolve left and error when invalid user uid is passed', async () => {
mockPrisma.user.update.mockRejectedValueOnce('NotFoundError');
const result = await userService.makeAdmin(user.uid);
expect(mockPrisma.user.update).toHaveBeenCalledWith({
where: {
uid: user.uid,
},
data: {
isAdmin: true,
},
});
expect(result).toEqualLeft(USER_NOT_FOUND);
});
});
describe('deleteUserByID', () => {
test('should resolve right for valid user uid and perform successful user deletion', () => {
// For a successful deletion, the handlers should allow user deletion
handler1.canAllowUserDeletion.mockImplementation(() => TO.none);
handler2.canAllowUserDeletion.mockImplementation(() => TO.none);
handler1.onUserDelete.mockImplementation(() => T.of(undefined));
handler2.onUserDelete.mockImplementation(() => T.of(undefined));
mockPrisma.user.delete.mockResolvedValueOnce(user);
const result = service.deleteUserByUID(user)();
return expect(result).resolves.toBeRight();
});
test('should resolve right for successful deletion and publish user deleted subscription', async () => {
// For a successful deletion, the handlers should allow user deletion
handler1.canAllowUserDeletion.mockImplementation(() => TO.none);
handler2.canAllowUserDeletion.mockImplementation(() => TO.none);
handler1.onUserDelete.mockImplementation(() => T.of(undefined));
handler2.onUserDelete.mockImplementation(() => T.of(undefined));
mockPrisma.user.delete.mockResolvedValueOnce(user);
const result = service.deleteUserByUID(user)();
await expect(result).resolves.toBeRight();
// fire the subscription for user deletion
expect(mockPubSub.publish).toHaveBeenCalledWith(
`user/${user.uid}/deleted`,
<User>{
uid: user.uid,
displayName: user.displayName,
email: user.email,
photoURL: user.photoURL,
isAdmin: user.isAdmin,
currentRESTSession: user.currentRESTSession,
currentGQLSession: user.currentGQLSession,
createdOn: user.createdOn,
},
);
});
test("should resolve left when one or both the handlers don't allow userDeletion", () => {
// Handlers don't allow user deletion
handler1.canAllowUserDeletion.mockImplementation(() => TO.some);
handler2.canAllowUserDeletion.mockImplementation(() => TO.some);
const result = service.deleteUserByUID(user)();
return expect(result).resolves.toBeLeft();
});
test('should resolve left when ther is an unsuccessful deletion of userdata from firestore', () => {
// Handlers allow deletion to proceed
handler1.canAllowUserDeletion.mockImplementation(() => TO.none);
handler2.canAllowUserDeletion.mockImplementation(() => TO.none);
handler1.onUserDelete.mockImplementation(() => T.of(undefined));
handler2.onUserDelete.mockImplementation(() => T.of(undefined));
// Deleting users errors out
mockPrisma.user.delete.mockRejectedValueOnce('NotFoundError');
const result = service.deleteUserByUID(user)();
return expect(result).resolves.toBeLeft();
});
});
describe('getUsersCount', () => {
test('should return count of all users in the organization', async () => {
mockPrisma.user.count.mockResolvedValueOnce(10);
const result = await userService.getUsersCount();
expect(result).toEqual(10);
});
});
});