From ebc737a8fca36b965e53899182f4ab7ccbd74b0d Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sun, 16 Dec 2012 09:55:47 +0900 Subject: [PATCH] t#30322:create provider license import func --- Gemfile | 1 + app/controllers/application_controller.rb | 23 ++++ app/controllers/demanders_controller.rb | 12 +- app/controllers/provider_statuses_controller.rb | 15 ++- app/models/demander.rb | 5 + app/models/license.rb | 7 + app/models/provider_license.rb | 33 +++++ app/views/demanders/index.html.erb | 4 + .../provider_statuses/licenses_import.html.erb | 29 ++++ app/views/provider_statuses/show.html.erb | 23 ++-- config/environment.rb | 1 + config/locales/pettanr.ja.yml | 9 ++ config/routes.rb | 2 + .../20121215010935_create_provider_licenses.rb | 11 ++ spec/controllers/demanders_controller_spec.rb | 87 +++++++++++- spec/controllers/licenses_controller_spec.rb | 2 +- .../provider_statuses_controller_spec.rb | 119 ++++++++++++++++- spec/factories.rb | 6 + spec/models/demander_spec.rb | 47 +++++++ spec/models/license_spec.rb | 91 +++++++------ spec/models/provider_license_spec.rb | 146 +++++++++++++++++++++ 21 files changed, 620 insertions(+), 53 deletions(-) create mode 100644 app/models/provider_license.rb create mode 100644 app/views/provider_statuses/licenses_import.html.erb create mode 100644 db/migrate/20121215010935_create_provider_licenses.rb create mode 100644 spec/models/provider_license_spec.rb diff --git a/Gemfile b/Gemfile index da2ec2c4..160f94e5 100644 --- 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" diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index bd48f0f8..d802fbf8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/demanders_controller.rb b/app/controllers/demanders_controller.rb index a8c08d82..9549dedb 100644 --- a/app/controllers/demanders_controller.rb +++ b/app/controllers/demanders_controller.rb @@ -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 diff --git a/app/controllers/provider_statuses_controller.rb b/app/controllers/provider_statuses_controller.rb index 95a60ef1..866fc146 100644 --- a/app/controllers/provider_statuses_controller.rb +++ b/app/controllers/provider_statuses_controller.rb @@ -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 diff --git a/app/models/demander.rb b/app/models/demander.rb index ef0078c5..7a882e08 100644 --- a/app/models/demander.rb +++ b/app/models/demander.rb @@ -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 diff --git a/app/models/license.rb b/app/models/license.rb index bd344c9b..388eb0cf 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -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 index 00000000..7c8efedb --- /dev/null +++ b/app/models/provider_license.rb @@ -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 diff --git a/app/views/demanders/index.html.erb b/app/views/demanders/index.html.erb index 763fce29..7c013189 100644 --- a/app/views/demanders/index.html.erb +++ b/app/views/demanders/index.html.erb @@ -15,6 +15,10 @@

<% if @demander.status == 3 %>

+ <%= t_m 'DemandUser.authentication_token' -%> + <%= (@demander.demand_user.authentication_token) if @demander.demand_user.authentication_token %> +

+

<%= t_m 'DemanderStatus.receive_hour1' -%> <%= h(@demander.demander_status.receive_hour1) %>

diff --git a/app/views/provider_statuses/licenses_import.html.erb b/app/views/provider_statuses/licenses_import.html.erb new file mode 100644 index 00000000..eb414cc3 --- /dev/null +++ b/app/views/provider_statuses/licenses_import.html.erb @@ -0,0 +1,29 @@ +

<%= t('.title') %>

+

<%= notice %>

+ +<% if @failures.empty? %> +

+ <%= t('provider_statuses.licenses_import.success') %> +

+<% else %> +

+ <%= t('provider_statuses.licenses_import.fail', :size => @failures.size) %> +

