OSDN Git Service

pass test
[pettanr/pettanr.git] / spec / controllers / original_licenses_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #オリジナルライセンス
3 require 'spec_helper'
4
5 describe OriginalLicensesController do
6   before do
7     @admin = Factory :admin
8     @lc = Factory :license
9     @user = Factory( :user_yas)
10     @author = @user.author
11     @artist = Factory :artist_yas, :author_id => @author.id
12   end
13   describe '一覧表示に於いて' do
14     before do
15       sign_in @user
16       @ol = Factory :original_license, :license_id => @lc.id
17       OriginalLicense.stub(:list).and_return([@ol, @ol, @ol])
18     end
19     context 'パラメータpageについて' do
20       it '@pageに値が入る' do
21         get :index, :page => 5
22         assigns(:page).should eq 5
23       end
24       it '省略されると@pageに1値が入る' do
25         get :index
26         assigns(:page).should eq 1
27       end
28       it '与えられたpage_sizeがセットされている' do
29         get :index, :page_size => 15
30         assigns(:page_size).should eq 15
31       end
32       it '省略されると@page_sizeにデフォルト値が入る' do
33         get :index
34         assigns(:page_size).should eq OriginalLicense.default_page_size
35       end
36       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
37         get :index, :page_size => 1500
38         assigns(:page_size).should eq OriginalLicense.max_page_size
39       end
40       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 0
42         assigns(:page_size).should eq OriginalLicense.default_page_size
43       end
44     end
45     context 'つつがなく終わるとき' do
46       it 'ステータスコード200 OKを返す' do
47         get :index
48         response.should be_success 
49       end
50       it 'ライセンスモデルに一覧を問い合わせている' do
51         OriginalLicense.should_receive(:list).exactly(1)
52         get :index
53       end
54       it '@original_licensesにリストを取得している' do
55         get :index
56         assigns(:original_licenses).should have_at_least(3).items
57       end
58       context 'html形式' do
59         it 'indexテンプレートを描画する' do
60           get :index
61           response.should render_template("index")
62         end
63       end
64       context 'json形式' do
65         it 'jsonデータを返す' do
66           get :index, :format => :json
67           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
68         end
69         it 'データがリスト構造になっている' do
70           get :index, :format => :json
71           json = JSON.parse response.body
72           json.should have_at_least(3).items
73         end
74         it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
75           get :index, :format => :json
76           json = JSON.parse response.body
77           json.first.has_key?("url").should be_true
78         end
79       end
80     end
81     context '作家権限がないとき' do
82       before do
83         sign_out @user
84       end
85       it 'ステータスコード200 okを返す' do
86         get :index
87         response.status.should eq 200
88       end
89     end
90   end
91   
92   describe '単体表示に於いて' do
93     before do
94       sign_in @user
95       @ol = Factory :original_license, :license_id => @lc.id
96       OriginalLicense.stub(:show).and_return(@ol)
97     end
98     context 'つつがなく終わるとき' do
99       it 'ステータスコード200 OKを返す' do
100         get :show, :id => @ol.id
101         response.should be_success
102       end
103       it 'ライセンスモデルに単体取得を問い合わせている' do
104         OriginalLicense.should_receive(:show).exactly(1)
105         get :show
106       end
107       it '@original_licenseにアレを取得している' do
108         get :show, :id => @ol.id
109         assigns(:original_license).id.should eq(@ol.id)
110       end
111       context 'html形式' do
112         it 'showテンプレートを描画する' do
113           get :show, :id => @ol.id
114           response.should render_template("show")
115         end
116       end
117       context 'json形式' do
118         it 'jsonデータを返す' do
119           get :show, :id => @ol.id, :format => :json
120           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
121         end
122         it 'データがアレになっている' do
123           get :show, :id => @ol.id, :format => :json
124           json = JSON.parse response.body
125           json["name"].should match(/peta/)
126         end
127       end
128     end
129     context '作家権限がないとき' do
130       before do
131         sign_out @user
132       end
133       it 'ステータスコード200 okを返す' do
134         get :show, :id => @ol.id
135         response.status.should eq 200
136       end
137     end
138   end
139   
140   describe '新規作成フォーム表示に於いて' do
141     before do
142       sign_in @admin
143       sign_in @user
144     end
145     context 'つつがなく終わるとき' do
146       it 'ステータスコード200 OKを返す' do
147         get :new
148         response.should be_success 
149       end
150       it '@original_licenseに新規データを用意している' do
151         get :new
152         assigns(:original_license).should be_a_new(OriginalLicense)
153       end
154       it 'オリジナルライセンスモデルにデフォルト値補充を依頼している' do
155         OriginalLicense.any_instance.should_receive(:supply_default).exactly(1)
156         get :new
157       end
158       context 'html形式' do
159         it 'newテンプレートを描画する' do
160           get :new
161           response.should render_template("new")
162         end
163       end
164       context 'js形式' do
165         it 'new.jsテンプレートを描画する' do
166           get :new, :format => :js
167           response.should render_template("new")
168         end
169       end
170     end
171     context '管理者権限がないとき' do
172       before do
173         sign_out @admin
174       end
175       context 'html形式' do
176         it 'ステータスコード302 Foundを返す' do
177           get :new
178           response.status.should eq 302
179         end
180         it 'サインインページへ遷移する' do
181           get :new
182           response.body.should redirect_to '/admins/sign_in'
183         end
184       end
185       context 'js形式' do
186         it 'ステータスコード401 Unauthorizedを返す' do
187           get :new, :format => :js
188           response.status.should eq 401
189         end
190         it '応答メッセージにUnauthorizedを返す' do
191           get :new, :format => :js
192           response.message.should match(/Unauthorized/)
193         end
194       end
195     end
196   end
197
198   describe '新規作成に於いて' do
199     before do
200       sign_in @admin
201       sign_in @user
202     end
203     context 'つつがなく終わるとき' do
204       it 'モデルに保存依頼する' do
205         OriginalLicense.any_instance.should_receive(:store).exactly(1)
206         post :create, :original_license => Factory.attributes_for(:original_license)
207       end
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
212       end
213       context 'html形式' do
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
218         end
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)
223         end
224       end
225       context 'json形式' do
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 
230         end
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)
234         end
235         it 'データがアレになっている' do
236           post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
237           json = JSON.parse response.body
238           json["name"].should match(/peta/)
239         end
240       end
241     end
242     context '管理者権限がないとき' do
243       before do
244         sign_out @admin
245       end
246       context 'html形式' do
247         it 'ステータスコード302 Foundを返す' do
248           post :create, :original_license => Factory.attributes_for(:original_license)
249           response.status.should eq 302
250         end
251         it 'サインインページへ遷移する' do
252           post :create, :original_license => Factory.attributes_for(:original_license)
253           response.body.should redirect_to '/admins/sign_in'
254         end
255       end
256       context 'json形式' do
257         it 'ステータスコード401 Unauthorizedを返す' do
258           post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
259           response.status.should eq 401
260         end
261         it '応答メッセージにUnauthorizedを返す' do
262           post :create, :original_license => Factory.attributes_for(:original_license), :format => :json
263           response.message.should match(/Unauthorized/)
264         end
265       end
266     end
267     context '検証、保存に失敗した' do
268       before do
269         OriginalLicense.any_instance.stub(:save).and_return(false)
270       end
271       it "未保存のオリジナルライセンスを保持している" do
272         post :create, :original_license => {}
273         assigns(:original_license).should be_a_new(OriginalLicense)
274       end
275       context 'html形式' do
276         it 'ステータスコード200 OKを返す' do
277           post :create, :original_license => {}
278           response.status.should eq 200
279         end
280         it '新規ページを描画する' do
281           post :create, :original_license => {}
282           response.should render_template("new")
283         end
284       end
285       context 'json形式' do
286         it 'ステータスコード422 unprocessable_entity を返す' do
287           post :create, :original_license => {}, :format => :json
288           response.status.should eq 422
289         end
290         it '応答メッセージUnprocessable Entityを返す' do
291           post :create, :original_license => {}, :format => :json
292           response.message.should match(/Unprocessable/)
293         end
294       end
295     end
296   end
297
298   describe '編集フォーム表示に於いて' do
299     before do
300       @ol = Factory :original_license, :license_id => @lc.id
301       sign_in @admin
302       sign_in @user
303       OriginalLicense.stub(:show).and_return(@ol)
304     end
305     context 'つつがなく終わるとき' do
306       it 'ステータスコード200 OKを返す' do
307         get :edit, :id => @ol.id
308         response.should be_success 
309       end
310       it 'オリジナルライセンスモデルに単体取得を問い合わせている' do
311         OriginalLicense.should_receive(:show).exactly(1)
312         get :edit, :id => @ol.id
313       end
314       it '@original_licenseにデータを用意している' do
315         get :edit, :id => @ol.id
316         assigns(:original_license).should eq @ol
317       end
318       context 'html形式' do
319         it 'editテンプレートを描画する' do
320           get :edit, :id => @ol.id
321           response.should render_template("edit")
322         end
323       end
324       context 'js形式' do
325         it 'edit.jsテンプレートを描画する' do
326           get :edit, :id => @ol.id, :format => :js
327           response.should render_template("edit")
328         end
329       end
330     end
331     context '管理者権限がないとき' do
332       before do
333         sign_out @admin
334       end
335       context 'html形式' do
336         it 'ステータスコード302 Foundを返す' do
337           get :edit, :id => @ol.id
338           response.status.should eq 302
339         end
340         it 'サインインページへ遷移する' do
341           get :edit, :id => @ol.id
342           response.body.should redirect_to '/admins/sign_in'
343         end
344       end
345       context 'js形式' do
346         it 'ステータスコード401 Unauthorizedを返す' do
347           get :edit, :id => @ol.id, :format => :js
348           response.status.should eq 401
349         end
350         it '応答メッセージにUnauthorizedを返す' do
351           get :edit, :id => @ol.id, :format => :js
352           response.message.should match(/Unauthorized/)
353         end
354       end
355     end
356   end
357
358   describe '更新に於いて' do
359     before do
360       @ol = Factory :original_license, :license_id => @lc.id
361       sign_in @admin
362       sign_in @user
363       @attr = Factory.attributes_for(:original_license, :name => 'new lc')
364     end
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
370       end
371       it 'モデルに更新を依頼する' do
372         OriginalLicense.any_instance.should_receive(:store).with(any_args)
373         put :update, :id => @ol.id, :original_license => @attr
374       end
375       it '@original_licenseにアレを取得している' do
376         put :update, :id => @ol.id, :original_license => @attr
377         assigns(:original_license).id.should eq(@ol.id)
378       end
379     end
380     context 'つつがなく終わるとき' do
381       it '更新される' do
382         put :update, :id => @ol.id, :original_license => @attr
383         OriginalLicense.find(@ol.id).name.should eq 'new lc'
384       end
385       context 'html形式' do
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
390         end
391         it '更新されたオリジナルライセンスの表示ページへ遷移する' do
392           put :update, :id => @ol.id, :original_license => @attr
393           response.should redirect_to(@ol)
394         end
395       end
396       context 'json形式' do
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 
401         end
402         it 'ページ本体は特に返さない' do
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 /./
406         end
407       end
408     end
409     context '管理者権限がないとき' do
410       before do
411         sign_out @admin
412       end
413       it 'ステータスコード302 Foundを返す' do
414         put :update, :id => @ol.id, :original_license => @attr
415         response.status.should eq 302
416       end
417       context 'html形式' do
418         it 'サインインページへ遷移する' do
419           put :update, :id => @ol.id, :original_license => @attr
420           response.body.should redirect_to '/admins/sign_in'
421         end
422       end
423       context 'json形式' do
424         it '応答メッセージにUnauthorizedを返す' do
425           put :update, :id => @ol.id, :original_license => @attr, :format => :json
426           response.message.should match(/Unauthorized/)
427         end
428       end
429     end
430     context '検証、保存に失敗したとき' do
431       before do
432         OriginalLicense.any_instance.stub(:store).and_return(false)
433       end
434       context 'html形式' do
435         it 'ステータスコード200 Okを返す' do
436           put :update, :id => @ol.id, :original_license => @attr
437           response.status.should eq 200
438         end
439         it '編集ページを描画する' do
440           put :update, :id => @ol.id, :original_license => @attr
441           response.should render_template("edit")
442         end
443       end
444       context 'json形式' do
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
449         end
450         it '応答メッセージUnprocessable Entityを返す' do
451           put :update, :id => @ol.id, :original_license => @attr, :format => :json
452           response.message.should match(/Unprocessable/)
453         end
454       end
455     end
456   end
457
458   describe 'インポートに於いて' do
459     before do
460       sign_in @admin
461       sign_in @user
462       #テストデータを用意してね
463       @f = Rails.root + 'spec/json/original_license.json'
464       @t = File.open(@f, 'r').read
465       @j = JSON.parse @t
466       @fs = Rails.root + 'spec/json/original_licenses.json'
467       @ts = File.open(@fs, 'r').read
468       @js = JSON.parse @ts
469       @fes = Rails.root + 'spec/json/invalid_original_licenses.json'
470       @tes = File.open(@fes, 'r').read
471       @jes = JSON.parse @tes
472     end
473     context '事前チェックしておく' do
474       before do
475         #異常な行を返すから、正常の意味で空を返す
476         OriginalLicense.stub(:import).with(any_args()).and_return([])
477       end
478       it "@dataに渡したデータを保持している" do
479         post :import, :file => @t
480         assigns(:data).should_not be_nil
481       end
482       it 'モデルにインポート依頼する' do
483         OriginalLicense.should_receive(:import).with(any_args()).exactly(1)
484         post :import, :file => @t
485       end
486       it "@errorsに結果を保持している" do
487         post :import, :file => @t
488         assigns(:errors).should eq []
489       end
490     end
491     context 'つつがなく終わるとき' do
492       before do
493       end
494       context 'html形式' do
495         it 'ステータスコード302 Foundを返す' do
496           post :import, :file => @t
497           response.status.should eq 302
498         end
499         it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
500           post :import, :file => @t
501           response.should redirect_to('/original_licenses/list')
502         end
503       end
504       context 'json形式' do
505         it 'ステータスコード200 OKを返す' do
506           post :import, :file => @t, :format => :json
507           response.should be_success 
508         end
509       end
510     end
511     context '管理者権限がないとき' do
512       before do
513         sign_out @admin
514       end
515       context 'html形式' do
516         it 'ステータスコード302 Foundを返す' do
517           post :import, :file => @t
518           response.status.should eq 302
519         end
520         it '管理者サインインページへ遷移する' do
521           post :import, :file => @t
522           response.body.should redirect_to '/admins/sign_in'
523         end
524       end
525       context 'json形式' do
526         it 'ステータスコード401 Unauthorizedを返す' do
527           post :import, :file => @t, :format => :json
528           response.status.should eq 401
529         end
530         it '応答メッセージにUnauthorizedを返す' do
531           post :import, :file => @t, :format => :json
532           response.message.should match(/Unauthorized/)
533         end
534       end
535     end
536     context '作家権限がないとき' do
537       #必要なのは管理者権限であって作家権限ではない。成功を見届ける
538       before do
539         sign_out @user
540       end
541       context 'html形式' do
542         it 'ステータスコード302 Foundを返す' do
543           post :import, :file => @t
544           response.status.should eq 302
545         end
546         it '管理者向けオリジナルライセンス一覧ページへ遷移する' do
547           post :import, :file => @t
548           response.should redirect_to('/original_licenses/list')
549         end
550       end
551       context 'json形式' do
552         it 'ステータスコード200 OKを返す' do
553           post :import, :file => @t, :format => :json
554           response.should be_success 
555         end
556       end
557     end
558     context '検証、保存に失敗した' do
559       before do
560         #異常な行を返す
561         OriginalLicense.stub(:import).with(any_args()).and_return(
562           [OriginalLicense.new(Factory.attributes_for(:original_license))]
563         )
564       end
565       context 'html形式' do
566         it 'ステータスコード200 OKを返す' do
567           post :import, :file => @t
568           response.status.should eq 200
569         end
570         it 'resultページを描画する' do
571           post :import, :file => @t
572           response.should render_template("result")
573         end
574       end
575       context 'json形式' do
576         it 'ステータスコード422 unprocessable_entity を返す' do
577           post :import, :file => @t, :format => :json
578           response.status.should eq 422
579         end
580         it '応答メッセージUnprocessable Entityを返す' do
581           post :import, :file => @t, :format => :json
582           response.message.should match(/Unprocessable/)
583         end
584       end
585     end
586   end
587   
588 end