forked from kaliber-scala/play-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
S3Spec.scala
428 lines (323 loc) · 13.3 KB
/
S3Spec.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package fly.play.s3
import java.io.File
import java.lang.IllegalArgumentException
import java.util.Date
import scala.concurrent.Await
import scala.concurrent.Awaitable
import scala.concurrent.Future
import scala.concurrent.duration.Duration
import scala.util.Failure
import scala.util.Success
import org.specs2.execute.AsResult
import org.specs2.mutable.Specification
import org.specs2.specification.Example
import fly.play.aws.auth.SimpleAwsCredentials
import fly.play.s3.acl.CanonicalUser
import fly.play.s3.acl.FULL_CONTROL
import fly.play.s3.acl.Grant
import fly.play.s3.acl.Group
import fly.play.s3.acl.READ
import fly.play.s3.upload.Condition
import fly.play.s3.upload.Form
import fly.play.s3.upload.FormElement
import play.api.http.HeaderNames.CONTENT_TYPE
import play.api.http.HeaderNames.LOCATION
import play.api.libs.json.Json
import play.api.libs.json.Json.toJsFieldJsValueWrapper
import play.api.libs.ws.WS
import play.api.test.FakeApplication
import play.api.test.Helpers.running
import utils.MultipartFormData
import java.net.URLEncoder
class S3Spec extends Specification {
sequential
val testBucketName = "s3playlibrary.rhinofly.net"
def fakeApplication(additionalConfiguration: Map[String, _ <: Any] = Map.empty) =
FakeApplication(new File("./test"), additionalConfiguration = additionalConfiguration)
implicit class InAppExample(s: String) {
def inApp[T: AsResult](r: => T): Example =
s in running(fakeApplication()) {
r
}
}
def s3WithCredentials = S3.fromConfig
def await[T](a: Awaitable[T]): T =
Await.result(a, Duration.Inf)
def noException[T](a: Awaitable[T]) =
await(a) must not(throwA[Throwable])
"S3" should {
"have the correct default value for host" inApp {
s3WithCredentials.host === "s3.amazonaws.com"
}
"have the correct default value for https" inApp {
s3WithCredentials.https === false
}
"get the correct value for host from the configuration" in {
running(fakeApplication(Map("s3.host" -> "testHost"))) {
s3WithCredentials.host === "testHost"
}
}
"get the correct value for https from the configuration" in {
running(fakeApplication(Map("s3.https" -> true))) {
s3WithCredentials.https === true
}
}
"return an instance of bucket" inApp {
S3(testBucketName) must beAnInstanceOf[Bucket]
}
"return an instance of bucket with different credentials" inApp {
implicit val awsCredentials = SimpleAwsCredentials("test", "test")
val bucket = S3(testBucketName)
bucket.s3.credentials must_== awsCredentials
}
"create the correct url" inApp {
S3.url("s3playlibrary.rhinofly.net", "privateREADME.txt", 1343845068) must_==
"http://s3playlibrary.rhinofly.net.s3.amazonaws.com/privateREADME.txt?AWSAccessKeyId=AKIAIJJLEMC6OSI2DN2A&Signature=jkbj7%2ByalcC%2Fw%2BKxtMXLIn7b%2Frc%3D&Expires=1343845068"
}
}
"Bucket" should {
def testBucket = S3(testBucketName)
"by default have / as delimiter" inApp {
testBucket.delimiter must_== Some("/")
}
"should be able to change delimiter" inApp {
var bucket = testBucket
bucket = bucket withDelimiter Some("-")
bucket.delimiter must_== Some("-")
bucket = bucket withDelimiter None
bucket.delimiter must_== None
bucket = bucket withDelimiter "/"
bucket.delimiter must_== Some("/")
}
"give an error if we request an element that does not exist" inApp {
val result = testBucket.get("nonExistingElement")
val value = Await.ready(result, Duration.Inf).value.get
value must beLike {
case Failure(S3Exception(404, "NoSuchKey", _, _)) => ok
case Failure(err) => failure("Unexpected failure: " + err)
case Success(x) => failure("Error was expected, no error received: " + x)
}
}
"be able to add a file" inApp {
val result = testBucket + BucketFile("README.txt", "text/plain", """
This is a bucket used for testing the S3 module of play
""".getBytes)
noException(result)
}
"with the correct mime type" inApp {
val result = s3WithCredentials.get(testBucket.name, Some("README.txt"), None, None)
val value = await(result)
value.header(CONTENT_TYPE) must_== Some("text/plain")
}
"be able to check if it exists" inApp {
val result = testBucket get "README.txt"
val value = await(result)
value match {
case BucketFile("README.txt", _, _, _, _) => success
case f => failure("Wrong file returned: " + f)
}
}
"be able add a file with a prefix" inApp {
val result = testBucket + BucketFile("testPrefix/README.txt", "text/plain", """
This is a bucket used for testing the S3 module of play
""".getBytes)
noException(result)
}
"list should be iterable" inApp {
testBucket.list must beAnInstanceOf[Future[Iterable[BucketItem]]]
}
"list should have a size of 2" inApp {
val result = testBucket.list
val value = await(result)
value.size === 2
}
"list should have the correct contents" inApp {
val result = testBucket.list
val value = await(result)
val seq = value.toSeq
seq(0) match {
case BucketItem("README.txt", false) => success
case f => failure("Wrong file returned: " + f)
}
seq(1) match {
case BucketItem("testPrefix/", true) => success
case f => failure("Wrong file returned: " + f)
}
}
"list with prefix should return the correct contents" inApp {
val result = testBucket.list("testPrefix/")
val value = await(result)
value.toSeq(0) match {
case BucketItem("testPrefix/README.txt", false) => success
case f => failure("Wrong file returned: " + f)
}
}
"be able to delete a file" inApp {
val result = testBucket - "testPrefix/README.txt"
noException(result)
}
var url = ""
"be able to add a file with private ACL and create a url for it" inApp {
val fileName = "privateREADME.txt"
url = testBucket.url(fileName, 86400)
val result = testBucket + BucketFile(fileName, "text/plain", """
This is a bucket used for testing the S3 module of play
""".getBytes, Some(AUTHENTICATED_READ), None)
noException(result)
}
"be able to retrieve the private file using the generated url" inApp {
val result = WS.url(url).get
val value = await(result)
value.status must_== 200
}
"be able to rename a file" inApp {
val result = testBucket rename ("privateREADME.txt", "private2README.txt", AUTHENTICATED_READ)
noException(result)
}
"be able to change the file's ACL" inApp {
val result = testBucket.updateAcl("private2README.txt", PUBLIC_READ)
noException(result)
}
"be able to check if the file's ACL has been updated" inApp {
val result = testBucket.getAcl("private2README.txt")
val value = await(result)
value must beLike {
case Seq(
Grant(FULL_CONTROL, CanonicalUser(_, _)),
Grant(READ, Group(uri))) =>
uri must endWith("AllUsers")
}
}
"be able to delete the renamed private file" inApp {
val result = testBucket remove "private2README.txt"
noException(result)
}
"be able to add a file with custom headers" inApp {
val result = testBucket + BucketFile("headerTest.txt", "text/plain", """
This file is used for testing custome headers
""".getBytes, None, Some(Map("x-amz-meta-testheader" -> "testHeaderValue")))
noException(result)
}
"be able to retrieve a file with custom headers" inApp {
val result = testBucket.get("headerTest.txt")
val value = await(result)
value match {
case BucketFile("headerTest.txt", _, _, _, Some(headers)) => (headers get "x-amz-meta-testheader") must_== Some("testHeaderValue")
case f => failure("Wrong file returned: " + f)
}
}
"be able to delete the file with custom headers" inApp {
val result = testBucket remove "headerTest.txt"
noException(result)
}
"throw an error if the bucket file contains content on initiateMultipartUpload" inApp {
val bucketFile = BucketFile("test-multipart-file.txt", "text/plain", Array(0))
testBucket.initiateMultipartUpload(bucketFile) should throwAn[IllegalArgumentException]
}
"throw an error if the BucketFilePart has a part number that is not between 1 and 10000" inApp {
val uploadTicket = BucketFileUploadTicket("test-multipart-file.txt", "")
val filePart1 = BucketFilePart(0, Array.empty)
val filePart2 = BucketFilePart(10001, Array.empty)
testBucket.uploadPart(uploadTicket, filePart1) must throwAn[IllegalArgumentException]
testBucket.uploadPart(uploadTicket, filePart2) must throwAn[IllegalArgumentException]
}
"be able to complete a multipart upload" inApp {
val fileName = "test-multipart-file.txt"
val fileContentType = "text/plain"
val partContent: Array[Byte] = Array.fill(100)(0)
val bucketFile = BucketFile(fileName, fileContentType)
val uploadTicket = await(testBucket.initiateMultipartUpload(bucketFile))
val filePart = BucketFilePart(1, partContent)
val partUploadTicket = await(testBucket.uploadPart(uploadTicket, filePart))
val result = testBucket.completeMultipartUpload(uploadTicket, Seq(partUploadTicket))
noException(result)
val file = await(testBucket get fileName)
file must beLike {
case BucketFile(name, contentType, content, _, _) =>
(name === fileName) and
(contentType must startWith(fileContentType)) and
(content === partContent)
}
noException(testBucket remove fileName)
}
"be able to supply an uploadPolicy" inApp {
val policyBuilder =
testBucket.uploadPolicy(new Date)
.withConditions(
Condition.key eq "privateREADME.txt",
Condition.acl eq PUBLIC_READ,
Condition.contentLengthRange from 0 to 10000,
CONTENT_TYPE -> "image/jpeg")
val policy = policyBuilder.json
val expectedConditions = Json.arr(
Json.obj("bucket" -> "s3playlibrary.rhinofly.net"),
Json.obj("key" -> "privateREADME.txt"),
Json.obj("acl" -> "public-read"),
Json.arr("content-length-range", 0, 10000),
Json.obj(CONTENT_TYPE -> "image/jpeg"))
(policy \ "conditions") === expectedConditions
}
"be able to supply an uploadPolicy that can be used to alow browser upload" in {
running(fakeApplication(Map("ws.followRedirects" -> false))) {
val `1 minute from now` = System.currentTimeMillis + (1 * 60 * 1000)
import Condition._
val key = Condition.key
val expectedFileName = "test/file.html"
val expectedRedirectUrl = "http://fakehost:9000"
val expectedTags = "one,two"
val expectedContentType = "text/html"
val policy =
testBucket.uploadPolicy(new Date(`1 minute from now`))
.withConditions(
key startsWith "test/",
acl eq PUBLIC_READ,
successActionRedirect eq expectedRedirectUrl,
header(CONTENT_TYPE) startsWith "text/",
meta("tag").any)
// provide user input
val formFieldsFromPolicy =
Form(policy).fields
.map {
case FormElement("key", _, true) =>
"key" -> expectedFileName
case FormElement("x-amz-meta-tag", _, true) =>
"x-amz-meta-tag" -> expectedTags
case FormElement(CONTENT_TYPE, _, true) =>
CONTENT_TYPE -> expectedContentType
case FormElement(name, value, false) =>
name -> value
}
val expectedContent = "test text"
// file should be the last field
val formFields = formFieldsFromPolicy :+ ("file" -> expectedContent)
val data = MultipartFormData(formFields, "asdfghjkl123")
val response = await(WS.url(testBucket.url("")).post(data.body))
response.status === 303
response.header(LOCATION).get must startWith(expectedRedirectUrl)
val bucketFile = await(testBucket.get(expectedFileName))
bucketFile must beLike {
case BucketFile(name, contentType, content, None, headers) =>
name === expectedFileName
contentType === expectedContentType
new String(content) === expectedContent
headers.get("x-amz-meta-tag") === expectedTags
}
noException(testBucket remove expectedFileName)
}
}
"be able to add and delete files with 'weird' names" inApp {
def uploadListAndRemoveFileWithName(prefix:String, name: String) = {
await(testBucket + BucketFile(URLEncoder.encode(prefix + name, "UTF-8"), "text/plain", "test".getBytes))
await(testBucket.list(prefix)) must beLike {
case Seq(BucketItem(itemName, false)) => itemName === (prefix + name)
}
await(testBucket - URLEncoder.encode(prefix + name, "UTF-8"))
success
}
uploadListAndRemoveFileWithName("sample/", "test file.txt")
uploadListAndRemoveFileWithName("sample/", "test&;-file.txt")
uploadListAndRemoveFileWithName("sample/", "test & file.txt")
uploadListAndRemoveFileWithName("sample/", "test+&+file.txt")
}
}
}