require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') require 'mocha' # See cucumber tests (ie. /features/edit_document.feature) for more tests, including ones that test the edit method & view # You can run the cucumber tests with # # cucumber --tags @edit # or # rake cucumber describe CatalogController do before :all do @behavior = Hydra::Controller::CatalogControllerBehavior.deprecation_behavior Hydra::Controller::CatalogControllerBehavior.deprecation_behavior = :silence end after :all do Hydra::Controller::CatalogControllerBehavior.deprecation_behavior = @behavior end before do #controller.stubs(:protect_from_forgery).returns("meh") session[:user]='bob' end it "should use CatalogController" do controller.should be_an_instance_of(CatalogController) end describe "Paths Generated by Custom Routes:" do # paths generated by custom routes it "should map {:controller=>'catalog', :action=>'index'} to GET /catalog" do { :get => "/catalog" }.should route_to(:controller => 'catalog', :action => 'index') end it "should map {:controller=>'catalog', :action=>'show', :id=>'test:3'} to GET /catalog/test:3" do { :get => "/catalog/test:3" }.should route_to(:controller => 'catalog', :action => 'show', :id=>'test:3') end it "should map {:controller=>'catalog', :action=>'edit', :id=>'test:3'} to GET /catalog/test:3" do { :get => "/catalog/test:3/edit" }.should route_to(:controller => 'catalog', :action => 'edit', :id=>'test:3') end it "should map catalog_path" do # catalog_path.should == '/catalog' catalog_path("test:3").should == '/catalog/test:3' end end it "should not choke on objects with periods in ids (ie Fedora system objects)" do pending "Need to override blacklight routes" ## We could do something like this to remove the catalog/show route and replace it with a route that allows dots (e.g. resources :catalog, :id=> /.+/) # def add_route # new_route = ActionController::Routing::Routes.builder.build(name, route_options) # ActionController::Routing::Routes.routes.insert(0, new_route) # end # def remove_route # ActionController::Routing::Routes.routes.reject! { |r| r.instance_variable_get(:@requirements)[:slug_id] == id } # end catalog_path("fedora-system:FedoraObject-3.0").should == '/catalog/fedora-system:FedoraObject-3.0' { :get => "/catalog/fedora-system:FedoraObject-3.0" }.should route_to(:controller => 'catalog', :action => 'show', :id=>'fedora-system:FedoraObject-3.0') end describe "index" do describe "access controls" do before(:all) do @public_only_results = Blacklight.solr.find Hash[:fq=>"access_t:public"] # @public_only_results = Blacklight.solr.find Hash[:phrases=>{:access_t=>"public"}] # @private_only_results = Blacklight.solr.find Hash[:phrases=>{:access_t=>"private"}] @private_only_results = Blacklight.solr.find Hash[:fq=>"access_t:private"] end it "should only return public documents if role does not have permissions" do controller.stubs(:current_user).returns(nil) get :index assigns(:document_list).count.should == @public_only_results.docs.count end it "should return all documents if role does have permissions" do mock_user = FactoryGirl.create(:user, :email=>"BigWig@example.com") # session[:superuser_mode] = true mock_user.stubs(:is_being_superuser?).returns(true) #sign_in mock_user controller.stubs(:current_user).returns(mock_user) get :index ### This fails when there are more than 10 public documents in the solr index ### TODO: instead, expect a certain query(especially the :fq component) to solr assigns(:document_list).count.should > @public_only_results.docs.count end end end describe "edit" do it "should trigger show action" do controller.expects(:show) controller.stubs(:enforce_access_controls) controller.stubs(:load_fedora_document) get :edit, :id=>'hydrangea:fixture_mods_article1' end it "should render show template (which then delegates to edit partials)" do controller.stubs(:enforce_access_controls) controller.stubs(:load_fedora_document) get :edit, :id=>'hydrangea:fixture_mods_article1' response.should render_template("show") end end describe "filters" do describe "index" do it "should trigger enforce_index_permissions" do controller.expects(:add_access_controls_to_solr_params) controller.expects(:enforce_index_permissions) get :index end end describe "show" do it "should trigger enforce_show_permissions and load_fedora_document" do controller.stubs(:current_user).returns(nil) controller.expects(:load_fedora_document) controller.expects(:enforce_show_permissions) get :show, :id=>'test:3' end end describe "edit" do it "should trigger enforce_edit_permissions and load_fedora_document" do controller.stubs(:current_user).returns(nil) controller.expects(:load_fedora_document) controller.expects(:enforce_edit_permissions) get :edit, :id=>'test:3' end end end end