/
AgoraMethodsSpec.scala
349 lines (310 loc) · 14.2 KB
/
AgoraMethodsSpec.scala
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package org.broadinstitute.dsde.agora.server.webservice
import org.broadinstitute.dsde.agora.server.AgoraTestData._
import org.broadinstitute.dsde.agora.server.model.AgoraApiJsonSupport._
import org.broadinstitute.dsde.agora.server.model.{AgoraEntity, AgoraEntityType}
import org.broadinstitute.dsde.agora.server.webservice.util.ApiUtil
import org.scalatest.{BeforeAndAfterAll, DoNotDiscover, FlatSpecLike}
import spray.http.MediaTypes._
import spray.http.StatusCodes._
import spray.http.{ContentType, HttpEntity}
import spray.httpx.SprayJsonSupport._
import spray.httpx.unmarshalling._
import spray.routing.ValidationRejection
@DoNotDiscover
class AgoraMethodsSpec extends ApiServiceSpec with FlatSpecLike {
var testEntity1WithId: AgoraEntity = _
var testEntity2WithId: AgoraEntity = _
var testEntity3WithId: AgoraEntity = _
var testEntity4WithId: AgoraEntity = _
var testEntity5WithId: AgoraEntity = _
var testEntity6WithId: AgoraEntity = _
var testEntity7WithId: AgoraEntity = _
var testEntityToBeRedactedWithId: AgoraEntity = _
override def beforeAll() = {
ensureDatabasesAreRunning()
testEntity1WithId = patiently(agoraBusiness.insert(testEntity1, mockAuthenticatedOwner.get))
testEntity2WithId = patiently(agoraBusiness.insert(testEntity2, mockAuthenticatedOwner.get))
testEntity3WithId = patiently(agoraBusiness.insert(testEntity3, mockAuthenticatedOwner.get))
testEntity4WithId = patiently(agoraBusiness.insert(testEntity4, mockAuthenticatedOwner.get))
testEntity5WithId = patiently(agoraBusiness.insert(testEntity5, mockAuthenticatedOwner.get))
testEntity6WithId = patiently(agoraBusiness.insert(testEntity6, mockAuthenticatedOwner.get))
testEntity7WithId = patiently(agoraBusiness.insert(testEntity7, mockAuthenticatedOwner.get))
testEntityToBeRedactedWithId = patiently(agoraBusiness.insert(testEntityToBeRedacted, mockAuthenticatedOwner.get))
}
override def afterAll() = {
clearDatabases()
}
"Agora" should "return information about a method, including metadata " in {
Get(ApiUtil.Methods.withLeadingVersion + "/" + namespace1.get + "/" + name1.get + "/"
+ testEntity1WithId.snapshotId.get) ~> methodsService.querySingleRoute ~> check {
handleError(entity.as[AgoraEntity], (entity: AgoraEntity) => assert(entity == testEntity1WithId))
assert(status == OK)
}
}
"Agora" should "return only payload in plain/text when parameter is set " in {
Get(ApiUtil.Methods.withLeadingVersion + "/" + namespace1.get + "/" + name1.get + "/"
+ testEntity1WithId.snapshotId.get + "?onlyPayload=true") ~> methodsService.querySingleRoute ~>
check {
assert(status == OK)
assert(mediaType == `text/plain`)
}
}
"Agora" should "return status 404, mediaType json when nothing matches query by namespace, name, snapshotId" in {
Get(ApiUtil.Methods.withLeadingVersion + "/foofoofoofoo/foofoofoo/99999"
) ~> methodsService.querySingleRoute ~> check {
assert(status == NotFound)
}
}
"Agora" should "return methods matching query by namespace and name" in {
Get(ApiUtil.Methods.withLeadingVersion + "?namespace=" + namespace1.get + "&name=" + name2.get) ~>
methodsService.queryRoute ~> check {
handleError(
entity.as[Seq[AgoraEntity]],
(entities: Seq[AgoraEntity]) =>
assert(entities.toSet == brief(Seq(testEntity3WithId, testEntity4WithId, testEntity5WithId, testEntity6WithId, testEntity7WithId)).toSet)
)
assert(status == OK)
}
}
"Agora" should "return methods matching query by synopsis and documentation" in {
Get(ApiUtil.Methods.withLeadingVersion + "?synopsis=" + uriEncode(synopsis1.get) + "&documentation=" +
uriEncode(documentation1.get)) ~>
methodsService.queryRoute ~>
check {
handleError(
entity.as[Seq[AgoraEntity]],
(entities: Seq[AgoraEntity]) =>
assert(entities.toSet == brief(Seq(testEntity1WithId, testEntity2WithId, testEntity3WithId, testEntity6WithId, testEntity7WithId)).toSet)
)
assert(status == OK)
}
}
"Agora" should "return methods matching query by owner and payload" in {
Get(ApiUtil.Methods.withLeadingVersion + "?owner=" + owner1.get + "&payload=" + uriEncode(payload1.get)) ~>
methodsService.queryRoute ~>
check {
handleError(
entity.as[Seq[AgoraEntity]],
(entities: Seq[AgoraEntity]) =>
assert(entities.toSet == brief(Seq(testEntity1WithId, testEntity3WithId, testEntity4WithId, testEntity5WithId)).toSet)
)
}
}
"Agora" should "create a method and return with a status of 201" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity) ~>
methodsService.postRoute ~> check {
handleError(entity.as[AgoraEntity], (entity: AgoraEntity) => {
assert(entity.namespace == namespace3)
assert(entity.name == name1)
assert(entity.synopsis == synopsis1)
assert(entity.documentation == documentation1)
assert(entity.owner == owner1)
assert(entity.payload == payload1)
assert(entity.snapshotId.isDefined)
assert(entity.createDate.isDefined)
})
assert(status == Created)
}
}
"Agora" should "return a 400 bad request when posting a malformed payload" in {
Post(ApiUtil.Methods.withLeadingVersion, testBadAgoraEntity) ~>
methodsService.postRoute ~> check {
assert(status === BadRequest)
assert(responseAs[String] != null)
}
}
"Agora" should "reject the request with validation errors when metadata is invalid" in {
val entity = new AgoraEntity(namespace= Option(" "), name= Option(" ") , synopsis= Option(" "), payload= Option(" "), entityType= Option(AgoraEntityType.Task))
Post(ApiUtil.Methods.withLeadingVersion, entity) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.nonEmpty)
}
}
"Agora" should "reject the request when posting with a payload of None" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(payload = None)) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("You must supply a payload.",None)))
}
}
"Agora" should "reject the request when posting with a payload of whitespace only" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(payload = Some(" "))) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("You must supply a payload.",None)))
}
}
"Agora" should "reject the request when posting with a snapshotId" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(snapshotId = Some(123))) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("You cannot specify a snapshotId. It will be assigned by the system.",None)))
}
}
"Agora" should "reject the request when posting with a namespace of None" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(namespace = None)) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Namespace cannot be empty",None)))
}
}
"Agora" should "reject the request when posting with a namespace of whitespace only" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(namespace = Some(" "))) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Namespace cannot be empty",None)))
}
}
"Agora" should "reject the request when posting with a name of None" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(name = None)) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Name cannot be empty",None)))
}
}
"Agora" should "reject the request when posting with a name of whitespace only" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(name = Some(" "))) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Name cannot be empty",None)))
}
}
"Agora" should "reject the request when posting with a synopsis of 81 characters" in {
val testSynopsis = Some(fillerText.take(81))
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(synopsis = testSynopsis)) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Synopsis must be less than 80 chars",None)))
}
}
"Agora" should "return a Created success code when posting with a synopsis of 80 characters" in {
val testSynopsis = Some(fillerText.take(80))
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(synopsis = testSynopsis)) ~>
methodsService.postRoute ~> check {
handleError(entity.as[AgoraEntity], (entity: AgoraEntity) => {
assert(entity.synopsis == testSynopsis)
})
assert(status == Created)
}
}
"Agora" should "return a Created success code when posting with a synopsis of 79 characters" in {
val testSynopsis = Some(fillerText.take(79))
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(synopsis = testSynopsis)) ~>
methodsService.postRoute ~> check {
handleError(entity.as[AgoraEntity], (entity: AgoraEntity) => {
assert(entity.synopsis == testSynopsis)
})
assert(status == Created)
}
}
"Agora" should "return a Created success code when posting with a synopsis of 74 characters" in {
val testSynopsis = Some(fillerText.take(74))
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(synopsis = testSynopsis)) ~>
methodsService.postRoute ~> check {
handleError(entity.as[AgoraEntity], (entity: AgoraEntity) => {
assert(entity.synopsis == testSynopsis)
})
assert(status == Created)
}
}
"Agora" should "reject the request when posting with a documentation of 10001 chars" in {
val testDocumentation = Some("x" * 10001)
Post(ApiUtil.Methods.withLeadingVersion, testAgoraEntity.copy(documentation = testDocumentation)) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(!handled)
assert(rejections.contains(ValidationRejection("Documentation must be less than 10kb",None)))
}
}
"Agora" should "reject 10kb of github markdown as method documentation" in {
val entityJSON = s"""{
| "namespace": "$namespace1",
| "name": "$name1",
| "synopsis": "",
| "documentation": "$getBigDocumentation",
| "payload": "",
| "entityType": "Task"
|}""".stripMargin
val entity = HttpEntity(
contentType = ContentType(`application/json`),
string = entityJSON)
Post(ApiUtil.Methods.withLeadingVersion, entity) ~>
wrapWithRejectionHandler {
methodsService.postRoute
} ~> check {
assert(status == BadRequest)
}
}
"Agora" should "not allow you to post a new configuration to the methods route" in {
Post(ApiUtil.Methods.withLeadingVersion, testAgoraConfigurationEntity) ~>
methodsService.postRoute ~> check {
rejection.isInstanceOf[ValidationRejection]
}
}
"Agora" should "allow method redaction" in {
Delete(ApiUtil.Methods.withLeadingVersion + "/" + testEntityToBeRedactedWithId.namespace.get + "/" +
testEntityToBeRedactedWithId.name.get + "/" + testEntityToBeRedactedWithId.snapshotId.get) ~>
methodsService.querySingleRoute ~> check {
assert(body.asString == "1")
}
}
"Agora" should "not allow redacted methods to be queried" in {
Get(ApiUtil.Methods.withLeadingVersion + "/" + testEntityToBeRedactedWithId.namespace.get + "/" +
testEntityToBeRedactedWithId.name.get + "/" + testEntityToBeRedactedWithId.snapshotId.get) ~>
methodsService.querySingleRoute ~> check {
assert(body.asString contains "not found")
}
}
"Agora" should "not let you specify a deserialized payload on the methods route" in {
Get(ApiUtil.Methods.withLeadingVersion + "/" + testMethodWithSnapshot1.namespace.get + "/" +
testMethodWithSnapshot1.name.get + "/" + testMethodWithSnapshot1.snapshotId.get + "?payloadAsObject=true") ~>
methodsService.querySingleRoute ~> check {
assert(body.asString contains "does not support payload deserialization")
assert(status == InternalServerError)
}
}
"Agora" should "accept and record a snapshot comment when creating the initial snapshot of a method" in {
Post(ApiUtil.Methods.withLeadingVersion, testMethodWithSnapshotComment1.copy(snapshotId = None)) ~>
methodsService.postRoute ~> check {
assert(status == Created)
assert(responseAs[AgoraEntity].snapshotComment == snapshotComment1)
assert(responseAs[AgoraEntity].snapshotId.contains(1))
}
}
"Agora" should "apply a new snapshot comment when creating a new method snapshot" in {
Post(ApiUtil.Methods.withLeadingVersion, testMethodWithSnapshotComment1.copy(snapshotId = None, snapshotComment = snapshotComment2)) ~>
methodsService.postRoute ~> check {
assert(status == Created)
assert(responseAs[AgoraEntity].snapshotComment == snapshotComment2)
assert(responseAs[AgoraEntity].snapshotId.contains(2))
}
}
"Agora" should "record no snapshot comment for a new method snapshot if none is provided" in {
Post(ApiUtil.Methods.withLeadingVersion, testMethodWithSnapshotComment1.copy(snapshotId = None, snapshotComment = None)) ~>
methodsService.postRoute ~> check {
assert(status == Created)
assert(responseAs[AgoraEntity].snapshotComment.isEmpty)
assert(responseAs[AgoraEntity].snapshotId.contains(3))
}
}
}