-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathProposal.js
270 lines (250 loc) · 9.38 KB
/
Proposal.js
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* eslint-disable @typescript-eslint/no-var-requires */
"use strict";
const utils = require("./LoginUtils");
const { TestData } = require("./TestData");
let accessTokenProposalIngestor = null,
accessTokenAdminIngestor = null,
accessTokenArchiveManager = null,
defaultProposalId = null,
proposalId = null,
attachmentId = null;
describe("1500: Proposal: Simple Proposal", () => {
before(() => {
db.collection("Proposal").deleteMany({});
});
beforeEach((done) => {
utils.getToken(
appUrl,
{
username: "proposalIngestor",
password: TestData.Accounts["proposalIngestor"]["password"],
},
(tokenVal) => {
accessTokenProposalIngestor = tokenVal;
utils.getToken(
appUrl,
{
username: "adminIngestor",
password: TestData.Accounts["adminIngestor"]["password"],
},
(tokenVal) => {
accessTokenAdminIngestor = tokenVal;
utils.getToken(
appUrl,
{
username: "archiveManager",
password: TestData.Accounts["archiveManager"]["password"],
},
(tokenVal) => {
accessTokenArchiveManager = tokenVal;
done();
},
);
},
);
},
);
});
// the following two function definition prepare for
// multi-delete actions to finish
async function deleteProposal(item) {
const response = await request(appUrl)
.delete("/api/v3/Proposals/" + item.proposalId)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenArchiveManager}` })
.expect(TestData.SuccessfulDeleteStatusCode);
return response;
}
async function processArray(array) {
for (const item of array) {
await deleteProposal(item);
}
}
it("0010: remove potentially existing proposals to guarantee uniqueness", async () => {
return request(appUrl)
.get("/api/v3/Proposals")
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
// now remove all these entries
processArray(res.body);
});
});
// check if proposal is valid
it("0020: check if minimal proposal is valid", async () => {
return request(appUrl)
.post("/api/v3/Proposals/isValid")
.send(TestData.ProposalCorrectMin)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryValidStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("valid").and.equal(true);
});
});
it("0030: adds a new proposal with minimal data", async () => {
return request(appUrl)
.post("/api/v3/Proposals")
.send(TestData.ProposalCorrectMin)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryCreatedStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("ownerGroup").and.be.string;
res.body.should.have.property("proposalId").and.be.string;
defaultProposalId = res.body["proposalId"];
proposalId = encodeURIComponent(res.body["proposalId"]);
});
});
it("0040: cannot add new proposal with same proposal id", async () => {
return request(appUrl)
.post("/api/v3/Proposals")
.send(TestData.ProposalCorrectMin)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.ConflictStatusCode);
});
// check if proposal is valid
it("0050: check if complete proposal is valid", async () => {
return request(appUrl)
.post("/api/v3/Proposals/isValid")
.send(TestData.ProposalCorrectComplete)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryValidStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("valid").and.equal(true);
});
});
it("0060: adds a new proposal with complete data", async () => {
return request(appUrl)
.post("/api/v3/Proposals")
.send(TestData.ProposalCorrectComplete)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryCreatedStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("ownerGroup").and.be.string;
res.body.should.have.property("proposalId").and.be.string;
defaultProposalId = res.body["proposalId"];
proposalId = encodeURIComponent(res.body["proposalId"]);
});
});
// check if proposal with additional field is valid
it("0070: check if complete proposal with extra field is valid", async () => {
return request(appUrl)
.post("/api/v3/Proposals/isValid")
.send(TestData.ProposalWrong_1)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryValidStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("valid").and.equal(false);
});
});
it("0080: check if complete proposal with two similar MeasurementPeriod.id is valid", async () => {
return request(appUrl)
.post("/api/v3/Proposals/isValid")
.send(TestData.ProposalWrong_2)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryValidStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("valid").and.equal(false);
});
});
it("0090: adds a new complete proposal with an extra field, which should fail", async () => {
return request(appUrl)
.post("/api/v3/Proposals")
.send(TestData.ProposalWrong_1)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.BadRequestStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.statusCode.should.not.equal(200);
});
});
it("0100: should fetch this new proposal", async () => {
return request(appUrl)
.get("/api/v3/Proposals/" + proposalId)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have.property("createdBy").and.be.string;
res.body.should.have.property("updatedBy").and.be.string;
});
});
it("0110: should add a new attachment to this proposal", async () => {
let testAttachment = { ...TestData.AttachmentCorrect };
testAttachment.proposalId = defaultProposalId;
return request(appUrl)
.post("/api/v3/Proposals/" + proposalId + "/attachments")
.send(testAttachment)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.EntryCreatedStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.have
.property("thumbnail")
.and.equal(testAttachment.thumbnail);
res.body.should.have
.property("caption")
.and.equal(testAttachment.caption);
res.body.should.have
.property("ownerGroup")
.and.equal(testAttachment.ownerGroup);
res.body.should.have.property("accessGroups");
res.body.should.have.property("createdBy").and.be.string;
res.body.should.have.property("updatedBy").and.be.string;
res.body.should.have.property("id").and.be.string;
res.body.should.have
.property("proposalId")
.and.equal(testAttachment.proposalId);
attachmentId = encodeURIComponent(res.body["id"]);
});
});
it("0120: should fetch this proposal attachment", async () => {
return request(appUrl)
.get("/api/v3/Proposals/" + proposalId + "/attachments")
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
res.body.should.be.instanceof(Array);
res.body[res.body.length - 1].id.should.be.equal(attachmentId);
});
});
it("0130: should delete this proposal attachment", async () => {
return request(appUrl)
.delete(
"/api/v3/Proposals/" + proposalId + "/attachments/" + attachmentId,
)
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenProposalIngestor}` })
.expect(TestData.SuccessfulDeleteStatusCode);
});
it("0140: admin can remove all existing proposals", async () => {
return await request(appUrl)
.get("/api/v3/Proposals")
.set("Accept", "application/json")
.set({ Authorization: `Bearer ${accessTokenAdminIngestor}` })
.expect(TestData.SuccessfulGetStatusCode)
.expect("Content-Type", /json/)
.then((res) => {
return processArray(res.body);
});
});
});