require 'spec_helper' require 'rspec_api_documentation/dsl' resource "Digital Assets", :api => true do get "/digital_assets" do let(:asset) {FactoryGirl.create :digital_asset } parameter :digital_asset_id, "legacy digital_asset_id id numbers assigned to teamsite docs" example_request "show all digital_assets" do status.should eq(200) end context "search by asset metadata" do example "query for a document by digital_asset_id" do do_request(digital_asset_id: asset.digital_asset_id) status.should eq(200) response_body.should == "[#{DigitalAsset.where(digital_asset_id: asset.digital_asset_id).first.to_json}]" end example "query for documents by fund-code" do FactoryGirl.create :digital_asset # add a 2nd asset to the DB FactoryGirl.create :digital_asset, fund_codes: ['00200'] # add a 3nd asset to the DB do_request(fund: '00200') status.should eq(200) rsp_json = JSON.parse(response_body) rsp_json.count.should == 2 end example "query for a document by digital_asset_id" do do_request(digital_asset_id: asset.digital_asset_id) status.should eq(200) response_body.should == "[#{DigitalAsset.where(digital_asset_id: asset.digital_asset_id).first.to_json}]" end example "query for documents by path" do other_path = '/some/other/path.pdf' other_asset = FactoryGirl.create :digital_asset, path: other_path # add a 2nd asset to the DB do_request(path: other_path) status.should eq(200) response_body.should == "[#{other_asset.to_json}]" end example "query for a document by digital_asset_id" do do_request(digital_asset_id: asset.digital_asset_id) status.should eq(200) response_body.should == "[#{DigitalAsset.where(digital_asset_id: asset.digital_asset_id).first.to_json}]" end example "query for documents by update date" do recent = FactoryGirl.create :digital_asset, published_at: 1.hour.ago do_request(published: 2.hours.ago) status.should eq(200) response_body.should == "[#{recent.to_json}]" end end end get "/digital_assets/:id" do before {FactoryGirl.create :digital_asset, digital_asset_id: 'foo-bar-id'} parameter :id, "the ID of the digital asset to get, equivalent to the digital_asset_id or Accent doc-id" let(:id) {'foo-bar-id'} example_request "find a DigitalAsset by ID" do status.should == 200 response_body.should == "#{DigitalAsset.find(id).to_json}" end example "returns a 404 for docs we dont have in the DB" do do_request(id: 'not-found-doc') status.should == 404 end end post "/digital_assets/updated_time" do parameter :ids, :require => true before { FactoryGirl.create :digital_asset, digital_asset_id: 'id-1' FactoryGirl.create :digital_asset, digital_asset_id: 'id-3' FactoryGirl.create :digital_asset, digital_asset_id: 'id-4' } let(:ids) {['id-1','id-2','id-3','id-4']} example "retrieve the updated_time for a list of id " do do_request() status.should eq(200) body_updated_time = JSON.parse(response_body) expect(body_updated_time.length).to eq(3) end end put "/digital_assets/:id" do parameter :digital_asset, :require => true parameter :digital_asset_id, "the ID of the digital asset to get, equivalent to the digital_asset_id or Accent doc-id", :require => true let(:da) {FactoryGirl.create :digital_asset, digital_asset_id: 'foo-bar-digital-asset-id'} let(:digital_asset) {da.attributes} let(:id) {da.digital_asset_id} example "update a digital_asset" do digital_asset['title'] = 'another title' do_request() status.should eq(200) updated = DigitalAsset.find(id) expect(updated.title.strip).to eq('another title') end end post "/digital_assets" do parameter :digital_asset, :require => true let(:da) {FactoryGirl.build :digital_asset, digital_asset_id: 'new-digital-asset-id'} let(:digital_asset) {da.attributes} example "insert a digital_asset if not found" do do_request() status.should eq(201) inserted = DigitalAsset.find('new-digital-asset-id') expect(inserted.present?).to be true end end delete "/digital_assets/:id" do parameter :id, "the ID of the digital asset to delete, equivalent to the guid or Accent doc-id", :require => true let(:da) {FactoryGirl.create :digital_asset, digital_asset_id: 'foo-bar-digital-asset-id'} let(:id) {da.digital_asset_id} example "delete a digital_asset" do do_request() status.should eq(200) expect { DigitalAsset.find(id) }.to raise_error end end get "/" do example_request "no-op" do true.should eq(true) end end end