|
1 | | -import sinon from 'sinon'; |
2 | | -import request from 'supertest'; |
3 | | -import _ from 'lodash'; |
4 | | -import Promise from 'bluebird'; |
5 | | - |
6 | | -import models from '../../models'; |
7 | | -import server from '../../app'; |
8 | | -import testUtil from '../../tests/util'; |
9 | | - |
10 | | -import { PROJECT_STATUS, PROJECT_MEMBER_ROLE, SCOPE_CHANGE_REQ_STATUS } from '../../constants'; |
11 | | - |
12 | | -/** |
13 | | - * Creates a project with given status |
14 | | - * @param {string} status - Status of the project |
15 | | - * |
16 | | - * @returns {Promise} - promise for project creation |
17 | | - */ |
18 | | -function createProject(status) { |
19 | | - const newMember = (userId, role, project) => ({ |
20 | | - userId, |
21 | | - projectId: project.id, |
22 | | - role, |
23 | | - isPrimary: true, |
24 | | - createdBy: 1, |
25 | | - updatedBy: 1, |
26 | | - }); |
27 | | - |
28 | | - return models.Project.create({ |
29 | | - type: 'generic', |
30 | | - billingAccountId: 1, |
31 | | - name: 'test1', |
32 | | - description: 'test project1', |
33 | | - status, |
34 | | - details: {}, |
35 | | - createdBy: 1, |
36 | | - updatedBy: 1, |
37 | | - lastActivityAt: 1, |
38 | | - lastActivityUserId: '1', |
39 | | - }).then(project => |
40 | | - Promise.all([ |
41 | | - models.ProjectMember.create(newMember(testUtil.userIds.member, PROJECT_MEMBER_ROLE.CUSTOMER, project)), |
42 | | - models.ProjectMember.create(newMember(testUtil.userIds.manager, PROJECT_MEMBER_ROLE.MANAGER, project)), |
43 | | - ]).then(() => project), |
44 | | - ); |
45 | | -} |
46 | | - |
47 | | -/** |
48 | | - * creates a new scope change request object |
49 | | - * @returns {Object} - scope change request object |
50 | | - */ |
51 | | -function newScopeChangeRequest() { |
52 | | - return { |
53 | | - newScope: { |
54 | | - appDefinition: { |
55 | | - numberScreens: '5-8', |
56 | | - }, |
57 | | - }, |
58 | | - oldScope: { |
59 | | - appDefinition: { |
60 | | - numberScreens: '2-4', |
61 | | - }, |
62 | | - }, |
63 | | - }; |
64 | | -} |
65 | | - |
66 | | -/** |
67 | | - * Asserts the status of the Scope change request |
68 | | - * @param {Object} response - Response object from the post service |
69 | | - * @param {string} expectedStatus - Expected status of the Scope Change Request |
70 | | - * |
71 | | - * @returns {undefined} - throws error if assertion failed |
72 | | - */ |
73 | | -function assertStatus(response, expectedStatus) { |
74 | | - const status = _.get(response, 'body.status'); |
75 | | - sinon.assert.match(status, expectedStatus); |
76 | | -} |
77 | | - |
78 | | -/** |
79 | | - * Updaes the status of scope change requests for the given project in db |
80 | | - * @param {Object} project - the project |
81 | | - * @param {string} status - the new status for update |
82 | | - * |
83 | | - * @returns {Promise} the promise to update the status |
84 | | - */ |
85 | | -function updateScopeChangeStatuses(project, status) { |
86 | | - return models.ScopeChangeRequest.update({ status }, { where: { projectId: project.id } }); |
87 | | -} |
88 | | - |
89 | | - |
90 | | -/*describe('Create Scope Change Rquest', () => { |
91 | | - let projects; |
92 | | - let projectWithPendingChange; |
93 | | - let projectWithApprovedChange; |
94 | | -
|
95 | | - before((done) => { |
96 | | - const projectStatuses = [ |
97 | | - PROJECT_STATUS.DRAFT, |
98 | | - PROJECT_STATUS.IN_REVIEW, |
99 | | - PROJECT_STATUS.REVIEWED, |
100 | | - PROJECT_STATUS.ACTIVE, |
101 | | - ]; |
102 | | -
|
103 | | - Promise.all(projectStatuses.map(status => createProject(status))) |
104 | | - .then(_projects => _projects.map((project, i) => [projectStatuses[i], project])) |
105 | | - .then((_projectStatusPairs) => { |
106 | | - projects = _.fromPairs(_projectStatusPairs); |
107 | | - }) |
108 | | - .then(() => done()); |
109 | | - }); |
110 | | -
|
111 | | - after((done) => { |
112 | | - testUtil.clearDb(done); |
113 | | - }); |
114 | | -
|
115 | | - describe('POST projects/{projectId}/scopeChangeRequests', () => { |
116 | | - it('Should create scope change request for project in reviewed status', (done) => { |
117 | | - const project = projects[PROJECT_STATUS.REVIEWED]; |
118 | | -
|
119 | | - request(server) |
120 | | - .post(`/v5/projects/${project.id}/scopeChangeRequests`) |
121 | | - .set({ |
122 | | - Authorization: `Bearer ${testUtil.jwts.manager}`, |
123 | | - }) |
124 | | - .send(newScopeChangeRequest()) |
125 | | - .expect(200) |
126 | | - .end((err, res) => { |
127 | | - if (err) { |
128 | | - done(err); |
129 | | - } else { |
130 | | - projectWithPendingChange = project; |
131 | | -
|
132 | | - assertStatus(res, SCOPE_CHANGE_REQ_STATUS.PENDING); |
133 | | - done(); |
134 | | - } |
135 | | - }); |
136 | | - }); |
137 | | -
|
138 | | - it('Should create scope change request for project in active status', (done) => { |
139 | | - const project = projects[PROJECT_STATUS.ACTIVE]; |
140 | | -
|
141 | | - request(server) |
142 | | - .post(`/v5/projects/${project.id}/scopeChangeRequests`) |
143 | | - .set({ |
144 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
145 | | - }) |
146 | | - .send(newScopeChangeRequest()) |
147 | | - .expect(200) |
148 | | - .end((err, res) => { |
149 | | - if (err) { |
150 | | - done(err); |
151 | | - } else { |
152 | | - projectWithApprovedChange = project; |
153 | | -
|
154 | | - assertStatus(res, SCOPE_CHANGE_REQ_STATUS.APPROVED); |
155 | | - done(); |
156 | | - } |
157 | | - }); |
158 | | - }); |
159 | | -
|
160 | | - it('Should return error with status 403 if project is in draft status', (done) => { |
161 | | - const project = projects[PROJECT_STATUS.DRAFT]; |
162 | | -
|
163 | | - request(server) |
164 | | - .post(`/v5/projects/${project.id}/scopeChangeRequests`) |
165 | | - .set({ |
166 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
167 | | - }) |
168 | | - .send(newScopeChangeRequest()) |
169 | | - .expect(403) |
170 | | - .end(err => done(err)); |
171 | | - }); |
172 | | -
|
173 | | - it('Should return error with status 403 if project is in in_review status', (done) => { |
174 | | - const project = projects[PROJECT_STATUS.IN_REVIEW]; |
175 | | -
|
176 | | - request(server) |
177 | | - .post(`/v5/projects/${project.id}/scopeChangeRequests`) |
178 | | - .set({ |
179 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
180 | | - }) |
181 | | - .send(newScopeChangeRequest()) |
182 | | - .expect(403) |
183 | | - .end(err => done(err)); |
184 | | - }); |
185 | | -
|
186 | | - it('Should return error with status 404 if project not present', (done) => { |
187 | | - const nonExistentProjectId = 341212; |
188 | | - request(server) |
189 | | - .post(`/v5/projects/${nonExistentProjectId}/scopeChangeRequests`) |
190 | | - .set({ |
191 | | - Authorization: `Bearer ${testUtil.jwts.manager}`, |
192 | | - }) |
193 | | - .send(newScopeChangeRequest()) |
194 | | - .expect(404) |
195 | | - .end(err => done(err)); |
196 | | - }); |
197 | | -
|
198 | | - it('Should return error with status 403 if there is a request in pending status', (done) => { |
199 | | - request(server) |
200 | | - .post(`/v5/projects/${projectWithPendingChange.id}/scopeChangeRequests`) |
201 | | - .set({ |
202 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
203 | | - }) |
204 | | - .send(newScopeChangeRequest()) |
205 | | - .expect(403) |
206 | | - .end(err => done(err)); |
207 | | - }); |
208 | | -
|
209 | | - it('Should return error with status 403 if there is a request in approved status', (done) => { |
210 | | - request(server) |
211 | | - .post(`/v5/projects/${projectWithApprovedChange.id}/scopeChangeRequests`) |
212 | | - .set({ |
213 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
214 | | - }) |
215 | | - .send(newScopeChangeRequest()) |
216 | | - .expect(403) |
217 | | - .end(err => done(err)); |
218 | | - }); |
219 | | -
|
220 | | - it('Should create scope change request if there is a request in canceled status', (done) => { |
221 | | - updateScopeChangeStatuses(projectWithApprovedChange, SCOPE_CHANGE_REQ_STATUS.CANCELED).then(() => { |
222 | | - request(server) |
223 | | - .post(`/v5/projects/${projectWithApprovedChange.id}/scopeChangeRequests`) |
224 | | - .set({ |
225 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
226 | | - }) |
227 | | - .send(newScopeChangeRequest()) |
228 | | - .expect(200) |
229 | | - .end((err, res) => { |
230 | | - if (err) { |
231 | | - done(err); |
232 | | - } else { |
233 | | - assertStatus(res, SCOPE_CHANGE_REQ_STATUS.APPROVED); |
234 | | - done(); |
235 | | - } |
236 | | - }); |
237 | | - }); |
238 | | - }); |
239 | | -
|
240 | | - it('Should create scope change request if there is a request in rejected status', (done) => { |
241 | | - updateScopeChangeStatuses(projectWithApprovedChange, SCOPE_CHANGE_REQ_STATUS.REJECTED).then(() => { |
242 | | - request(server) |
243 | | - .post(`/v5/projects/${projectWithApprovedChange.id}/scopeChangeRequests`) |
244 | | - .set({ |
245 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
246 | | - }) |
247 | | - .send(newScopeChangeRequest()) |
248 | | - .expect(200) |
249 | | - .end((err, res) => { |
250 | | - if (err) { |
251 | | - done(err); |
252 | | - } else { |
253 | | - assertStatus(res, SCOPE_CHANGE_REQ_STATUS.APPROVED); |
254 | | - done(); |
255 | | - } |
256 | | - }); |
257 | | - }); |
258 | | - }); |
259 | | -
|
260 | | - it('Should create scope change request if there is a request in activated status', (done) => { |
261 | | - updateScopeChangeStatuses(projectWithApprovedChange, SCOPE_CHANGE_REQ_STATUS.ACTIVATED).then(() => { |
262 | | - request(server) |
263 | | - .post(`/v5/projects/${projectWithApprovedChange.id}/scopeChangeRequests`) |
264 | | - .set({ |
265 | | - Authorization: `Bearer ${testUtil.jwts.member}`, |
266 | | - }) |
267 | | - .send(newScopeChangeRequest()) |
268 | | - .expect(200) |
269 | | - .end((err, res) => { |
270 | | - if (err) { |
271 | | - done(err); |
272 | | - } else { |
273 | | - assertStatus(res, SCOPE_CHANGE_REQ_STATUS.APPROVED); |
274 | | - done(); |
275 | | - } |
276 | | - }); |
277 | | - }); |
278 | | - }); |
279 | | - }); |
280 | | -});*/ |
0 commit comments