|
1 |
| -import sinon from 'sinon'; |
2 |
| -import request from 'supertest'; |
3 |
| -import _ from 'lodash'; |
4 |
| - |
5 |
| -import models from '../../models'; |
6 |
| -import server from '../../app'; |
7 |
| -import testUtil from '../../tests/util'; |
8 |
| - |
9 |
| -import { PROJECT_STATUS, PROJECT_MEMBER_ROLE, SCOPE_CHANGE_REQ_STATUS } from '../../constants'; |
10 |
| - |
11 |
| -/** |
12 |
| - * Creates a project with given status |
13 |
| - * @param {string} status - Status of the project |
14 |
| - * |
15 |
| - * @returns {Promise} - promise for project creation |
16 |
| - */ |
17 |
| -function createProject(status) { |
18 |
| - const newMember = (userId, role, project) => ({ |
19 |
| - userId, |
20 |
| - projectId: project.id, |
21 |
| - role, |
22 |
| - isPrimary: true, |
23 |
| - createdBy: 1, |
24 |
| - updatedBy: 1, |
25 |
| - }); |
26 |
| - |
27 |
| - return models.Project.create({ |
28 |
| - type: 'generic', |
29 |
| - billingAccountId: 1, |
30 |
| - name: 'test1', |
31 |
| - description: 'test project1', |
32 |
| - status, |
33 |
| - details: {}, |
34 |
| - createdBy: 1, |
35 |
| - updatedBy: 1, |
36 |
| - lastActivityAt: 1, |
37 |
| - lastActivityUserId: '1', |
38 |
| - }).then(project => |
39 |
| - Promise.all([ |
40 |
| - models.ProjectMember.create(newMember(testUtil.userIds.member, PROJECT_MEMBER_ROLE.CUSTOMER, project)), |
41 |
| - models.ProjectMember.create(newMember(testUtil.userIds.manager, PROJECT_MEMBER_ROLE.MANAGER, project)), |
42 |
| - ]).then(() => project), |
43 |
| - ); |
44 |
| -} |
45 |
| - |
46 |
| -/** |
47 |
| - * Asserts the status of the Scope change request |
48 |
| - * @param {Object} updatedScopeChangeRequest - the updated scope change request from db |
49 |
| - * @param {string} expectedStatus - Expected status of the Scope Change Request |
50 |
| - * |
51 |
| - * @returns {undefined} - throws error if assertion failed |
52 |
| - */ |
53 |
| -function assertStatus(updatedScopeChangeRequest, expectedStatus) { |
54 |
| - sinon.assert.match(updatedScopeChangeRequest.status, expectedStatus); |
55 |
| -} |
56 |
| - |
57 |
| -/** |
58 |
| - * create scope change request for the given project |
59 |
| - * @param {Object} project - the project |
60 |
| - * |
61 |
| - * @returns {Promise} - the promise to create scope change request |
62 |
| - */ |
63 |
| -function createScopeChangeRequest(project) { |
64 |
| - return models.ScopeChangeRequest.create({ |
65 |
| - newScope: { |
66 |
| - appDefinition: { |
67 |
| - numberScreens: '5-8', |
68 |
| - }, |
69 |
| - }, |
70 |
| - oldScope: { |
71 |
| - appDefinition: { |
72 |
| - numberScreens: '2-4', |
73 |
| - }, |
74 |
| - }, |
75 |
| - projectId: project.id, |
76 |
| - status: SCOPE_CHANGE_REQ_STATUS.PENDING, |
77 |
| - createdBy: 1, |
78 |
| - updatedBy: 1, |
79 |
| - lastActivityAt: 1, |
80 |
| - lastActivityUserId: '1', |
81 |
| - }); |
82 |
| -} |
83 |
| - |
84 |
| -/** |
85 |
| - * Updates the details json of the project |
86 |
| - * @param {string} projectId The project id |
87 |
| - * @param {Object} detailsChange The changes to be merged with details json |
88 |
| - * |
89 |
| - * @returns {Promise} A promise to update details json in the project |
90 |
| - */ |
91 |
| -function updateProjectDetails(projectId, detailsChange) { |
92 |
| - return models.Project.findByPk(projectId).then((project) => { |
93 |
| - const updatedDetails = _.merge({}, project.details, detailsChange); |
94 |
| - return project.update({ details: updatedDetails }); |
95 |
| - }); |
96 |
| -} |
97 |
| - |
98 |
| -describe('Update Scope Change Rquest', () => { |
99 |
| - let project; |
100 |
| - let scopeChangeRequest; |
101 |
| - |
102 |
| - before((done) => { |
103 |
| - testUtil |
104 |
| - .clearDb() |
105 |
| - .then(() => createProject(PROJECT_STATUS.REVIEWED)) |
106 |
| - .then((_project) => { |
107 |
| - project = _project; |
108 |
| - return project; |
109 |
| - }) |
110 |
| - .then(_project => createScopeChangeRequest(_project)) |
111 |
| - .then((_scopeChangeRequest) => { |
112 |
| - scopeChangeRequest = _scopeChangeRequest; |
113 |
| - return scopeChangeRequest; |
114 |
| - }) |
115 |
| - .then(() => done()); |
116 |
| - }); |
117 |
| - |
118 |
| - after((done) => { |
119 |
| - testUtil.clearDb(done); |
120 |
| - }); |
121 |
| - |
122 |
| - describe('PATCH projects/{projectId}/scopeChangeRequests/{requestId}', () => { |
123 |
| - it('Should approve change request with customer login', (done) => { |
124 |
| - request(server) |
125 |
| - .patch(`/v5/projects/${project.id}/scopeChangeRequests/${scopeChangeRequest.id}`) |
126 |
| - .set({ |
127 |
| - Authorization: `Bearer ${testUtil.jwts.member}`, |
128 |
| - }) |
129 |
| - .send({ |
130 |
| - status: SCOPE_CHANGE_REQ_STATUS.APPROVED, |
131 |
| - }) |
132 |
| - .expect(200) |
133 |
| - .end((err) => { |
134 |
| - if (err) { |
135 |
| - done(err); |
136 |
| - } else { |
137 |
| - models.ScopeChangeRequest.findOne({ where: { id: scopeChangeRequest.id } }).then((_scopeChangeRequest) => { |
138 |
| - assertStatus(_scopeChangeRequest, SCOPE_CHANGE_REQ_STATUS.APPROVED); |
139 |
| - done(); |
140 |
| - }); |
141 |
| - } |
142 |
| - }); |
143 |
| - }); |
144 |
| - |
145 |
| - it('Should activate change request with manager login', (done) => { |
146 |
| - // Updating project details before activation. This is used in a later test case |
147 |
| - updateProjectDetails(project.id, { apiDefinition: { notes: 'Please include swagger docs' } }).then(() => { |
148 |
| - request(server) |
149 |
| - .patch(`/v5/projects/${project.id}/scopeChangeRequests/${scopeChangeRequest.id}`) |
150 |
| - .set({ |
151 |
| - Authorization: `Bearer ${testUtil.jwts.manager}`, |
152 |
| - }) |
153 |
| - .send({ |
154 |
| - status: SCOPE_CHANGE_REQ_STATUS.ACTIVATED, |
155 |
| - }) |
156 |
| - .expect(200) |
157 |
| - .end((err) => { |
158 |
| - if (err) { |
159 |
| - done(err); |
160 |
| - } else { |
161 |
| - models.ScopeChangeRequest.findOne({ where: { id: scopeChangeRequest.id } }) |
162 |
| - .then((_scopeChangeRequest) => { |
163 |
| - assertStatus(_scopeChangeRequest, SCOPE_CHANGE_REQ_STATUS.ACTIVATED); |
164 |
| - done(); |
165 |
| - }); |
166 |
| - } |
167 |
| - }); |
168 |
| - }); |
169 |
| - }); |
170 |
| - |
171 |
| - it('Should update details field of project on activation', (done) => { |
172 |
| - models.Project.findOne({ where: { id: project.id } }).then((_project) => { |
173 |
| - const numberScreens = _.get(_project, 'details.appDefinition.numberScreens'); |
174 |
| - sinon.assert.match(numberScreens, '5-8'); |
175 |
| - done(); |
176 |
| - }); |
177 |
| - }); |
178 |
| - |
179 |
| - it("Should preserve fields of details json that doesn't change the scope on activation", (done) => { |
180 |
| - models.Project.findOne({ where: { id: project.id } }).then((_project) => { |
181 |
| - const apiNotes = _.get(_project, 'details.apiDefinition.notes'); |
182 |
| - sinon.assert.match(apiNotes, 'Please include swagger docs'); |
183 |
| - done(); |
184 |
| - }); |
185 |
| - }); |
186 |
| - |
187 |
| - it('Should not allow updating oldScope', (done) => { |
188 |
| - request(server) |
189 |
| - .patch(`/v5/projects/${project.id}/scopeChangeRequests/${scopeChangeRequest.id}`) |
190 |
| - .set({ |
191 |
| - Authorization: `Bearer ${testUtil.jwts.manager}`, |
192 |
| - }) |
193 |
| - .send({ |
194 |
| - oldScope: {}, |
195 |
| - }) |
196 |
| - .expect(400) |
197 |
| - .end(err => done(err)); |
198 |
| - }); |
199 |
| - |
200 |
| - it('Should not allow updating newScope', (done) => { |
201 |
| - request(server) |
202 |
| - .patch(`/v5/projects/${project.id}/scopeChangeRequests/${scopeChangeRequest.id}`) |
203 |
| - .set({ |
204 |
| - Authorization: `Bearer ${testUtil.jwts.manager}`, |
205 |
| - }) |
206 |
| - .send({ |
207 |
| - newScope: {}, |
208 |
| - }) |
209 |
| - .expect(400) |
210 |
| - .end(err => done(err)); |
211 |
| - }); |
212 |
| - }); |
213 |
| -}); |
0 commit comments