Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

edited specs to use add_role and remove_role

  • Loading branch information...
commit a1e826d4998beb2fa2fe8c5c43a64bb3870fcb2f 1 parent 767ecc2
Florent Monbillard EppO authored
38 spec/generators/rolify/role/role_generator_spec.rb
View
@@ -6,7 +6,7 @@
describe Rolify::Generators::RoleGenerator do
# Tell the generator where to put its output (what it thinks of as Rails.root)
destination File.expand_path("../../../../../tmp", __FILE__)
- teardown :cleanup_destination_root
+ #teardown :cleanup_destination_root
before {
prepare_destination
@@ -48,20 +48,20 @@ def cleanup_destination_root
end
describe 'migration file' do
- subject { file('db/migrate/rolify_create_roles.rb') }
+ subject { migration_file('db/migrate/rolify_create_roles.rb') }
- # should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
- it { should be_a_migration }
+ it { should contain "create_table(:roles) do" }
+ it { should contain "create_table(:users_roles, :id => false) do" }
end
end
describe 'specifying user and role names' do
- before(:all) { arguments %w(Rank Client) }
+ before(:all) { arguments %w(AdminRole AdminUser) }
before {
capture(:stdout) {
- generator.create_file "app/models/client.rb" do
- "class Client < ActiveRecord::Base\nend"
+ generator.create_file "app/models/admin_user.rb" do
+ "class AdminUser < ActiveRecord::Base\nend"
end
}
run_generator
@@ -69,29 +69,31 @@ def cleanup_destination_root
describe 'config/initializers/rolify.rb' do
subject { file('config/initializers/rolify.rb') }
+
it { should exist }
it { should contain "# c.use_dynamic_shortcuts" }
end
describe 'app/models/rank.rb' do
- subject { file('app/models/rank.rb') }
+ subject { file('app/models/admin_role.rb') }
+
it { should exist }
- it { should contain "class Rank < ActiveRecord::Base" }
- it { should contain "has_and_belongs_to_many :clients, :join_table => :clients_ranks" }
+ it { should contain "class AdminRole < ActiveRecord::Base" }
+ it { should contain "has_and_belongs_to_many :admin_users, :join_table => :admin_users_admin_roles" }
it { should contain "belongs_to :resource, :polymorphic => true" }
end
describe 'app/models/client.rb' do
- subject { file('app/models/client.rb') }
+ subject { file('app/models/admin_user.rb') }
+
it { should contain "rolify" }
end
describe 'migration file' do
- subject { file('db/migrate/rolify_create_ranks.rb') }
+ subject { migration_file('db/migrate/rolify_create_admin_roles.rb') }
- # should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
- it { should be_a_migration }
- #it { should contain "create_table(:ranks)" }
+ it { should contain "create_table(:admin_roles)" }
+ it { should contain "create_table(:admin_users_admin_roles, :id => false) do" }
end
end
@@ -127,10 +129,10 @@ def cleanup_destination_root
end
describe 'migration file' do
- subject { file('db/migrate/rolify_create_roles.rb') }
+ subject { migration_file('db/migrate/rolify_create_roles.rb') }
- # should be_a_migration - verifies the file exists with a migration timestamp as part of the filename
- it { should be_a_migration }
+ it { should contain "create_table(:roles) do" }
+ it { should contain "create_table(:users_roles, :id => false) do" }
end
end
57 spec/rolify/resource_spec.rb
View
@@ -3,7 +3,6 @@
describe Rolify::Resource do
before(:all) do
reset_defaults
- #Role.destroy_all
User.rolify :role_cname => "Role"
Forum.resourcify :role_cname => "Role"
Group.resourcify :role_cname => "Role"
@@ -14,11 +13,11 @@
let(:tourist) { User.last }
# roles
- let!(:forum_role) { admin.add_role("forum", Forum.first) }
- let!(:godfather_role) { admin.add_role("godfather", Forum) }
- let!(:group_role) { admin.add_role("group", Group.last) }
- let!(:tourist_role) { tourist.add_role("forum", Forum.last) }
- let!(:sneaky_role) { tourist.add_role("group", Forum.first) }
+ let!(:forum_role) { admin.add_role(:forum, Forum.first) }
+ let!(:godfather_role) { admin.add_role(:godfather, Forum) }
+ let!(:group_role) { admin.add_role(:group, Group.last) }
+ let!(:tourist_role) { tourist.add_role(:forum, Forum.last) }
+ let!(:sneaky_role) { tourist.add_role(:group, Forum.first) }
describe ".with_roles" do
subject { Group }
@@ -31,10 +30,10 @@
subject { Forum }
it "should include Forum instances with forum role" do
- subject.with_role("forum").should include(Forum.first, Forum.last)
+ subject.with_role(:forum).should include(Forum.first, Forum.last)
end
it "should include Forum instances with godfather role" do
- subject.with_role("godfather").should eq(Forum.all)
+ subject.with_role(:godfather).should eq(Forum.all)
end
end
@@ -42,7 +41,7 @@
subject { Group }
it "should include Group instances with group role" do
- subject.with_role("group").should include(Group.last)
+ subject.with_role(:group).should include(Group.last)
end
end
@@ -53,27 +52,27 @@
subject { Forum }
it "should get all Forum instances binded to the forum role and the admin user" do
- subject.with_role("forum", admin).should include(Forum.first)
+ subject.with_role(:forum, admin).should include(Forum.first)
end
it "should get all Forum instances binded to the forum role and the tourist user" do
- subject.with_role("forum", tourist).should include(Forum.last)
+ subject.with_role(:forum, tourist).should include(Forum.last)
end
it "should get all Forum instances binded to the godfather role and the admin user" do
- subject.with_role("godfather", admin).should == Forum.all
+ subject.with_role(:godfather, admin).should == Forum.all
end
it "should get all Forum instances binded to the godfather role and the tourist user" do
- subject.with_role("godfather", tourist).should be_empty
+ subject.with_role(:godfather, tourist).should be_empty
end
it "should get Forum instances binded to the group role and the tourist user" do
- subject.with_role("group", tourist).should include(Forum.first)
+ subject.with_role(:group, tourist).should include(Forum.first)
end
it "should not get Forum instances not binded to the group role and the tourist user" do
- subject.with_role("group", tourist).should_not include(Forum.last)
+ subject.with_role(:group, tourist).should_not include(Forum.last)
end
end
@@ -81,11 +80,11 @@
subject { Group }
it "should get all resources binded to the group role and the admin user" do
- subject.with_role("group", admin).should include(Group.last)
+ subject.with_role(:group, admin).should include(Group.last)
end
it "should not get resources not binded to the group role and the admin user" do
- subject.with_role("group", admin).should_not include(Group.first)
+ subject.with_role(:group, admin).should_not include(Group.first)
end
end
end
@@ -146,31 +145,31 @@
context "without using a user parameter" do
it "should get all roles binded to a Forum class or instance and forum role name" do
- subject.find_roles("forum").should include(forum_role, tourist_role)
+ subject.find_roles(:forum).should include(forum_role, tourist_role)
end
it "should not get roles not binded to a Forum class or instance and forum role name" do
- subject.find_roles("forum").should_not include(godfather_role, sneaky_role, group_role)
+ subject.find_roles(:forum).should_not include(godfather_role, sneaky_role, group_role)
end
end
context "using a user parameter" do
it "should get all roles binded to any resource" do
- subject.find_roles("forum", admin).should include(forum_role)
+ subject.find_roles(:forum, admin).should include(forum_role)
end
it "should not get roles not binded to the admin user and forum role name" do
- subject.find_roles("forum", admin).should_not include(godfather_role, tourist_role, sneaky_role, group_role)
+ subject.find_roles(:forum, admin).should_not include(godfather_role, tourist_role, sneaky_role, group_role)
end
end
context "using :any parameter" do
it "should get all roles binded to any resource with forum role name" do
- subject.find_roles("forum", :any).should include(forum_role, tourist_role)
+ subject.find_roles(:forum, :any).should include(forum_role, tourist_role)
end
it "should not get roles not binded to a resource with forum role name" do
- subject.find_roles("forum", :any).should_not include(godfather_role, sneaky_role, group_role)
+ subject.find_roles(:forum, :any).should_not include(godfather_role, sneaky_role, group_role)
end
end
end
@@ -180,31 +179,31 @@
context "without using a user parameter" do
it "should get all roles binded to a Group class or instance and group role name" do
- subject.find_roles("group").should include(group_role)
+ subject.find_roles(:group).should include(group_role)
end
it "should not get roles not binded to a Forum class or instance and forum role name" do
- subject.find_roles("group").should_not include(tourist_role, godfather_role, sneaky_role, forum_role)
+ subject.find_roles(:group).should_not include(tourist_role, godfather_role, sneaky_role, forum_role)
end
end
context "using a user parameter" do
it "should get all roles binded to any resource" do
- subject.find_roles("group", admin).should include(group_role)
+ subject.find_roles(:group, admin).should include(group_role)
end
it "should not get roles not binded to the admin user and forum role name" do
- subject.find_roles("group", admin).should_not include(godfather_role, tourist_role, sneaky_role, forum_role)
+ subject.find_roles(:group, admin).should_not include(godfather_role, tourist_role, sneaky_role, forum_role)
end
end
context "using :any parameter" do
it "should get all roles binded to any resource with forum role name" do
- subject.find_roles("group", :any).should include(group_role)
+ subject.find_roles(:group, :any).should include(group_role)
end
it "should not get roles not binded to a resource with forum role name" do
- subject.find_roles("group", :any).should_not include(godfather_role, sneaky_role, forum_role, tourist_role)
+ subject.find_roles(:group, :any).should_not include(godfather_role, sneaky_role, forum_role, tourist_role)
end
end
end
34 spec/rolify/shared_contexts.rb
View
@@ -11,13 +11,13 @@
def load_roles
role_class.destroy_all
admin.roles = []
- admin.add_role "admin"
- admin.add_role "staff"
- admin.add_role "manager", Group
- admin.add_role "player", Forum
- admin.add_role "moderator", Forum.last
- admin.add_role "moderator", Group.last
- admin.add_role "anonymous", Forum.first
+ admin.add_role :admin
+ admin.add_role :staff
+ admin.add_role :manager, Group
+ admin.add_role :player, Forum
+ admin.add_role :moderator, Forum.last
+ admin.add_role :moderator, Group.last
+ admin.add_role :anonymous, Forum.first
end
end
@@ -34,12 +34,12 @@ def load_roles
def load_roles
role_class.destroy_all
manager.roles = []
- manager.add_role "manager", Forum
- manager.add_role "player", Forum
- manager.add_role "warrior"
- manager.add_role "moderator", Forum.last
- manager.add_role "moderator", Group.last
- manager.add_role "anonymous", Forum.first
+ manager.add_role :manager, Forum
+ manager.add_role :player, Forum
+ manager.add_role :warrior
+ manager.add_role :moderator, Forum.last
+ manager.add_role :moderator, Group.last
+ manager.add_role :anonymous, Forum.first
end
end
@@ -56,10 +56,10 @@ def load_roles
def load_roles
role_class.destroy_all
moderator.roles = []
- moderator.add_role "moderator", Forum.first
- moderator.add_role "anonymous", Forum.last
- moderator.add_role "visitor", Forum
- moderator.add_role "soldier"
+ moderator.add_role :moderator, Forum.first
+ moderator.add_role :anonymous, Forum.last
+ moderator.add_role :visitor, Forum
+ moderator.add_role :soldier
end
end
2  ...d_examples/shared_examples_for_has_role_setter.rb → ...y/shared_examples/shared_examples_for_add_role.rb
View
@@ -1,4 +1,4 @@
-shared_examples_for "#has_role_examples" do |param_name, param_method|
+shared_examples_for "#add_role_examples" do |param_name, param_method|
context "using #{param_name} as parameter" do
context "with a global role", :scope => :global do
it "should add the role to the user" do
12 spec/rolify/shared_examples/shared_examples_for_dynamic.rb
View
@@ -10,8 +10,8 @@
context "using a global role" do
subject do
admin = user_class.first
- admin.add_role "admin"
- admin.add_role "moderator", Forum.first
+ admin.add_role :admin
+ admin.add_role :moderator, Forum.first
admin
end
@@ -27,7 +27,7 @@
context "using a resource scoped role" do
subject do
moderator = user_class.where(:login => "moderator").first
- moderator.add_role "moderator", Forum.first
+ moderator.add_role :moderator, Forum.first
moderator
end
@@ -49,7 +49,7 @@
context "using a class scoped role" do
subject do
manager = user_class.where(:login => "god").first
- manager.add_role "manager", Forum
+ manager.add_role :manager, Forum
manager
end
@@ -77,7 +77,7 @@
context "using a global role" do
before(:all) do
other_guy = user_class.last
- other_guy.add_role "superman"
+ other_guy.add_role :superman
end
it { should respond_to(:is_superman?).with(0).arguments }
@@ -90,7 +90,7 @@
context "using a resource scope role" do
before(:all) do
other_guy = user_class.last
- other_guy.add_role("batman", Forum.first)
+ other_guy.add_role :batman, Forum.first
end
it { should respond_to(:is_batman?).with(0).arguments }
0  ...d_examples/shared_examples_for_has_role_getter.rb → ...y/shared_examples/shared_examples_for_has_role.rb
View
File renamed without changes
24 ...hared_examples/shared_examples_for_has_no_role.rb → ...hared_examples/shared_examples_for_remove_role.rb
View
@@ -1,4 +1,4 @@
-shared_examples_for "#has_no_role_examples" do |param_name, param_method|
+shared_examples_for "#remove_role_examples" do |param_name, param_method|
context "using #{param_name} as parameter" do
context "removing a global role", :scope => :global do
context "being a global role of the user" do
@@ -13,7 +13,7 @@
end
context "being a class scoped role to the user" do
- it { expect { subject.remove_role("manager") }.to change{ subject.roles.size }.by(-1) }
+ it { expect { subject.remove_role("manager".send(param_method)) }.to change{ subject.roles.size }.by(-1) }
it { subject.has_role?("admin".send(param_method)).should be_false }
it { subject.has_role?("staff".send(param_method)).should be_true }
@@ -24,7 +24,7 @@
end
context "being instance scoped roles to the user" do
- it { expect { subject.remove_role("moderator") }.to change{ subject.roles.size }.by(-2) }
+ it { expect { subject.remove_role("moderator".send(param_method)) }.to change{ subject.roles.size }.by(-2) }
it { subject.has_role?("admin".send(param_method)).should be_false }
it { subject.has_role?("staff".send(param_method)).should be_true }
@@ -35,17 +35,17 @@
end
context "not being a role of the user" do
- it { expect { subject.remove_role("superhero") }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("superhero".send(param_method)) }.not_to change{ subject.roles.size } }
end
end
context "removing a class scoped role", :scope => :class do
context "being a global role of the user" do
- it { expect { subject.remove_role("warrior", Forum) }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("warrior".send(param_method), Forum) }.not_to change{ subject.roles.size } }
end
context "being a class scoped role to the user" do
- it { expect { subject.remove_role("manager", Forum) }.to change{ subject.roles.size }.by(-1) }
+ it { expect { subject.remove_role("manager".send(param_method), Forum) }.to change{ subject.roles.size }.by(-1) }
it { subject.has_role?("warrior").should be_true }
it { subject.has_role?("manager", Forum).should be_false }
@@ -56,7 +56,7 @@
end
context "being instance scoped role to the user" do
- it { expect { subject.remove_role("moderator", Forum) }.to change{ subject.roles.size }.by(-1) }
+ it { expect { subject.remove_role("moderator".send(param_method), Forum) }.to change{ subject.roles.size }.by(-1) }
it { subject.has_role?("warrior").should be_true }
it { subject.has_role?("manager", Forum).should be_false }
@@ -67,21 +67,21 @@
end
context "not being a role of the user" do
- it { expect { subject.remove_role("manager", Group) }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("manager".send(param_method), Group) }.not_to change{ subject.roles.size } }
end
end
context "removing a instance scoped role", :scope => :instance do
context "being a global role of the user" do
- it { expect { subject.remove_role("soldier", Group.first) }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("soldier".send(param_method), Group.first) }.not_to change{ subject.roles.size } }
end
context "being a class scoped role to the user" do
- it { expect { subject.remove_role("visitor", Forum.first) }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("visitor".send(param_method), Forum.first) }.not_to change{ subject.roles.size } }
end
context "being instance scoped role to the user" do
- it { expect { subject.remove_role("moderator", Forum.first) }.to change{ subject.roles.size }.by(-1) }
+ it { expect { subject.remove_role("moderator".send(param_method), Forum.first) }.to change{ subject.roles.size }.by(-1) }
it { subject.has_role?("soldier").should be_true }
it { subject.has_role?("visitor", Forum).should be_true }
@@ -90,7 +90,7 @@
end
context "not being a role of the user" do
- it { expect { subject.remove_role("anonymous", Forum.first) }.not_to change{ subject.roles.size } }
+ it { expect { subject.remove_role("anonymous".send(param_method), Forum.first) }.not_to change{ subject.roles.size } }
end
end
end
18 spec/rolify/shared_examples/shared_examples_for_roles.rb
View
@@ -1,9 +1,9 @@
require "rolify/shared_contexts"
-require "rolify/shared_examples/shared_examples_for_has_role_setter"
-require "rolify/shared_examples/shared_examples_for_has_role_getter"
+require "rolify/shared_examples/shared_examples_for_add_role"
+require "rolify/shared_examples/shared_examples_for_has_role"
require "rolify/shared_examples/shared_examples_for_has_all_roles"
require "rolify/shared_examples/shared_examples_for_has_any_role"
-require "rolify/shared_examples/shared_examples_for_has_no_role"
+require "rolify/shared_examples/shared_examples_for_remove_role"
shared_examples_for Rolify::Role do
@@ -19,8 +19,8 @@
context "in a Instance level" do
before(:all) do
admin = user_class.first
- admin.add_role "admin"
- admin.add_role "moderator", Forum.first
+ admin.add_role :admin
+ admin.add_role :moderator, Forum.first
admin
end
@@ -52,8 +52,8 @@
end
describe "#has_role" do
- it_should_behave_like "#has_role_examples", "String", :to_s
- it_should_behave_like "#has_role_examples", "Symbol", :to_sym
+ it_should_behave_like "#add_role_examples", "String", :to_s
+ it_should_behave_like "#add_role_examples", "Symbol", :to_sym
end
describe "#has_role?" do
@@ -72,7 +72,7 @@
end
describe "#has_no_role" do
- it_should_behave_like "#has_no_role_examples", "String", :to_s
- it_should_behave_like "#has_no_role_examples", "Symbol", :to_sym
+ it_should_behave_like "#remove_role_examples", "String", :to_s
+ it_should_behave_like "#remove_role_examples", "Symbol", :to_sym
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.