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 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 User.any_instance.stubs(:email).returns("BigWig@example.com") mock_user = User.new # session[:superuser_mode] = true mock_user.stubs(:is_being_superuser?).returns(true) controller.stubs(:current_user).returns(mock_user) get :index assigns(:document_list).count.should > @public_only_results.docs.count end end end describe "load_fedora_document" do it "should load @document_fedora and @file_assets" do # controller.stubs(:params).returns({:id=>"foo:id"}) # stub_base_object = stub("Base Object") # ActiveFedora::ContentModel.expects(:known_models_for).with( stub_base_object ).returns( [ModsAsset] ) # stub_mods_asset = stub("MODS Asset") # stub_mods_asset.expects(:file_objects).with(:response_format=>:solr).returns("file assets response") # Note: Had to stub Fedora::Repository.instance.find_model rather than stubbing ActiveFedora::Base.load_instance and ModsAsset.load_instance because # Mocha was not unstubbing the ModsAsset class # # ActiveFedora::Base.stubs(:load_instance).with("foo:id").returns( stub_base_object ) # ModsAsset.stubs(:load_instance).with("foo:id").returns( stub_mods_asset ) # Fedora::Repository.instance.expects(:find_model).with("foo:id", ActiveFedora::Base).returns( stub_base_object ) # Fedora::Repository.instance.expects(:find_model).with("foo:id", ModsAsset).returns( stub_mods_asset ) controller.params[:id]='hydrangea:fixture_mods_article1' controller.load_fedora_document assigns[:document_fedora].class.should == ModsAsset assigns[:document_fedora].pid.should == 'hydrangea:fixture_mods_article1' assigns[:file_assets].class.should == Solr::Response::Standard assigns[:file_assets].hits.size.should == 1 assigns[:file_assets].hits.first["id"].should == "hydrangea:fixture_uploaded_svg1" 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.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.expects(:load_fedora_document) controller.expects(:enforce_edit_permissions) get :edit, :id=>'test:3' end end end end