Compare commits
11 Commits
feat/seed
...
test/backe
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c188f865a2 | ||
|
|
a2a675dd86 | ||
|
|
b867ba9139 | ||
|
|
d2ca631492 | ||
|
|
39a4fd8ab2 | ||
|
|
76d52a3b05 | ||
|
|
b83cc38a1c | ||
|
|
db42073d42 | ||
|
|
6c928e72d4 | ||
|
|
ddd0a67da3 | ||
|
|
295304feeb |
@@ -10,11 +10,23 @@ import { TeamInvitationService } from '../team-invitation/team-invitation.servic
|
||||
import { TeamCollectionService } from '../team-collection/team-collection.service';
|
||||
import { MailerService } from '../mailer/mailer.service';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { User as DbUser } from '@prisma/client';
|
||||
import {
|
||||
DUPLICATE_EMAIL,
|
||||
INVALID_EMAIL,
|
||||
ONLY_ONE_ADMIN_ACCOUNT,
|
||||
TEAM_INVITE_ALREADY_MEMBER,
|
||||
TEAM_MEMBER_NOT_FOUND,
|
||||
USER_ALREADY_INVITED,
|
||||
USER_IS_ADMIN,
|
||||
USER_NOT_FOUND,
|
||||
} from '../errors';
|
||||
import { Team, TeamMember, TeamMemberRole } from 'src/team/team.model';
|
||||
import { TeamInvitation } from 'src/team-invitation/team-invitation.model';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import * as O from 'fp-ts/Option';
|
||||
import * as TE from 'fp-ts/TaskEither';
|
||||
import * as utils from 'src/utils';
|
||||
|
||||
const mockPrisma = mockDeep<PrismaService>();
|
||||
const mockPubSub = mockDeep<PubSubService>();
|
||||
@@ -52,7 +64,582 @@ const invitedUsers: InvitedUsers[] = [
|
||||
invitedOn: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
const allUsers: DbUser[] = [
|
||||
{
|
||||
uid: 'uid1',
|
||||
displayName: 'user1',
|
||||
email: 'user1@hoppscotch.io',
|
||||
photoURL: 'https://hoppscotch.io',
|
||||
isAdmin: true,
|
||||
refreshToken: 'refreshToken',
|
||||
currentRESTSession: null,
|
||||
currentGQLSession: null,
|
||||
createdOn: new Date(),
|
||||
},
|
||||
{
|
||||
uid: 'uid2',
|
||||
displayName: 'user2',
|
||||
email: 'user2@hoppscotch.io',
|
||||
photoURL: 'https://hoppscotch.io',
|
||||
isAdmin: false,
|
||||
refreshToken: 'refreshToken',
|
||||
currentRESTSession: null,
|
||||
currentGQLSession: null,
|
||||
createdOn: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
const teamMembers: TeamMember[] = [
|
||||
{
|
||||
membershipID: 'teamMember1',
|
||||
userUid: allUsers[0].uid,
|
||||
role: TeamMemberRole.OWNER,
|
||||
},
|
||||
];
|
||||
|
||||
const teams: Team[] = [
|
||||
{
|
||||
id: 'team1',
|
||||
name: 'team1',
|
||||
},
|
||||
{
|
||||
id: 'team2',
|
||||
name: 'team2',
|
||||
},
|
||||
];
|
||||
|
||||
const teamInvitations: TeamInvitation[] = [
|
||||
{
|
||||
id: 'teamInvitation1',
|
||||
teamID: 'team1',
|
||||
creatorUid: 'uid1',
|
||||
inviteeEmail: '',
|
||||
inviteeRole: TeamMemberRole.OWNER,
|
||||
},
|
||||
];
|
||||
|
||||
describe('AdminService', () => {
|
||||
describe('fetchUsers', () => {
|
||||
test('should resolve right and return an array of users if cursorID is null', async () => {
|
||||
mockUserService.fetchAllUsers.mockResolvedValueOnce(allUsers);
|
||||
|
||||
const result = await adminService.fetchUsers(null, 10);
|
||||
|
||||
expect(result).toEqual(allUsers);
|
||||
expect(mockUserService.fetchAllUsers).toHaveBeenCalledWith(null, 10);
|
||||
});
|
||||
test('should resolve right and return an array of users if cursorID is not null', async () => {
|
||||
mockUserService.fetchAllUsers.mockResolvedValueOnce([allUsers[1]]);
|
||||
|
||||
const cursorID = allUsers[0].uid;
|
||||
const result = await adminService.fetchUsers(cursorID, 10);
|
||||
|
||||
expect(result).toEqual([allUsers[1]]);
|
||||
expect(mockUserService.fetchAllUsers).toHaveBeenCalledWith(cursorID, 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchAllTeams', () => {
|
||||
test('should resolve right and return an array of teams if cursorID is null', async () => {
|
||||
mockTeamService.fetchAllTeams.mockResolvedValueOnce(teams);
|
||||
|
||||
const result = await adminService.fetchAllTeams(null, 10);
|
||||
|
||||
expect(result).toEqual(teams);
|
||||
expect(mockTeamService.fetchAllTeams).toHaveBeenCalledWith(null, 10);
|
||||
});
|
||||
test('should resolve right and return an array of teams if cursorID is not null', async () => {
|
||||
mockTeamService.fetchAllTeams.mockResolvedValueOnce([teams[1]]);
|
||||
|
||||
const cursorID = teams[0].id;
|
||||
const result = await adminService.fetchAllTeams(cursorID, 10);
|
||||
|
||||
expect(result).toEqual([teams[1]]);
|
||||
expect(mockTeamService.fetchAllTeams).toHaveBeenCalledWith(cursorID, 10);
|
||||
});
|
||||
});
|
||||
|
||||
describe('membersCountInTeam', () => {
|
||||
test('should resolve right and return the count of members in a team', async () => {
|
||||
mockTeamService.getCountOfMembersInTeam.mockResolvedValueOnce(10);
|
||||
|
||||
const result = await adminService.membersCountInTeam('team1');
|
||||
|
||||
expect(result).toEqual(10);
|
||||
expect(mockTeamService.getCountOfMembersInTeam).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('collectionCountInTeam', () => {
|
||||
test('should resolve right and return the count of collections in a team', async () => {
|
||||
mockTeamCollectionService.totalCollectionsInTeam.mockResolvedValueOnce(
|
||||
10,
|
||||
);
|
||||
|
||||
const result = await adminService.collectionCountInTeam('team1');
|
||||
|
||||
expect(result).toEqual(10);
|
||||
expect(
|
||||
mockTeamCollectionService.totalCollectionsInTeam,
|
||||
).toHaveBeenCalledWith('team1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('requestCountInTeam', () => {
|
||||
test('should resolve right and return the count of requests in a team', async () => {
|
||||
mockTeamRequestService.totalRequestsInATeam.mockResolvedValueOnce(10);
|
||||
|
||||
const result = await adminService.requestCountInTeam('team1');
|
||||
|
||||
expect(result).toEqual(10);
|
||||
expect(mockTeamRequestService.totalRequestsInATeam).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('environmentCountInTeam', () => {
|
||||
test('should resolve right and return the count of environments in a team', async () => {
|
||||
mockTeamEnvironmentsService.totalEnvsInTeam.mockResolvedValueOnce(10);
|
||||
|
||||
const result = await adminService.environmentCountInTeam('team1');
|
||||
|
||||
expect(result).toEqual(10);
|
||||
expect(mockTeamEnvironmentsService.totalEnvsInTeam).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('pendingInvitationCountInTeam', () => {
|
||||
test('should resolve right and return the count of pending invitations in a team', async () => {
|
||||
mockTeamInvitationService.getTeamInvitations.mockResolvedValueOnce(
|
||||
teamInvitations,
|
||||
);
|
||||
|
||||
const result = await adminService.pendingInvitationCountInTeam('team1');
|
||||
|
||||
expect(result).toEqual(teamInvitations);
|
||||
expect(
|
||||
mockTeamInvitationService.getTeamInvitations,
|
||||
).toHaveBeenCalledWith('team1');
|
||||
});
|
||||
});
|
||||
|
||||
describe('changeRoleOfUserTeam', () => {
|
||||
test('should resolve right and return the count of pending invitations in a team', async () => {
|
||||
const teamMember = teamMembers[0];
|
||||
|
||||
mockTeamService.updateTeamMemberRole.mockResolvedValueOnce(
|
||||
E.right(teamMember),
|
||||
);
|
||||
|
||||
const result = await adminService.changeRoleOfUserTeam(
|
||||
teamMember.userUid,
|
||||
'team1',
|
||||
teamMember.role,
|
||||
);
|
||||
|
||||
expect(result).toEqualRight(teamMember);
|
||||
expect(mockTeamService.updateTeamMemberRole).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
teamMember.userUid,
|
||||
teamMember.role,
|
||||
);
|
||||
});
|
||||
|
||||
test('should resolve left and return the error if any error occurred', async () => {
|
||||
const teamMember = teamMembers[0];
|
||||
const errorMessage = 'Team member not found';
|
||||
|
||||
mockTeamService.updateTeamMemberRole.mockResolvedValueOnce(
|
||||
E.left(errorMessage),
|
||||
);
|
||||
|
||||
const result = await adminService.changeRoleOfUserTeam(
|
||||
teamMember.userUid,
|
||||
'team1',
|
||||
teamMember.role,
|
||||
);
|
||||
|
||||
expect(result).toEqualLeft(errorMessage);
|
||||
expect(mockTeamService.updateTeamMemberRole).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
teamMember.userUid,
|
||||
teamMember.role,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeUserFromTeam', () => {
|
||||
test('should resolve right and remove user from a team', async () => {
|
||||
const teamMember = teamMembers[0];
|
||||
|
||||
mockTeamService.leaveTeam.mockResolvedValueOnce(E.right(true));
|
||||
|
||||
const result = await adminService.removeUserFromTeam(
|
||||
teamMember.userUid,
|
||||
'team1',
|
||||
);
|
||||
|
||||
expect(result).toEqualRight(true);
|
||||
expect(mockTeamService.leaveTeam).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
teamMember.userUid,
|
||||
);
|
||||
});
|
||||
|
||||
test('should resolve left and return the error if any error occurred', async () => {
|
||||
const teamMember = teamMembers[0];
|
||||
const errorMessage = 'Team member not found';
|
||||
|
||||
mockTeamService.leaveTeam.mockResolvedValueOnce(E.left(errorMessage));
|
||||
|
||||
const result = await adminService.removeUserFromTeam(
|
||||
teamMember.userUid,
|
||||
'team1',
|
||||
);
|
||||
|
||||
expect(result).toEqualLeft(errorMessage);
|
||||
expect(mockTeamService.leaveTeam).toHaveBeenCalledWith(
|
||||
'team1',
|
||||
teamMember.userUid,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('addUserToTeam', () => {
|
||||
test('should return INVALID_EMAIL when email is invalid', async () => {
|
||||
const teamID = 'team1';
|
||||
const userEmail = 'invalidEmail';
|
||||
const role = TeamMemberRole.EDITOR;
|
||||
|
||||
const mockValidateEmail = jest.spyOn(utils, 'validateEmail');
|
||||
mockValidateEmail.mockReturnValueOnce(false);
|
||||
|
||||
const result = await adminService.addUserToTeam(teamID, userEmail, role);
|
||||
|
||||
expect(result).toEqual(E.left(INVALID_EMAIL));
|
||||
expect(mockValidateEmail).toHaveBeenCalledWith(userEmail);
|
||||
expect(mockUserService.findUserByEmail).not.toHaveBeenCalled();
|
||||
expect(mockTeamService.getTeamMemberTE).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should return USER_NOT_FOUND when user is not found', async () => {
|
||||
const teamID = 'team1';
|
||||
const userEmail = 'u@example.com';
|
||||
const role = TeamMemberRole.EDITOR;
|
||||
|
||||
const mockValidateEmail = jest.spyOn(utils, 'validateEmail');
|
||||
mockValidateEmail.mockReturnValueOnce(true);
|
||||
mockUserService.findUserByEmail.mockResolvedValue(O.none);
|
||||
|
||||
const result = await adminService.addUserToTeam(teamID, userEmail, role);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
expect(mockValidateEmail).toHaveBeenCalledWith(userEmail);
|
||||
});
|
||||
|
||||
test('should return TEAM_INVITE_ALREADY_MEMBER when user is already a member of the team', async () => {
|
||||
const teamID = 'team1';
|
||||
const userEmail = allUsers[0].email;
|
||||
const role = TeamMemberRole.EDITOR;
|
||||
|
||||
const mockValidateEmail = jest.spyOn(utils, 'validateEmail');
|
||||
mockValidateEmail.mockReturnValueOnce(true);
|
||||
mockUserService.findUserByEmail.mockResolvedValueOnce(
|
||||
O.some(allUsers[0]),
|
||||
);
|
||||
mockTeamService.getTeamMemberTE.mockReturnValueOnce(
|
||||
TE.right(teamMembers[0]),
|
||||
);
|
||||
|
||||
const result = await adminService.addUserToTeam(teamID, userEmail, role);
|
||||
|
||||
expect(result).toEqual(E.left(TEAM_INVITE_ALREADY_MEMBER));
|
||||
expect(mockValidateEmail).toHaveBeenCalledWith(userEmail);
|
||||
expect(mockUserService.findUserByEmail).toHaveBeenCalledWith(userEmail);
|
||||
expect(mockTeamService.getTeamMemberTE).toHaveBeenCalledWith(
|
||||
teamID,
|
||||
allUsers[0].uid,
|
||||
);
|
||||
});
|
||||
|
||||
test('should add user to the team and return the result when user is not a member of the team', async () => {
|
||||
const teamID = 'team1';
|
||||
const userEmail = allUsers[0].email;
|
||||
const role = TeamMemberRole.EDITOR;
|
||||
|
||||
const mockValidateEmail = jest.spyOn(utils, 'validateEmail');
|
||||
mockValidateEmail.mockReturnValueOnce(true);
|
||||
mockUserService.findUserByEmail.mockResolvedValueOnce(
|
||||
O.some(allUsers[0]),
|
||||
);
|
||||
mockTeamService.getTeamMemberTE.mockReturnValueOnce(
|
||||
TE.left(TEAM_MEMBER_NOT_FOUND),
|
||||
);
|
||||
mockTeamService.addMemberToTeamWithEmail.mockResolvedValueOnce(
|
||||
E.right(teamMembers[0]),
|
||||
);
|
||||
mockTeamInvitationService.getTeamInviteByEmailAndTeamID.mockResolvedValueOnce(
|
||||
E.right(teamInvitations[0])
|
||||
);
|
||||
|
||||
const result = await adminService.addUserToTeam(teamID, userEmail, role);
|
||||
|
||||
expect(result).toEqual(E.right(teamMembers[0]));
|
||||
expect(mockValidateEmail).toHaveBeenCalledWith(userEmail);
|
||||
expect(mockUserService.findUserByEmail).toHaveBeenCalledWith(userEmail);
|
||||
expect(mockTeamService.getTeamMemberTE).toHaveBeenCalledWith(
|
||||
teamID,
|
||||
allUsers[0].uid,
|
||||
);
|
||||
expect(mockTeamService.addMemberToTeamWithEmail).toHaveBeenCalledWith(
|
||||
teamID,
|
||||
allUsers[0].email,
|
||||
role,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('createATeam', () => {
|
||||
test('should return USER_NOT_FOUND when user is not found', async () => {
|
||||
const userUid = allUsers[0].uid;
|
||||
const teamName = 'team1';
|
||||
|
||||
mockUserService.findUserById.mockResolvedValue(O.none);
|
||||
|
||||
const result = await adminService.createATeam(userUid, teamName);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
expect(mockUserService.findUserById).toHaveBeenCalledWith(userUid);
|
||||
expect(mockTeamService.createTeam).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should create a team and return the result when the team is created successfully', async () => {
|
||||
const user = allUsers[0];
|
||||
const team = teams[0];
|
||||
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
mockTeamService.createTeam.mockResolvedValueOnce(E.right(team));
|
||||
|
||||
const result = await adminService.createATeam(user.uid, team.name);
|
||||
|
||||
expect(result).toEqual(E.right(team));
|
||||
expect(mockUserService.findUserById).toHaveBeenCalledWith(user.uid);
|
||||
expect(mockTeamService.createTeam).toHaveBeenCalledWith(
|
||||
team.name,
|
||||
user.uid,
|
||||
);
|
||||
});
|
||||
|
||||
test('should return the error when the team creation fails', async () => {
|
||||
const user = allUsers[0];
|
||||
const team = teams[0];
|
||||
const errorMessage = 'error';
|
||||
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
mockTeamService.createTeam.mockResolvedValueOnce(E.left(errorMessage));
|
||||
|
||||
const result = await adminService.createATeam(user.uid, team.name);
|
||||
|
||||
expect(result).toEqual(E.left(errorMessage));
|
||||
expect(mockUserService.findUserById).toHaveBeenCalledWith(user.uid);
|
||||
expect(mockTeamService.createTeam).toHaveBeenCalledWith(
|
||||
team.name,
|
||||
user.uid,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('renameATeam', () => {
|
||||
test('should rename a team and return the result when the team is renamed successfully', async () => {
|
||||
const team = teams[0];
|
||||
const newName = 'new name';
|
||||
|
||||
mockTeamService.renameTeam.mockResolvedValueOnce(E.right(team));
|
||||
|
||||
const result = await adminService.renameATeam(team.id, newName);
|
||||
|
||||
expect(result).toEqual(E.right(team));
|
||||
expect(mockTeamService.renameTeam).toHaveBeenCalledWith(team.id, newName);
|
||||
});
|
||||
|
||||
test('should return the error when the team renaming fails', async () => {
|
||||
const team = teams[0];
|
||||
const newName = 'new name';
|
||||
const errorMessage = 'error';
|
||||
|
||||
mockTeamService.renameTeam.mockResolvedValueOnce(E.left(errorMessage));
|
||||
|
||||
const result = await adminService.renameATeam(team.id, newName);
|
||||
|
||||
expect(result).toEqual(E.left(errorMessage));
|
||||
expect(mockTeamService.renameTeam).toHaveBeenCalledWith(team.id, newName);
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteATeam', () => {
|
||||
test('should delete a team and return the result when the team is deleted successfully', async () => {
|
||||
const team = teams[0];
|
||||
|
||||
mockTeamService.deleteTeam.mockResolvedValueOnce(E.right(true));
|
||||
|
||||
const result = await adminService.deleteATeam(team.id);
|
||||
|
||||
expect(result).toEqual(E.right(true));
|
||||
expect(mockTeamService.deleteTeam).toHaveBeenCalledWith(team.id);
|
||||
});
|
||||
|
||||
test('should return the error when the team deletion fails', async () => {
|
||||
const team = teams[0];
|
||||
const errorMessage = 'error';
|
||||
|
||||
mockTeamService.deleteTeam.mockResolvedValueOnce(E.left(errorMessage));
|
||||
|
||||
const result = await adminService.deleteATeam(team.id);
|
||||
|
||||
expect(result).toEqual(E.left(errorMessage));
|
||||
expect(mockTeamService.deleteTeam).toHaveBeenCalledWith(team.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchAdmins', () => {
|
||||
test('should return the list of admin users', async () => {
|
||||
const adminUsers = [];
|
||||
mockUserService.fetchAdminUsers.mockResolvedValueOnce(adminUsers);
|
||||
const result = await adminService.fetchAdmins();
|
||||
|
||||
expect(result).toEqual(adminUsers);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchUserInfo', () => {
|
||||
test('should return the user info when the user is found', async () => {
|
||||
const user = allUsers[0];
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
const result = await adminService.fetchUserInfo(user.uid);
|
||||
|
||||
expect(result).toEqual(E.right(user));
|
||||
});
|
||||
|
||||
test('should return USER_NOT_FOUND when the user is not found', async () => {
|
||||
const user = allUsers[0];
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.none);
|
||||
const result = await adminService.fetchUserInfo(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeUserAccount', () => {
|
||||
test('should return USER_NOT_FOUND when the user is not found', async () => {
|
||||
const user = allUsers[0];
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.none);
|
||||
const result = await adminService.removeUserAccount(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
});
|
||||
|
||||
test('should return USER_IS_ADMIN when the user is an admin', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
const result = await adminService.removeUserAccount(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(USER_IS_ADMIN));
|
||||
});
|
||||
|
||||
test('should remove the user account and return the result when the user is not an admin', async () => {
|
||||
const user = allUsers[1];
|
||||
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
mockUserService.deleteUserByUID.mockReturnValueOnce(TE.right(true));
|
||||
const result = await adminService.removeUserAccount(user.uid);
|
||||
|
||||
expect(result).toEqual(E.right(true));
|
||||
});
|
||||
|
||||
test('should return the error when the user account deletion fails', async () => {
|
||||
const user = allUsers[1];
|
||||
const errorMessage = 'error';
|
||||
|
||||
mockUserService.findUserById.mockResolvedValueOnce(O.some(user));
|
||||
mockUserService.deleteUserByUID.mockReturnValueOnce(
|
||||
TE.left(errorMessage),
|
||||
);
|
||||
const result = await adminService.removeUserAccount(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(errorMessage));
|
||||
});
|
||||
});
|
||||
|
||||
describe('makeUserAdmin', () => {
|
||||
test('should make the user an admin and return true when the operation is successful', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.makeAdmin.mockResolvedValueOnce(E.right(user));
|
||||
const result = await adminService.makeUserAdmin(user.uid);
|
||||
|
||||
expect(result).toEqual(E.right(true));
|
||||
});
|
||||
|
||||
test('should return the error when making the user an admin fails', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.makeAdmin.mockResolvedValueOnce(E.left(USER_NOT_FOUND));
|
||||
const result = await adminService.makeUserAdmin(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
});
|
||||
});
|
||||
|
||||
describe('removeUserAsAdmin', () => {
|
||||
test('should return ONLY_ONE_ADMIN_ACCOUNT when there is only one admin account', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.fetchAdminUsers.mockResolvedValueOnce([user]);
|
||||
const result = await adminService.removeUserAsAdmin(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(ONLY_ONE_ADMIN_ACCOUNT));
|
||||
});
|
||||
|
||||
test('should remove the user as an admin and return true when the operation is successful', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.fetchAdminUsers.mockResolvedValueOnce(allUsers);
|
||||
mockUserService.removeUserAsAdmin.mockResolvedValueOnce(E.right(user));
|
||||
const result = await adminService.removeUserAsAdmin(user.uid);
|
||||
|
||||
expect(result).toEqual(E.right(true));
|
||||
});
|
||||
|
||||
test('should return the error when removing the user as an admin fails', async () => {
|
||||
const user = allUsers[0];
|
||||
|
||||
mockUserService.fetchAdminUsers.mockResolvedValueOnce(allUsers);
|
||||
mockUserService.removeUserAsAdmin.mockResolvedValueOnce(
|
||||
E.left(USER_NOT_FOUND),
|
||||
);
|
||||
const result = await adminService.removeUserAsAdmin(user.uid);
|
||||
|
||||
expect(result).toEqual(E.left(USER_NOT_FOUND));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTeamInfo', () => {
|
||||
test('should return the team info when the team is found', async () => {
|
||||
const team = teams[0];
|
||||
mockTeamService.getTeamWithIDTE.mockReturnValue(TE.right(team));
|
||||
const result = await adminService.getTeamInfo(team.id);
|
||||
|
||||
expect(result).toEqual(E.right(team));
|
||||
});
|
||||
});
|
||||
|
||||
describe('fetchInvitedUsers', () => {
|
||||
test('should resolve right and return an array of invited users', async () => {
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
|
||||
@@ -240,6 +240,7 @@ export class AdminService {
|
||||
teamID,
|
||||
user.value.uid,
|
||||
)();
|
||||
|
||||
if (E.isLeft(teamMember)) {
|
||||
const addedUser = await this.teamService.addMemberToTeamWithEmail(
|
||||
teamID,
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { Team, TeamCollection as DBTeamCollection } from '@prisma/client';
|
||||
import { mock, mockDeep, mockReset } from 'jest-mock-extended';
|
||||
import {
|
||||
Team,
|
||||
TeamCollection as DBTeamCollection,
|
||||
TeamRequest as DBTeamRequest,
|
||||
} from '@prisma/client';
|
||||
import { mockDeep, mockReset } from 'jest-mock-extended';
|
||||
import {
|
||||
TEAM_COLL_DEST_SAME,
|
||||
TEAM_COLL_INVALID_JSON,
|
||||
@@ -17,9 +21,8 @@ import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||
import { AuthUser } from 'src/types/AuthUser';
|
||||
import { TeamCollectionService } from './team-collection.service';
|
||||
import { TeamCollection } from './team-collection.model';
|
||||
import { TeamCollectionModule } from './team-collection.module';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import { CollectionFolder } from 'src/types/CollectionFolder';
|
||||
|
||||
const mockPrisma = mockDeep<PrismaService>();
|
||||
const mockPubSub = mockDeep<PubSubService>();
|
||||
@@ -276,11 +279,188 @@ const childTeamCollectionList: DBTeamCollection[] = [
|
||||
},
|
||||
];
|
||||
|
||||
const teamRequestList: DBTeamRequest[] = [
|
||||
{
|
||||
id: 'req1',
|
||||
collectionID: childTeamCollection.id,
|
||||
teamID: team.id,
|
||||
title: 'request 1',
|
||||
request: {},
|
||||
orderIndex: 1,
|
||||
createdOn: new Date(),
|
||||
updatedOn: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
mockReset(mockPrisma);
|
||||
mockPubSub.publish.mockClear();
|
||||
});
|
||||
|
||||
describe('exportCollectionsToJSON', () => {
|
||||
test('should export collections to JSON string successfully for structure-1', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection
|
||||
|-> childTeamCollection
|
||||
| |-> <no request of child coll>
|
||||
|-> <no request of root coll>
|
||||
|
||||
*/
|
||||
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
rootTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Root Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(rootTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
childTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 2: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Child Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(childTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce([]);
|
||||
// return { name: childTeamCollection.title, folders: [], requests: [], };
|
||||
|
||||
// Back to RCV CALL 1
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
const returnedValue: CollectionFolder = {
|
||||
name: rootTeamCollection.title,
|
||||
folders: [
|
||||
{
|
||||
name: childTeamCollection.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
},
|
||||
],
|
||||
requests: [],
|
||||
};
|
||||
|
||||
const result = await teamCollectionService.exportCollectionsToJSON(team.id);
|
||||
expect(result).toEqualRight(JSON.stringify([returnedValue]));
|
||||
});
|
||||
|
||||
test('should export collections to JSON string successfully for structure-2', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection
|
||||
|-> childTeamCollection
|
||||
| |-> request1
|
||||
|-> <no request of root coll>
|
||||
|
||||
*/
|
||||
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
rootTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Root Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(rootTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
childTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 2: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Child Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(childTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce(teamRequestList);
|
||||
// return { name: childTeamCollection.title, folders: [], requests: teamRequestList, };
|
||||
|
||||
// Back to RCV CALL 1
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
const returnedValue: CollectionFolder = {
|
||||
name: rootTeamCollection.title,
|
||||
folders: [
|
||||
{
|
||||
name: childTeamCollection.title,
|
||||
folders: [],
|
||||
requests: teamRequestList.map((req) => req.request),
|
||||
},
|
||||
],
|
||||
requests: [],
|
||||
};
|
||||
|
||||
const result = await teamCollectionService.exportCollectionsToJSON(team.id);
|
||||
expect(result).toEqualRight(JSON.stringify([returnedValue]));
|
||||
});
|
||||
|
||||
test('should export collections to JSON string successfully for structure-3', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection
|
||||
|-> childTeamCollection
|
||||
| |-> child-request1
|
||||
|-> root-request1
|
||||
|
||||
*/
|
||||
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
rootTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Root Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(rootTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([
|
||||
childTeamCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 2: Inside exportCollectionsToJSON.exportCollectionToJSONObject for Child Collection
|
||||
jest
|
||||
.spyOn(teamCollectionService, 'getCollection')
|
||||
.mockResolvedValueOnce(E.right(childTeamCollection));
|
||||
mockPrisma.teamCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce(teamRequestList);
|
||||
// return { name: childTeamCollection.title, folders: [], requests: teamRequestList, };
|
||||
|
||||
// Back to RCV CALL 1
|
||||
mockPrisma.teamRequest.findMany.mockResolvedValueOnce(teamRequestList);
|
||||
|
||||
const returnedValue: CollectionFolder = {
|
||||
name: rootTeamCollection.title,
|
||||
folders: [
|
||||
{
|
||||
name: childTeamCollection.title,
|
||||
folders: [],
|
||||
requests: teamRequestList.map((req) => req.request),
|
||||
},
|
||||
],
|
||||
requests: teamRequestList.map((req) => req.request),
|
||||
};
|
||||
|
||||
const result = await teamCollectionService.exportCollectionsToJSON(team.id);
|
||||
expect(result).toEqualRight(JSON.stringify([returnedValue]));
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCollectionCount', () => {
|
||||
test('should return the count of collections successfully', async () => {
|
||||
const count = 10;
|
||||
|
||||
mockPrisma.teamCollection.count.mockResolvedValueOnce(count);
|
||||
const result = await teamCollectionService.getCollectionCount(
|
||||
rootTeamCollection.id,
|
||||
);
|
||||
expect(result).toEqual(count);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getTeamOfCollection', () => {
|
||||
test('should return the team of a collection successfully with valid collectionID', async () => {
|
||||
mockPrisma.teamCollection.findUnique.mockResolvedValueOnce({
|
||||
@@ -1460,5 +1640,3 @@ describe('totalCollectionsInTeam', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
//ToDo: write test cases for exportCollectionsToJSON
|
||||
|
||||
@@ -306,8 +306,8 @@ describe('TeamEnvironmentsService', () => {
|
||||
);
|
||||
|
||||
mockPrisma.teamEnvironment.create.mockResolvedValueOnce({
|
||||
...teamEnvironment,
|
||||
id: 'newid',
|
||||
...teamEnvironment,
|
||||
});
|
||||
|
||||
const result = await teamEnvironmentsService.createDuplicateEnvironment(
|
||||
@@ -337,8 +337,8 @@ describe('TeamEnvironmentsService', () => {
|
||||
);
|
||||
|
||||
mockPrisma.teamEnvironment.create.mockResolvedValueOnce({
|
||||
...teamEnvironment,
|
||||
id: 'newid',
|
||||
...teamEnvironment,
|
||||
});
|
||||
|
||||
const result = await teamEnvironmentsService.createDuplicateEnvironment(
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
TEAM_REQ_NOT_FOUND,
|
||||
TEAM_REQ_REORDERING_FAILED,
|
||||
TEAM_COLL_NOT_FOUND,
|
||||
JSON_INVALID,
|
||||
} from 'src/errors';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import { mockDeep, mockReset } from 'jest-mock-extended';
|
||||
@@ -239,7 +240,7 @@ describe('deleteTeamRequest', () => {
|
||||
});
|
||||
|
||||
describe('createTeamRequest', () => {
|
||||
test('rejects for invalid collection id', async () => {
|
||||
test('should rejects for invalid collection id', async () => {
|
||||
mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
|
||||
E.left(TEAM_INVALID_COLL_ID),
|
||||
);
|
||||
@@ -255,7 +256,42 @@ describe('createTeamRequest', () => {
|
||||
expect(mockPrisma.teamRequest.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('resolves for valid collection id', async () => {
|
||||
test('should rejects for invalid team ID', async () => {
|
||||
mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
|
||||
E.right(team),
|
||||
);
|
||||
|
||||
const response = await teamRequestService.createTeamRequest(
|
||||
'testcoll',
|
||||
'invalidteamid',
|
||||
'Test Request',
|
||||
'{}',
|
||||
);
|
||||
|
||||
expect(response).toEqualLeft(TEAM_INVALID_ID);
|
||||
expect(mockPrisma.teamRequest.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should reject for invalid request body', async () => {
|
||||
mockTeamCollectionService.getTeamOfCollection.mockResolvedValue(
|
||||
E.right(team),
|
||||
);
|
||||
teamRequestService.getRequestsCountInCollection = jest
|
||||
.fn()
|
||||
.mockResolvedValueOnce(0);
|
||||
|
||||
const response = await teamRequestService.createTeamRequest(
|
||||
'testcoll',
|
||||
team.id,
|
||||
'Test Request',
|
||||
'invalidjson',
|
||||
);
|
||||
|
||||
expect(response).toEqualLeft(JSON_INVALID);
|
||||
expect(mockPrisma.teamRequest.create).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test('should resolves and create team request', async () => {
|
||||
const dbRequest = dbTeamRequests[0];
|
||||
const teamRequest = teamRequests[0];
|
||||
|
||||
@@ -536,6 +572,52 @@ describe('findRequestAndNextRequest', () => {
|
||||
|
||||
expect(result).resolves.toEqualLeft(TEAM_REQ_NOT_FOUND);
|
||||
});
|
||||
test('should resolve left if the next request and given destCollId are different', () => {
|
||||
const args: MoveTeamRequestArgs = {
|
||||
srcCollID: teamRequests[0].collectionID,
|
||||
destCollID: 'different_coll_id',
|
||||
requestID: teamRequests[0].id,
|
||||
nextRequestID: teamRequests[4].id,
|
||||
};
|
||||
|
||||
mockPrisma.teamRequest.findFirst
|
||||
.mockResolvedValueOnce(dbTeamRequests[0])
|
||||
.mockResolvedValueOnce(dbTeamRequests[4]);
|
||||
|
||||
const result = teamRequestService.findRequestAndNextRequest(
|
||||
args.srcCollID,
|
||||
args.requestID,
|
||||
args.destCollID,
|
||||
args.nextRequestID,
|
||||
);
|
||||
|
||||
expect(result).resolves.toEqualLeft(TEAM_REQ_INVALID_TARGET_COLL_ID);
|
||||
});
|
||||
test('should resolve left if the request and the next request are from different teams', async () => {
|
||||
const args: MoveTeamRequestArgs = {
|
||||
srcCollID: teamRequests[0].collectionID,
|
||||
destCollID: teamRequests[4].collectionID,
|
||||
requestID: teamRequests[0].id,
|
||||
nextRequestID: teamRequests[4].id,
|
||||
};
|
||||
|
||||
const request = {
|
||||
...dbTeamRequests[0],
|
||||
teamID: 'different_team_id',
|
||||
};
|
||||
mockPrisma.teamRequest.findFirst
|
||||
.mockResolvedValueOnce(request)
|
||||
.mockResolvedValueOnce(dbTeamRequests[4]);
|
||||
|
||||
const result = await teamRequestService.findRequestAndNextRequest(
|
||||
args.srcCollID,
|
||||
args.requestID,
|
||||
args.destCollID,
|
||||
args.nextRequestID,
|
||||
);
|
||||
|
||||
expect(result).toEqualLeft(TEAM_REQ_INVALID_TARGET_COLL_ID);
|
||||
});
|
||||
});
|
||||
|
||||
describe('moveRequest', () => {
|
||||
@@ -725,13 +807,12 @@ describe('totalRequestsInATeam', () => {
|
||||
});
|
||||
expect(result).toEqual(0);
|
||||
});
|
||||
});
|
||||
describe('getTeamRequestsCount', () => {
|
||||
test('should return count of all Team Collections in the organization', async () => {
|
||||
mockPrisma.teamRequest.count.mockResolvedValueOnce(10);
|
||||
|
||||
describe('getTeamRequestsCount', () => {
|
||||
test('should return count of all Team Collections in the organization', async () => {
|
||||
mockPrisma.teamRequest.count.mockResolvedValueOnce(10);
|
||||
|
||||
const result = await teamRequestService.getTeamRequestsCount();
|
||||
expect(result).toEqual(10);
|
||||
});
|
||||
const result = await teamRequestService.getTeamRequestsCount();
|
||||
expect(result).toEqual(10);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { UserCollection } from '@prisma/client';
|
||||
import { UserCollection, UserRequest as DbUserRequest } from '@prisma/client';
|
||||
import { mockDeep, mockReset } from 'jest-mock-extended';
|
||||
import {
|
||||
USER_COLL_DEST_SAME,
|
||||
@@ -11,12 +11,17 @@ import {
|
||||
USER_COLL_SHORT_TITLE,
|
||||
USER_COLL_ALREADY_ROOT,
|
||||
USER_NOT_OWNER,
|
||||
USER_NOT_FOUND,
|
||||
USER_COLL_INVALID_JSON,
|
||||
} from 'src/errors';
|
||||
import { PrismaService } from 'src/prisma/prisma.service';
|
||||
import { PubSubService } from 'src/pubsub/pubsub.service';
|
||||
import { AuthUser } from 'src/types/AuthUser';
|
||||
import { ReqType } from 'src/types/RequestTypes';
|
||||
import { UserCollectionService } from './user-collection.service';
|
||||
import * as E from 'fp-ts/Either';
|
||||
import { CollectionFolder } from 'src/types/CollectionFolder';
|
||||
import { UserCollectionExportJSONData } from './user-collections.model';
|
||||
|
||||
const mockPrisma = mockDeep<PrismaService>();
|
||||
const mockPubSub = mockDeep<PubSubService>();
|
||||
@@ -341,11 +346,485 @@ const rootGQLGQLUserCollectionList: UserCollection[] = [
|
||||
},
|
||||
];
|
||||
|
||||
const userRESTRequestList: DbUserRequest[] = [
|
||||
{
|
||||
id: '123',
|
||||
collectionID: rootRESTUserCollection.id,
|
||||
userUid: user.uid,
|
||||
title: 'Request 1',
|
||||
request: {},
|
||||
type: ReqType.REST,
|
||||
orderIndex: 1,
|
||||
createdOn: new Date(),
|
||||
updatedOn: new Date(),
|
||||
},
|
||||
];
|
||||
|
||||
beforeEach(() => {
|
||||
mockReset(mockPrisma);
|
||||
mockPubSub.publish.mockClear();
|
||||
});
|
||||
|
||||
describe('importCollectionsFromJSON', () => {
|
||||
test('should resolve left for invalid JSON string', async () => {
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
'invalidJSONString',
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
|
||||
expect(result).toEqual(E.left(USER_COLL_INVALID_JSON));
|
||||
});
|
||||
test('should resolve left if JSON string is not an array', async () => {
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify({}),
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
|
||||
expect(result).toEqual(E.left(USER_COLL_INVALID_JSON));
|
||||
});
|
||||
test('should resolve left if destCollectionID is invalid', async () => {
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.left(USER_COLL_NOT_FOUND));
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([]),
|
||||
user.uid,
|
||||
'invalidID',
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqual(E.left(USER_COLL_NOT_FOUND));
|
||||
});
|
||||
test('should resolve left if destCollectionID is not owned by this user', async () => {
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([]),
|
||||
'anotherUserUid',
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqual(E.left(USER_NOT_OWNER));
|
||||
});
|
||||
test('should resolve left if destCollection type miss match', async () => {
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([]),
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.GQL,
|
||||
);
|
||||
expect(result).toEqual(E.left(USER_COLL_NOT_SAME_TYPE));
|
||||
});
|
||||
test('should resolve right for valid JSON and destCollectionID provided', async () => {
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
|
||||
// private getChildCollectionsCount function call
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
mockPrisma.$transaction.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([]),
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqual(E.right(true));
|
||||
});
|
||||
test('should resolve right for importing in root directory (destCollectionID == null)', async () => {
|
||||
// private getChildCollectionsCount function call
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
mockPrisma.$transaction.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([
|
||||
{
|
||||
name: 'collection-name',
|
||||
folders: [],
|
||||
requests: [{ name: 'request-name' }],
|
||||
},
|
||||
]),
|
||||
user.uid,
|
||||
null,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqual(E.right(true));
|
||||
});
|
||||
test('should resolve right and publish event', async () => {
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
|
||||
// private getChildCollectionsCount function call
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
mockPrisma.$transaction.mockResolvedValueOnce([{}]);
|
||||
|
||||
const result = await userCollectionService.importCollectionsFromJSON(
|
||||
JSON.stringify([
|
||||
{
|
||||
name: 'collection-name',
|
||||
folders: [],
|
||||
requests: [{ name: 'request-name' }],
|
||||
},
|
||||
]),
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqual(E.right(true));
|
||||
expect(mockPubSub.publish).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('exportUserCollectionsToJSON', () => {
|
||||
test('should return a list of user collections successfully for valid collectionID input and structure - 1', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection (id: 1 [exporting this collection])
|
||||
|-> childTeamCollection
|
||||
| |-> <no request of root coll>
|
||||
|-> <no request of root coll>
|
||||
*/
|
||||
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([
|
||||
childRESTUserCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: exportUserCollectionToJSONObject
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(childRESTUserCollection));
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce([]);
|
||||
const returnFromCallee: CollectionFolder = {
|
||||
id: childRESTUserCollection.id,
|
||||
name: childRESTUserCollection.title,
|
||||
folders: [],
|
||||
requests: [],
|
||||
};
|
||||
|
||||
// Back to exportUserCollectionsToJSON
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
const returnedValue: UserCollectionExportJSONData = {
|
||||
exportedCollection: JSON.stringify({
|
||||
id: rootRESTUserCollection.id,
|
||||
name: rootRESTUserCollection.title,
|
||||
folders: [returnFromCallee],
|
||||
requests: [],
|
||||
}),
|
||||
collectionType: ReqType.REST,
|
||||
};
|
||||
|
||||
const result = await userCollectionService.exportUserCollectionsToJSON(
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqualRight(returnedValue);
|
||||
});
|
||||
|
||||
test('should return a list of user collections successfully for valid collectionID input and structure - 2', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection (id: 1 [exporting this collection])
|
||||
|-> childTeamCollection
|
||||
| |-> request1
|
||||
|-> <no request of root coll>
|
||||
*/
|
||||
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([
|
||||
childRESTUserCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: exportUserCollectionToJSONObject
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(childRESTUserCollection));
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce(userRESTRequestList);
|
||||
const returnFromCallee: CollectionFolder = {
|
||||
id: childRESTUserCollection.id,
|
||||
name: childRESTUserCollection.title,
|
||||
folders: [],
|
||||
requests: userRESTRequestList.map((r) => {
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.title,
|
||||
...(r.request as Record<string, unknown>),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
// Back to exportUserCollectionsToJSON
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
const returnedValue: UserCollectionExportJSONData = {
|
||||
exportedCollection: JSON.stringify({
|
||||
id: rootRESTUserCollection.id,
|
||||
name: rootRESTUserCollection.title,
|
||||
folders: [returnFromCallee],
|
||||
requests: [],
|
||||
}),
|
||||
collectionType: ReqType.REST,
|
||||
};
|
||||
|
||||
const result = await userCollectionService.exportUserCollectionsToJSON(
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqualRight(returnedValue);
|
||||
});
|
||||
test('should return a list of user collections successfully for valid collectionID input and structure - 3', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection (id: 1 [exporting this collection])
|
||||
|-> childTeamCollection
|
||||
| |-> request1
|
||||
|-> request2
|
||||
*/
|
||||
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([
|
||||
childRESTUserCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: exportUserCollectionToJSONObject
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(childRESTUserCollection));
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce(userRESTRequestList);
|
||||
const returnFromCallee: CollectionFolder = {
|
||||
id: childRESTUserCollection.id,
|
||||
name: childRESTUserCollection.title,
|
||||
folders: [],
|
||||
requests: userRESTRequestList.map((r) => {
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.title,
|
||||
...(r.request as Record<string, unknown>),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
// Back to exportUserCollectionsToJSON
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(rootRESTUserCollection));
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce(userRESTRequestList);
|
||||
|
||||
const returnedValue: UserCollectionExportJSONData = {
|
||||
exportedCollection: JSON.stringify({
|
||||
id: rootRESTUserCollection.id,
|
||||
name: rootRESTUserCollection.title,
|
||||
folders: [returnFromCallee],
|
||||
requests: userRESTRequestList.map((x) => {
|
||||
return {
|
||||
id: x.id,
|
||||
name: x.title,
|
||||
...(x.request as Record<string, unknown>), // type casting x.request of type Prisma.JSONValue to an object to enable spread
|
||||
};
|
||||
}),
|
||||
}),
|
||||
collectionType: ReqType.REST,
|
||||
};
|
||||
|
||||
const result = await userCollectionService.exportUserCollectionsToJSON(
|
||||
user.uid,
|
||||
rootRESTUserCollection.id,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqualRight(returnedValue);
|
||||
});
|
||||
test('should return a list of user collections successfully for collectionID == null', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection (id: 1 [exporting this collection])
|
||||
|-> childTeamCollection
|
||||
| |-> request1
|
||||
|-> request2
|
||||
*/
|
||||
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([
|
||||
childRESTUserCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: exportUserCollectionToJSONObject
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.right(childRESTUserCollection));
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
mockPrisma.userRequest.findMany.mockResolvedValueOnce(userRESTRequestList);
|
||||
const returnFromCallee: CollectionFolder = {
|
||||
id: childRESTUserCollection.id,
|
||||
name: childRESTUserCollection.title,
|
||||
folders: [],
|
||||
requests: userRESTRequestList.map((r) => {
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.title,
|
||||
...(r.request as Record<string, unknown>),
|
||||
};
|
||||
}),
|
||||
};
|
||||
|
||||
// Back to exportUserCollectionsToJSON
|
||||
|
||||
const returnedValue: UserCollectionExportJSONData = {
|
||||
exportedCollection: JSON.stringify([returnFromCallee]),
|
||||
collectionType: ReqType.REST,
|
||||
};
|
||||
|
||||
const result = await userCollectionService.exportUserCollectionsToJSON(
|
||||
user.uid,
|
||||
null,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqualRight(returnedValue);
|
||||
});
|
||||
test('should return USER_COLL_NOT_FOUND if collectionID or its child not found in DB', async () => {
|
||||
/*
|
||||
Assuming collection and request structure is as follows:
|
||||
|
||||
rootTeamCollection (id: 1 [exporting this collection])
|
||||
|-> childTeamCollection
|
||||
| |-> request1 <NOT FOUND IN DATABASE>
|
||||
|-> request2
|
||||
*/
|
||||
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([
|
||||
childRESTUserCollection,
|
||||
]);
|
||||
|
||||
// RCV CALL 1: exportUserCollectionToJSONObject
|
||||
jest
|
||||
.spyOn(userCollectionService, 'getUserCollection')
|
||||
.mockResolvedValueOnce(E.left(USER_COLL_NOT_FOUND));
|
||||
|
||||
// Back to exportUserCollectionsToJSON
|
||||
|
||||
const result = await userCollectionService.exportUserCollectionsToJSON(
|
||||
user.uid,
|
||||
null,
|
||||
ReqType.REST,
|
||||
);
|
||||
expect(result).toEqualLeft(USER_COLL_NOT_FOUND);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserOfCollection', () => {
|
||||
test('should return a user successfully with valid collectionID', async () => {
|
||||
mockPrisma.userCollection.findUniqueOrThrow.mockResolvedValueOnce({
|
||||
...rootRESTUserCollection,
|
||||
user: user,
|
||||
} as any);
|
||||
|
||||
const result = await userCollectionService.getUserOfCollection(
|
||||
rootRESTUserCollection.id,
|
||||
);
|
||||
expect(result).toEqualRight(user);
|
||||
});
|
||||
test('should return null with invalid collectionID', async () => {
|
||||
mockPrisma.userCollection.findUniqueOrThrow.mockRejectedValue('error');
|
||||
|
||||
const result = await userCollectionService.getUserOfCollection('invalidId');
|
||||
expect(result).toEqualLeft(USER_NOT_FOUND);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getUserChildCollections', () => {
|
||||
test('should return a list of child collections successfully with valid collectionID and userID', async () => {
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce(
|
||||
childRESTUserCollectionList,
|
||||
);
|
||||
|
||||
const result = await userCollectionService.getUserChildCollections(
|
||||
user,
|
||||
rootRESTUserCollection.id,
|
||||
null,
|
||||
10,
|
||||
ReqType.REST,
|
||||
);
|
||||
|
||||
expect(result).toEqual(childRESTUserCollectionList);
|
||||
expect(mockPrisma.userCollection.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userUid: user.uid,
|
||||
parentID: rootRESTUserCollection.id,
|
||||
type: ReqType.REST,
|
||||
},
|
||||
take: 10,
|
||||
skip: 0,
|
||||
cursor: undefined,
|
||||
});
|
||||
});
|
||||
test('should return an empty list if no child collections found', async () => {
|
||||
mockPrisma.userCollection.findMany.mockResolvedValueOnce([]);
|
||||
|
||||
const result = await userCollectionService.getUserChildCollections(
|
||||
user,
|
||||
rootRESTUserCollection.id,
|
||||
null,
|
||||
10,
|
||||
ReqType.REST,
|
||||
);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
expect(mockPrisma.userCollection.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userUid: user.uid,
|
||||
parentID: rootRESTUserCollection.id,
|
||||
type: ReqType.REST,
|
||||
},
|
||||
take: 10,
|
||||
skip: 0,
|
||||
cursor: undefined,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getCollectionCount', () => {
|
||||
test('should return the count of collections', async () => {
|
||||
const collectionID = 'collection123';
|
||||
const count = 5;
|
||||
|
||||
mockPrisma.userCollection.count.mockResolvedValueOnce(count);
|
||||
|
||||
const result = await userCollectionService.getCollectionCount(collectionID);
|
||||
|
||||
expect(result).toEqual(count);
|
||||
expect(mockPrisma.userCollection.count).toHaveBeenCalledTimes(1);
|
||||
expect(mockPrisma.userCollection.count).toHaveBeenCalledWith({
|
||||
where: { parentID: collectionID },
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getParentOfUserCollection', () => {
|
||||
test('should return a user-collection successfully with valid collectionID', async () => {
|
||||
mockPrisma.userCollection.findUnique.mockResolvedValueOnce({
|
||||
|
||||
@@ -140,13 +140,15 @@ describe('UserHistoryService', () => {
|
||||
});
|
||||
describe('createUserHistory', () => {
|
||||
test('Should resolve right and create a REST request to users history and return a `UserHistory` object', async () => {
|
||||
const executedOn = new Date();
|
||||
|
||||
mockPrisma.userHistory.create.mockResolvedValueOnce({
|
||||
userUid: 'abc',
|
||||
id: '1',
|
||||
request: [{}],
|
||||
responseMetadata: [{}],
|
||||
reqType: ReqType.REST,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
});
|
||||
|
||||
@@ -156,7 +158,7 @@ describe('UserHistoryService', () => {
|
||||
request: JSON.stringify([{}]),
|
||||
responseMetadata: JSON.stringify([{}]),
|
||||
reqType: ReqType.REST,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
};
|
||||
|
||||
@@ -170,13 +172,15 @@ describe('UserHistoryService', () => {
|
||||
).toEqualRight(userHistory);
|
||||
});
|
||||
test('Should resolve right and create a GQL request to users history and return a `UserHistory` object', async () => {
|
||||
const executedOn = new Date();
|
||||
|
||||
mockPrisma.userHistory.create.mockResolvedValueOnce({
|
||||
userUid: 'abc',
|
||||
id: '1',
|
||||
request: [{}],
|
||||
responseMetadata: [{}],
|
||||
reqType: ReqType.GQL,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
});
|
||||
|
||||
@@ -186,7 +190,7 @@ describe('UserHistoryService', () => {
|
||||
request: JSON.stringify([{}]),
|
||||
responseMetadata: JSON.stringify([{}]),
|
||||
reqType: ReqType.GQL,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
};
|
||||
|
||||
@@ -210,13 +214,15 @@ describe('UserHistoryService', () => {
|
||||
).toEqualLeft(USER_HISTORY_INVALID_REQ_TYPE);
|
||||
});
|
||||
test('Should create a GQL request to users history and publish a created subscription', async () => {
|
||||
const executedOn = new Date();
|
||||
|
||||
mockPrisma.userHistory.create.mockResolvedValueOnce({
|
||||
userUid: 'abc',
|
||||
id: '1',
|
||||
request: [{}],
|
||||
responseMetadata: [{}],
|
||||
reqType: ReqType.GQL,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
});
|
||||
|
||||
@@ -226,7 +232,7 @@ describe('UserHistoryService', () => {
|
||||
request: JSON.stringify([{}]),
|
||||
responseMetadata: JSON.stringify([{}]),
|
||||
reqType: ReqType.GQL,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
};
|
||||
|
||||
@@ -243,13 +249,15 @@ describe('UserHistoryService', () => {
|
||||
);
|
||||
});
|
||||
test('Should create a REST request to users history and publish a created subscription', async () => {
|
||||
const executedOn = new Date();
|
||||
|
||||
mockPrisma.userHistory.create.mockResolvedValueOnce({
|
||||
userUid: 'abc',
|
||||
id: '1',
|
||||
request: [{}],
|
||||
responseMetadata: [{}],
|
||||
reqType: ReqType.REST,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
});
|
||||
|
||||
@@ -259,7 +267,7 @@ describe('UserHistoryService', () => {
|
||||
request: JSON.stringify([{}]),
|
||||
responseMetadata: JSON.stringify([{}]),
|
||||
reqType: ReqType.REST,
|
||||
executedOn: new Date(),
|
||||
executedOn,
|
||||
isStarred: false,
|
||||
};
|
||||
|
||||
|
||||
@@ -5,6 +5,9 @@ import {
|
||||
import { mockDeep, mockReset } from 'jest-mock-extended';
|
||||
import {
|
||||
JSON_INVALID,
|
||||
USER_COLLECTION_NOT_FOUND,
|
||||
USER_COLL_NOT_FOUND,
|
||||
USER_REQUEST_INVALID_TYPE,
|
||||
USER_REQUEST_NOT_FOUND,
|
||||
USER_REQUEST_REORDERING_FAILED,
|
||||
} from 'src/errors';
|
||||
@@ -373,6 +376,101 @@ describe('UserRequestService', () => {
|
||||
|
||||
expect(result).resolves.toEqualLeft(JSON_INVALID);
|
||||
});
|
||||
test('Should resolve left for invalid collection ID', () => {
|
||||
const args: CreateUserRequestArgs = {
|
||||
collectionID: 'invalid-collection-id',
|
||||
title: userRequests[0].title,
|
||||
request: userRequests[0].request,
|
||||
type: userRequests[0].type,
|
||||
};
|
||||
|
||||
mockPrisma.userRequest.count.mockResolvedValue(
|
||||
dbUserRequests[0].orderIndex - 1,
|
||||
);
|
||||
mockUserCollectionService.getUserCollection.mockResolvedValue(
|
||||
E.left(USER_COLL_NOT_FOUND),
|
||||
);
|
||||
|
||||
const result = userRequestService.createRequest(
|
||||
args.collectionID,
|
||||
args.title,
|
||||
args.request,
|
||||
args.type,
|
||||
user,
|
||||
);
|
||||
|
||||
expect(result).resolves.toEqualLeft(USER_COLL_NOT_FOUND);
|
||||
});
|
||||
test('Should resolve left for wrong collection ID (using other users collection ID)', () => {
|
||||
const args: CreateUserRequestArgs = {
|
||||
collectionID: userRequests[0].collectionID,
|
||||
title: userRequests[0].title,
|
||||
request: userRequests[0].request,
|
||||
type: userRequests[0].type,
|
||||
};
|
||||
|
||||
mockPrisma.userRequest.count.mockResolvedValue(
|
||||
dbUserRequests[0].orderIndex - 1,
|
||||
);
|
||||
mockUserCollectionService.getUserCollection.mockResolvedValue(
|
||||
E.right({ type: userRequests[0].type, userUid: 'another-user' } as any),
|
||||
);
|
||||
|
||||
const result = userRequestService.createRequest(
|
||||
args.collectionID,
|
||||
args.title,
|
||||
args.request,
|
||||
args.type,
|
||||
user,
|
||||
);
|
||||
|
||||
expect(result).resolves.toEqualLeft(USER_COLLECTION_NOT_FOUND);
|
||||
});
|
||||
test('Should resolve left for collection type and request type miss match', () => {
|
||||
const args: CreateUserRequestArgs = {
|
||||
collectionID: userRequests[0].collectionID,
|
||||
title: userRequests[0].title,
|
||||
request: userRequests[0].request,
|
||||
type: userRequests[0].type,
|
||||
};
|
||||
|
||||
mockUserCollectionService.getUserCollection.mockResolvedValue(
|
||||
E.right({ type: 'invalid-type', userUid: user.uid } as any),
|
||||
);
|
||||
|
||||
const result = userRequestService.createRequest(
|
||||
args.collectionID,
|
||||
args.title,
|
||||
args.request,
|
||||
args.type,
|
||||
user,
|
||||
);
|
||||
|
||||
expect(result).resolves.toEqualLeft(USER_REQUEST_INVALID_TYPE);
|
||||
});
|
||||
test('Should resolve left if DB request type and parameter type is different', () => {
|
||||
const args: CreateUserRequestArgs = {
|
||||
collectionID: userRequests[0].collectionID,
|
||||
title: userRequests[0].title,
|
||||
request: userRequests[0].request,
|
||||
type: userRequests[0].type,
|
||||
};
|
||||
|
||||
mockPrisma.userRequest.count.mockResolvedValue(
|
||||
dbUserRequests[0].orderIndex - 1,
|
||||
);
|
||||
mockPrisma.userRequest.create.mockResolvedValue(dbUserRequests[0]);
|
||||
|
||||
const result = userRequestService.createRequest(
|
||||
args.collectionID,
|
||||
args.title,
|
||||
args.request,
|
||||
ReqType.GQL,
|
||||
user,
|
||||
);
|
||||
|
||||
expect(result).resolves.toEqualLeft(USER_REQUEST_INVALID_TYPE);
|
||||
});
|
||||
});
|
||||
|
||||
describe('updateRequest', () => {
|
||||
|
||||
Reference in New Issue
Block a user