-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproject-service-get-all-projects.spec.ts
More file actions
253 lines (207 loc) · 7.84 KB
/
project-service-get-all-projects.spec.ts
File metadata and controls
253 lines (207 loc) · 7.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { Project } from "../../src/entities/project";
import { Team } from "../../src/entities/team";
import { User } from "../../src/entities/user";
import { Settings } from "../../src/entities/settings";
import { UserRole } from "../../src/entities/user-role";
import {
IProjectService,
ProjectService,
} from "../../src/services/project-service";
import { TestDatabaseService } from "./mock/mock-database-service";
import { defaultSettings } from "./mock/mock-settings-service";
describe(ProjectService.name, () => {
let service: IProjectService;
let database: TestDatabaseService;
let adminUser: User;
let regularUser: User;
beforeAll(async () => {
database = new TestDatabaseService();
await database.bootstrap();
});
beforeEach(async () => {
await database.nuke();
service = new ProjectService(database);
await service.bootstrap();
// Create admin user
adminUser = new User();
adminUser.firstName = "Admin";
adminUser.lastName = "User";
adminUser.email = "admin@test.com";
adminUser.password = "";
adminUser.role = UserRole.Root;
adminUser.verifyToken = "";
adminUser.tokenSecret = "";
adminUser.forgotPasswordToken = "";
// Create regular user
regularUser = new User();
regularUser.firstName = "Regular";
regularUser.lastName = "User";
regularUser.email = "user@test.com";
regularUser.password = "";
regularUser.role = UserRole.User;
regularUser.verifyToken = "";
regularUser.tokenSecret = "";
regularUser.forgotPasswordToken = "";
const userRepo = database.getRepository(User);
await userRepo.save([adminUser, regularUser]);
const settingsRepo = database.getRepository(Settings);
await settingsRepo.save([
{
...defaultSettings,
project: {
...defaultSettings.project,
allowRatingProjects: false,
},
},
]);
});
/**
* Update the default settings from beforeEach with a different value for
* allowRatingProjects
*/
const allowRatingProjects = async (value: boolean): Promise<void> => {
const settingsRepo = database.getRepository(Settings);
const settings = {
project: {
allowRatingProjects: value,
},
};
const [savedSettings] = await settingsRepo.find();
const merged = settingsRepo.merge(savedSettings, settings);
await settingsRepo.save(merged);
};
describe("getAllProjects", () => {
it("gets all projects if the user is an admin", async () => {
expect.assertions(4);
// Create teams with projects
const team1 = new Team();
team1.title = "Team 1";
team1.users = [regularUser.id.toString()];
team1.teamImg = "";
team1.description = "";
team1.requests = [];
const team2 = new Team();
team2.title = "Team 2";
team2.users = [regularUser.id.toString()];
team2.teamImg = "";
team2.description = "";
team2.requests = [];
const teamRepo = database.getRepository(Team);
const savedTeams = await teamRepo.save([team1, team2]);
// Create projects for teams (including those not assigned to admin)
const project1 = new Project();
project1.team = savedTeams[0];
project1.title = "Project 1";
project1.description = "Description 1";
project1.allowRating = true;
const project2 = new Project();
project2.team = savedTeams[1];
project2.title = "Project 2";
project2.description = "Description 2";
project2.allowRating = false;
const project3 = new Project();
project3.team = savedTeams[0];
project3.title = "Project 3";
project3.description = "Description 3";
project3.allowRating = true;
const projectRepo = database.getRepository(Project);
await projectRepo.save([project1, project2, project3]);
const allProjects = await service.getAllProjects(adminUser);
expect(allProjects).toHaveLength(3);
expect(allProjects[0]).toEqual(project1);
expect(allProjects[1]).toEqual(project2);
expect(allProjects[2]).toEqual(project3);
});
it("gets no projects if the user is a regular user", async () => {
expect.assertions(1);
// Create a team and project that the regular user is not part of
const team = new Team();
team.title = "Other Team";
team.users = []; // Regular user is not part of this team
team.teamImg = "";
team.description = "";
team.requests = [];
const teamRepo = database.getRepository(Team);
const savedTeam = await teamRepo.save(team);
const project = new Project();
project.team = savedTeam;
project.title = "Project";
project.description = "Description";
project.allowRating = true;
const projectRepo = database.getRepository(Project);
await projectRepo.save(project);
const allProjects = await service.getAllProjects(regularUser);
expect(allProjects).toHaveLength(0);
});
it("regular user, part of two teams, gets 3 projects", async () => {
expect.assertions(4);
// Create two teams with the regular user
const team1 = new Team();
team1.title = "Team 1";
team1.users = [regularUser.id.toString()];
team1.teamImg = "";
team1.description = "";
team1.requests = [];
const team2 = new Team();
team2.title = "Team 2";
team2.users = [regularUser.id.toString()];
team2.teamImg = "";
team2.description = "";
team2.requests = [];
const teamRepo = database.getRepository(Team);
const savedTeams = await teamRepo.save([team1, team2]);
// Create 2 projects for team1 and 1 project for team2
const project1 = new Project();
project1.team = savedTeams[0];
project1.title = "Project 1 - Team 1";
project1.description = "Description 1";
project1.allowRating = true;
const project2 = new Project();
project2.team = savedTeams[0];
project2.title = "Project 2 - Team 1";
project2.description = "Description 2";
project2.allowRating = false;
const project3 = new Project();
project3.team = savedTeams[1];
project3.title = "Project 1 - Team 2";
project3.description = "Description 3";
project3.allowRating = true;
const projectRepo = database.getRepository(Project);
await projectRepo.save([project1, project2, project3]);
const allProjects = await service.getAllProjects(regularUser);
expect(allProjects).toHaveLength(3);
expect(allProjects[0]).toEqual(project1);
expect(allProjects[1]).toEqual(project2);
expect(allProjects[2]).toEqual(project3);
});
it("regular user without team gets projects that can be rated", async () => {
expect.assertions(2);
await allowRatingProjects(true);
// Create a team with no users
const team = new Team();
team.title = "Public Team";
team.users = [];
team.teamImg = "";
team.description = "";
team.requests = [];
const teamRepo = database.getRepository(Team);
const savedTeam = await teamRepo.save(team);
// Create projects with different allowRating settings
const ratingProject = new Project();
ratingProject.team = savedTeam;
ratingProject.title = "Project with Rating";
ratingProject.description = "Description 1";
ratingProject.allowRating = true;
const noRatingProject = new Project();
noRatingProject.team = savedTeam;
noRatingProject.title = "Project without Rating";
noRatingProject.description = "Description 2";
noRatingProject.allowRating = false;
const projectRepo = database.getRepository(Project);
await projectRepo.save([ratingProject, noRatingProject]);
const allProjects = await service.getAllProjects(regularUser);
expect(allProjects).toHaveLength(1);
expect(allProjects[0]).toEqual(ratingProject);
});
});
});