require 'spec_helper' describe DigitalAssetsController do let(:digital_asset) { FactoryGirl.create :digital_asset } describe "index" do it "assigns all digital_assets as @digital_assets" do get :index assigns(:digital_assets).should eq([digital_asset]) end end describe "fund docs" do before do 10.times {FactoryGirl.create :digital_asset} FactoryGirl.create :digital_asset, fund_codes: [] end it "returns only fund docs" do pending "test returns wrong response code, don't know why" get :fund_docs response.code.should eq(200) assigns(:digital_assets).count.should eq(10) end end describe "create/update" do let(:params) {FactoryGirl.attributes_for :digital_asset} it "inserts if the ID+PATH does not exist" do put :update, :id => params[:digital_asset_id], :digital_asset => params response.code.should eq('201') end it "updates if ID exists" do asset = FactoryGirl.create :digital_asset put :update, :id => asset.digital_asset_id, :digital_asset => params response.code.should eq('200') DigitalAsset.where(digital_asset_id: params[:digital_asset_id]).count.should == 0 DigitalAsset.where(digital_asset_id: asset.digital_asset_id).count.should == 1 end it "replaces if PATH exists" do asset = FactoryGirl.create :digital_asset params[:path] = asset.path # duplicate path put :update, :id => params[:digital_asset_id], :digital_asset => params response.code.should eq('200') pp "replacer id = #{params[:digital_asset_id]}" DigitalAsset.where(digital_asset_id: params[:digital_asset_id]).count.should == 1 DigitalAsset.where(digital_asset_id: asset.digital_asset_id).count.should == 0 end it "removes overlapping records with the same ID OR PATH" do end end describe "show" do it 'returns a single asset when given a digital_asset_id' do get :show, :id => digital_asset.digital_asset_id assigns(:digital_asset).digital_asset_id.should eq(digital_asset.digital_asset_id) end it 'returns a single asset when given a sami_code' do get :show, :id => digital_asset.sami_code assigns(:digital_asset).should eq(digital_asset) end it 'returns the most recent asset when given a SAMI ID' do FactoryGirl.create :digital_asset, :sami_code => digital_asset.sami_code, :changed_at => 2.years.ago get :show, :id => digital_asset.sami_code assigns(:digital_asset).should eq(digital_asset) end it "assigns the requested digital_asset as @digital_asset" do get :show, :id => digital_asset.id assigns(:digital_asset).should eq(digital_asset) end # context "documents" do # it 'can get the individual documents via a nested route' do # get :show, :id => digital_asset.documents.first.id, # :digital_asset_id => digital_asset.id # end # end end describe 'search' do context 'blank query' do it 'returns nothing' do get :search assigns(:digital_assets).should be_empty end end context 'path' do before do 5.times { FactoryGirl.create :digital_asset } @single = FactoryGirl.create :digital_asset @single.path = '/one/off.path' @single.doc_changed_at = Time.now @single.content_type = '999' @single.save! end it 'returns a single resource for a complete path' do get :search, :path => '/one/off.path' assigns(:digital_assets).should have(1).result assigns(:digital_assets).first.should eq(@single) end it 'returns an array of resources for a partial match' do end end # /digital_assets/search?content_type[]=777,content_type[]=666 context 'content_type' do before do @second = FactoryGirl.create :digital_asset, :digital_asset_id => 'some-new-asset_134', :content_type => 'prospectus' end it 'returns all documents for a single content type' do get :search, :doctype => 'fact_sheet' assigns(:digital_assets).should == [digital_asset] end it 'returns all documents for a set of multiple content_types' do get :search, :doctype => ['fact_sheet', 'prospectus'] assigns(:digital_assets).should include(@second) assigns(:digital_assets).should include(digital_asset) # assigns(:digital_assets).entries.count.should be(2) end end context 'sami_code' do before do 3.times { FactoryGirl.create :digital_asset, :sami_code => 'SOMETHING.001' } end it 'returns all documents with a particular sami_code' do get :search, :sami => 'SOMETHING.001' assigns(:digital_assets).size.should be(3) end end context 'audience' do before do 3.times { FactoryGirl.create :digital_asset, :audiences => ['492'] } end it 'returns all documents with a particular audience' do get :search, :audience => '492' assigns(:digital_assets).size.should be(3) end end context 'title' do it 'returns all documents with a particular title' do get :search, :title => 'Doc Title' assigns(:digital_assets).should include(digital_asset) end end context 'digital_asset_id' do it 'returns all documents with a particular digital_asset_id' do get :search, :digital_asset_id => digital_asset.digital_asset_id assigns(:digital_assets).entries.should == [digital_asset] end end context 'business owner' do it 'returns all documents with a particular business owner' do get :search, :business_owner => 'biz owner' assigns(:digital_assets).should include(digital_asset) end end context "combination searches" do before {5.times {FactoryGirl.create :digital_asset}} it 'returns and-ed results for multiple criteria' do get :search, :digital_asset_id => digital_asset.digital_asset_id, :title => digital_asset.title assigns(:digital_assets).entries.should == [digital_asset] end it 'returns multiple results' do get :search, :title => digital_asset.title, :audiences => digital_asset.audiences, :sami => digital_asset.sami_code assigns(:digital_assets).size.should be(6) end it 'returns nothing if not all criteria match' do get :search, :digital_asset_id => 'blargh-blargh-blargh', :title => 'Doc Title' assigns(:digital_assets).entries.should == [] end end end end