OSDN Git Service

t#30322:create provider license import func
authoryasushiito <yas@pen-chan.jp>
Sun, 16 Dec 2012 00:55:47 +0000 (09:55 +0900)
committeryasushiito <yas@pen-chan.jp>
Sun, 16 Dec 2012 00:55:47 +0000 (09:55 +0900)
21 files changed:
Gemfile
app/controllers/application_controller.rb
app/controllers/demanders_controller.rb
app/controllers/provider_statuses_controller.rb
app/models/demander.rb
app/models/license.rb
app/models/provider_license.rb [new file with mode: 0644]
app/views/demanders/index.html.erb
app/views/provider_statuses/licenses_import.html.erb [new file with mode: 0644]
app/views/provider_statuses/show.html.erb
config/environment.rb
config/locales/pettanr.ja.yml
config/routes.rb
db/migrate/20121215010935_create_provider_licenses.rb [new file with mode: 0644]
spec/controllers/demanders_controller_spec.rb
spec/controllers/licenses_controller_spec.rb
spec/controllers/provider_statuses_controller_spec.rb
spec/factories.rb
spec/models/demander_spec.rb
spec/models/license_spec.rb
spec/models/provider_license_spec.rb [new file with mode: 0644]

diff --git a/Gemfile b/Gemfile
index da2ec2c..160f94e 100644 (file)
--- a/Gemfile
+++ b/Gemfile
@@ -2,6 +2,7 @@ source 'http://rubygems.org'
 
 gem 'rails', '3.1.1'
 gem 'devise', '=1.5.2'
+gem 'rest-client'
 gem 'aws-s3'
 gem 'validates_existence'
 gem "validate_url"
index bd48f0f..d802fbf 100644 (file)
@@ -87,6 +87,29 @@ class ApplicationController < ActionController::Base
     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
 
index a8c08d8..9549ded 100644 (file)
@@ -1,6 +1,6 @@
 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
@@ -96,4 +96,14 @@ class DemandersController < ApplicationController
     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
index 95a60ef..866fc14 100644 (file)
@@ -1,6 +1,6 @@
 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]
@@ -58,4 +58,17 @@ class ProviderStatusesController < ApplicationController
       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
index ef0078c..7a882e0 100644 (file)
@@ -118,4 +118,9 @@ class Demander < ActiveRecord::Base
     self.demander_status.stop
   end
   
+  def licenses_export date_str = nil
+    raise ActiveRecord::Forbidden unless self.status == 3
+    License.export date_str
+  end
+  
 end
index bd344c9..388eb0c 100644 (file)
@@ -79,4 +79,11 @@ class License < ActiveRecord::Base
     @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
diff --git a/app/models/provider_license.rb b/app/models/provider_license.rb
new file mode 100644 (file)
index 0000000..7c8efed
--- /dev/null
@@ -0,0 +1,33 @@
+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
index 763fce2..7c01318 100644 (file)
 </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>
diff --git a/app/views/provider_statuses/licenses_import.html.erb b/app/views/provider_statuses/licenses_import.html.erb
new file mode 100644 (file)
index 0000000..eb414cc
--- /dev/null
@@ -0,0 +1,29 @@
+<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 %>
index 4edf450..8d772e9 100644 (file)
 </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 %>
 
index 89544d5..a175c5d 100644 (file)
@@ -1,6 +1,7 @@
 # Load the rails application
 require File.expand_path('../application', __FILE__)
 require 'RMagick'
+require 'rest_client'
 
 # Initialize the rails application
 Pettanr::Application.initialize!
index 5747107..9ab7888 100644 (file)
@@ -257,6 +257,9 @@ ja:
         created_at: 作成
         updated_at: 更新
       provider_license:
+        provider_id: 貸手
+        providers_license_id: 貸手側ライセンス
+        demanders_license_id: 借手側ライセンス
         created_at: 作成
         updated_at: 更新
       provider_artist:
@@ -622,10 +625,16 @@ ja:
       receive_announce: 借受するには借手となって借受申請する。
       receive_start: この貸手からの借受を開始する
       receive_stop: 借受を停止する
+      licenses_import: ライセンスをインポートする
     new:
       title: 借受状況登録
     edit:
       title: 借受状況編集
+    licenses_import:
+      title: ライセンスインポート
+      success: インポートが完了しました
+      fail: 次のライセンス(%{size}件)でエラーがありました。ライセンスのインストール状況を確認してください。
+      to_show: 借受状況を表示する
     status: 状態
     open: 開く
     to_provider: 貸手に切り替える
index 0bbd8ad..f0b5e89 100644 (file)
@@ -298,6 +298,7 @@ Pettanr::Application.routes.draw do
       get :edit
       put :update
       delete :destroy
+      post :licenses_import
     end
   end
   resources :demanders do
@@ -311,6 +312,7 @@ Pettanr::Application.routes.draw do
       get :list
       get :browse
       post :req
+      get :licenses_export
     end
     member do
       get :edit
diff --git a/db/migrate/20121215010935_create_provider_licenses.rb b/db/migrate/20121215010935_create_provider_licenses.rb
new file mode 100644 (file)
index 0000000..52c1d86
--- /dev/null
@@ -0,0 +1,11 @@
+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
index c5f513f..ed7b6f0 100644 (file)
@@ -550,7 +550,7 @@ describe DemandersController do
         end
       end
     end
-    context '管理者権限がないとき' do
+    context '借手権限がないとき' do
       before do
         sign_out @demand_user
       end
@@ -601,5 +601,88 @@ describe DemandersController do
       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
index 4888255..8085a25 100644 (file)
@@ -18,7 +18,7 @@ describe LicensesController do
       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 
index e70216a..ed08f0e 100644 (file)
@@ -357,5 +357,122 @@ describe ProviderStatusesController do
       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
index 5c940d9..2fe602a 100644 (file)
@@ -243,4 +243,10 @@ FactoryGirl.define do
     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
index da6c9ce..525e717 100644 (file)
@@ -497,4 +497,51 @@ describe Demander do
       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
index a81390f..e0dafd4 100644 (file)
@@ -32,14 +32,14 @@ describe License do
         @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
@@ -75,8 +75,8 @@ describe License 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
@@ -84,14 +84,14 @@ describe License 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
   
@@ -124,13 +124,13 @@ describe License do
       @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]
@@ -176,24 +176,24 @@ describe License do
       @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
@@ -341,4 +341,21 @@ describe License 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
diff --git a/spec/models/provider_license_spec.rb b/spec/models/provider_license_spec.rb
new file mode 100644 (file)
index 0000000..50501fc
--- /dev/null
@@ -0,0 +1,146 @@
+# -*- 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
+