1 # -*- encoding: utf-8 -*-
5 describe OriginalLicensesController do
7 @admin = Factory :admin
9 @user = Factory( :user_yas)
10 @author = @user.author
11 @artist = Factory :artist_yas, :author_id => @author.id
13 describe '一覧表示に於いて' do
16 @ol = Factory :original_license, :license_id => @lc.id
17 OriginalLicense.stub(:list).and_return([@ol, @ol, @ol])
19 context 'パラメータpageについて' do
21 get :index, :page => 5
22 assigns(:page).should eq 5
24 it '省略されると@pageに1値が入る' do
26 assigns(:page).should eq 1
28 it '与えられたpage_sizeがセットされている' do
29 get :index, :page_size => 15
30 assigns(:page_size).should eq 15
32 it '省略されると@page_sizeにデフォルト値が入る' do
34 assigns(:page_size).should eq OriginalLicense.default_page_size
36 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
37 get :index, :page_size => 1500
38 assigns(:page_size).should eq OriginalLicense.max_page_size
40 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
41 get :index, :page_size => 0
42 assigns(:page_size).should eq OriginalLicense.default_page_size
45 context 'つつがなく終わるとき' do
46 it 'ステータスコード200 OKを返す' do
48 response.should be_success
50 it 'ライセンスモデルに一覧を問い合わせている' do
51 OriginalLicense.should_receive(:list).exactly(1)
54 it '@original_licensesにリストを取得している' do
56 assigns(:original_licenses).should have_at_least(3).items
59 it 'indexテンプレートを描画する' do
61 response.should render_template("index")
66 get :index, :format => :json
67 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
69 it 'データがリスト構造になっている' do
70 get :index, :format => :json
71 json = JSON.parse response.body
72 json.should have_at_least(3).items
74 it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
75 get :index, :format => :json
76 json = JSON.parse response.body
77 json.first.has_key?("url").should be_true
81 context '作家権限がないとき' do
85 it 'ステータスコード200 okを返す' do
87 response.status.should eq 200
92 describe '単体表示に於いて' do
95 @ol = Factory :original_license, :license_id => @lc.id
96 OriginalLicense.stub(:show).and_return(@ol)
98 context 'つつがなく終わるとき' do
99 it 'ステータスコード200 OKを返す' do
100 get :show, :id => @ol.id
101 response.should be_success
103 it 'ライセンスモデルに単体取得を問い合わせている' do
104 OriginalLicense.should_receive(:show).exactly(1)
107 it '@original_licenseにアレを取得している' do
108 get :show, :id => @ol.id
109 assigns(:original_license).id.should eq(@ol.id)
112 it 'showテンプレートを描画する' do
113 get :show, :id => @ol.id
114 response.should render_template("show")
119 get :show, :id => @ol.id, :format => :json
120 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
123 get :show, :id => @ol.id, :format => :json
124 json = JSON.parse response.body
125 json["name"].should match(/peta/)
129 context '作家権限がないとき' do
133 it 'ステータスコード200 okを返す' do
134 get :show, :id => @ol.id
135 response.status.should eq 200
140 describe '新規作成フォーム表示に於いて' do
145 context 'つつがなく終わるとき' do
146 it 'ステータスコード200 OKを返す' do
148 response.should be_success
150 it '@original_licenseに新規データを用意している' do
152 assigns(:original_license).should be_a_new(OriginalLicense)
154 it 'オリジナルライセンスモデルにデフォルト値補充を依頼している' do
155 OriginalLicense.any_instance.should_receive(:supply_default).exactly(1)
159 it 'newテンプレートを描画する' do
161 response.should render_template("new")
165 it 'new.jsテンプレートを描画する' do
166 get :new, :format => :js
167 response.should render_template("new")
171 context '管理者権限がないとき' do
176 it 'ステータスコード302 Foundを返す' do
178 response.status.should eq 302
180 it 'サインインページへ遷移する' do
182 response.body.should redirect_to '/admins/sign_in'
186 it 'ステータスコード401 Unauthorizedを返す' do
187 get :new, :format => :js
188 response.status.should eq 401
190 it '応答メッセージにUnauthorizedを返す' do
191 get :new, :format => :js
192 response.message.should match(/Unauthorized/)
198 describe '新規作成に於いて' do
203 context 'つつがなく終わるとき' do
205 OriginalLicense.any_instance.should_receive(:store).exactly(1)
206 post :create, :original_license => Factory.attributes_for(:original_license)
208 it "@original_licenseに作成されたオリジナルライセンスを保持していて、それがDBにある" do
209 post :create, :original_license => Factory.attributes_for(:original_license)
210 assigns(:original_license).should be_a(OriginalLicense)
211 assigns(:original_license).should be_persisted
214 it 'ステータスコード302 Foundを返す' do
215 OriginalLicense.any_instance.stub(:store).and_return(true)
216 post :create, :original_license => Factory.attributes_for(:original_license)
217 response.status.should eq 302
219 it '作成されたオリジナルライセンスの表示ページへ遷移する' do
220 # OriginalLicense.any_instance.stub(:save).and_return(true)
221 post :create, :original_license => Factory.attributes_for(:original_license)
222 response.should redirect_to(OriginalLicense.last)
226 it 'ステータスコード200 OKを返す' do
227 # OriginalLicense.any_instance.stub(:save).and_return(true)
228 post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
229 response.should be_success
231 it '作成されたオリジナルライセンスをjsonデータで返す' do
232 post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
233 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
236 post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
237 json = JSON.parse response.body
238 json["name"].should match(/peta/)
242 context '管理者権限がないとき' do
247 it 'ステータスコード302 Foundを返す' do
248 post :create, :original_license => Factory.attributes_for(:original_license)
249 response.status.should eq 302
251 it 'サインインページへ遷移する' do
252 post :create, :original_license => Factory.attributes_for(:original_license)
253 response.body.should redirect_to '/admins/sign_in'
257 it 'ステータスコード401 Unauthorizedを返す' do
258 post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
259 response.status.should eq 401
261 it '応答メッセージにUnauthorizedを返す' do
262 post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
263 response.message.should match(/Unauthorized/)
267 context '検証、保存に失敗した' do
269 OriginalLicense.any_instance.stub(:save).and_return(false)
271 it "未保存のオリジナルライセンスを保持している" do
272 post :create, :original_license => {}
273 assigns(:original_license).should be_a_new(OriginalLicense)
276 it 'ステータスコード200 OKを返す' do
277 post :create, :original_license => {}
278 response.status.should eq 200
281 post :create, :original_license => {}
282 response.should render_template("new")
286 it 'ステータスコード422 unprocessable_entity を返す' do
287 post :create, :original_license => {}, :format => :json
288 response.status.should eq 422
290 it '応答メッセージUnprocessable Entityを返す' do
291 post :create, :original_license => {}, :format => :json
292 response.message.should match(/Unprocessable/)
298 describe '編集フォーム表示に於いて' do
300 @ol = Factory :original_license, :license_id => @lc.id
303 OriginalLicense.stub(:show).and_return(@ol)
305 context 'つつがなく終わるとき' do
306 it 'ステータスコード200 OKを返す' do
307 get :edit, :id => @ol.id
308 response.should be_success
310 it 'オリジナルライセンスモデルに単体取得を問い合わせている' do
311 OriginalLicense.should_receive(:show).exactly(1)
312 get :edit, :id => @ol.id
314 it '@original_licenseにデータを用意している' do
315 get :edit, :id => @ol.id
316 assigns(:original_license).should eq @ol
319 it 'editテンプレートを描画する' do
320 get :edit, :id => @ol.id
321 response.should render_template("edit")
325 it 'edit.jsテンプレートを描画する' do
326 get :edit, :id => @ol.id, :format => :js
327 response.should render_template("edit")
331 context '管理者権限がないとき' do
336 it 'ステータスコード302 Foundを返す' do
337 get :edit, :id => @ol.id
338 response.status.should eq 302
340 it 'サインインページへ遷移する' do
341 get :edit, :id => @ol.id
342 response.body.should redirect_to '/admins/sign_in'
346 it 'ステータスコード401 Unauthorizedを返す' do
347 get :edit, :id => @ol.id, :format => :js
348 response.status.should eq 401
350 it '応答メッセージにUnauthorizedを返す' do
351 get :edit, :id => @ol.id, :format => :js
352 response.message.should match(/Unauthorized/)
360 @ol = Factory :original_license, :license_id => @lc.id
363 @attr = Factory.attributes_for(:original_license, :name => 'new lc')
365 context '事前チェックしておく' do
366 it 'オリジナルライセンスモデルに単体取得を問い合わせている' do
367 OriginalLicense.stub(:show).with(any_args()).and_return @ol
368 OriginalLicense.should_receive(:show).exactly(1)
369 put :update, :id => @ol.id, :original_license => @attr
372 OriginalLicense.any_instance.should_receive(:store).with(any_args)
373 put :update, :id => @ol.id, :original_license => @attr
375 it '@original_licenseにアレを取得している' do
376 put :update, :id => @ol.id, :original_license => @attr
377 assigns(:original_license).id.should eq(@ol.id)
380 context 'つつがなく終わるとき' do
382 put :update, :id => @ol.id, :original_license => @attr
383 OriginalLicense.find(@ol.id).name.should eq 'new lc'
386 it 'ステータスコード302 Foundを返す' do
387 OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true)
388 put :update, :id => @ol.id, :original_license => @attr
389 response.status.should eq 302
391 it '更新されたオリジナルライセンスの表示ページへ遷移する' do
392 put :update, :id => @ol.id, :original_license => @attr
393 response.should redirect_to(@ol)
397 it 'ステータスコード200 OKを返す' do
398 OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true)
399 put :update, :id => @ol.id, :original_license => @attr, :format => :json
400 response.should be_success
403 OriginalLicense.any_instance.stub(:store).with(any_args()).and_return(true)
404 put :update, :id => @ol.id, :original_license => @attr, :format => :json
405 response.body.should match /./
409 context '管理者権限がないとき' do
413 it 'ステータスコード302 Foundを返す' do
414 put :update, :id => @ol.id, :original_license => @attr
415 response.status.should eq 302
418 it 'サインインページへ遷移する' do
419 put :update, :id => @ol.id, :original_license => @attr
420 response.body.should redirect_to '/admins/sign_in'
424 it '応答メッセージにUnauthorizedを返す' do
425 put :update, :id => @ol.id, :original_license => @attr, :format => :json
426 response.message.should match(/Unauthorized/)
430 context '検証、保存に失敗したとき' do
432 OriginalLicense.any_instance.stub(:store).and_return(false)
435 it 'ステータスコード200 Okを返す' do
436 put :update, :id => @ol.id, :original_license => @attr
437 response.status.should eq 200
440 put :update, :id => @ol.id, :original_license => @attr
441 response.should render_template("edit")
445 it 'ステータスコード422 unprocessable_entity を返す' do
446 OriginalLicense.any_instance.stub(:store).and_return(false)
447 put :update, :id => @ol.id, :original_license => @attr, :format => :json
448 response.status.should eq 422
450 it '応答メッセージUnprocessable Entityを返す' do
451 put :update, :id => @ol.id, :original_license => @attr, :format => :json
452 response.message.should match(/Unprocessable/)
458 describe 'インポートに於いて' do
463 @f = Rails.root + 'spec/json/original_license.json'
464 @t = File.open(@f, 'r').read
466 @fs = Rails.root + 'spec/json/original_licenses.json'
467 @ts = File.open(@fs, 'r').read
469 @fes = Rails.root + 'spec/json/invalid_original_licenses.json'
470 @tes = File.open(@fes, 'r').read
471 @jes = JSON.parse @tes
473 context '事前チェックしておく' do
475 #異常な行を返すから、正常の意味で空を返す
476 OriginalLicense.stub(:import).with(any_args()).and_return([])
478 it "@dataに渡したデータを保持している" do
479 post :import, :file => @t
480 assigns(:data).should_not be_nil
482 it 'モデルにインポート依頼する' do
483 OriginalLicense.should_receive(:import).with(any_args()).exactly(1)
484 post :import, :file => @t
486 it "@errorsに結果を保持している" do
487 post :import, :file => @t
488 assigns(:errors).should eq []
491 context 'つつがなく終わるとき' do
495 it 'ステータスコード302 Foundを返す' do
496 post :import, :file => @t
497 response.status.should eq 302
499 it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
500 post :import, :file => @t
501 response.should redirect_to('/original_licenses/list')
505 it 'ステータスコード200 OKを返す' do
506 post :import, :file => @t, :format => :json
507 response.should be_success
511 context '管理者権限がないとき' do
516 it 'ステータスコード302 Foundを返す' do
517 post :import, :file => @t
518 response.status.should eq 302
520 it '管理者サインインページへ遷移する' do
521 post :import, :file => @t
522 response.body.should redirect_to '/admins/sign_in'
526 it 'ステータスコード401 Unauthorizedを返す' do
527 post :import, :file => @t, :format => :json
528 response.status.should eq 401
530 it '応答メッセージにUnauthorizedを返す' do
531 post :import, :file => @t, :format => :json
532 response.message.should match(/Unauthorized/)
536 context '作家権限がないとき' do
537 #必要なのは管理者権限であって作家権限ではない。成功を見届ける
542 it 'ステータスコード302 Foundを返す' do
543 post :import, :file => @t
544 response.status.should eq 302
546 it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
547 post :import, :file => @t
548 response.should redirect_to('/original_licenses/list')
552 it 'ステータスコード200 OKを返す' do
553 post :import, :file => @t, :format => :json
554 response.should be_success
558 context '検証、保存に失敗した' do
561 OriginalLicense.stub(:import).with(any_args()).and_return(
562 [OriginalLicense.new(Factory.attributes_for(:original_license))]
566 it 'ステータスコード200 OKを返す' do
567 post :import, :file => @t
568 response.status.should eq 200
570 it 'resultページを描画する' do
571 post :import, :file => @t
572 response.should render_template("result")
576 it 'ステータスコード422 unprocessable_entity を返す' do
577 post :import, :file => @t, :format => :json
578 response.status.should eq 422
580 it '応答メッセージUnprocessable Entityを返す' do
581 post :import, :file => @t, :format => :json
582 response.message.should match(/Unprocessable/)