require 'spec_helper' require 'rspec_api_documentation' require 'rspec_api_documentation/dsl' require 'securerandom' resource 'Content Folder', :api => true do get '/content_folders' do context 'has a content folder' do before do ContentFolder.delete_all root = FactoryGirl.build :content_folder lit_center = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::LIT_CENTER, :label => ContentFolder::FolderIds::LIT_CENTER merrill_lynch = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::MERRILL_LYNCH, :label => ContentFolder::FolderIds::MERRILL_LYNCH asset_one = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_1111" lit_center.documents << [asset_one] root.child_folders = [lit_center,merrill_lynch] root.save end example 'returns all content folders under root folder' do do_request expect(status).to eq(200) result = JSON.parse(response_body) expect(result).not_to be_nil a_folder_hash = result['content_folders'].first #puts "****** #{a_folder_hash.inspect}" expect(result['content_folders'].count).to eq(2) expect( a_folder_hash['label']).to eq(ContentFolder::FolderIds::LIT_CENTER) end example 'returns 404 when the folder does not exist' do ContentFolder.stub(:last).and_return(nil) do_request expect(status).to eq(404) end end end get '/content_folders/lit_center' do context 'returns the requested application content folder' do before do ContentFolder.delete_all root = FactoryGirl.build :content_folder lit_center = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::LIT_CENTER, :label => ContentFolder::FolderIds::LIT_CENTER merrill_lynch = FactoryGirl.build :content_folder, :folder_id => ContentFolder::FolderIds::MERRILL_LYNCH, :label => ContentFolder::FolderIds::MERRILL_LYNCH asset_one = FactoryGirl.create :digital_asset, :digital_asset_id => "guid_1111" lit_center.documents << [asset_one] root.child_folders = [lit_center,merrill_lynch] root.save end example 'returns the app content folder with documents by folder_id' do do_request expect(status).to eq(200) result = JSON.parse(response_body) expect(result).not_to be_nil expect(result['label']).to eq(ContentFolder::FolderIds::LIT_CENTER) expect(result['documents'].count).to eq(1) asset =result['documents'].first expect(asset['digital_asset_id']).to eq("guid_1111") end example 'returns 404 when the folder does not exist' do ContentFolder.stub(:last).and_return(nil) do_request expect(status).to eq(404) end end end post '/content_folders' do parameter :folder_id, 'folder id', :require => true, :scope => 'content_folder' parameter :label, 'folder label', :require => true, :scope => 'content_folder' parameter :document_ids, 'array of document ids associated with folder', :require => true, :scope => 'content_folder' parameter :child_folders, 'array of child folder hashes', :require => true, :scope => 'content_folder' context 'successful' do folder_id = 'folder id' label = 'label' document_ids = %w(id1 id2) child_folders = nil let(:folder_id) { folder_id } let(:label) { label } let(:document_ids) { document_ids } let(:child_folders) { [{:folder_id => 'child folder id', :label => 'child label', :document_ids => document_ids}] } example 'stores the content folder and returns the content folder instance' do do_request expect(status).to eq(201) result = JSON.parse(response_body) expect(result).not_to be_nil end end context 'missing required values' do folder_id = 'folder id' label = 'label' document_ids = nil child_folders = nil let(:folder_id) { folder_id } let(:label) { label } let(:document_ids) { document_ids } let(:child_folders) { child_folders } example 'returns status 422 when missing document id and child_folders' do do_request expect(status).to eq(422) end end end end