From 9ffd93ec6449ed003acbe8022902d9fe9f41c2c9 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Sun, 27 May 2012 10:13:38 +0900 Subject: [PATCH] update license test --- app/controllers/common_licenses_controller.rb | 2 + app/controllers/original_licenses_controller.rb | 30 ++++- app/models/common_license.rb | 8 ++ app/models/license.rb | 3 + app/views/common_licenses/result.html.erb | 8 ++ app/views/original_licenses/index.html.erb | 4 +- app/views/original_licenses/list.html.erb | 6 +- app/views/original_licenses/show.html.erb | 4 - config/routes.rb | 1 + db/original_licenses.json | 12 ++ .../original_licenses_controller_spec.rb | 127 +++++++++++++++++++++ spec/models/common_license_spec.rb | 25 ++++ spec/models/license_spec.rb | 11 ++ 13 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 db/original_licenses.json diff --git a/app/controllers/common_licenses_controller.rb b/app/controllers/common_licenses_controller.rb index 674e1612..08c7dd06 100644 --- a/app/controllers/common_licenses_controller.rb +++ b/app/controllers/common_licenses_controller.rb @@ -1,3 +1,4 @@ +#コモンライセンス class CommonLicensesController < ApplicationController layout 'test' if Pettanr::TestLayout before_filter :authenticate_user!, :only => [:index, :show] @@ -67,6 +68,7 @@ class CommonLicensesController < ApplicationController format.html { redirect_to :action => :list } format.json { render text: 'ok', status: :created } else + #@errorsがNILならJson展開できない全体異常 format.html { render action: "result" } format.json { render json: @errors, status: :unprocessable_entity } end diff --git a/app/controllers/original_licenses_controller.rb b/app/controllers/original_licenses_controller.rb index a6bf378c..41a320e2 100644 --- a/app/controllers/original_licenses_controller.rb +++ b/app/controllers/original_licenses_controller.rb @@ -1,6 +1,19 @@ class OriginalLicensesController < ApplicationController - before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy] + layout 'test' if Pettanr::TestLayout + before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy, :import] + private + + def set_data(file) + if file.respond_to?(:read) + file.read + else + file + end + end + + public + # GET /original_licenses # GET /original_licenses.json def index @@ -109,4 +122,19 @@ class OriginalLicensesController < ApplicationController format.json { head :ok } end end + + def import + @data = set_data params[:file] + respond_to do |format| + @errors = OriginalLicense.import @data + if @errors and @errors.empty? + format.html { redirect_to :action => :list } + format.json { render text: 'ok', status: :created } + else + format.html { render action: "result" } + format.json { render json: @errors, status: :unprocessable_entity } + end + end + end + end diff --git a/app/models/common_license.rb b/app/models/common_license.rb index 906fbfd3..c525c4a6 100644 --- a/app/models/common_license.rb +++ b/app/models/common_license.rb @@ -130,4 +130,12 @@ class CommonLicense < ActiveRecord::Base CommonLicense.import t end + def import_error_message(c = '\n') + self.errors.each do |atr, messages| + if atr == :base + end + full_messages.join c + end + end + end diff --git a/app/models/license.rb b/app/models/license.rb index fd286d1f..11252f83 100644 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -11,6 +11,9 @@ class License < ActiveRecord::Base :name => cl.name, :url => cl.url, :cc_by => cl.cc_by, :cc_sa => cl.cc_sa, :cc_nd => cl.cc_nd, :cc_nc => cl.cc_nc, :no_resize => cl.no_resize, :no_flip => cl.no_flip, :no_convert => cl.no_convert, :keep_aspect_ratio => cl.keep_aspect_ratio } + if cl.new_record? and l.new_record? == false + l.errors.add :base, 'dupulicate url' + end l end diff --git a/app/views/common_licenses/result.html.erb b/app/views/common_licenses/result.html.erb index 4e27dac2..f686e1c3 100644 --- a/app/views/common_licenses/result.html.erb +++ b/app/views/common_licenses/result.html.erb @@ -1,2 +1,10 @@ + + + + + <% @errors.each do |common_license| %> + + <% end %> +
namemessage
<%= h common_license.name %><%= h common_license.import_error_message('
') %>
diff --git a/app/views/original_licenses/index.html.erb b/app/views/original_licenses/index.html.erb index 4ebdbc7f..ce66269d 100644 --- a/app/views/original_licenses/index.html.erb +++ b/app/views/original_licenses/index.html.erb @@ -2,14 +2,14 @@ - + <% @original_licenses.each do |original_license| %> - + diff --git a/app/views/original_licenses/list.html.erb b/app/views/original_licenses/list.html.erb index 4e5f8767..f0de2541 100644 --- a/app/views/original_licenses/list.html.erb +++ b/app/views/original_licenses/list.html.erb @@ -1,9 +1,12 @@

Listing original_licenses

+<%= form_tag( {:controller => 'original_licenses',:action => "import"} , { :multipart => true }) do %> + <%= file_field_tag "file" %> + <%= submit_tag 'upload' -%> +<% end -%>
name
<%= link_to 'Show', original_license %><%= link_to h(original_license.name), original_license %> <%= link_to 'Edit', edit_original_license_path(original_license) %> <%= link_to 'Destroy', original_license, confirm: 'Are you sure?', method: :delete %>
- @@ -24,7 +27,6 @@ <% @original_licenses.each do |original_license| %> - diff --git a/app/views/original_licenses/show.html.erb b/app/views/original_licenses/show.html.erb index 2f6b3050..4401b257 100644 --- a/app/views/original_licenses/show.html.erb +++ b/app/views/original_licenses/show.html.erb @@ -11,10 +11,6 @@

- artist: - <%= @original_license.artist_id %> -

-

cc_by: <%= @original_license.cc_by %>

diff --git a/config/routes.rb b/config/routes.rb index 298471fe..a86668e6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -166,6 +166,7 @@ Pettanr::Application.routes.draw do get :show get :new post :create + post :import get :list get :browse end diff --git a/db/original_licenses.json b/db/original_licenses.json new file mode 100644 index 00000000..4a5ce396 --- /dev/null +++ b/db/original_licenses.json @@ -0,0 +1,12 @@ +{ + "name": "Local ebi", + "url": "http://eni.doc/", + "cc_by": 0, + "cc_sa": 0, + "cc_nd": 0, + "cc_nc": 0, + "no_resize": 0, + "no_flip": 0, + "no_convert": 0, + "keep_aspect_ratio": 0 +} diff --git a/spec/controllers/original_licenses_controller_spec.rb b/spec/controllers/original_licenses_controller_spec.rb index b6f01ebb..4c5df40c 100644 --- a/spec/controllers/original_licenses_controller_spec.rb +++ b/spec/controllers/original_licenses_controller_spec.rb @@ -456,6 +456,133 @@ describe OriginalLicensesController do end describe 'インポートに於いて' do + before do + sign_in @admin + sign_in @user + #テストデータを用意してね + @f = Rails.root + 'spec/json/original_license.json' + @t = File.open(@f, 'r').read + @j = JSON.parse @t + @fs = Rails.root + 'spec/json/original_licenses.json' + @ts = File.open(@fs, 'r').read + @js = JSON.parse @ts + @fes = Rails.root + 'spec/json/invalid_original_licenses.json' + @tes = File.open(@fes, 'r').read + @jes = JSON.parse @tes + end + context '事前チェックしておく' do + before do + #異常な行を返すから、正常の意味で空を返す + OriginalLicense.stub(:import).with(any_args()).and_return([]) + end + it "@dataに渡したデータを保持している" do + post :import, :file => @t + assigns(:data).should_not be_nil + end + it 'モデルにインポート依頼する' do + OriginalLicense.should_receive(:import).with(any_args()).exactly(1) + post :import, :file => @t + end + it "@errorsに結果を保持している" do + post :import, :file => @t + assigns(:errors).should eq [] + end + end + context 'つつがなく終わるとき' do + before do + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者向けオリジナルライセンス一覧ページへ遷移する' do + post :import, :file => @t + response.should redirect_to('/original_licenses/list') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t, :format => :json + response.should be_success + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者サインインページへ遷移する' do + post :import, :file => @t + response.body.should redirect_to '/admins/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + post :import, :file => @t, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + post :import, :file => @t, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '作家権限がないとき' do + #必要なのは管理者権限であって作家権限ではない。成功を見届ける + before do + sign_out @user + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + post :import, :file => @t + response.status.should eq 302 + end + it '管理者向けオリジナルライセンス一覧ページへ遷移する' do + post :import, :file => @t + response.should redirect_to('/original_licenses/list') + end + end + context 'json形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t, :format => :json + response.should be_success + end + end + end + context '検証、保存に失敗した' do + before do + #異常な行を返す + OriginalLicense.stub(:import).with(any_args()).and_return( + [OriginalLicense.new(Factory.attributes_for(:original_license))] + ) + end + context 'html形式' do + it 'ステータスコード200 OKを返す' do + post :import, :file => @t + response.status.should eq 200 + end + it 'resultページを描画する' do + post :import, :file => @t + response.should render_template("result") + end + end + context 'json形式' do + it 'ステータスコード422 unprocessable_entity を返す' do + post :import, :file => @t, :format => :json + response.status.should eq 422 + end + it '応答メッセージUnprocessable Entityを返す' do + post :import, :file => @t, :format => :json + response.message.should match(/Unprocessable/) + end + end + end end end diff --git a/spec/models/common_license_spec.rb b/spec/models/common_license_spec.rb index de13ece0..389fd699 100644 --- a/spec/models/common_license_spec.rb +++ b/spec/models/common_license_spec.rb @@ -510,6 +510,31 @@ describe CommonLicense do end end + describe 'インポートエラーの表示に於いて' do + before do + @l = Factory :license + @cl = Factory.build :common_license, :license_id => @l.id + end + it '全体エラーだけなら、そのまま返す' do + @cl.errors.add :base, 'base error' + @cl.import_error_message.should eq 'base error' + end + context '複数でエラーのとき' do + it '各エラーを改行で区切って結合して返す' do + @cl.errors.add :name, 'name error' + @cl.errors.add :url, 'url error' + @cl.import_error_message.should eq 'name error\nurl error' + end + end + context '区切り指定が
で複数でエラーのとき' do + it '各エラーを改行で区切って結合して返す' do + @cl.errors.add :name, 'name error' + @cl.errors.add :url, 'url error' + @cl.import_error_message('
').should eq 'name error
url error' + end + end + end + describe 'ファイル取り込みに於いて' do before do CommonLicense.stub(:import).with(any_args).and_return(true) diff --git a/spec/models/license_spec.rb b/spec/models/license_spec.rb index ea5a8d9d..39fda16b 100644 --- a/spec/models/license_spec.rb +++ b/spec/models/license_spec.rb @@ -64,6 +64,17 @@ describe License do r.name.should eq @lc.name end end + #コモンライセンスとオリジナルライセンスをまたいだUrl重複チェックはここでやるよりない + #コモンライセンスが新規オブジェクトなのにライセンスが取得できるのは、 + #そのUrlがオリジナルライセンスから登録されているということ + context '新規でユニークチェックするとき' do + it 'ライセンスの全体エラーに重複メッセージを入れて返す' do + cl = Factory.build(:common_license, :url => 'http://domain.no') + License.stub(:find_by_url).with(any_args).and_return(@lc) + r = License.update_license cl + r.errors[:base].should_not be_empty + end + end end #作成が -- 2.11.0
idartist_id license_id name url
<%= link_to original_license.id, :action => :browse, :id => original_license.id %><%= link_to original_license.artist_id, :controller => 'artists', :action => :browse, :id => original_license.artist_id %> <%= h original_license.name %> <%= h original_license.url %> <%= original_license.cc_by %>