rad_kit 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +11 -0
- data/lib/components/kit.rb +16 -0
- data/lib/components/kit.yml +3 -0
- data/lib/components/models.rb +7 -0
- data/lib/kit/factories.rb +9 -0
- data/lib/kit/gems.rb +16 -0
- data/lib/kit/http_controller.rb +4 -0
- data/lib/kit/http_controller/authorized.rb +51 -0
- data/lib/kit/http_controller/localized.rb +13 -0
- data/lib/kit/kit.rb +29 -0
- data/lib/kit/models.rb +8 -0
- data/lib/kit/models/attachment_uploader.rb +15 -0
- data/lib/kit/models/attachments_uploader_helper.rb +79 -0
- data/lib/kit/models/authorized.rb +188 -0
- data/lib/kit/models/authorized_object.rb +167 -0
- data/lib/kit/models/default_permissions.yml +29 -0
- data/lib/kit/models/file_uploader.rb +26 -0
- data/lib/kit/models/micelaneous.rb +1 -0
- data/lib/kit/models/role.rb +88 -0
- data/lib/kit/models_after.rb +27 -0
- data/lib/kit/mongoid.rb +22 -0
- data/lib/kit/mongoid/rad_micelaneous.rb +36 -0
- data/lib/kit/mongoid/text_processor.rb +44 -0
- data/lib/kit/spec.rb +77 -0
- data/lib/kit/spec/items_controller_crud.rb +64 -0
- data/lib/kit/support.rb +14 -0
- data/lib/kit/support/string.rb +6 -0
- data/lib/kit/tasks.rb +18 -0
- data/lib/kit/text_utils.rb +43 -0
- data/lib/kit/text_utils/code_highlighter.rb +58 -0
- data/lib/kit/text_utils/custom_markdown.rb +90 -0
- data/lib/kit/text_utils/ensure_utf.rb +8 -0
- data/lib/kit/text_utils/github_flavoured_markdown.rb +32 -0
- data/lib/kit/text_utils/html_sanitizer.rb +89 -0
- data/lib/kit/text_utils/image_box.rb +35 -0
- data/lib/kit/text_utils/markup.rb +43 -0
- data/lib/kit/text_utils/processor.rb +25 -0
- data/lib/kit/text_utils/tag_shortcuts.rb +14 -0
- data/lib/kit/text_utils/truncate.rb +29 -0
- data/lib/kit/text_utils/truncator.rb +15 -0
- data/lib/kit/text_utils/urls.rb +13 -0
- data/readme.md +10 -0
- data/spec/controller/authorization_spec.rb +149 -0
- data/spec/controller/comments_spec.rb +54 -0
- data/spec/controller/items_spec.rb +45 -0
- data/spec/models/attachments_spec.rb +24 -0
- data/spec/models/attachments_spec/a.txt +1 -0
- data/spec/models/attachments_uploader_helper_spec.rb +108 -0
- data/spec/models/attachments_uploader_helper_spec/v1/a.txt +1 -0
- data/spec/models/attachments_uploader_helper_spec/v1/b.txt +1 -0
- data/spec/models/attachments_uploader_helper_spec/v2/a.txt +1 -0
- data/spec/models/authorization_spec.rb +77 -0
- data/spec/models/authorized_object_spec.rb +254 -0
- data/spec/models/comments_spec.rb +1 -0
- data/spec/models/item_spec.rb +51 -0
- data/spec/models/role_spec.rb +17 -0
- data/spec/models/tags_spec.rb +44 -0
- data/spec/models/uploader_spec.rb +37 -0
- data/spec/models/uploader_spec//321/204/320/260/320/270/314/206/320/273 /321/201 /320/277/321/200/320/276/320/261/320/265/320/273/320/260/320/274/320/270.txt" +1 -0
- data/spec/mongoid/basic_spec.rb +36 -0
- data/spec/spec_helper.rb +20 -0
- data/spec/spec_helper/controller.rb +9 -0
- data/spec/spec_helper/factories.rb +24 -0
- data/spec/spec_helper/user.rb +17 -0
- data/spec/utils/text_utils_spec.rb +280 -0
- metadata +232 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Attachments" do
|
4
|
+
with_tmp_spec_dir
|
5
|
+
with_models
|
6
|
+
with_files
|
7
|
+
login_as :user
|
8
|
+
|
9
|
+
before do
|
10
|
+
@a = File.open("#{spec_dir}/a.txt")
|
11
|
+
end
|
12
|
+
after do
|
13
|
+
@a.close if @a
|
14
|
+
end
|
15
|
+
|
16
|
+
it "smoke test" do
|
17
|
+
item = Factory.create :item, name: 'my note', attachments_as_attachments: [@a]
|
18
|
+
item.reload
|
19
|
+
|
20
|
+
item.attachments.size.should == 1
|
21
|
+
# item.attachments.first.file.path.should =~ /\/my note\/a\.txt/
|
22
|
+
item.attachments.first.file.path.should =~ /\/#{item.id}\/a\.txt/
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
a
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Attachments" do
|
4
|
+
with_tmp_spec_dir
|
5
|
+
with_mongoid
|
6
|
+
with_files
|
7
|
+
|
8
|
+
before :all do
|
9
|
+
class TheFileUploader < Models::FileUploader
|
10
|
+
end
|
11
|
+
|
12
|
+
class TheFile
|
13
|
+
include Mongoid::Document
|
14
|
+
|
15
|
+
embedded_in :post, class_name: 'ThePost'
|
16
|
+
mount_uploader :file, TheFileUploader
|
17
|
+
end
|
18
|
+
|
19
|
+
class ThePost
|
20
|
+
include Mongoid::Document
|
21
|
+
|
22
|
+
embeds_many :files, class_name: 'TheFile'
|
23
|
+
mount_embedded_uploader :files, :file
|
24
|
+
mount_attachments_uploader :files, :file
|
25
|
+
|
26
|
+
def sorted_files
|
27
|
+
files.sort{|a, b| a.file.url <=> b.file.url}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
after(:all){remove_constants :ThePost, :TheImage, :TheImageUploader}
|
32
|
+
|
33
|
+
before do
|
34
|
+
@a = File.open("#{spec_dir}/v1/a.txt")
|
35
|
+
@b = File.open("#{spec_dir}/v1/b.txt")
|
36
|
+
@a_v2 = File.open("#{spec_dir}/v2/a.txt")
|
37
|
+
end
|
38
|
+
after do
|
39
|
+
@a.close if @a
|
40
|
+
@b.close if @b
|
41
|
+
@a_v2.close if @a_v2
|
42
|
+
end
|
43
|
+
|
44
|
+
def post_with_two_files
|
45
|
+
params = {
|
46
|
+
files_as_attachments: [@a, @b]
|
47
|
+
}
|
48
|
+
|
49
|
+
post = ThePost.new params
|
50
|
+
post.save!
|
51
|
+
post.reload
|
52
|
+
post.files.size.should == 2
|
53
|
+
post
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should add files" do
|
57
|
+
post = post_with_two_files
|
58
|
+
|
59
|
+
post.files.size.should == 2
|
60
|
+
a, b = post.sorted_files
|
61
|
+
a.file.path.should =~ /\/a\.txt/
|
62
|
+
b.file.path.should =~ /\/b\.txt/
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should remove files" do
|
66
|
+
post = post_with_two_files
|
67
|
+
a, b = post.sorted_files
|
68
|
+
File.should exist(b.file.path)
|
69
|
+
|
70
|
+
params = {
|
71
|
+
files_as_attachments: ['a.txt']
|
72
|
+
}
|
73
|
+
post.update_attributes(params).should be_true
|
74
|
+
post.reload
|
75
|
+
|
76
|
+
post.files.size.should == 1
|
77
|
+
post.files.first.file.path.should =~ /\/a\.txt/
|
78
|
+
|
79
|
+
# should also remove physical file
|
80
|
+
File.should_not exist(b.file.path)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should update files" do
|
84
|
+
post = post_with_two_files
|
85
|
+
|
86
|
+
params = {
|
87
|
+
files_as_attachments: [@a_v2, 'b.txt']
|
88
|
+
}
|
89
|
+
post.update_attributes(params).should be_true
|
90
|
+
post.reload
|
91
|
+
|
92
|
+
post.files.size.should == 2
|
93
|
+
a, b = post.sorted_files
|
94
|
+
a.file.path.should =~ /\/a\.txt/
|
95
|
+
File.read(a.file.path).should == 'a v2'
|
96
|
+
b.file.path.should =~ /\/b\.txt/
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should provide :files_as_attachments getter" do
|
100
|
+
post = post_with_two_files
|
101
|
+
a, b = post.sorted_files
|
102
|
+
|
103
|
+
post.files_as_attachments.should == [
|
104
|
+
{name: 'a.txt', url: a.file.url},
|
105
|
+
{name: 'b.txt', url: b.file.url}
|
106
|
+
]
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
a
|
@@ -0,0 +1 @@
|
|
1
|
+
b
|
@@ -0,0 +1 @@
|
|
1
|
+
a v2
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Authorization" do
|
4
|
+
with_models
|
5
|
+
|
6
|
+
describe "Roles" do
|
7
|
+
it "user should have it's name in roles" do
|
8
|
+
user = Factory.build :user, name: 'some_name'
|
9
|
+
user.roles.should include('user:some_name')
|
10
|
+
end
|
11
|
+
|
12
|
+
it ":anonymous, :registered, :user roles" do
|
13
|
+
anonymous = Factory.build :anonymous
|
14
|
+
anonymous.roles.should == %w{anonymous user user:anonymous}
|
15
|
+
|
16
|
+
user = Factory.build :user, name: 'john'
|
17
|
+
user.roles.should == %w{registered user user:john}
|
18
|
+
|
19
|
+
admin = Factory.build :admin, name: 'john'
|
20
|
+
admin.roles.should == %w{admin manager member registered user user:john}
|
21
|
+
end
|
22
|
+
|
23
|
+
it "all managers should also have the member role, always" do
|
24
|
+
user = Factory.build :manager, name: 'john'
|
25
|
+
user.roles.should == %w{manager member registered user user:john}
|
26
|
+
end
|
27
|
+
|
28
|
+
it "handy methods" do
|
29
|
+
u = Factory.build :anonymous
|
30
|
+
u.roles.anonymous?.should be_true
|
31
|
+
u.roles.registered?.should be_false
|
32
|
+
u.roles.has?(:anonymous).should be_true
|
33
|
+
u.should have_role(:anonymous)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "add_role" do
|
37
|
+
u = Factory.build :member, name: 'john'
|
38
|
+
u.should_not have_role('manager')
|
39
|
+
u.add_role :manager
|
40
|
+
u.save!
|
41
|
+
u.reload
|
42
|
+
u.should have_role('manager')
|
43
|
+
end
|
44
|
+
|
45
|
+
it "remove_role" do
|
46
|
+
u = Factory.build :manager, name: 'john'
|
47
|
+
u.remove_role :member
|
48
|
+
u.save!
|
49
|
+
u.reload
|
50
|
+
u.should_not have_role('manager')
|
51
|
+
u.should_not have_role('member')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should add also all lover roles" do
|
55
|
+
u = Factory.build :user, name: 'john'
|
56
|
+
u.roles.should_not include('member')
|
57
|
+
u.add_role :manager
|
58
|
+
u.roles.should include('member')
|
59
|
+
end
|
60
|
+
|
61
|
+
it "special case, admin role" do
|
62
|
+
u = Factory.build :user, name: 'john'
|
63
|
+
u.should_not have_role('manager')
|
64
|
+
u.add_role :admin
|
65
|
+
u.save!
|
66
|
+
u.reload
|
67
|
+
u.should have_role('admin')
|
68
|
+
u.should have_role('manager')
|
69
|
+
end
|
70
|
+
|
71
|
+
it "major_roles" do
|
72
|
+
u = Factory.build :member, name: 'john'
|
73
|
+
u.add_role :director
|
74
|
+
u.major_roles.should == %w{director member user:john}
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,254 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "Authorized Object" do
|
4
|
+
with_models
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
class ::AModel
|
8
|
+
include Mongoid::Document
|
9
|
+
include Mongoid::AuthorizedObject
|
10
|
+
end
|
11
|
+
end
|
12
|
+
after(:all){remove_constants :AModel}
|
13
|
+
|
14
|
+
def custom_permissions= value
|
15
|
+
Mongoid::Authorized.custom_permissions = value
|
16
|
+
end
|
17
|
+
|
18
|
+
after{self.custom_permissions = {}}
|
19
|
+
|
20
|
+
describe "Owner, Viewers, Collaborators" do
|
21
|
+
before do
|
22
|
+
rad.delete :user
|
23
|
+
@user = Factory.create :user, name: 'auser'
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should be abel to create objects (from error)" do
|
27
|
+
rad.user = @user
|
28
|
+
o = AModel.new
|
29
|
+
o.save!
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should by default set current user_name as owner_name if there is current user" do
|
33
|
+
o = AModel.new
|
34
|
+
o.owner_name.should be_nil
|
35
|
+
|
36
|
+
rad.user = @user
|
37
|
+
o = AModel.new
|
38
|
+
o.owner_name.should == 'auser'
|
39
|
+
end
|
40
|
+
|
41
|
+
it "owner" do
|
42
|
+
o = AModel.new
|
43
|
+
o.owner = @user
|
44
|
+
o.owner_name.should == @user.name
|
45
|
+
o.viewers.should == %w{manager user:auser}
|
46
|
+
o.should be_valid
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'viewers' do
|
50
|
+
o = AModel.new
|
51
|
+
o.owner = @user
|
52
|
+
o.add_viewer :user
|
53
|
+
o.viewers.should == %w{manager member user user:auser}
|
54
|
+
o.should be_valid
|
55
|
+
|
56
|
+
o.remove_viewer :user
|
57
|
+
o.viewers.should == %w{manager user:auser}
|
58
|
+
o.should be_valid
|
59
|
+
|
60
|
+
o.add_viewer :member
|
61
|
+
o.viewers.should == %w{manager member user:auser}
|
62
|
+
o.should be_valid
|
63
|
+
|
64
|
+
o.add_viewer :user
|
65
|
+
o.viewers.should == %w{manager member user user:auser}
|
66
|
+
o.should be_valid
|
67
|
+
end
|
68
|
+
|
69
|
+
it "duplicate roles (from error)" do
|
70
|
+
o = AModel.new
|
71
|
+
o.owner = @user
|
72
|
+
o.save!
|
73
|
+
|
74
|
+
o = AModel.first # don't use reload, it willn't catch this error
|
75
|
+
o.viewers.should == %w{manager user:auser}
|
76
|
+
end
|
77
|
+
|
78
|
+
it "collaborators" do
|
79
|
+
o = AModel.new
|
80
|
+
o.owner = @user
|
81
|
+
o.add_collaborator :member
|
82
|
+
o.collaborators.should == %w{member}
|
83
|
+
o.should be_valid
|
84
|
+
|
85
|
+
o.add_collaborator :director
|
86
|
+
o.collaborators.should == %w{member director}
|
87
|
+
o.should be_valid
|
88
|
+
|
89
|
+
o.remove_collaborator :member
|
90
|
+
o.collaborators.should == %w{director}
|
91
|
+
o.should be_valid
|
92
|
+
end
|
93
|
+
|
94
|
+
it "normalized_collaborators" do
|
95
|
+
o = AModel.new
|
96
|
+
o.owner = @user
|
97
|
+
o.add_collaborator :member
|
98
|
+
o.normalized_collaborators.should == %w{manager member user:auser}
|
99
|
+
end
|
100
|
+
|
101
|
+
it "viewers and collaborators dependance" do
|
102
|
+
o = AModel.new
|
103
|
+
o.owner = @user
|
104
|
+
o.add_collaborator :user
|
105
|
+
o.collaborators.should == %w{user}
|
106
|
+
o.viewers.should == %w{manager member user user:auser}
|
107
|
+
o.should be_valid
|
108
|
+
|
109
|
+
o.remove_viewer :member
|
110
|
+
o.viewers.should == %w{manager user user:auser}
|
111
|
+
o.collaborators.should == %w{user}
|
112
|
+
|
113
|
+
o.remove_viewer :user
|
114
|
+
o.viewers.should == %w{manager user:auser}
|
115
|
+
o.collaborators.should == %w{}
|
116
|
+
end
|
117
|
+
|
118
|
+
it "major viewers" do
|
119
|
+
o = AModel.new
|
120
|
+
o.owner = @user
|
121
|
+
o.add_viewer :member
|
122
|
+
o.add_viewer :director
|
123
|
+
o.viewers.should == %w{director manager member user:auser}
|
124
|
+
o.minor_viewers.should == %w{director member user:auser}
|
125
|
+
end
|
126
|
+
|
127
|
+
it "collaborators should be able to change object (from error)" do
|
128
|
+
col = Factory.create :member, name: 'collaborator'
|
129
|
+
|
130
|
+
o = AModel.new
|
131
|
+
o.owner = @user
|
132
|
+
col.can?(:update, o).should be_false
|
133
|
+
|
134
|
+
o.add_collaborator :member
|
135
|
+
o.clear_cache
|
136
|
+
col.can?(:update, o).should be_true
|
137
|
+
@user.can?(:update, o).should be_true
|
138
|
+
|
139
|
+
o.save!
|
140
|
+
o = AModel.find o.id
|
141
|
+
col.can?(:update, o).should be_true
|
142
|
+
@user.can?(:update, o).should be_true
|
143
|
+
end
|
144
|
+
|
145
|
+
it 'should allow to create public or private (by default) objects'
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "Permissions" do
|
149
|
+
describe "General" do
|
150
|
+
before do
|
151
|
+
self.custom_permissions = {'manage' => %w{manager}}
|
152
|
+
end
|
153
|
+
|
154
|
+
it "should works for new user" do
|
155
|
+
u = Models::User.new
|
156
|
+
u.can?(:manage, AModel)
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'permissions' do
|
160
|
+
u = Factory.create :user
|
161
|
+
u.can?(:manage, AModel).should be_false
|
162
|
+
|
163
|
+
u = Factory.create :manager
|
164
|
+
u.can?(:manage, AModel).should be_true
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
describe "as Owner" do
|
169
|
+
before do
|
170
|
+
self.custom_permissions = {'manage' => %w{manager owner}}
|
171
|
+
|
172
|
+
@user = Factory.create :user
|
173
|
+
|
174
|
+
@object = AModel.new
|
175
|
+
@owned_object = AModel.new
|
176
|
+
@owned_object.owner = @user
|
177
|
+
end
|
178
|
+
|
179
|
+
it "owner?" do
|
180
|
+
@user.should_not be_owner(@object)
|
181
|
+
@user.should be_owner(@owned_object)
|
182
|
+
end
|
183
|
+
|
184
|
+
it "anonymous should never be owner of anything" do
|
185
|
+
@user = Factory.build :anonymous
|
186
|
+
|
187
|
+
@owned_object = AModel.new
|
188
|
+
@owned_object.owner = @user
|
189
|
+
|
190
|
+
@user.should_not be_owner(@owned_object)
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'permissions for owner' do
|
194
|
+
@user.can?(:manage, @object).should be_false
|
195
|
+
@user.can?(:manage, @owned_object).should be_true
|
196
|
+
end
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "Special :view permission" do
|
200
|
+
before do
|
201
|
+
# managers can see anything, always, it's hardcoded
|
202
|
+
self.custom_permissions = {'view' => %w{manager}}
|
203
|
+
end
|
204
|
+
|
205
|
+
it "user (public) viewers" do
|
206
|
+
user = Factory.create :user
|
207
|
+
|
208
|
+
o = AModel.new
|
209
|
+
o.stub!(:viewers){%w{user}}
|
210
|
+
|
211
|
+
user.can?(:view, o).should be_true
|
212
|
+
end
|
213
|
+
|
214
|
+
it "member viewers" do
|
215
|
+
self.custom_permissions = {'view' => []}
|
216
|
+
|
217
|
+
user = Factory.create :user
|
218
|
+
member = Factory.create :member
|
219
|
+
manager = Factory.create :manager
|
220
|
+
|
221
|
+
o = AModel.new
|
222
|
+
o.stub!(:viewers).and_return(%w{member manager})
|
223
|
+
|
224
|
+
user.can?(:view, o).should be_false
|
225
|
+
member.can?(:view, o).should be_true
|
226
|
+
manager.can?(:view, o).should be_true
|
227
|
+
end
|
228
|
+
|
229
|
+
it "owner (private) viewers" do
|
230
|
+
self.custom_permissions = {'view' => []}
|
231
|
+
|
232
|
+
owner = Factory.create :user, name: "aname"
|
233
|
+
user = Factory.create :user
|
234
|
+
member = Factory.create :member
|
235
|
+
manager = Factory.create :manager
|
236
|
+
|
237
|
+
o = AModel.new
|
238
|
+
o.stub!(:owner_name){owner.name}
|
239
|
+
o.stub!(:viewers){%w{user:aname manager}}
|
240
|
+
|
241
|
+
owner.can?(:view, o).should be_true
|
242
|
+
user.can?(:view, o).should be_false
|
243
|
+
member.can?(:view, o).should be_false
|
244
|
+
manager.can?(:view, o).should be_true
|
245
|
+
end
|
246
|
+
|
247
|
+
it "should correct works with non authorized objects (from error)" do
|
248
|
+
user = Factory.create :user
|
249
|
+
user.can?(:view, Object.new).should be_false
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
end
|
254
|
+
end
|