s3_relay 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +209 -0
  4. data/Rakefile +31 -0
  5. data/app/assets/javascripts/s3_relay.coffee +112 -0
  6. data/app/assets/stylesheets/s3_relay.css +31 -0
  7. data/app/controllers/s3_relay/uploads_controller.rb +65 -0
  8. data/app/helpers/s3_relay/uploads_helper.rb +23 -0
  9. data/app/models/s3_relay/upload.rb +46 -0
  10. data/config/routes.rb +5 -0
  11. data/db/migrate/20141009000804_create_s3_relay_uploads.rb +19 -0
  12. data/lib/s3_relay.rb +4 -0
  13. data/lib/s3_relay/base.rb +35 -0
  14. data/lib/s3_relay/engine.rb +18 -0
  15. data/lib/s3_relay/model.rb +54 -0
  16. data/lib/s3_relay/private_url.rb +33 -0
  17. data/lib/s3_relay/s3_relay.rb +4 -0
  18. data/lib/s3_relay/upload_presigner.rb +60 -0
  19. data/lib/s3_relay/version.rb +3 -0
  20. data/test/controllers/s3_relay/uploads_controller_test.rb +138 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +13 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  25. data/test/dummy/app/controllers/application_controller.rb +5 -0
  26. data/test/dummy/app/helpers/application_helper.rb +2 -0
  27. data/test/dummy/app/models/product.rb +6 -0
  28. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/test/dummy/bin/bundle +3 -0
  30. data/test/dummy/bin/rails +4 -0
  31. data/test/dummy/bin/rake +4 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/config/application.rb +23 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/database.yml +22 -0
  36. data/test/dummy/config/environment.rb +5 -0
  37. data/test/dummy/config/environments/development.rb +37 -0
  38. data/test/dummy/config/environments/production.rb +78 -0
  39. data/test/dummy/config/environments/test.rb +39 -0
  40. data/test/dummy/config/initializers/assets.rb +8 -0
  41. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  42. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  43. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  44. data/test/dummy/config/initializers/inflections.rb +16 -0
  45. data/test/dummy/config/initializers/mime_types.rb +4 -0
  46. data/test/dummy/config/initializers/session_store.rb +3 -0
  47. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  48. data/test/dummy/config/locales/en.yml +23 -0
  49. data/test/dummy/config/routes.rb +7 -0
  50. data/test/dummy/config/secrets.yml +22 -0
  51. data/test/dummy/db/migrate/20141021002149_create_products.rb +9 -0
  52. data/test/dummy/db/schema.rb +41 -0
  53. data/test/dummy/log/development.log +53 -0
  54. data/test/dummy/log/test.log +14631 -0
  55. data/test/dummy/public/404.html +67 -0
  56. data/test/dummy/public/422.html +67 -0
  57. data/test/dummy/public/500.html +66 -0
  58. data/test/dummy/public/favicon.ico +0 -0
  59. data/test/factories/products.rb +5 -0
  60. data/test/factories/uploads.rb +15 -0
  61. data/test/helpers/s3_relay/uploads_helper_test.rb +13 -0
  62. data/test/lib/s3_relay/model_test.rb +81 -0
  63. data/test/lib/s3_relay/private_url_test.rb +28 -0
  64. data/test/lib/s3_relay/upload_presigner_test.rb +38 -0
  65. data/test/models/s3_relay/upload_test.rb +128 -0
  66. data/test/support/database_cleaner.rb +14 -0
  67. data/test/test_helper.rb +28 -0
  68. metadata +302 -0
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ body {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body>
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ FactoryGirl.define do
2
+ factory :product do
3
+ name "Flux Capacitor"
4
+ end
5
+ end
@@ -0,0 +1,15 @@
1
+ FactoryGirl.define do
2
+ factory :upload, class: "S3Relay::Upload" do
3
+ uuid SecureRandom.uuid
4
+ filename "cat.png"
5
+ content_type "image/png"
6
+ end
7
+
8
+ factory :icon_upload, parent: :upload do
9
+ upload_type "IconUpload"
10
+ end
11
+
12
+ factory :photo_upload, parent: :upload do
13
+ upload_type "PhotoUpload"
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ require "test_helper"
2
+
3
+ describe S3Relay::UploadsHelper do
4
+ before { @product = FactoryGirl.create(:product) }
5
+
6
+ describe "#s3_relay_field" do
7
+ it do
8
+ s3_relay_field(@product, :photo_uploads, multiple: true)
9
+ .must_equal %Q{<div class="s3r-container" data-association="photo_uploads" data-parent-id="#{@product.id}" data-parent-type="product"><input class="s3r-field" id="file" multiple="multiple" name="file" type="file" /><table class="s3r-upload-list"></table></div>}
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,81 @@
1
+ require "test_helper"
2
+
3
+ describe S3Relay::Model do
4
+ before do
5
+ @product = FactoryGirl.build(:product)
6
+ end
7
+
8
+ describe "#s3_relay" do
9
+
10
+ describe "associations" do
11
+ it "has_one" do
12
+ Product.reflect_on_association(:icon_upload).macro
13
+ .must_equal(:has_one)
14
+ end
15
+
16
+ it "has_many" do
17
+ Product.reflect_on_association(:photo_uploads).macro
18
+ .must_equal(:has_many)
19
+ end
20
+ end
21
+
22
+ describe "scopes" do
23
+ before do
24
+ @product.save
25
+ @icon = FactoryGirl.create(:icon_upload, parent: @product)
26
+ @photo_1 = FactoryGirl.create(:photo_upload, parent: @product)
27
+ @photo_2 = FactoryGirl.create(:photo_upload, parent: @product)
28
+ end
29
+
30
+ describe "has_one" do
31
+ it { @product.icon_upload.must_equal @icon }
32
+ end
33
+
34
+ describe "has_many" do
35
+ it do
36
+ @product.photo_uploads.pluck(:id).sort
37
+ .must_equal [@photo_1.id, @photo_2.id]
38
+ end
39
+ end
40
+ end
41
+
42
+ describe "attributes" do
43
+ describe "has_one" do
44
+ it { @product.must_respond_to :new_photo_uploads_uuids= }
45
+ end
46
+
47
+ describe "has_many" do
48
+ it { @product.must_respond_to :new_icon_upload_uuid= }
49
+ end
50
+ end
51
+
52
+ describe "association method" do
53
+ before do
54
+ @product = FactoryGirl.create(:product)
55
+ @icon = FactoryGirl.create(:icon_upload)
56
+ @photo_1 = FactoryGirl.create(:photo_upload)
57
+ @photo_2 = FactoryGirl.create(:photo_upload)
58
+ end
59
+
60
+ describe "has_one" do
61
+ it do
62
+ @product.icon_upload.must_equal nil
63
+ @product.new_icon_upload_uuid = @icon.uuid
64
+ @product.associate_icon_upload
65
+ @product.icon_upload.must_equal @icon
66
+ end
67
+ end
68
+
69
+ describe "has_many" do
70
+ it do
71
+ @product.photo_uploads.must_equal []
72
+ @product.new_photo_uploads_uuids = [@photo_1.uuid, @photo_2.uuid]
73
+ @product.associate_photo_uploads
74
+ @product.photo_uploads.must_equal [@photo_1, @photo_2]
75
+ end
76
+ end
77
+ end
78
+
79
+ end
80
+
81
+ end
@@ -0,0 +1,28 @@
1
+ require "test_helper"
2
+
3
+ describe S3Relay::PrivateUrl do
4
+ before do
5
+ S3Relay::PrivateUrl.any_instance.stubs(:access_key_id)
6
+ .returns("access-key-id")
7
+ S3Relay::PrivateUrl.any_instance.stubs(:secret_access_key)
8
+ .returns("secret-access-key")
9
+ S3Relay::PrivateUrl.any_instance.stubs(:region)
10
+ .returns("region")
11
+ S3Relay::PrivateUrl.any_instance.stubs(:bucket)
12
+ .returns("bucket")
13
+ S3Relay::PrivateUrl.any_instance.stubs(:acl)
14
+ .returns("acl")
15
+ end
16
+
17
+ describe "#generate" do
18
+ it do
19
+ uuid = "123-456-789"
20
+ file = "Crazy + c@t picture.png"
21
+ time = Time.parse("2014-01-01 12:00am")
22
+ url = S3Relay::PrivateUrl.new(uuid, file, expires: time).generate
23
+
24
+ url.must_equal "https://bucket.s3-region.amazonaws.com/123-456-789/Crazy%20%2B%20c@t%20picture.png?AWSAccessKeyId=access-key-id&Expires=1388563200&Signature=8Kp5NL77iycg4CFFwxboo905t%2Fs%3D"
25
+ end
26
+ end
27
+
28
+ end
@@ -0,0 +1,38 @@
1
+ require "test_helper"
2
+
3
+ describe S3Relay::UploadPresigner do
4
+ before do
5
+ S3Relay::UploadPresigner.any_instance.stubs(:access_key_id)
6
+ .returns("access-key-id")
7
+ S3Relay::UploadPresigner.any_instance.stubs(:secret_access_key)
8
+ .returns("secret-access-key")
9
+ S3Relay::UploadPresigner.any_instance.stubs(:region)
10
+ .returns("region")
11
+ S3Relay::UploadPresigner.any_instance.stubs(:bucket)
12
+ .returns("bucket")
13
+ S3Relay::UploadPresigner.any_instance.stubs(:acl)
14
+ .returns("acl")
15
+ end
16
+
17
+ describe "#form_data" do
18
+ it do
19
+ uuid = SecureRandom.uuid
20
+ time = Time.parse("2014-12-01 12:00am")
21
+
22
+ S3Relay::UploadPresigner.any_instance.stubs(:uuid).returns(uuid)
23
+
24
+ data = S3Relay::UploadPresigner.new(expires: time).form_data
25
+
26
+ data["awsaccesskeyid"].must_equal "access-key-id"
27
+ data["x_amz_server_side_encryption"].must_equal "AES256"
28
+ data["key"].must_equal "#{uuid}/${filename}"
29
+ data["success_action_status"].must_equal "201"
30
+ data["acl"].must_equal "acl"
31
+ data["endpoint"].must_equal "https://bucket.s3-region.amazonaws.com"
32
+ data["policy"].length.must_equal 344 # TODO: Improve this
33
+ data["signature"].length.must_equal 28 # TODO: Improve this
34
+ data["uuid"].must_equal uuid
35
+ end
36
+ end
37
+
38
+ end
@@ -0,0 +1,128 @@
1
+ require "test_helper"
2
+
3
+ describe S3Relay::Upload do
4
+ before do
5
+ @upload = S3Relay::Upload.new
6
+ end
7
+
8
+ describe "associations" do
9
+ describe "parent" do
10
+ it do
11
+ S3Relay::Upload.reflect_on_association(:parent).macro
12
+ .must_equal :belongs_to
13
+ end
14
+ end
15
+ end
16
+
17
+ describe "validations" do
18
+ describe "uuid" do
19
+ it "validates presence" do
20
+ @upload.valid?
21
+ @upload.errors[:uuid].must_include("can't be blank")
22
+ end
23
+
24
+ it "validates uniqueness" do
25
+ S3Relay::Upload.new(uuid: @upload.uuid).save!(validate: false)
26
+ @upload.valid?
27
+ @upload.errors[:uuid].must_include("has already been taken")
28
+ end
29
+ end
30
+
31
+ describe "upload_type" do
32
+ it "validates presence" do
33
+ @upload.valid?
34
+ @upload.errors[:upload_type].must_include("can't be blank")
35
+ end
36
+ end
37
+
38
+ describe "filename" do
39
+ it "validates presence" do
40
+ @upload.valid?
41
+ @upload.errors[:filename].must_include("can't be blank")
42
+ end
43
+ end
44
+
45
+ describe "content_type" do
46
+ it "validates presence" do
47
+ @upload.valid?
48
+ @upload.errors[:content_type].must_include("can't be blank")
49
+ end
50
+ end
51
+
52
+ describe "pending_at" do
53
+ it "validates presence" do
54
+ @upload.pending_at = nil
55
+ @upload.valid?
56
+ @upload.errors[:pending_at].must_include("can't be blank")
57
+ end
58
+ end
59
+ end
60
+
61
+ describe "scopes" do
62
+ before do
63
+ @pending = S3Relay::Upload.new
64
+ .tap { |u| u.save(validate: false) }
65
+ @imported = S3Relay::Upload.new(state: "imported")
66
+ .tap { |u| u.save(validate: false) }
67
+ end
68
+
69
+ describe "pending" do
70
+ it do
71
+ results = S3Relay::Upload.pending.all
72
+ results.must_include @pending
73
+ results.wont_include @imported
74
+ end
75
+ end
76
+
77
+ describe "imported" do
78
+ it do
79
+ results = S3Relay::Upload.imported.all
80
+ results.wont_include @pending
81
+ results.must_include @imported
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "upon finalization" do
87
+ it do
88
+ @upload.state.must_equal "pending"
89
+ @upload.pending_at.wont_equal nil
90
+ end
91
+ end
92
+
93
+ describe "#pending?" do
94
+ it { @upload.pending?.must_equal true }
95
+ end
96
+
97
+ describe "#imported" do
98
+ it do
99
+ @upload.state = "imported"
100
+ @upload.imported?.must_equal true
101
+ end
102
+ end
103
+
104
+ describe "#mark_imported!" do
105
+ it do
106
+ @upload.mark_imported!
107
+ @upload.state.must_equal "imported"
108
+ @upload.imported_at.wont_equal nil
109
+ end
110
+ end
111
+
112
+ describe "#private_url" do
113
+ it do
114
+ uuid = SecureRandom.uuid
115
+ filename = "cat.png"
116
+ @upload.uuid = uuid
117
+ @upload.filename = filename
118
+
119
+ url = "url"
120
+ klass = stub
121
+ S3Relay::PrivateUrl.expects(:new).with(uuid, filename).returns(klass)
122
+ klass.expects(:generate).returns(url)
123
+
124
+ @upload.private_url.must_equal url
125
+ end
126
+ end
127
+
128
+ end