rubytus 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,123 @@
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+ require 'stringio'
4
+
5
+ class TestCommandRunner < MiniTest::Test
6
+ include Rubytus::Mock
7
+ include Goliath::TestHelper
8
+
9
+ def setup
10
+ @err = Proc.new { assert false, 'API request failed' }
11
+ end
12
+
13
+ def teardown
14
+ remove_data_dir
15
+ end
16
+
17
+ def test_setup_exit_for_invalid_data_dir
18
+ options = default_options.merge({
19
+ :data_dir => '/opt/123='
20
+ })
21
+
22
+ is_exit = false
23
+
24
+ begin
25
+ capture_io do
26
+ with_api(Rubytus::Command, options) {}
27
+ end
28
+ rescue SystemExit
29
+ is_exit = true
30
+ end
31
+
32
+ assert is_exit
33
+ end
34
+
35
+ def test_setup_exit_for_invalid_data_dir_permission
36
+ options = default_options.merge({
37
+ :data_dir => '/root'
38
+ })
39
+
40
+ is_exit = false
41
+
42
+ begin
43
+ capture_io do
44
+ with_api(Rubytus::Command, options) {}
45
+ end
46
+ rescue SystemExit
47
+ is_exit = true
48
+ end
49
+
50
+ assert is_exit
51
+ end
52
+
53
+ def test_setup_exit_for_invalid_max_size
54
+ options = {
55
+ :data_dir => data_dir,
56
+ :max_size => 'a'
57
+ }
58
+
59
+ is_exit = false
60
+
61
+ begin
62
+ capture_io do
63
+ with_api(Rubytus::Command, options) {}
64
+ end
65
+ rescue SystemExit
66
+ is_exit = true
67
+ end
68
+
69
+ assert is_exit
70
+ end
71
+
72
+ def test_setup_exit_for_invalid_base_path
73
+ options = {
74
+ :data_dir => data_dir,
75
+ :max_size => 512,
76
+ :base_path => '+foo+'
77
+ }
78
+
79
+ is_exit = false
80
+
81
+ begin
82
+ capture_io do
83
+ with_api(Rubytus::Command, options) {}
84
+ end
85
+ rescue SystemExit
86
+ is_exit = true
87
+ end
88
+
89
+ assert is_exit
90
+ end
91
+
92
+ def test_setup
93
+ options = {
94
+ :data_dir => data_dir,
95
+ :max_size => '512',
96
+ :base_path => '/uploads/'
97
+ }
98
+
99
+ with_api(Rubytus::Command, options) do |runner|
100
+ opts = runner.api.instance_variable_get('@options')
101
+
102
+ assert_kind_of Rubytus::Storage, opts[:storage]
103
+ assert_equal 512, opts[:max_size]
104
+ EM.stop
105
+ end
106
+ end
107
+
108
+ def test_options_parser
109
+ dir = data_dir
110
+
111
+ with_api(Rubytus::Command, default_options) do |runner|
112
+ op = runner.api.options_parser(OptionParser.new, default_options)
113
+ op.parse!(['-m', '512', '-f', dir, '-b', '/uploads/'])
114
+
115
+ options = runner.api.instance_variable_get('@options')
116
+
117
+ assert_equal dir, options[:data_dir]
118
+ assert_equal '512', options[:max_size]
119
+ assert_equal '/uploads/', options[:base_path]
120
+ EM.stop
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,57 @@
1
+ require 'test_helper'
2
+ require 'fileutils'
3
+ require 'stringio'
4
+
5
+ class TestHelpers < MiniTest::Test
6
+ include Rubytus::Mock
7
+
8
+ def setup
9
+ @config = Rubytus::Mock::Config.new
10
+ end
11
+
12
+ def teardown
13
+ remove_data_dir
14
+ end
15
+
16
+ def test_validates_data_dir
17
+ dir = @config.validates_data_dir(data_dir)
18
+ assert File.directory?(dir)
19
+ end
20
+
21
+ def test_validates_data_dir_world_writable
22
+ dir = @config.validates_data_dir(data_dir)
23
+ assert_equal "777", sprintf("%o", File.world_writable?(dir))
24
+ end
25
+
26
+ def test_validates_data_dir_relative_path
27
+ config = @config.dup
28
+ dir = "rubytus-#{rand(1000)}"
29
+ assert_equal dir, File.basename(config.validates_data_dir(dir))
30
+ assert_equal "777", sprintf("%o", File.world_writable?(dir))
31
+ FileUtils.rm_rf(dir)
32
+ end
33
+
34
+ def test_validates_data_dir_permission_error
35
+ assert_raises(Rubytus::PermissionError) { @config.validates_data_dir('/opt/rubytus') }
36
+ end
37
+
38
+ def test_validates_max_size_blank
39
+ assert_raises(Rubytus::ConfigurationError) { @config.validates_max_size('') }
40
+ end
41
+
42
+ def test_validates_max_size_string
43
+ assert_equal 512, @config.validates_max_size('512')
44
+ end
45
+
46
+ def test_validates_base_path_error
47
+ assert_raises(Rubytus::ConfigurationError) { @config.validates_base_path('abc+def=gh') }
48
+ end
49
+
50
+ def test_validates_base_path
51
+ assert_equal '/user-uploads/', @config.validates_base_path('/user-uploads/')
52
+ end
53
+
54
+ def test_uid
55
+ assert_equal 32, (@config.generate_uid).length
56
+ end
57
+ end
@@ -0,0 +1,94 @@
1
+ require 'test_helper'
2
+ require 'rubytus/storage'
3
+ require 'rubytus/helpers'
4
+
5
+ class TestStorage < MiniTest::Test
6
+ include Rubytus::Mock
7
+ include Rubytus::Helpers
8
+ include Rubytus::StorageHelper
9
+
10
+ def setup
11
+ options = default_options
12
+ validates_data_dir(options[:data_dir])
13
+
14
+ @uid = uid
15
+ @storage = Rubytus::Storage.new(options)
16
+ end
17
+
18
+ def teardown
19
+ remove_data_dir
20
+ end
21
+
22
+ def test_file_path
23
+ assert_match "#{@uid}.bin", @storage.file_path(@uid)
24
+ end
25
+
26
+ def test_info_path
27
+ assert_match "#{@uid}.info", @storage.info_path(@uid)
28
+ end
29
+
30
+ def test_create_file
31
+ @storage.create_file(@uid, 512)
32
+ assert File.exist?(@storage.file_path(@uid))
33
+ assert File.exist?(@storage.info_path(@uid))
34
+ end
35
+
36
+ def test_write_file_failed
37
+ storage = Rubytus::Storage.new(read_only_options)
38
+ assert_raises(Rubytus::PermissionError) { storage.create_file(@uid, 512) }
39
+ end
40
+
41
+ def test_patch_file_failed
42
+ data = 'abc'
43
+ storage = Rubytus::Storage.new(read_only_options)
44
+ file = Object.new
45
+ stub(file).write(data) { raise SystemCallError.new('', 13) }
46
+ assert_raises(Rubytus::PermissionError) { storage.patch_file(file, data) }
47
+ end
48
+
49
+ def test_read_info
50
+ File.open(@storage.info_path(@uid), 'w') do |f|
51
+ f.write('{"Offset":100,"FinalLength":500,"Meta":null}')
52
+ end
53
+
54
+ info = @storage.read_info(@uid)
55
+ assert_kind_of Hash, info
56
+ assert_equal 100, info['Offset']
57
+ end
58
+
59
+ def test_read_info_failure
60
+ storage = Rubytus::Storage.new(read_only_options)
61
+ assert_raises(Rubytus::PermissionError) { storage.read_info(@uid) }
62
+ end
63
+
64
+ def test_update_info
65
+ File.open(@storage.info_path(@uid), 'w') do |f|
66
+ f.write('{"Offset":100,"FinalLength":500,"Meta":null}')
67
+ end
68
+
69
+ @storage.update_info(@uid, 250)
70
+
71
+ info = @storage.read_info(@uid)
72
+ assert_kind_of Hash, info
73
+ assert_equal 250, info['Offset']
74
+ end
75
+
76
+ def test_update_info_failure
77
+ storage = Rubytus::Storage.new(read_only_options)
78
+ stub(storage).read_info(@uid) { Rubytus::Info.new('Offset' => 100) }
79
+ assert_raises(Rubytus::PermissionError) { storage.update_info(@uid, 250) }
80
+ end
81
+
82
+ def test_read_file
83
+ File.open(@storage.file_path(@uid), 'w') do |f|
84
+ f.write('1234567')
85
+ end
86
+
87
+ assert_equal '1234567', @storage.read_file(@uid)
88
+ end
89
+
90
+ def test_read_file_failure
91
+ storage = Rubytus::Storage.new(read_only_options)
92
+ assert_raises(Rubytus::PermissionError) { storage.read_file(@uid) }
93
+ end
94
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class TestVersion < MiniTest::Test
4
+ def test_version
5
+ refute_nil Rubytus::VERSION
6
+ end
7
+ end
@@ -0,0 +1,79 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ require 'minitest/autorun'
4
+ require 'minitest/pride'
5
+ require 'rr'
6
+ require 'goliath/test_helper'
7
+ require 'rubytus/error'
8
+ require 'rubytus/helpers'
9
+ require 'rubytus/common'
10
+ require 'rubytus/storage'
11
+ require 'pry'
12
+
13
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
14
+ SimpleCov::Formatter::HTMLFormatter,
15
+ Coveralls::SimpleCov::Formatter
16
+ ]
17
+
18
+ Goliath.env = :test
19
+
20
+ module Rubytus
21
+ module Mock
22
+ include Rubytus::Helpers
23
+ include Rubytus::Common
24
+
25
+ def data_dir
26
+ "/tmp/rubytus-#{rand(1000)}"
27
+ end
28
+
29
+ def remove_data_dir
30
+ FileUtils.rm_rf(Dir.glob("/tmp/rubytus-*"))
31
+ end
32
+
33
+ def uid
34
+ generate_uid
35
+ end
36
+
37
+ def default_options
38
+ {
39
+ :data_dir => data_dir,
40
+ :max_size => 1024 * 1024,
41
+ :base_path => '/uploads/'
42
+ }
43
+ end
44
+
45
+ def read_only_options
46
+ default_options.merge({
47
+ :data_dir => '/opt/rubytus'
48
+ })
49
+ end
50
+
51
+ class Config
52
+ include Rubytus::Helpers
53
+ include Rubytus::Common
54
+ include Rubytus::StorageHelper
55
+ end
56
+ end
57
+ end
58
+
59
+ module Goliath
60
+ module TestHelper
61
+ def server(api, port, options = {}, &blk)
62
+ op = OptionParser.new
63
+
64
+ s = Goliath::Server.new
65
+ s.logger = setup_logger(options)
66
+ s.api = api.new
67
+ s.app = Goliath::Rack::Builder.build(api, s.api)
68
+ s.api.options_parser(op, options)
69
+ s.options = options
70
+ s.port = port
71
+ s.plugins = api.plugins
72
+ s.api.setup if s.api.respond_to?(:setup)
73
+ @test_server_port = s.port if blk
74
+ s.start(&blk)
75
+ s
76
+ end
77
+ end
78
+ end
79
+
metadata ADDED
@@ -0,0 +1,227 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubytus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alif Rachmawadi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: minitest
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: rr
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: simplecov
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: em-http-request
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: pry
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: coveralls
128
+ requirement: !ruby/object:Gem::Requirement
129
+ none: false
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ type: :development
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ - !ruby/object:Gem::Dependency
143
+ name: goliath
144
+ requirement: !ruby/object:Gem::Requirement
145
+ none: false
146
+ requirements:
147
+ - - ~>
148
+ - !ruby/object:Gem::Version
149
+ version: 1.0.3
150
+ type: :runtime
151
+ prerelease: false
152
+ version_requirements: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ~>
156
+ - !ruby/object:Gem::Version
157
+ version: 1.0.3
158
+ description: Resumable upload protocol implementation in Ruby
159
+ email:
160
+ - subosito@gmail.com
161
+ executables:
162
+ - rubytusd
163
+ extensions: []
164
+ extra_rdoc_files: []
165
+ files:
166
+ - .editorconfig
167
+ - .gitignore
168
+ - .simplecov
169
+ - .travis.yml
170
+ - Gemfile
171
+ - LICENSE.txt
172
+ - README.md
173
+ - Rakefile
174
+ - bin/rubytusd
175
+ - lib/rubytus.rb
176
+ - lib/rubytus/api.rb
177
+ - lib/rubytus/command.rb
178
+ - lib/rubytus/common.rb
179
+ - lib/rubytus/constants.rb
180
+ - lib/rubytus/error.rb
181
+ - lib/rubytus/helpers.rb
182
+ - lib/rubytus/info.rb
183
+ - lib/rubytus/middlewares/storage_barrier.rb
184
+ - lib/rubytus/middlewares/tus_barrier.rb
185
+ - lib/rubytus/request.rb
186
+ - lib/rubytus/storage.rb
187
+ - lib/rubytus/version.rb
188
+ - rubytus.gemspec
189
+ - test/rubytus/test_command.rb
190
+ - test/rubytus/test_command_runner.rb
191
+ - test/rubytus/test_helpers.rb
192
+ - test/rubytus/test_storage.rb
193
+ - test/rubytus/test_version.rb
194
+ - test/test_helper.rb
195
+ homepage: ''
196
+ licenses:
197
+ - MIT
198
+ post_install_message:
199
+ rdoc_options: []
200
+ require_paths:
201
+ - lib
202
+ required_ruby_version: !ruby/object:Gem::Requirement
203
+ none: false
204
+ requirements:
205
+ - - ! '>='
206
+ - !ruby/object:Gem::Version
207
+ version: '0'
208
+ required_rubygems_version: !ruby/object:Gem::Requirement
209
+ none: false
210
+ requirements:
211
+ - - ! '>='
212
+ - !ruby/object:Gem::Version
213
+ version: '0'
214
+ requirements: []
215
+ rubyforge_project:
216
+ rubygems_version: 1.8.23
217
+ signing_key:
218
+ specification_version: 3
219
+ summary: Resumable upload protocol implementation in Ruby
220
+ test_files:
221
+ - test/rubytus/test_command.rb
222
+ - test/rubytus/test_command_runner.rb
223
+ - test/rubytus/test_helpers.rb
224
+ - test/rubytus/test_storage.rb
225
+ - test/rubytus/test_version.rb
226
+ - test/test_helper.rb
227
+ has_rdoc: