siterest 0.1.0.pre1
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.
- data/Gemfile +9 -0
- data/Gemfile.lock +38 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/bin/siterest +10 -0
- data/config.ru +6 -0
- data/lib/encoded_attachment/.gitignore +7 -0
- data/lib/encoded_attachment/Gemfile +11 -0
- data/lib/encoded_attachment/LICENSE +20 -0
- data/lib/encoded_attachment/README.md +107 -0
- data/lib/encoded_attachment/Rakefile +41 -0
- data/lib/encoded_attachment/encoded_attachment.gemspec +23 -0
- data/lib/encoded_attachment/lib/activerecord/base.rb +63 -0
- data/lib/encoded_attachment/lib/activeresource/base.rb +121 -0
- data/lib/encoded_attachment/lib/activeresource/connection.rb +7 -0
- data/lib/encoded_attachment/lib/encoded_attachment/version.rb +3 -0
- data/lib/encoded_attachment/lib/encoded_attachment.rb +47 -0
- data/lib/encoded_attachment/test/active_record_test.rb +136 -0
- data/lib/encoded_attachment/test/active_resource_test.rb +276 -0
- data/lib/encoded_attachment/test/avatars/.gitignore +0 -0
- data/lib/encoded_attachment/test/config/database.yml +19 -0
- data/lib/encoded_attachment/test/config/schema.rb +17 -0
- data/lib/encoded_attachment/test/fixtures/kitten.jpg +0 -0
- data/lib/encoded_attachment/test/fixtures/tapir.jpg +0 -0
- data/lib/encoded_attachment/test/test_helper.rb +73 -0
- data/lib/siterest/asset.rb +27 -0
- data/lib/siterest/client.rb +149 -0
- data/lib/siterest/command.rb +105 -0
- data/lib/siterest/server.rb +58 -0
- data/lib/siterest/site.rb +6 -0
- data/lib/siterest/template/filters/core_filters.rb +57 -0
- data/lib/siterest/template/filters/datetime_filters.rb +8 -0
- data/lib/siterest/template/filters/url_filters.rb +82 -0
- data/lib/siterest/template/objects/article.rb +35 -0
- data/lib/siterest/template/objects/page.rb +71 -0
- data/lib/siterest/template/objects/site.rb +51 -0
- data/lib/siterest/template.rb +6 -0
- data/lib/siterest/user.rb +6 -0
- data/lib/siterest.rb +56 -0
- data/lib/upfile.rb +51 -0
- data/readme.md +86 -0
- data/site/data/articles/2008-04-24-my-first-blog-post.md +12 -0
- data/site/data/pages/about-us/something.md +0 -0
- data/site/data/pages/about-us.md +12 -0
- data/site/data/pages/contact.md +12 -0
- data/site/data/pages/home.md +14 -0
- data/site/data/site.yaml +2 -0
- data/siterest.gemspec +101 -0
- metadata +203 -0
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ActiveRecordCreationTest < ActiveSupport::TestCase
|
4
|
+
load_schema
|
5
|
+
|
6
|
+
test "build, save and destroy model without exception" do
|
7
|
+
assert_nothing_raised {
|
8
|
+
@user = User.new(:name => 'John Doe', :avatar => File.open("test/fixtures/kitten.jpg"))
|
9
|
+
@user.save!
|
10
|
+
@user.destroy
|
11
|
+
}
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ActiveRecordUserWithEncodedAttachmentsTest < ActiveSupport::TestCase
|
16
|
+
load_schema
|
17
|
+
|
18
|
+
setup do
|
19
|
+
@user = User.create(:name => 'John Doe', :avatar => File.open("test/fixtures/kitten.jpg"))
|
20
|
+
end
|
21
|
+
|
22
|
+
teardown do
|
23
|
+
User.destroy_all
|
24
|
+
end
|
25
|
+
|
26
|
+
test "should wrap to_xml methods" do
|
27
|
+
assert @user.methods.include?("to_xml_with_encoded_avatar"), 'Should include wrapped method'
|
28
|
+
assert @user.methods.include?("to_xml_without_encoded_avatar"), 'Should include unwrapped method'
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should generate file tags" do
|
32
|
+
user_xml = Hash.from_xml(@user.to_xml)['user']
|
33
|
+
|
34
|
+
assert user_xml.has_key?('avatar'), 'Should have avatar key'
|
35
|
+
assert_equal 'kitten.jpg', user_xml['avatar'].original_filename, 'Should set avatar filename'
|
36
|
+
assert_equal 'image/jpeg', user_xml['avatar'].content_type, 'Should set avatar content type'
|
37
|
+
end
|
38
|
+
|
39
|
+
test "should not generate file tags with :include_attachments => false" do
|
40
|
+
assert !(Hash.from_xml(@user.to_xml(:include_attachments => false))['user'].has_key?('avatar'))
|
41
|
+
end
|
42
|
+
|
43
|
+
test "file tag should be nil on new or destroyed records" do
|
44
|
+
@new_user = User.new(:name => 'John Doe', :avatar => File.open("test/fixtures/kitten.jpg"))
|
45
|
+
assert Hash.from_xml(@new_user.to_xml)['user']['avatar'].nil?
|
46
|
+
end
|
47
|
+
|
48
|
+
test "file tag should be nil on destroyed records" do
|
49
|
+
@user.destroy
|
50
|
+
assert Hash.from_xml(@user.to_xml)['user']['avatar'].nil?
|
51
|
+
end
|
52
|
+
|
53
|
+
test "should copy file attributes from an existing record to a new one" do
|
54
|
+
@new_user = User.new.from_xml @user.to_xml
|
55
|
+
|
56
|
+
assert_equal 'John Doe', @new_user.name, 'New user name should be the same'
|
57
|
+
assert @new_user.avatar.file?, 'New user should have a file'
|
58
|
+
assert @new_user.save, 'New user should save'
|
59
|
+
assert_equal 'kitten.jpg', @new_user.avatar_file_name, 'New user should have correct file name'
|
60
|
+
assert_equal 'image/jpeg', @new_user.avatar_content_type, 'New user should have correct content type'
|
61
|
+
end
|
62
|
+
|
63
|
+
test "should update attributes from one record to another" do
|
64
|
+
@other_user = User.create(:name => 'Bill Tapir', :avatar => File.open("test/fixtures/tapir.jpg"))
|
65
|
+
@other_user.update_attributes Hash.from_xml(@user.to_xml)['user']
|
66
|
+
|
67
|
+
assert_equal 'John Doe', @other_user.name, 'Other user name should be the same'
|
68
|
+
assert @other_user.avatar.file?, 'Other user should have a file'
|
69
|
+
assert_equal 'kitten.jpg', @other_user.avatar_file_name, 'Other user should have correct file name'
|
70
|
+
assert_equal 'image/jpeg', @other_user.avatar_content_type, 'Other user should have correct content type'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
class ActiveSupportWithURLAttachmentsTest < ActiveSupport::TestCase
|
75
|
+
load_schema
|
76
|
+
|
77
|
+
setup do
|
78
|
+
@user = User.create(:name => 'John Doe', :avatar_remote => File.open("test/fixtures/kitten.jpg"))
|
79
|
+
end
|
80
|
+
|
81
|
+
teardown do
|
82
|
+
User.destroy_all
|
83
|
+
end
|
84
|
+
|
85
|
+
test "should create avatar_remote_url tags" do
|
86
|
+
assert Hash.from_xml(@user.to_xml)['user'].has_key?('avatar_remote_url')
|
87
|
+
end
|
88
|
+
|
89
|
+
test "should not create avatar_remote_url tags with :include_attachments => false" do
|
90
|
+
assert !(Hash.from_xml(@user.to_xml(:include_attachments => false))['user'].has_key?('avatar_remote_url'))
|
91
|
+
end
|
92
|
+
|
93
|
+
test "should not create avatar_remote tags" do
|
94
|
+
assert !(Hash.from_xml(@user.to_xml)['user'].has_key?('avatar_remote'))
|
95
|
+
end
|
96
|
+
|
97
|
+
test "avatar_remote_url tags should be nil if the user is new" do
|
98
|
+
@new_user = User.new(:name => 'John Doe', :avatar_remote => File.open("test/fixtures/kitten.jpg"))
|
99
|
+
assert Hash.from_xml(@new_user.to_xml)['user']['avatar_remote_url'].nil?, 'Should have nil URL tag'
|
100
|
+
assert !(Hash.from_xml(@new_user.to_xml)['user'].has_key?('avatar_remote')), 'Should not have file tag'
|
101
|
+
end
|
102
|
+
|
103
|
+
test "avatar_remote_url tags should be nil if the user is destroyed" do
|
104
|
+
@user.destroy
|
105
|
+
assert Hash.from_xml(@user.to_xml)['user']['avatar_remote_url'].nil?, 'Should have nil URL tag'
|
106
|
+
assert !(Hash.from_xml(@user.to_xml)['user'].has_key?('avatar_remote')), 'Should not have file tag'
|
107
|
+
end
|
108
|
+
|
109
|
+
test "avatar_remote_url tag should point to the image's URL" do
|
110
|
+
assert_equal "http://localhost/users/#{@user.id}.jpg", Hash.from_xml(@user.to_xml)['user']['avatar_remote_url']
|
111
|
+
end
|
112
|
+
|
113
|
+
test "avatar_remote_url tag should not raise exception when applied to another ActiveRecord" do
|
114
|
+
assert_nothing_raised { @new_user = User.new.from_xml(@user.to_xml) }
|
115
|
+
end
|
116
|
+
|
117
|
+
test "avatar_remote_url tag should not set the file when applied to another ActiveRecord" do
|
118
|
+
@new_user = User.new.from_xml(@user.to_xml)
|
119
|
+
assert !(@new_user.avatar_remote.file?)
|
120
|
+
end
|
121
|
+
|
122
|
+
test "avatar_remote_url tag should not be generated with :encode_attachments => true" do
|
123
|
+
assert !(Hash.from_xml(@user.to_xml(:encode_attachments => true))['user'].has_key?('avatar_remote_url'))
|
124
|
+
end
|
125
|
+
|
126
|
+
test "avatar_remote file tag should be generated with :encode_attachments => true" do
|
127
|
+
assert Hash.from_xml(@user.to_xml(:encode_attachments => true))['user'].has_key?('avatar_remote')
|
128
|
+
end
|
129
|
+
|
130
|
+
test "avatar_remote file tag should include all necessary attributes" do
|
131
|
+
user_xml = Hash.from_xml(@user.to_xml(:encode_attachments => true))['user']
|
132
|
+
assert user_xml.has_key?('avatar_remote')
|
133
|
+
assert_equal 'kitten.jpg', user_xml['avatar_remote'].original_filename, 'Filename should be set'
|
134
|
+
assert_equal 'image/jpeg', user_xml['avatar_remote'].content_type, 'Content type should be set'
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,276 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
class ActiveResourceBaseTest < ActiveSupport::TestCase
|
5
|
+
load_schema
|
6
|
+
|
7
|
+
test "should add get_attachment to ActiveResource::Connection" do
|
8
|
+
@connection = ActiveResource::Connection.new("http://localhost/")
|
9
|
+
assert @connection.methods.include?("get_attachment")
|
10
|
+
end
|
11
|
+
|
12
|
+
test "build user resource without exception" do
|
13
|
+
assert_nothing_raised { Api::User.new(:name => 'John Doe') }
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class ActiveResourceMethodsTest < ActiveSupport::TestCase
|
18
|
+
setup do
|
19
|
+
@user = Api::User.new(:name => 'John Doe')
|
20
|
+
end
|
21
|
+
|
22
|
+
test "should add attributes to schema" do
|
23
|
+
assert @user.known_attributes.include?("name"), "Should have name attribute"
|
24
|
+
assert @user.known_attributes.include?("avatar_file_name"), "Should have avatar_file_name attribute"
|
25
|
+
assert @user.known_attributes.include?("avatar_file_size"), "Should have avatar_file_size attribute"
|
26
|
+
assert @user.known_attributes.include?("avatar_content_type"), "Should have avatar_content_type attribute"
|
27
|
+
assert @user.known_attributes.include?("avatar_updated_at"), "Should have avatar_updated_at attribute"
|
28
|
+
assert @user.known_attributes.include?("avatar"), "Should have avatar attribute"
|
29
|
+
end
|
30
|
+
|
31
|
+
test "should wrap to_xml method" do
|
32
|
+
assert @user.methods.include?("to_xml_with_encoded_avatar"), "Should have to_xml_with_encoded_avatar"
|
33
|
+
assert @user.methods.include?("to_xml_without_encoded_avatar"), "Should have to_xml_without_encoded_avatar"
|
34
|
+
end
|
35
|
+
|
36
|
+
test "should have changed= and changed? methods" do
|
37
|
+
assert @user.methods.include?("avatar_changed="), "Should have avatar_changed= method"
|
38
|
+
assert @user.methods.include?("avatar_changed?"), "Should have avatar_changed method"
|
39
|
+
end
|
40
|
+
|
41
|
+
test "should not be changed? in new record" do
|
42
|
+
assert !(@user.avatar_changed?)
|
43
|
+
end
|
44
|
+
|
45
|
+
test "changed? should be true after attachment is set" do
|
46
|
+
@user.avatar = File.open("test/fixtures/kitten.jpg")
|
47
|
+
assert @user.avatar_changed?
|
48
|
+
end
|
49
|
+
|
50
|
+
test "should have file= method" do
|
51
|
+
assert @user.methods.include?("avatar=")
|
52
|
+
end
|
53
|
+
|
54
|
+
test "should have file_path= method" do
|
55
|
+
assert @user.methods.include?("avatar_path=")
|
56
|
+
end
|
57
|
+
|
58
|
+
test "should have file_url= method" do
|
59
|
+
assert @user.methods.include?("avatar_url=")
|
60
|
+
end
|
61
|
+
|
62
|
+
test "should have save_file_as method" do
|
63
|
+
assert @user.methods.include?("save_avatar_as")
|
64
|
+
end
|
65
|
+
|
66
|
+
test "should wrap file_updated_at" do
|
67
|
+
@user.avatar_updated_at = "Wed May 26 01:29:08 UTC 2010"
|
68
|
+
assert @user.avatar_updated_at.is_a?(Time)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
class ActiveResourceAttributesTest < ActiveSupport::TestCase
|
73
|
+
setup do
|
74
|
+
@user_record = User.create(:name => 'John Doe', :avatar => File.open("test/fixtures/tapir.jpg"))
|
75
|
+
@user = Api::User.new(Hash.from_xml(@user_record.to_xml)['user'])
|
76
|
+
end
|
77
|
+
|
78
|
+
teardown do
|
79
|
+
User.destroy_all
|
80
|
+
end
|
81
|
+
|
82
|
+
test "should set initial attributes" do
|
83
|
+
assert_equal 'tapir.jpg', @user.avatar_file_name, 'Should set name'
|
84
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
85
|
+
assert @user.avatar.is_a?(StringIO), 'Should set attachment to StringIO'
|
86
|
+
assert !(@user.avatar_changed?), 'Should not be changed?'
|
87
|
+
end
|
88
|
+
|
89
|
+
test "should set attributes if attachment is nil" do
|
90
|
+
@user_record = User.create(:name => 'John Doe', :avatar => nil)
|
91
|
+
@user = Api::User.new(Hash.from_xml(@user_record.to_xml)['user'])
|
92
|
+
|
93
|
+
assert_nil @user.avatar_file_name, 'Name should be nil'
|
94
|
+
assert_nil @user.avatar_content_type, 'Content type should be nil'
|
95
|
+
assert_nil @user.avatar, 'Attachment should be nil'
|
96
|
+
assert !(@user.avatar_changed?), 'Should not be changed?'
|
97
|
+
end
|
98
|
+
|
99
|
+
test "should update attributes if initial attachment was nil" do
|
100
|
+
@user_record = User.create(:name => 'John Doe', :avatar => nil)
|
101
|
+
@user = Api::User.new(Hash.from_xml(@user_record.to_xml)['user'])
|
102
|
+
@user.attributes = { :avatar => File.open('test/fixtures/kitten.jpg') }
|
103
|
+
|
104
|
+
assert_equal 'kitten.jpg', @user.avatar_file_name, 'Should set name'
|
105
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
106
|
+
assert @user.avatar.is_a?(File), 'Should set attachment to File'
|
107
|
+
assert @user.avatar_changed?, 'Should be changed?'
|
108
|
+
end
|
109
|
+
|
110
|
+
test "should be able to update attachment" do
|
111
|
+
@user.avatar = File.open('test/fixtures/kitten.jpg')
|
112
|
+
assert_equal 'kitten.jpg', @user.avatar_file_name, 'Should set name'
|
113
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
114
|
+
assert @user.avatar.is_a?(File), 'Should set attachment to File'
|
115
|
+
assert @user.avatar_changed?, 'Should be changed?'
|
116
|
+
end
|
117
|
+
|
118
|
+
test "should be able to update attachment using attributes=" do
|
119
|
+
@user.attributes = { :avatar => File.open('test/fixtures/kitten.jpg') }
|
120
|
+
assert_equal 'kitten.jpg', @user.avatar_file_name, 'Should set name'
|
121
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
122
|
+
assert @user.avatar.is_a?(File), 'Should set attachment to File'
|
123
|
+
assert @user.avatar_changed?, 'Should be changed?'
|
124
|
+
end
|
125
|
+
|
126
|
+
test "should be able to update attachment using path" do
|
127
|
+
@user.avatar_path = 'test/fixtures/kitten.jpg'
|
128
|
+
assert_equal 'kitten.jpg', @user.avatar_file_name, 'Should set name'
|
129
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
130
|
+
assert @user.avatar.is_a?(File), 'Should set attachment to File'
|
131
|
+
assert @user.avatar_changed?, 'Should be changed?'
|
132
|
+
end
|
133
|
+
|
134
|
+
test "should be able to save attachment" do
|
135
|
+
assert_nothing_raised { @user.save_avatar_as "test/test_file.jpg" }
|
136
|
+
File.delete 'test/test_file.jpg' if File.exist?('test/test_file.jpg')
|
137
|
+
end
|
138
|
+
|
139
|
+
test "should overwrite attachment if file exists" do
|
140
|
+
FileUtils.touch 'test/test_file.jpg'
|
141
|
+
assert_nothing_raised { @user.save_avatar_as 'test/test_file.jpg' }
|
142
|
+
File.delete 'test/test_file.jpg' if File.exist?('test/test_file.jpg')
|
143
|
+
end
|
144
|
+
|
145
|
+
test "should not overwrite attachment if file exists and save_as is called with false" do
|
146
|
+
FileUtils.touch 'test/test_file.jpg'
|
147
|
+
assert_raises(RuntimeError) { @user.save_avatar_as 'test/test_file.jpg', false }
|
148
|
+
File.delete 'test/test_file.jpg' if File.exist?('test/test_file.jpg')
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
class ActiveResourceConnectionMethods < ActiveSupport::TestCase
|
153
|
+
def setup
|
154
|
+
@user_record = User.create(:name => 'John Doe', :avatar => File.open('test/fixtures/kitten.jpg'),
|
155
|
+
:avatar_remote => File.open('test/fixtures/tapir.jpg'))
|
156
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
157
|
+
mock.get "/users/#{@user_record.id}.xml", {}, @user_record.to_xml
|
158
|
+
mock.post "/users.xml", {}, @user_record.to_xml, 201, "Location" => "/people/1.xml"
|
159
|
+
mock.put "/users/#{@user_record.id}.xml", {}, nil, 204
|
160
|
+
mock.delete "/users/#{@user_record.id}.xml", {}, nil, 200
|
161
|
+
mock.get "/users/#{@user_record.id}.jpg", { 'Accept' => 'image/jpeg' }, File.read('test/fixtures/tapir.jpg'),
|
162
|
+
200, "Content-Type" => "image/jpeg"
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
teardown do
|
167
|
+
User.destroy_all
|
168
|
+
end
|
169
|
+
|
170
|
+
test "HTTP mock is working" do
|
171
|
+
@user = Api::User.find(@user_record.id)
|
172
|
+
assert ActiveResource::HttpMock.requests.include?(ActiveResource::Request.new(:get, "/users/#{@user_record.id}.xml")),
|
173
|
+
"Should receive find request"
|
174
|
+
assert ActiveResource::HttpMock.requests.include?(ActiveResource::Request.new(:get, "/users/#{@user_record.id}.jpg")),
|
175
|
+
"Should receive avatar_remote image request"
|
176
|
+
end
|
177
|
+
|
178
|
+
test "should receive and parse XML, including a remote attachment" do
|
179
|
+
@user = Api::User.find(@user_record.id)
|
180
|
+
|
181
|
+
assert_equal 'kitten.jpg', @user.avatar_file_name, 'Should set name'
|
182
|
+
assert_equal 'image/jpeg', @user.avatar_content_type, 'Should set content type'
|
183
|
+
assert @user.avatar.is_a?(StringIO), 'Should set attachment to StringIO'
|
184
|
+
assert !(@user.avatar_changed?), 'Should not be changed?'
|
185
|
+
|
186
|
+
assert_equal "#{@user_record.id}.jpg", @user.avatar_remote_file_name, 'Should set remote name'
|
187
|
+
assert_equal 'image/jpeg', @user.avatar_remote_content_type, 'Should set remote content type'
|
188
|
+
assert @user.avatar_remote.is_a?(StringIO), 'Should set remote attachment to StringIO'
|
189
|
+
assert !(@user.avatar_remote_changed?), 'Remote should not be changed?'
|
190
|
+
end
|
191
|
+
|
192
|
+
test "should POST a new user" do
|
193
|
+
@user = Api::User.new(:name => 'John Doe')
|
194
|
+
@user.save
|
195
|
+
assert ActiveResource::HttpMock.requests.include?(ActiveResource::Request.new(:post, "/users.xml"))
|
196
|
+
end
|
197
|
+
|
198
|
+
test "should PUT an update" do
|
199
|
+
@user = Api::User.find(@user_record.id)
|
200
|
+
@user.name = 'Jane Doe'
|
201
|
+
@user.save
|
202
|
+
assert ActiveResource::HttpMock.requests.include?(ActiveResource::Request.new(:put, "/users/#{@user_record.id}.xml"))
|
203
|
+
end
|
204
|
+
|
205
|
+
test "should DELETE when destroyed" do
|
206
|
+
@user = Api::User.find(@user_record.id)
|
207
|
+
@user.destroy
|
208
|
+
assert ActiveResource::HttpMock.requests.include?(ActiveResource::Request.new(:delete, "/users/#{@user_record.id}.xml")),
|
209
|
+
"Should have sent a DELETE request"
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
class ActiveResourceXMLGenerationTest < ActiveSupport::TestCase
|
214
|
+
def setup
|
215
|
+
@user_record = User.create(:name => 'John Doe', :avatar => File.open("test/fixtures/tapir.jpg"),
|
216
|
+
:avatar_remote => File.open('test/fixtures/kitten.jpg'))
|
217
|
+
ActiveResource::HttpMock.respond_to do |mock|
|
218
|
+
mock.get "/users/#{@user_record.id}.xml", {}, @user_record.to_xml
|
219
|
+
mock.post "/users.xml", {}, @user_record.to_xml, 201, "Location" => "/people/1.xml"
|
220
|
+
mock.put "/users/#{@user_record.id}.xml", {}, nil, 204
|
221
|
+
mock.delete "/users/#{@user_record.id}.xml", {}, nil, 200
|
222
|
+
mock.get "/users/#{@user_record.id}.jpg", { 'Accept' => 'image/jpeg' }, File.read('test/fixtures/tapir.jpg'),
|
223
|
+
200, "Content-Type" => "image/jpeg"
|
224
|
+
end
|
225
|
+
@user = Api::User.find(@user_record.id)
|
226
|
+
end
|
227
|
+
|
228
|
+
def teardown
|
229
|
+
User.destroy_all
|
230
|
+
end
|
231
|
+
|
232
|
+
test "should not generate XML tags because the files haven't changed" do
|
233
|
+
user_xml = Hash.from_xml(@user.to_xml)['user']
|
234
|
+
assert !(user_xml.has_key?('avatar')), 'Should not have avatar tag'
|
235
|
+
assert !(user_xml.has_key?('avatar_remote')), 'Should not have avatar_remote tag'
|
236
|
+
end
|
237
|
+
|
238
|
+
test "should generate XML tags when the files have changed" do
|
239
|
+
@user.avatar_path = 'test/fixtures/kitten.jpg'
|
240
|
+
@user.avatar_remote = File.open("test/fixtures/tapir.jpg")
|
241
|
+
user_xml = Hash.from_xml(@user.to_xml)['user']
|
242
|
+
assert user_xml.has_key?('avatar'), 'Should have avatar tag'
|
243
|
+
assert user_xml.has_key?('avatar_remote'), 'Should have avatar_remote tag'
|
244
|
+
|
245
|
+
assert user_xml['avatar'].is_a?(StringIO), 'Should be a StringIO'
|
246
|
+
assert_equal 'kitten.jpg', user_xml['avatar'].original_filename, 'Should have the original filename'
|
247
|
+
end
|
248
|
+
|
249
|
+
test "should not generate XML tags when user is new" do
|
250
|
+
user_xml = Hash.from_xml(Api::User.new(:name => 'Jane Doe').to_xml)['user']
|
251
|
+
assert !(user_xml.has_key?('avatar')), 'Should not have avatar tag'
|
252
|
+
assert !(user_xml.has_key?('avatar_remote')), 'Should not have avatar_remote tag'
|
253
|
+
end
|
254
|
+
|
255
|
+
test "should generate XML tags when user is new and files have been assigned" do
|
256
|
+
user_xml = Hash.from_xml(Api::User.new(:name => 'Jane Doe', :avatar => File.open("test/fixtures/tapir.jpg"),
|
257
|
+
:avatar_remote => File.open('test/fixtures/kitten.jpg')).to_xml)['user']
|
258
|
+
assert user_xml.has_key?('avatar'), 'Should have avatar tag'
|
259
|
+
assert user_xml.has_key?('avatar_remote'), 'Should have avatar_remote tag'
|
260
|
+
end
|
261
|
+
|
262
|
+
test "should create a User from XML" do
|
263
|
+
@new_user = User.new.from_xml Api::User.new(:name => 'Jane Doe', :avatar => File.open("test/fixtures/tapir.jpg"),
|
264
|
+
:avatar_remote => File.open('test/fixtures/kitten.jpg')).to_xml
|
265
|
+
assert @new_user.save, 'User should save'
|
266
|
+
assert @new_user.avatar.file?, 'User should have avatar file'
|
267
|
+
assert File.exist?(@new_user.avatar_remote.path(:original)), 'User\'s avatar remote file should exist'
|
268
|
+
end
|
269
|
+
|
270
|
+
test "should update a User's files from XML" do
|
271
|
+
@user.avatar = File.open('test/fixtures/kitten.jpg')
|
272
|
+
@user_record.update_attributes Hash.from_xml(@user.to_xml)['user']
|
273
|
+
assert_equal 'kitten.jpg', @user_record.avatar_file_name, 'File name updated'
|
274
|
+
assert File.exist?(@user_record.avatar.path(:original)), 'File saved'
|
275
|
+
end
|
276
|
+
end
|
File without changes
|
@@ -0,0 +1,19 @@
|
|
1
|
+
sqlite3:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: test/db/test.sqlite3
|
4
|
+
pool: 5
|
5
|
+
timeout: 5000
|
6
|
+
|
7
|
+
postgresql:
|
8
|
+
adapter: postgresql
|
9
|
+
username: postgres
|
10
|
+
password: postgres
|
11
|
+
database: encoded_attachment_plugin_test
|
12
|
+
min_messages: ERROR
|
13
|
+
|
14
|
+
mysql:
|
15
|
+
adapter: mysql
|
16
|
+
host: localhost
|
17
|
+
username: root
|
18
|
+
password:
|
19
|
+
database: encoded_attachment_plugin_test
|
@@ -0,0 +1,17 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 0) do
|
2
|
+
create_table :users, :force => true do |t|
|
3
|
+
t.string "name"
|
4
|
+
|
5
|
+
t.string "avatar_file_name"
|
6
|
+
t.string "avatar_content_type"
|
7
|
+
t.integer "avatar_file_size"
|
8
|
+
t.datetime "avatar_updated_at"
|
9
|
+
|
10
|
+
t.string "avatar_remote_file_name"
|
11
|
+
t.string "avatar_remote_content_type"
|
12
|
+
t.integer "avatar_remote_file_size"
|
13
|
+
t.datetime "avatar_remote_updated_at"
|
14
|
+
|
15
|
+
t.timestamps
|
16
|
+
end
|
17
|
+
end
|
Binary file
|
Binary file
|
@@ -0,0 +1,73 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'logger'
|
4
|
+
require 'bundler'
|
5
|
+
|
6
|
+
Bundler.setup
|
7
|
+
Bundler.require
|
8
|
+
|
9
|
+
Paperclip.configure
|
10
|
+
Paperclip::Railtie.insert
|
11
|
+
|
12
|
+
class User < ActiveRecord::Base
|
13
|
+
has_attached_file :avatar,
|
14
|
+
:path => File.dirname(__FILE__) + "/avatars/:id_partition/:attachment/:style.:extension",
|
15
|
+
:url => "/users/:id.:extension"
|
16
|
+
encode_attachment_in_xml :avatar
|
17
|
+
|
18
|
+
has_attached_file :avatar_remote,
|
19
|
+
:path => File.dirname(__FILE__) + "/avatars/:id_partition/:attachment/:style.:extension",
|
20
|
+
:url => "/users/:id.:extension"
|
21
|
+
encode_attachment_in_xml :avatar_remote, :send_urls => true, :root_url => "http://localhost/"
|
22
|
+
end
|
23
|
+
|
24
|
+
module Api
|
25
|
+
class User < ActiveResource::Base
|
26
|
+
self.site = "http://localhost/"
|
27
|
+
|
28
|
+
has_encoded_attachment :avatar
|
29
|
+
has_encoded_attachment :avatar_remote
|
30
|
+
|
31
|
+
schema do
|
32
|
+
string "name"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class ActiveSupport::TestCase
|
38
|
+
def self.load_schema
|
39
|
+
config = YAML::load(IO.read(File.dirname(__FILE__) + '/config/database.yml'))
|
40
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/log/debug.log")
|
41
|
+
|
42
|
+
db_adapter = ENV['DB']
|
43
|
+
|
44
|
+
# no db passed, try one of these fine config-free DBs before bombing.
|
45
|
+
db_adapter ||=
|
46
|
+
begin
|
47
|
+
require 'rubygems'
|
48
|
+
require 'sqlite'
|
49
|
+
'sqlite'
|
50
|
+
rescue MissingSourceFile
|
51
|
+
begin
|
52
|
+
require 'sqlite3'
|
53
|
+
'sqlite3'
|
54
|
+
rescue MissingSourceFile
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
if db_adapter.nil?
|
59
|
+
raise "No DB Adapter selected. Pass the DB= option to pick one, or install Sqlite or Sqlite3."
|
60
|
+
end
|
61
|
+
|
62
|
+
ActiveRecord::Base.establish_connection(config[db_adapter])
|
63
|
+
load(File.dirname(__FILE__) + "/config/schema.rb")
|
64
|
+
end
|
65
|
+
|
66
|
+
def self.user_with_encoded_files
|
67
|
+
User.encode_attachment_in_xml :avatar
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.user_urls
|
71
|
+
User.encode_attachment_in_xml :avatar, :send_urls => true, :root_url => "http://localhost/"
|
72
|
+
end
|
73
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'mime/types'
|
3
|
+
require 'stringio'
|
4
|
+
require 'upfile'
|
5
|
+
require 'encoded_attachment/lib/encoded_attachment'
|
6
|
+
|
7
|
+
|
8
|
+
module Siterest
|
9
|
+
class Asset < ActiveResource::Base
|
10
|
+
self.site = Siterest.site_resource_url
|
11
|
+
self.user = Siterest.token
|
12
|
+
|
13
|
+
has_encoded_attachment :data
|
14
|
+
|
15
|
+
def url
|
16
|
+
"http://assets.siterest.com/#{self.path}"
|
17
|
+
end
|
18
|
+
|
19
|
+
def binary?
|
20
|
+
MIME::Types[data_content_type].first.try(:binary?)
|
21
|
+
end
|
22
|
+
|
23
|
+
def to_file
|
24
|
+
open(url)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|