require 'spec_helper' require 'app/models/service_digital_asset' describe ServiceDigitalAsset do subject { FactoryGirl.build :service_digital_asset } context '#fields' do let(:defined_fields) { [:title, :changed_at, :sami_code, :product_ids, :program_ids, :summary, :published_at, :expires_at, :digital_asset_id, :audiences, :path, :doc_changed_at, :content_type, :pages, :size, :mime_type, :subject, :keywords, :author, :finra_path, :fund_codes, :display_on_website] } it 'has all defined fields' do defined_fields.each { |f| should respond_to(f) } end end context '#validation' do required_fields = [:digital_asset_id, :title, :changed_at, :published_at, :expires_at, :path, :audiences] required_fields.each do |f| it "validates #{f} is required" do should be_valid subject.send("#{f}=", nil) should be_invalid "should be invalid if #{f} is missing" end end it 'cannot have a past expiration date' do subject.expires_at = 2.hours.ago subject.should be_invalid end it "cannot have duplicate digital_asset_id" do FactoryGirl.create :digital_asset, :digital_asset_id => "aaaa_kkkk_1234" digital_asset = FactoryGirl.build :digital_asset, :digital_asset_id => "aaaa_kkkk_1234" # p "errors = #{digital_asset.errors.inspect}" digital_asset.should be_invalid end end context 'helper functions' do let(:asset) { FactoryGirl.build :service_digital_asset } it 'is effective if displayed on website and has an expiration date' do asset.display_on_website = true asset.expires_at = Time.now asset.effective?.should == true end it 'is not effective if not displayed on website' do asset.display_on_website = false asset.expires_at = Time.now asset.effective?.should == false end it 'is not effective if not it does not have an expiration date' do asset.display_on_website = true asset.expires_at = nil asset.effective?.should == false end it 'is expired if it does not have an expiration date' do asset.expires_at = nil asset.expired?.should == true end it 'is expired if its expiration date is in the past' do asset.expires_at = 1.hour.ago asset.expired?.should == true end it 'is not expired if its expiration date is in the future' do asset.expires_at = 1.hour.from_now asset.expired?.should == false end it 'is a FINRA document if the content type is the appropriate value' do asset.content_type = DigitalAsset::ContentType::FINRA asset.finra?.should == true end it 'is a not FINRA document if the content type is some other value' do asset.content_type = DigitalAsset::ContentType::PROSPECTUS asset.finra?.should == false end it 'is not ready for deletion if there is no unpublished at date' do asset.unpublished_at = nil asset.delete?.should == false end it 'is ready for deletion if there is a unpublished at date' do asset.unpublished_at = Time.now asset.delete?.should == true end it 'is ready for deletion if it is expired' do asset.expires_at = nil asset.delete?.should == true end it 'is ready for deletion when marked for deletion' do asset.delete?.should == false asset.mark_for_deletion asset.delete?.should == true end it 'returns a 10 year old date if the time field is blank' do asset.published_at = nil result = asset.default_blank_time(:published_at) result.to_i.should == 10.years.ago.to_i end it 'returns the time field value if it is not blank' do expected = asset.published_at result = asset.default_blank_time(:published_at) result.to_i.should == expected.to_i end end end