+ + + + + + <% @failures.each do |license| %> + + + + + <% end %> +
<%= t_m 'License.caption' -%><%= t_m 'License.name' -%>
+ <%= link_to h(license['caption']), license['url'] %> + + <%= h(license['name']) %> +
+ <%= link_to t('provider_statuses.licenses_import.to_show'), provider_status_path(@provider_status) %> +<% end %> diff --git a/app/views/provider_statuses/show.html.erb b/app/views/provider_statuses/show.html.erb index 4edf450f..8d772e9a 100644 --- a/app/views/provider_statuses/show.html.erb +++ b/app/views/provider_statuses/show.html.erb @@ -25,17 +25,20 @@

<%= link_to t('provider_statuses.show.demander_open'), @provider_status.provider.demander_url %> -

- -

- <%= t('provider_statuses.show.receive_announce') %>

-

- <% 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 %> +

+ <%= t('provider_statuses.show.receive_announce') %> +

+ <%= link_to t('provider_statuses.show.receive_start'), edit_provider_status_path(@provider_status) %> +<% else %> +

+ <%= link_to t('provider_statuses.show.licenses_import'), licenses_import_provider_status_path(@provider_status), :method => :post %> +

+

<%= link_to t('provider_statuses.show.receive_stop'), provider_status_path(@provider_status), :method => :delete %> - <% end %> -

+

+<% end %> <%= link_to t('link.back'), provider_statuses_path %> diff --git a/config/environment.rb b/config/environment.rb index 89544d5b..a175c5d7 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -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! diff --git a/config/locales/pettanr.ja.yml b/config/locales/pettanr.ja.yml index 57471073..9ab78885 100644 --- a/config/locales/pettanr.ja.yml +++ b/config/locales/pettanr.ja.yml @@ -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: 貸手に切り替える diff --git a/config/routes.rb b/config/routes.rb index 0bbd8ad7..f0b5e89f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 index 00000000..52c1d868 --- /dev/null +++ b/db/migrate/20121215010935_create_provider_licenses.rb @@ -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 diff --git a/spec/controllers/demanders_controller_spec.rb b/spec/controllers/demanders_controller_spec.rb index c5f513f7..ed7b6f01 100644 --- a/spec/controllers/demanders_controller_spec.rb +++ b/spec/controllers/demanders_controller_spec.rb @@ -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 diff --git a/spec/controllers/licenses_controller_spec.rb b/spec/controllers/licenses_controller_spec.rb index 4888255d..8085a255 100644 --- a/spec/controllers/licenses_controller_spec.rb +++ b/spec/controllers/licenses_controller_spec.rb @@ -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 diff --git a/spec/controllers/provider_statuses_controller_spec.rb b/spec/controllers/provider_statuses_controller_spec.rb index e70216a8..ed08f0ef 100644 --- a/spec/controllers/provider_statuses_controller_spec.rb +++ b/spec/controllers/provider_statuses_controller_spec.rb @@ -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 diff --git a/spec/factories.rb b/spec/factories.rb index 5c940d96..2fe602a5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -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 diff --git a/spec/models/demander_spec.rb b/spec/models/demander_spec.rb index da6c9cea..525e7175 100644 --- a/spec/models/demander_spec.rb +++ b/spec/models/demander_spec.rb @@ -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 diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index a81390fb..e0dafd4e 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -32,14 +32,14 @@ describe License do @l.license_group_id = '' @l.should_not be_valid end - 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 + 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 - @l.should_not be_valid + @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 - @l.system_picture_id = 'a' - @l.should_not be_valid - end - it '存在するシステム画像でなければ失敗する' do - @l.system_picture_id = 0 - @l.should_not be_valid - end + 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 - it '一覧取得オプションを利用している' do - License.stub(:list_opt).with(any_args).and_return({}) - License.should_receive(:list_opt).with(any_args).exactly(1) + 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 - end + 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 - it '単体取得オプションを利用している' do - License.stub(:show_opt).with(any_args).and_return({}) - License.should_receive(:show_opt).with(any_args).exactly(1) + 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 - end + end + end it '指定のライセンスを返す' do l = License.show @l.id l.should eq @l end - context '存在しないライセンスを開こうとしたとき' do - it '404RecordNotFound例外を返す' do - lambda{ - License.show 110 - }.should raise_error(ActiveRecord::RecordNotFound) - end - end + 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 index 00000000..50501fcf --- /dev/null +++ b/spec/models/provider_license_spec.rb @@ -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 + -- 2.11.0