Guest User

Untitled

a guest
Jul 22nd, 2018
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.45 KB | None | 0 0
  1. class Ability
  2. include CanCan::Ability
  3.  
  4. def initialize(user)
  5. user ||= User.new
  6.  
  7. case user.role
  8. when "admin"
  9. can :manage, :all
  10.  
  11. when "lead"
  12. can :read, Company, :users => {:id => user.id}
  13.  
  14. can :read, Project, :users => {:id => user.id}
  15.  
  16. can :read, User do |user_model|
  17. user_model.company = user.company
  18. end
  19.  
  20. can :update, User do |user_model|
  21. user_model.company = user.company
  22. end
  23.  
  24. can :destroy, User do |user_model|
  25. user_model.company = user.company
  26. end
  27.  
  28. can :create, User do |user_model|
  29. user_model.company = user.company
  30. end
  31.  
  32.  
  33. can :read, Shop, do |shop|
  34. user.projects.include?(shop.project_id)
  35. end
  36.  
  37. # can :manage, User do |user_model|
  38. # user_model.company = user.company
  39. # end
  40.  
  41. when "client"
  42. can :read, Company, :users => {:id => user.id}
  43.  
  44. can :read, Project, :users => {:id => user.id}
  45.  
  46. # can :read, Shop, do |shop|
  47. # user.projects.include?(shop.project_id)
  48. # end
  49.  
  50. can :read, Shop, :project => { :users => { :id => user.id } }
  51.  
  52. when "shopper"
  53. can :read, Shop, :shopper_id => user.id
  54.  
  55. can :read, Project, :users => {:id => user.id}
  56. end
  57.  
  58. end
  59.  
  60. end
  61.  
  62. Spec
  63.  
  64. require 'spec_helper'
  65.  
  66.  
  67. Projects::ShopsController
  68. describe Projects::ShopsController do
  69. include Devise::TestHelpers
  70.  
  71. def mock_company(stubs={})
  72. (@mock_company ||= mock_model(Company).as_null_object).tap do |company|
  73. company.stub(stubs) unless stubs.empty?
  74. end
  75. end
  76.  
  77. def mock_project(stubs={})
  78. (@mock_project ||= mock_model(Project).as_null_object).tap do |project|
  79. project.stub(stubs) unless stubs.empty?
  80. end
  81. end
  82.  
  83. def mock_shop(stubs={})
  84. (@mock_shop ||= mock_model(Shop).as_null_object).tap do |shop|
  85. shop.stub(stubs) unless stubs.empty?
  86. end
  87. end
  88.  
  89.  
  90. describe "shouble be authenticated" do
  91. it "should fail if we are not authenticated" do
  92. get :index, :project_id => 1, :company_id => 1
  93. response.should_not be_success
  94. end
  95. end
  96.  
  97. describe "Admin examples" do
  98. before(:each) do
  99. @admin = User.create!(:first_name => "admin",
  100. :last_name => "admin",
  101. :email => "admin@perstrat.com",
  102. :password => "password",
  103. :password_confirmaiton => "password",
  104. :role => "admin")
  105.  
  106. @admin.company = mock_company
  107. sign_in @admin
  108.  
  109. @ability = Ability.new(@admin)
  110.  
  111. @controller.stubs(:company_id).returns(@mock_company.id)
  112. Project.stub(:find) { mock_project }
  113. mock_project.stub(:shops) { mock_shop }
  114. end
  115.  
  116. it "should use Projects::ShopsController" do
  117. controller.should be_an_instance_of(Projects::ShopsController)
  118. end
  119.  
  120. #Index
  121. describe "GET projects" do
  122. it "should authorize an admin to view" do
  123. @ability.should be_able_to(:index, Shop)
  124. end
  125.  
  126. it "assigns all pending shops as @shops" do
  127. mock_shop.stub(:paginate) { mock_shop }
  128.  
  129. get :index, :project_id => mock_project.id
  130. assigns(:shops).should eq(mock_shop)
  131. end
  132.  
  133. it "should render the index template" do
  134. get :index, :project_id => mock_project.id
  135. response.should render_template("projects/shops/index")
  136. end
  137. end
  138.  
  139. #Show
  140.  
  141. #New
  142. describe "New: GET projects/1/shops/new" do
  143. before(:each) do
  144. Shop.stub(:new) { mock_shop }
  145. Project.stub(:find) { mock_project }
  146. end
  147. describe "with a locked project" do
  148. before(:each) do
  149. mock_project.stub(:status => 1)
  150. end
  151. it "should find the project" do
  152. Project.expects(:find).with(1).returns(mock_project)
  153. get :new, :project_id => 1
  154. end
  155. it "should create a new instance of Shop" do
  156. Shop.expects(:new).returns(mock_shop)
  157. get :new, :project_id => 1
  158. end
  159. it "should assign an instance of Shop" do
  160. get :new, :project_id => 1
  161. assigns[:shop].should == mock_shop
  162. end
  163. it "should render the new layout" do
  164. get :new, :project_id => 1
  165. response.should render_template("projects/shops/new")
  166. end
  167. end
  168.  
  169. describe "with an unlocked project" do
  170. before(:each) do
  171. mock_project.stub(:status => 0)
  172. end
  173. it "should present a flash message" do
  174. get :new, :project_id => 1
  175. flash[:error].should contain("Project is not locked.")
  176. end
  177. it "should redirect to the project's shops page" do
  178. get :new, :project_id => 1
  179. response.should redirect_to(project_shops_path(mock_project))
  180. end
  181. end
  182.  
  183. #Edit
  184.  
  185. #Create
  186.  
  187. #Update
  188.  
  189. #Destroy
  190.  
  191.  
  192. end
  193.  
  194. describe "Client examples" do
  195. before(:each) do
  196. @client = User.create!(:first_name => "client",
  197. :last_name => "client",
  198. :email => "client@perstrat.com",
  199. :password => "password",
  200. :password_confirmaiton => "password",
  201. :role => "client")
  202.  
  203. @client.company = mock_company
  204. sign_in @client
  205.  
  206. @ability = Ability.new(@client)
  207.  
  208. @controller.stubs(:company_id).returns(@mock_company.id)
  209. Project.stub(:find) { mock_project }
  210. mock_project.stub(:users) { [@client] }
  211. end
  212.  
  213. #Index
  214. describe "GET projects" do
  215. it "should authorize a client to view" do
  216. @ability.should be_able_to(:index, Shop)
  217. end
  218.  
  219. it "assigns all pending shops as @shops" do
  220. mock_shop.stub(:paginate) { mock_shop }
  221.  
  222. get :index, :project_id => mock_project.id
  223. assigns(:shops).should eq(mock_shop)
  224. end
  225. end
  226.  
  227. #Show
  228.  
  229. #New
  230. describe "New: GET projects/1/shops/new" do
  231. it "should not authorize a client to view" do
  232. @ability.should_not be_able_to(:new, Shop)
  233. end
  234. end
  235.  
  236.  
  237. #Edit
  238.  
  239. #Create
  240.  
  241. #Update
  242.  
  243. #Destroy
  244.  
  245.  
  246. end
  247.  
  248. describe "Client not belonging to project examples" do
  249. before(:each) do
  250. @client = User.create!(:first_name => "client",
  251. :last_name => "client",
  252. :email => "client@perstrat.com",
  253. :password => "password",
  254. :password_confirmaiton => "password",
  255. :role => "client")
  256.  
  257. @client.company = mock_company
  258. sign_in @client
  259.  
  260. @ability = Ability.new(@client)
  261.  
  262. @controller.stubs(:company_id).returns(@mock_company.id)
  263. Project.stub(:find) { mock_project }
  264. mock_project.stub(:users) { [] }
  265. end
  266.  
  267. #Index
  268. describe "GET projects" do
  269. it "should authorize a client to view" do
  270. @ability.should_not be_able_to(:index, Shop)
  271. end
  272.  
  273. it "assigns all pending shops as @shops" do
  274. mock_shop.stub(:paginate) { mock_shop }
  275. get :index, :project_id => mock_project.id
  276. assigns(:shops).should eq(mock_shop)
  277. end
  278. end
  279.  
  280.  
  281. end
  282. end
  283. end
Add Comment
Please, Sign In to add comment