gem 'rails', '3.1.1'
gem 'devise', '=1.5.2'
+gem 'rest-client'
gem 'aws-s3'
gem 'validates_existence'
gem "validate_url"
end
end
+ def ymd_to_time ymd_str
+ return nil if ymd_str.blank?
+ date = nil
+ begin
+ date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+ rescue
+ date = nil
+ end
+ date
+ end
+
+ def export_url demander_url, token, date
+ u = demander_url + (demander_url[-1] == '/' ? '' : '/')
+ u = URI.join(u, 'licenses_export.json?auth_token=' + token)
+ u = URI.join(u, '&date=' + date) unless date.blank?
+ u.to_s
+ end
+
+ def export_from_provider url
+ json = RestClient.get url
+ JSON.parse json
+ end
+
=begin
rescue_from ActiveRecord::RecordNotFound, :with => :render_404
class DemandersController < ApplicationController
layout 'test' if MagicNumber['test_layout']
- before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req]
+ before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export]
def index
@demander = @demand_user.demander
end
end
+ def licenses_export
+ @demander = @demand_user.demander
+ date = ymd_to_time params[:date]
+ @licenses = @demander.licenses_export(date)
+ respond_to do |format|
+ format.html { render :text }
+ format.json { render :json => @licenses.to_json }
+ end
+ end
+
end
class ProviderStatusesController < ApplicationController
layout 'test' if MagicNumber['test_layout']
- before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy]
+ before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import]
def index
@page = ProviderStatus.page params[:page]
format.json { head :ok }
end
end
+
+ def licenses_import
+ @provider_status = ProviderStatus.show(params[:id], @admin)
+ raise ActiveRecord::Forbidden if @provider_status.status == 0
+ t = ymd_to_time params[:date]
+ url = export_url @provider_status.provider.demander_url, @provider_status.token, t
+ @failures = ProviderLicense.import @provider_status.provider.id, export_from_provider(url)
+ respond_to do |format|
+ format.html # show.html.erb
+ format.json { render :json => @provider_status.to_json(ProviderStatus.show_json_opt) }
+ end
+ end
+
end
self.demander_status.stop
end
+ def licenses_export date_str = nil
+ raise ActiveRecord::Forbidden unless self.status == 3
+ License.export date_str
+ end
+
end
@credit_pictures_attr
end
+ def self.export(dt = nil)
+ opt = {}
+ opt.merge!({:conditions => ['updated_at >= ?', dt]}) if dt
+# opt.merge!({:order => 'name'})
+ License.find(:all, opt)
+ end
+
end
--- /dev/null
+class ProviderLicense < ActiveRecord::Base
+ belongs_to :provider
+
+ validates :provider_id, :presence => true, :numericality => true, :existence => true
+ validates :providers_license_id, :presence => true, :numericality => true
+ validates :demanders_license_id, :presence => true, :numericality => true
+
+ def self.exist_license pid, plid
+ ProviderLicense.find_by_provider_id_and_providers_license_id(pid, plid) != nil
+ end
+
+ def self.import pid, providers_licenses
+ res = []
+ ProviderLicense.transaction do
+ providers_licenses.each do |providers_license|
+ if ProviderLicense.exist_license(pid, providers_license['id'])
+ else
+ if demander_license = License.find_by_name(providers_license['name'])
+ pl = ProviderLicense.new :provider_id => pid, :providers_license_id => providers_license['id'], :demanders_license_id => demander_license.id
+ unless pl.save
+ res << providers_license
+ end
+ else
+ res << providers_license
+ end
+ end
+ end
+ raise ActiveRecord::Rollback if res.any?
+ end
+ res
+ end
+
+end
</p>
<% if @demander.status == 3 %>
<p>
+ <%= t_m 'DemandUser.authentication_token' -%>
+ <%= (@demander.demand_user.authentication_token) if @demander.demand_user.authentication_token %>
+ </p>
+ <p>
<%= t_m 'DemanderStatus.receive_hour1' -%>
<%= h(@demander.demander_status.receive_hour1) %>
</p>
--- /dev/null
+<h1><%= t('.title') %></h1>
+<p id="notice"><%= notice %></p>
+
+<% if @failures.empty? %>
+ <h2>
+ <%= t('provider_statuses.licenses_import.success') %>
+ </h2>
+<% else %>
+ <h2>
+ <%= t('provider_statuses.licenses_import.fail', :size => @failures.size) %>
+ </h2>
+ <table>
+ <tr>
+ <th><%= t_m 'License.caption' -%></th>
+ <th><%= t_m 'License.name' -%></th>
+ </tr>
+ <% @failures.each do |license| %>
+ <tr>
+ <td>
+ <%= link_to h(license['caption']), license['url'] %>
+ </td>
+ <td>
+ <%= h(license['name']) %>
+ </td>
+ </tr>
+ <% end %>
+ </table>
+ <%= link_to t('provider_statuses.licenses_import.to_show'), provider_status_path(@provider_status) %>
+<% end %>
</p>
<p>
<%= link_to t('provider_statuses.show.demander_open'), @provider_status.provider.demander_url %>
-<p>
-
-<p>
- <%= t('provider_statuses.show.receive_announce') %>
</p>
-<p>
- <% if @provider_status.status == 0 %>
- <%= link_to t('provider_statuses.show.receive_start'), edit_provider_status_path(@provider_status) %>
- <% else %>
+
+<% if @provider_status.status == 0 %>
+ <p>
+ <%= t('provider_statuses.show.receive_announce') %>
+ </p>
+ <%= link_to t('provider_statuses.show.receive_start'), edit_provider_status_path(@provider_status) %>
+<% else %>
+ <p>
+ <%= link_to t('provider_statuses.show.licenses_import'), licenses_import_provider_status_path(@provider_status), :method => :post %>
+ </p>
+ <p>
<%= link_to t('provider_statuses.show.receive_stop'), provider_status_path(@provider_status), :method => :delete %>
- <% end %>
-</p>
+ </p>
+<% end %>
<%= link_to t('link.back'), provider_statuses_path %>
# Load the rails application
require File.expand_path('../application', __FILE__)
require 'RMagick'
+require 'rest_client'
# Initialize the rails application
Pettanr::Application.initialize!
created_at: 作成
updated_at: 更新
provider_license:
+ provider_id: 貸手
+ providers_license_id: 貸手側ライセンス
+ demanders_license_id: 借手側ライセンス
created_at: 作成
updated_at: 更新
provider_artist:
receive_announce: 借受するには借手となって借受申請する。
receive_start: この貸手からの借受を開始する
receive_stop: 借受を停止する
+ licenses_import: ライセンスをインポートする
new:
title: 借受状況登録
edit:
title: 借受状況編集
+ licenses_import:
+ title: ライセンスインポート
+ success: インポートが完了しました
+ fail: 次のライセンス(%{size}件)でエラーがありました。ライセンスのインストール状況を確認してください。
+ to_show: 借受状況を表示する
status: 状態
open: 開く
to_provider: 貸手に切り替える
get :edit
put :update
delete :destroy
+ post :licenses_import
end
end
resources :demanders do
get :list
get :browse
post :req
+ get :licenses_export
end
member do
get :edit
--- /dev/null
+class CreateProviderLicenses < ActiveRecord::Migration
+ def change
+ create_table :provider_licenses do |t|
+ t.integer :provider_id, :null => false, :default => 0
+ t.integer :providers_license_id, :null => false, :default => 0
+ t.integer :demanders_license_id, :null => false, :default => 0
+
+ t.timestamps
+ end
+ end
+end
end
end
end
- context '管理者権限がないとき' do
+ context '借手権限がないとき' do
before do
sign_out @demand_user
end
end
end
end
-
+
+ describe 'ライセンスエクスポートに於いて' do
+ before do
+ @ds = FactoryGirl.create :demander_status
+ @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id, :demand_user_id => @demand_user.id
+ sign_in @demand_user
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Demander.any_instance.stub(:licenses_export).with(any_args).and_return([@license, @license, @license])
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :licenses_export, :format => :json
+ response.should be_success
+ end
+ it '日付文字列変換を依頼している' do
+ DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+ get :licenses_export, :date => '20111010', :format => :json
+ end
+ it '借手モデルにライセンスエクスポートを問い合わせている' do
+ Demander.any_instance.should_receive(:licenses_export).with(Time.parse('2011/10/10')).exactly(1)
+ get :licenses_export, :date => '20111010', :format => :json
+ end
+ it '@licensesにリストを取得している' do
+ get :licenses_export, :format => :json
+ assigns(:licenses).should have_at_least(3).items
+ end
+ context 'html形式' do
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :licenses_export, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ it 'データがリスト構造になっている' do
+ get :licenses_export, :format => :json
+ json = JSON.parse response.body
+ json.should have_at_least(3).items
+ end
+ it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
+ get :licenses_export, :format => :json
+ json = JSON.parse response.body
+ json.first.has_key?("name").should be_true
+ json.first.has_key?("caption").should be_true
+ json.first.has_key?("url").should be_true
+ end
+ end
+ end
+ context '借手権限がないとき' do
+ before do
+ sign_out @demand_user
+ end
+ it 'ステータスコード302 Foundを返す' do
+ get :licenses_export
+ response.status.should eq 302
+ end
+ context 'html形式' do
+ it 'サインインページへ遷移する' do
+ get :licenses_export
+ response.body.should redirect_to '/demand_users/sign_in'
+ end
+ end
+ context 'json形式' do
+ it '応答メッセージにUnauthorizedを返す' do
+ get :licenses_export, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ context '借受権限がないとき' do
+ before do
+ Demander.any_instance.stub(:status).and_return(1)
+ end
+ context 'html形式' do
+ end
+ context 'json形式' do
+ it 'ステータスコード403 forbiddenを返す' do
+ lambda{
+ get :licenses_export, :format => :json
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ end
+ end
end
sign_in @user
License.stub(:list).and_return([@l, @l, @l])
end
- context 'つつがなく終わるとき' do
+ context 'つつがなく終わるとき' do
it 'ステータスコード200 OKを返す' do
get :index
response.should be_success
end
end
end
-
+
+ describe 'ライセンスインポートに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ ProviderStatus.stub(:show).and_return(@ps)
+ sign_in @admin
+ end
+ context '事前チェックしておく' do
+ before do
+ ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+ ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+ ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+ ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+ ProviderLicense.stub(:import).with(any_args).and_return([])
+ end
+ it '借受状況モデルに単体取得を問い合わせている' do
+ ProviderStatus.should_receive(:show).exactly(1)
+ get :licenses_import, :id => @ps.id
+ end
+ it '日付文字列変換を依頼している' do
+ ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
+ get :licenses_import, :id => @ps.id, :date => '20111010'
+ end
+ it 'エクスポートurl取得を依頼している' do
+ ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+ ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, @ps.token, Time.parse('2011/10/10')).exactly(1)
+ get :licenses_import, :id => @ps.id, :date => '20111010'
+ end
+ it '貸手からのエクスポートを依頼している' do
+ ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
+ get :licenses_import, :id => @ps.id
+ end
+ it 'ライセンス対照表モデルにインポートを依頼している' do
+ ProviderLicense.should_receive(:import).exactly(1)
+ get :licenses_import, :id => @ps.id
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
+ ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
+ ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
+ ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps)
+ ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+ ProviderLicense.stub(:import).with(any_args).and_return([])
+ end
+ it 'ステータスコード200 OKを返す' do
+ get :licenses_import, :id => @ps.id
+ response.should be_success
+ end
+ it '@provider_statusに借受状況を取得している' do
+ get :licenses_import, :id => @ps.id
+ assigns(:provider_status).should eq(@ps)
+ end
+ it '@failuresにインポート失敗リストを取得している' do
+ get :licenses_import, :id => @ps.id
+ assigns(:failures).should eq([])
+ end
+ context 'html形式' do
+ it 'licenses_importテンプレートを描画する' do
+ get :licenses_import, :id => @ps.id
+ response.should render_template("licenses_import")
+ end
+ end
+ context 'json形式' do
+ it 'jsonデータを返す' do
+ get :licenses_import, :id => @ps.id, :format => :json
+ lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
+ end
+ end
+ end
+ context '管理者権限がないとき' do
+ before do
+ sign_out @admin
+ end
+ context 'html形式' do
+ it 'ステータスコード302 Foundを返す' do
+ get :licenses_import, :id => @ps.id
+ response.status.should eq 302
+ end
+ it 'サインインページへ遷移する' do
+ get :licenses_import, :id => @ps.id
+ response.body.should redirect_to '/admins/sign_in'
+ end
+ end
+ context 'json形式' do
+ it 'ステータスコード401 Unauthorizedを返す' do
+ get :licenses_import, :id => @ps.id, :format => :json
+ response.status.should eq 401
+ end
+ it '応答メッセージにUnauthorizedを返す' do
+ get :licenses_import, :id => @ps.id, :format => :json
+ response.message.should match(/Unauthorized/)
+ end
+ end
+ end
+ context '借受権限がないとき' do
+ before do
+ ProviderStatus.any_instance.stub(:status).and_return(0)
+ end
+ context 'html形式' do
+ it '例外403 forbiddenを返す' do
+ lambda{
+ get :licenses_import, :id => @ps.id
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context 'json形式' do
+ it '例外403 forbiddenを返す' do
+ lambda{
+ get :licenses_import, :id => @ps.id, :format => :json
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ end
+ end
+
end
demander_status.receive_hour1 nil
demander_status.receive_hour2 nil
end
+
+ factory :provider_license, :class => ProviderLicense do |provider_license|
+ provider_license.provider_id nil
+ provider_license.providers_license_id nil
+ provider_license.demanders_license_id nil
+ end
end
end
end
end
+
+ describe 'ライセンスエクスポートに於いて' do
+ before do
+ @ds = FactoryGirl.create :demander_status, :requested_at => Time.now, :approved_at => Time.now, :rejected_at => Time.now, :receive_hour1 => 0, :receive_hour2 => 0
+ @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id
+ end
+ context '事前チェックする' do
+ before do
+ Demander.any_instance.stub(:status).and_return(3)
+ end
+ it 'ライセンスモデルにエクスポートを依頼している' do
+ License.stub(:export).with(nil).and_return([@license])
+ License.should_receive(:export).with(nil).exactly(1)
+ r = @demander.licenses_export
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Demander.any_instance.stub(:status).and_return(3)
+ end
+ it 'ライセンスリストを返す' do
+ License.stub(:export).with(Time.parse('2011/10/10')).and_return([@license])
+ r = @demander.licenses_export Time.parse('2011/10/10')
+ r.should eq [@license]
+ end
+ end
+ context '日時指定しないとき' do
+ before do
+ Demander.any_instance.stub(:status).and_return(3)
+ end
+ it 'パラメータなしでリストを取得する' do
+ License.stub(:export).with(nil).and_return([@license])
+ License.should_receive(:export).with(nil).exactly(1)
+ r = @demander.licenses_export nil
+ end
+ end
+ #例外ケース
+ context '状態が貸与中でないとき' do
+ it '例外403 ActiveRecord::Forbiddenを発生させる' do
+ Demander.any_instance.stub(:status).and_return(2)
+ lambda{
+ r = @demander.licenses_export
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ end
+
end
@l.license_group_id = ''
@l.should_not be_valid
end
- it '数値でなければ失敗する' do\r
- @l.license_group_id = 'a'\r
- @l.should_not be_valid\r
- end\r
- it '存在するライセンスグループでなければ失敗する' do\r
- @l.license_group_id = 0\r
- @l.should_not be_valid\r
- end\r
+ it '数値でなければ失敗する' do
+ @l.license_group_id = 'a'
+ @l.should_not be_valid
+ end
+ it '存在するライセンスグループでなければ失敗する' do
+ @l.license_group_id = 0
+ @l.should_not be_valid
+ end
end
context 'nameを検証するとき' do
it 'nullなら失敗する' do
@l.should_not be_valid
end
it 'url形式でないなら失敗する' do
- @l.url = 'a'*200\r
- @l.should_not be_valid\r
+ @l.url = 'a'*200
+ @l.should_not be_valid
end
end
context 'system_picture_idを検証するとき' do
@l.system_picture_id = ''
@l.should_not be_valid
end
- it '数値でなければ失敗する' do\r
- @l.system_picture_id = 'a'\r
- @l.should_not be_valid\r
- end\r
- it '存在するシステム画像でなければ失敗する' do\r
- @l.system_picture_id = 0\r
- @l.should_not be_valid\r
- end\r
+ it '数値でなければ失敗する' do
+ @l.system_picture_id = 'a'
+ @l.should_not be_valid
+ end
+ it '存在するシステム画像でなければ失敗する' do
+ @l.system_picture_id = 0
+ @l.should_not be_valid
+ end
end
end
@l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
@lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
end
- context 'つつがなく終わるとき' do\r
- it '一覧取得オプションを利用している' do\r
- License.stub(:list_opt).with(any_args).and_return({})\r
- License.should_receive(:list_opt).with(any_args).exactly(1)\r
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ License.stub(:list_opt).with(any_args).and_return({})
+ License.should_receive(:list_opt).with(any_args).exactly(1)
r = License.list
- end\r
- end\r
+ end
+ end
it 'リストを返す' do
l = License.list
l.should eq [@l]
@lg = FactoryGirl.create :license_group
@l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
end
- context 'つつがなく終わるとき' do\r
- it '単体取得オプションを利用している' do\r
- License.stub(:show_opt).with(any_args).and_return({})\r
- License.should_receive(:show_opt).with(any_args).exactly(1)\r
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ License.stub(:show_opt).with(any_args).and_return({})
+ License.should_receive(:show_opt).with(any_args).exactly(1)
r = License.show @l.id
- end\r
- end\r
+ end
+ end
it '指定のライセンスを返す' do
l = License.show @l.id
l.should eq @l
end
- context '存在しないライセンスを開こうとしたとき' do\r
- it '404RecordNotFound例外を返す' do\r
- lambda{\r
- License.show 110\r
- }.should raise_error(ActiveRecord::RecordNotFound)\r
- end\r
- end\r
+ context '存在しないライセンスを開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ License.show 110
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
end
describe '単体取得オプションに於いて' do
it 'includeキーを含んでいる' do
end
end
+ describe 'エクスポートに於いて' do
+ before do
+ @sp = FactoryGirl.create :system_picture
+ @lg = FactoryGirl.create :license_group
+ @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+ @l2 = FactoryGirl.create :license, :name => 'l2[old ls]', :url => @l.url+'2', :license_group_id => @lg.id, :system_picture_id => @sp.id, :updated_at => Time.now - 3000
+ end
+ it '開始日時が省略された場合はすべてのライセンスを返す' do
+ r = License.export
+ r.should eq [@l, @l2]
+ end
+ it '開始日時以降に更新されたライセンスを返す' do
+ r = License.export @l.updated_at - 100
+ r.should eq [@l]
+ end
+ end
+
end
--- /dev/null
+# -*- encoding: utf-8 -*-
+#ライセンス対照表
+require 'spec_helper'
+
+describe ProviderLicense 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 #ユーザ作成時に連動して作成される
+ @provider_status = FactoryGirl.create :provider_status
+ @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id
+ end
+ describe '検証に於いて' do
+ before do
+ @pl = FactoryGirl.build :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
+ end
+
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @pl.should be_valid
+ end
+ it '上限データが通る' do
+ @pl.should be_valid
+ end
+ end
+
+ context 'provider_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.provider_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.provider_id = 'a'
+ @pl.should_not be_valid
+ end
+ it '存在する貸手ユーザでなければ失敗する' do
+ @pl.provider_id = 0
+ @pl.should_not be_valid
+ end
+ end
+ context 'providers_license_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.providers_license_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.providers_license_id = 'a'
+ @pl.should_not be_valid
+ end
+ end
+ context 'demanders_license_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @pl.demanders_license_id = nil
+ @pl.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @pl.demanders_license_id = 'a'
+ @pl.should_not be_valid
+ end
+ end
+ end
+
+ describe '対照表取得に於いて' do
+ before do
+ @pl = FactoryGirl.create :provider_license, :provider_id => @provider.id, :providers_license_id => 2, :demanders_license_id => @license.id
+ end
+ it '取得できれば真を返す' do
+ r = ProviderLicense.exist_license @provider.id, 2
+ r.should be_true
+ end
+ it '取得できなければ偽を返す' do
+ r = ProviderLicense.exist_license @provider.id, 1
+ r.should be_false
+ r = ProviderLicense.exist_license 0, 2
+ r.should be_false
+ end
+ end
+
+ describe 'インポートに於いて' do
+ before do
+ @license2 = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id, :name => 'license2', :url => 'http://licen.se/2'
+ end
+ context '事前チェックする' do
+ it '対照表取得を問い合わせている' do
+ ProviderLicense.stub(:exist_license).with(any_args).and_return(true)
+ ProviderLicense.should_receive(:exist_license).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ it 'ライセンスモデルに管理名取得を依頼している' do
+ License.stub(:find_by_name).with(any_args).and_return(@license)
+ License.should_receive(:find_by_name).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ it '対照表オブジェクトを保存している' do
+ ProviderLicense.any_instance.stub(:save).with(any_args).and_return(true)
+ ProviderLicense.any_instance.should_receive(:save).with(any_args).exactly(1)
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ end
+ end
+ context 'つつがなく終わるとき' do
+ it '空っぽの配列を返す' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should be_empty
+ end
+ it '対照表が追加される' do
+ lambda {
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ }.should change ProviderLicense, :count
+ end
+ end
+ context '複数インポートのとき' do
+ it '空っぽの配列を返す' do
+ r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
+ r.should be_empty
+ end
+ it '対照表が追加される' do
+ lambda {
+ r = ProviderLicense.import @provider.id, [@license.attributes, @license2.attributes]
+ }.should change(ProviderLicense, :count).by(2)
+ end
+ end
+ #警告ケース
+ context '対照表オブジェクトの保存に失敗したとき' do
+ before do
+ ProviderLicense.any_instance.stub(:save).with(any_args).and_return(false)
+ end
+ it '結果に貸手側ライセンスのカラム値を追加している' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should_not be_empty
+ end
+ end
+ context 'ライセンスの管理名取得に失敗したとき' do
+ before do
+ License.stub(:find_by_name).with(any_args).and_return(nil)
+ end
+ it '結果に貸手側ライセンスのカラム値を追加している' do
+ r = ProviderLicense.import @provider.id, [@license.attributes]
+ r.should_not be_empty
+ end
+ end
+ end
+end
+