+# -*- encoding: utf-8 -*-
+#貸手
+require 'spec_helper'
+
+describe Provider do
+ before do
+ @admin = FactoryGirl.create :admin
+ @sp = FactoryGirl.create :system_picture
+ @lg = FactoryGirl.create :license_group
+ @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+ @user = FactoryGirl.create :user_yas
+ @author = @user.author #ユーザ作成時に連動して作成される
+
+ #テストデータを用意してね
+ @f = Rails.root + 'spec/json/provider_source.json'
+ @t = File.open(@f, 'r').read
+ @j = JSON.parse @t
+ end
+ describe '検証に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ end
+
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @pd.name = 'a'
+ @pd.caption = 'a'
+ @pd.url = 'http://test.jp/'
+ @pd.description = 'a'
+ @pd.demander_url = 'http://test.jp/'
+ @pd.should be_valid
+ end
+ it '上限データが通る' do
+ @pd.name = 'a'*50
+ @pd.caption = 'a'*30
+ @pd.url = 'http://test.jp/aaaaa' + 'a' * 180
+ @pd.description = 'a' * 99999
+ @pd.demander_url = 'http://test.jp/aaaaa' + 'a' * 180
+ @pd.should be_valid
+ end
+ end
+
+ context 'provider_status_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.provider_status_id = nil
+ @pd.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pd.provider_status_id = 'a'
+ @pd.should_not be_valid
+ end
+ it '存在する借受状況でなければ失敗する' do
+ @pd.provider_status_id = 0
+ @pd.should_not be_valid
+ end
+ end
+ context 'nameを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.name = ''
+ @pd.should_not be_valid
+ end
+ it '51文字以上なら失敗する' do
+ @pd.name = 'a'*51
+ @pd.should_not be_valid
+ end
+ it '重複していたら失敗する' do
+ l = FactoryGirl.create :provider
+ @pd.should_not be_valid
+ end
+ end
+ context 'captionを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.caption = ''
+ @pd.should_not be_valid
+ end
+ it '31文字以上なら失敗する' do
+ @pd.caption = 'a'*31
+ @pd.should_not be_valid
+ end
+ end
+ context 'urlを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.url = ''
+ @pd.should_not be_valid
+ end
+ it '201文字以上なら失敗する' do
+ @pd.url = 'http://test.jp/aaaaa' + 'a' * 181
+ @pd.should_not be_valid
+ end
+ it 'url形式でないなら失敗する' do
+ @pd.url = 'aaaaaaa'
+ @pd.should_not be_valid
+ end
+ end
+ context 'descriptionを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.description= ''
+ @pd.should_not be_valid
+ end
+ end
+ context 'demander_urlを検証するとき' do
+ it 'nullなら失敗する' do
+ @pd.demander_url = ''
+ @pd.should_not be_valid
+ end
+ it '201文字以上なら失敗する' do
+ @pd.demander_url = 'http://test.jp/aaaaa' + 'a' * 181
+ @pd.should_not be_valid
+ end
+ it 'url形式でないなら失敗する' do
+ @pd.demander_url = 'aaaaaaa'
+ @pd.should_not be_valid
+ end
+ end
+ end
+
+ describe 'デフォルト値補充に於いて' do
+ it 'defined' do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ @pd.supply_default
+ end
+ end
+
+ describe '上書き補充に於いて' do
+ it 'defined' do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ @pd.overwrite
+ end
+ end
+
+ describe '所持判定に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ end
+ it '管理者ならyes' do
+ @pd.own?(@admin).should == true
+ end
+ it '作家ならno' do
+ @pd.own?(@author).should == false
+ end
+ it 'パラメータが管理者でないならno' do
+ @pd.own?(nil).should == false
+ end
+ end
+
+ describe '閲覧許可に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ end
+ it '管理者なら許可する' do
+ r = @pd.visible?(@admin)
+ r.should == true
+ end
+ it '作家なら許可しない' do
+ r = @pd.visible?(@author)
+ r.should == false
+ end
+ it 'それ以外のとき不許可を返す。' do
+ r = @pd.visible?(nil)
+ r.should be_false
+ end
+ end
+
+ describe '状態に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ it '借り受けしていないのとき0を返す' do
+ ProviderStatus.any_instance.stub(:token).with(any_args).and_return(nil)
+ r = @pd.status
+ r.should eq 0
+ end
+ it '借り受けしているのとき1を返す' do
+ ProviderStatus.any_instance.stub(:token).with(any_args).and_return('a')
+ r = @pd.status
+ r.should eq 1
+ end
+ end
+
+ describe '一覧取得に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
+ end
+ context 'page補正について' do
+ it '文字列から数値に変換される' do
+ Provider.page('8').should eq 8
+ end
+ it 'nilの場合は1になる' do
+ Provider.page().should eq 1
+ end
+ it '0以下の場合は1になる' do
+ Provider.page('0').should eq 1
+ end
+ end
+ context 'page_size補正について' do
+ it '文字列から数値に変換される' do
+ Provider.page_size('7').should eq 7
+ end
+ it 'nilの場合はProvider.default_page_sizeになる' do
+ Provider.page_size().should eq Provider.default_page_size
+ end
+ it '0以下の場合はProvider.default_page_sizeになる' do
+ Provider.page_size('0').should eq Provider.default_page_size
+ end
+ it 'Provider.max_page_sizeを超えた場合はProvider.max_page_sizeになる' do
+ Provider.page_size('1000').should eq Provider.max_page_size
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ Provider.stub(:list_opt).with(any_args).and_return({})
+ Provider.should_receive(:list_opt).with(any_args).exactly(1)
+ r = Provider.list
+ end
+ end
+ it 'リストを返す' do
+ r = Provider.list
+ r.should eq [@pd]
+ end
+ it '管理名で並んでいる' do
+ v = FactoryGirl.create :provider, :name => "0"
+ r = Provider.list
+ r.should eq [v, @pd]
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @ps2 = FactoryGirl.create :provider_status
+ @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+ @ps3 = FactoryGirl.create :provider_status
+ @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+ @ps4 = FactoryGirl.create :provider_status
+ @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+ @ps5 = FactoryGirl.create :provider_status
+ @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+ Provider.stub(:default_page_size).and_return(2)
+ end
+ it '通常は2件を返す' do
+ r = Provider.list
+ r.should have(2).items
+ end
+ it 'page=1なら末尾2件を返す' do
+ #管理名で並んでいる
+ r = Provider.list(1)
+ r.should eq [@pd5, @pd4]
+ end
+ it 'page=2なら中間2件を返す' do
+ r = Provider.list(2)
+ r.should eq [@pd3, @pd2]
+ end
+ it 'page=3なら先頭1件を返す' do
+ r = Provider.list(3)
+ r.should eq [@pd]
+ end
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @ps2 = FactoryGirl.create :provider_status
+ @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+ @ps3 = FactoryGirl.create :provider_status
+ @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+ @ps4 = FactoryGirl.create :provider_status
+ @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+ @ps5 = FactoryGirl.create :provider_status
+ @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+ Provider.stub(:default_page_size).and_return(2)
+ end
+ it '件数0は全件(5件)を返す' do
+ r = Provider.list 5, 0
+ r.should have(5).items
+ end
+ end
+ end
+ describe '待機中一覧取得に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id, :name => "6"
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ Provider.stub(:list_opt).with(any_args).and_return({:include => {:provider_status => {}} })
+ Provider.should_receive(:list_opt).with(any_args).exactly(1)
+ r = Provider.available_list
+ end
+ end
+ it 'リストを返す' do
+ r = Provider.available_list
+ r.should eq [@pd]
+ end
+ it '管理名で並んでいる' do
+ @ps2 = FactoryGirl.create :provider_status
+ v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
+ r = Provider.available_list
+ r.should eq [v, @pd]
+ end
+ it '借受状況のトークンが設定されていない貸手に限る' do
+ @ps2 = FactoryGirl.create :provider_status, :token => 'aaaaa'
+ v = FactoryGirl.create :provider, :name => "0", :provider_status_id => @ps2.id
+ r = Provider.available_list
+ r.should eq [@pd]
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @ps2 = FactoryGirl.create :provider_status
+ @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+ @ps3 = FactoryGirl.create :provider_status
+ @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+ @ps4 = FactoryGirl.create :provider_status
+ @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+ @ps5 = FactoryGirl.create :provider_status
+ @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+ Provider.stub(:default_page_size).and_return(2)
+ end
+ it '通常は2件を返す' do
+ r = Provider.available_list
+ r.should have(2).items
+ end
+ it 'page=1なら末尾2件を返す' do
+ #管理名で並んでいる
+ r = Provider.available_list(1)
+ r.should eq [@pd5, @pd4]
+ end
+ it 'page=2なら中間2件を返す' do
+ r = Provider.available_list(2)
+ r.should eq [@pd3, @pd2]
+ end
+ it 'page=3なら先頭1件を返す' do
+ r = Provider.available_list(3)
+ r.should eq [@pd]
+ end
+ end
+ context 'DBに5件あって1ページの件数を2件に変えたとして' do
+ before do
+ @ps2 = FactoryGirl.create :provider_status
+ @pd2 = FactoryGirl.create :provider, :provider_status_id => @ps2.id, :name => "5"
+ @ps3 = FactoryGirl.create :provider_status
+ @pd3 = FactoryGirl.create :provider, :provider_status_id => @ps3.id, :name => "4"
+ @ps4 = FactoryGirl.create :provider_status
+ @pd4 = FactoryGirl.create :provider, :provider_status_id => @ps4.id, :name => "3"
+ @ps5 = FactoryGirl.create :provider_status
+ @pd5 = FactoryGirl.create :provider, :provider_status_id => @ps5.id, :name => "2"
+ Provider.stub(:default_page_size).and_return(2)
+ end
+ it '件数0は全件(5件)を返す' do
+ r = Provider.available_list 5, 0
+ r.should have(5).items
+ end
+ end
+ end
+ describe '一覧取得オプションに於いて' do
+ it 'includeキーを含んでいる' do
+ r = Provider.list_opt
+ r.has_key?(:include).should be_true
+ end
+ it '1つの項目を含んでいる' do
+ r = Provider.list_opt[:include]
+ r.should have(1).items
+ end
+ it '借受状況を含んでいる' do
+ r = Provider.list_opt[:include]
+ r.has_key?(:provider_status).should be_true
+ end
+ end
+ describe 'json一覧出力オプションに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ it '借受状況を含んでいる' do
+ r = Provider.list.to_json Provider.list_json_opt
+ j = JSON.parse r
+ i = j.first
+ i.has_key?('provider_status').should be_true
+ end
+ end
+
+ describe '単体取得に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ Provider.stub(:show_opt).with(any_args).and_return({})
+ Provider.should_receive(:show_opt).with(any_args).exactly(1)
+ r = Provider.show @pd.id, @admin
+ end
+ it '閲覧許可を問い合わせている' do
+ Provider.any_instance.stub(:visible?).with(any_args).and_return(true)
+ Provider.any_instance.should_receive(:visible?).with(any_args).exactly(1)
+ r = Provider.show @pd.id, @admin
+ end
+ end
+ it '指定の貸手を返す' do
+ r = Provider.show @pd.id, @admin
+ r.should eq @pd
+ end
+ context '閲覧許可が出なかったとき' do
+ it '403Forbidden例外を返す' do
+ Provider.any_instance.stub(:visible?).and_return(false)
+ lambda{
+ Provider.show @pd.id, @admin
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context '存在しない貸手を開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ Provider.show 110, @admin
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+
+ describe '編集取得に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ Provider.stub(:show_opt).with(any_args).and_return({})
+ Provider.should_receive(:show_opt).with(any_args).exactly(1)
+ r = Provider.edit @pd.id, @admin
+ end
+ it '所持判定を問い合わせている' do
+ Provider.any_instance.stub(:own?).with(any_args).and_return(true)
+ Provider.any_instance.should_receive(:own?).with(any_args).exactly(1)
+ r = Provider.edit @pd.id, @admin
+ end
+ end
+ it '指定の貸手を返す' do
+ Provider.any_instance.stub(:own?).and_return(true)
+ r = Provider.edit @pd.id, @admin
+ r.should eq @pd
+ end
+ context '権限がなかったとき' do
+ it '403Forbidden例外を返す' do
+ Provider.any_instance.stub(:own?).and_return(false)
+ lambda{
+ Provider.edit @pd.id, @admin
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context '存在しない貸手を開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ Provider.edit 110, @admin
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+ describe '単体取得オプションに於いて' do
+ it 'includeキーを含んでいる' do
+ r = Provider.show_opt
+ r.has_key?(:include).should be_true
+ end
+ it '1つの項目を含んでいる' do
+ r = Provider.show_opt[:include]
+ r.should have(1).items
+ end
+ it '借受状況を含んでいる' do
+ r = Provider.show_opt[:include]
+ r.has_key?(:provider_status).should be_true
+ end
+ end
+ describe 'json単体出力オプションに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ it '借受状況を含んでいる' do
+ r = Provider.show(@pd.id, @admin).to_json Provider.show_json_opt
+ j = JSON.parse r
+ i = j
+ i.has_key?('provider_status').should be_true
+ end
+ end
+
+ describe '更新作成に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @pd = FactoryGirl.build :provider, :provider_status_id => @ps.id
+ @name = @pd.name
+ @attr = @pd.attributes
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ end
+ it '貸手が追加される' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should change Provider, :count
+ end
+ it '借受状況が作成される' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should change ProviderStatus, :count
+ end
+ it '貸手を返す' do
+ r = Provider.store(@name, @attr)
+ r.is_a?(Provider).should be_true
+ r.valid?.should be_true
+ end
+ end
+ context '借受状況があるとき' do
+ before do
+ @pd = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ @name = @pd.name
+ @attr = @pd.attributes
+ end
+ it '貸手は変化しない' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should_not change Provider, :count
+ end
+ it '借受状況は変化しない' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should_not change ProviderStatus, :count
+ end
+ end
+ context '借受状況の作成に失敗したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:save).with(any_args).and_return(false)
+ end
+ it '貸手は変化しない' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should_not change Provider, :count
+ end
+ it '借受状況は変化しない' do
+ lambda {
+ r = Provider.store(@name, @attr)
+ }.should_not change ProviderStatus, :count
+ end
+ it '貸手の検証は失敗している' do
+ r = Provider.store(@name, @attr)
+ r.valid?.should be_false
+ end
+ end
+ end
+
+ describe 'インポートに於いて' do
+ before do
+ @urls = ['http://pettan.net/provider_source.json']
+ end
+ context '事前チェック' do
+ before do
+ WebMock.stub_request(:get, @urls.first).to_return(:body => @t)
+ end
+ it '貸手文献インポートを依頼する' do
+ Provider.should_receive(:import_urls).with(@urls).exactly(1)
+ Provider.stub(:import_urls).with(@urls).and_return({@urls.first => {:validations => []}})
+ Provider.import(@urls)
+ end
+ it '貸手更新を一回依頼する' do
+ Provider.stub(:store).with(any_args).and_return(Provider.new)
+ Provider.should_receive(:store).with(any_args).exactly(1)
+ Provider.import(@urls)
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ WebMock.stub_request(:get, @urls.first).to_return(:body => @t)
+ end
+ it '貸手が追加される' do
+ lambda {
+ Provider.import(@urls)
+ }.should change Provider, :count
+ end
+ it '借受状況がなかったら、借受状況を作成される' do
+ lambda {
+ Provider.import(@urls)
+ }.should change ProviderStatus, :count
+ end
+ it '借受状況があるなら、借受状況は作成しない' do
+ Provider.import(@urls)
+ ProviderStatus.any_instance.should_not_receive(:save).exactly(1)
+ Provider.import(@urls)
+ end
+ it 'Hashを返す' do
+ r = Provider.import(@urls)
+ r.is_a?(Hash).should be_true
+ end
+ end
+ context '貸手作成に失敗したとき' do
+ before do
+ WebMock.stub_request(:get, @urls.first).to_return(:status => 404)
+ end
+ it '貸手の数に変化がない' do
+ lambda {
+ Provider.import(@urls)
+ }.should_not change Provider, :count
+ end
+ it 'Hashを返す' do
+ r = Provider.import(@urls)
+ r.is_a?(Hash).should be_true
+ end
+ end
+ end
+
+end