1 # -*- encoding: utf-8 -*-
7 SpeechBalloonTemplate.delete_all
8 @admin = FactoryGirl.create :admin
9 @demand_user = FactoryGirl.create :demand_user
10 @sp = FactoryGirl.create :system_picture
11 @lg = FactoryGirl.create :license_group
12 @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
13 @user = FactoryGirl.create( :user_yas)
14 @author = FactoryGirl.create :author, :user_id => @user.id
15 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
16 @other_user = FactoryGirl.create( :user_yas)
17 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
18 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
23 @sheet = FactoryGirl.build :sheet, :author_id => @author.id
26 context 'オーソドックスなデータのとき' do
30 @sheet.should be_valid
33 @sheet.caption = 'a'*100
35 @sheet.should be_valid
39 context 'captionを検証するとき' do
42 @sheet.should_not be_valid
45 @sheet.caption = 'a'*101
46 @sheet.should_not be_valid
49 context 'visibleを検証するとき' do
52 @sheet.should_not be_valid
56 @sheet.should_not be_valid
60 @sheet.should_not be_valid
65 describe '文字コード検証に於いて' do
67 @sheet = FactoryGirl.build :sheet, :author_id => @author.id
70 context 'captionを検証するとき' do
71 it 'Shift JISなら失敗する' do
72 @sheet.caption = "\x83G\x83r\x83]\x83D"
75 }.should raise_error(Pettanr::BadRequest)
81 describe 'デフォルト値補充に於いて' do
82 it 'visibleが0になっている' do
83 @sheet = FactoryGirl.build :sheet, :visible => nil
85 @sheet.visible.should eq 0
89 describe '上書き補充に於いて' do
91 @sheet = FactoryGirl.build :sheet, :author_id => nil
92 @sheet.overwrite @author
93 @sheet.author_id.should eq @author.id
97 describe '所持判定に於いて' do
99 @sheet = FactoryGirl.build :sheet, :author_id => @author.id
101 context '事前チェックする' do
102 it '自身にロールリストからの作家取得を依頼している' do
103 Sheet.should_receive(:get_author_from_roles).with(any_args).exactly(1)
104 r = @sheet.own?([@author])
107 context 'ロール内作家が取得できるとき' do
110 it 'ロール内作家のidが自身の作家idと一致するなら許可する' do
111 Sheet.stub(:get_author_from_roles).with(any_args).and_return(@author)
112 r = @sheet.own?([@author])
115 it 'ロール内作家のidが自身の作家idと一致しないならno' do
116 Sheet.stub(:get_author_from_roles).with(any_args).and_return(@other_author)
117 @sheet.own?(@other_author).should be_false
120 context 'ロール内作家が取得できないとき' do
122 Sheet.stub(:get_author_from_roles).with(any_args).and_return(nil)
125 r = @sheet.own?([@author])
131 describe '閲覧許可に於いて' do
133 @sheet = FactoryGirl.build :sheet, :author_id => @author.id
135 context 'オープンモードのとき' do
137 MagicNumber['run_mode'] = 0
139 it '自身にゲスト用ロールチェックを問い合わせしている' do
140 Sheet.any_instance.stub(:guest_role_check).and_return(true)
141 Sheet.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1)
142 r = @sheet.visible?([@author])
144 it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do
145 Sheet.any_instance.stub(:guest_role_check).and_return(false)
146 r = @sheet.visible?([@author])
150 context 'クローズドモードのとき' do
152 MagicNumber['run_mode'] = 1
154 it '自身に読者用ロールチェックを問い合わせしている' do
155 Sheet.any_instance.stub(:reader_role_check).and_return(true)
156 Sheet.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1)
157 r = @sheet.visible?([@author])
159 it '読者用ロールチェックが失敗したとき、falseを返す' do
160 Sheet.any_instance.stub(:reader_role_check).and_return(false)
161 r = @sheet.visible?([@author])
165 context '事前チェックする' do
167 MagicNumber['run_mode'] = 1
168 Sheet.any_instance.stub(:reader_role_check).and_return(true)
169 Sheet.any_instance.stub(:own?).and_return(true)
171 it '自身に所持判定を問い合わせしている' do
172 Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
173 r = @sheet.visible?([@author])
176 context 'つつがなく終わるとき' do
178 MagicNumber['run_mode'] = 1
179 Sheet.any_instance.stub(:reader_role_check).and_return(true)
182 Sheet.any_instance.stub(:own?).and_return(true)
183 Sheet.any_instance.stub(:visible).and_return(0)
184 r = @sheet.visible?([@author])
187 it '他人の非公開用紙なら許可しない' do
188 Sheet.any_instance.stub(:own?).and_return(false)
189 Sheet.any_instance.stub(:visible).and_return(0)
190 r = @sheet.visible?([@author])
193 it '他人の用紙でも公開なら許可する' do
194 Sheet.any_instance.stub(:own?).and_return(false)
195 Sheet.any_instance.stub(:visible).and_return(1)
196 r = @sheet.visible?([@author])
202 describe '一覧取得に於いて' do
204 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
206 context 'page補正について' do
207 it '文字列から数値に変換される' do
208 Sheet.page('8').should eq 8
211 Sheet.page().should eq 1
214 Sheet.page('0').should eq 1
217 context 'page_size補正について' do
218 it '文字列から数値に変換される' do
219 Sheet.page_size('7').should eq 7
221 it 'nilの場合はSheet.default_page_sizeになる' do
222 Sheet.page_size().should eq Sheet.default_page_size
224 it '0以下の場合はSheet.default_page_sizeになる' do
225 Sheet.page_size('0').should eq Sheet.default_page_size
227 it 'Sheet.max_page_sizeを超えた場合はSheet.max_page_sizeになる' do
228 Sheet.page_size('1000').should eq Sheet.max_page_size
231 context 'つつがなく終わるとき' do
232 it '一覧取得オプションを利用している' do
233 Sheet.stub(:list_opt).with(any_args).and_return({})
234 Sheet.should_receive(:list_opt).with(any_args).exactly(1)
242 it '非公開用紙は(自分の用紙であっても)含んでいない' do
243 FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0
248 #公開用紙は(他人の用紙であっても)含んでいる
249 v = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
251 c.should eq [v, @sheet]
253 context 'DBに5件あって1ページの件数を2件に変えたとして' do
255 @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
256 @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
257 @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
258 @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
259 Sheet.stub(:default_page_size).and_return(2)
263 c.should have(2).items
265 it 'page=1なら末尾2件を返す' do
268 c.should eq [@sheet5, @sheet4]
270 it 'page=2なら中間2件を返す' do
272 c.should eq [@sheet3, @sheet2]
274 it 'page=3なら先頭1件を返す' do
281 describe '自分の用紙一覧取得に於いて' do
283 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
285 context 'つつがなく終わるとき' do
286 it '一覧取得オプションを利用している' do
287 Sheet.stub(:list_opt).with(any_args).and_return({})
288 Sheet.should_receive(:list_opt).with(any_args).exactly(1)
289 r = Sheet.mylist @author
293 c = Sheet.mylist @author
297 nc = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
298 cl = Sheet.mylist @author
299 cl.should eq [nc, @sheet]
301 it '他人の用紙は公開でも含まない' do
302 nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
303 cl = Sheet.mylist @author
304 cl.should eq [@sheet]
306 it '自分の用紙は非公開でも含んでいる' do
307 nc = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 0, :updated_at => Time.now + 100
308 cl = Sheet.mylist @author
309 cl.should eq [nc, @sheet]
311 context 'DBに5件あって1ページの件数を2件に変えたとして' do
313 @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 100
314 @sheet3 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 200
315 @sheet4 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 300
316 @sheet5 = FactoryGirl.create :sheet, :author_id => @author.id, :updated_at => Time.now + 400
319 c = Sheet.mylist @author, 1, 2
320 c.should have(2).items
322 it 'page=1なら末尾2件を返す' do
324 c = Sheet.mylist(@author, 1, 2)
325 c.should eq [@sheet5, @sheet4]
327 it 'page=2なら中間2件を返す' do
328 c = Sheet.mylist(@author, 2, 2)
329 c.should eq [@sheet3, @sheet2]
331 it 'page=3なら先頭1件を返す' do
332 c = Sheet.mylist(@author, 3, 2)
338 describe '他作家の用紙一覧取得に於いて' do
340 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
341 @other_sheet = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 1
343 context 'つつがなく終わるとき' do
344 it '一覧取得オプションを利用している' do
345 Sheet.stub(:list_opt).with(any_args).and_return({})
346 Sheet.should_receive(:list_opt).with(any_args).exactly(1)
347 r = Sheet.himlist @other_author
350 it '指定した作家のリストを返す' do
351 r = Sheet.himlist @other_author
352 r.should eq [@other_sheet]
355 nc = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
356 r = Sheet.himlist @other_author
357 r.should eq [nc, @other_sheet]
360 hidden = FactoryGirl.create :sheet, :author_id => @other_author.id, :visible => 0
361 r = Sheet.himlist @other_author
362 r.should eq [@other_sheet]
364 context 'DBに5件あって1ページの件数を2件に変えたとして' do
366 @other_sheet2 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 100
367 @other_sheet3 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 200
368 @other_sheet4 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 300
369 @other_sheet5 = FactoryGirl.create :sheet, :author_id => @other_author.id, :updated_at => Time.now + 400
372 c = Sheet.himlist @other_author, 1, 2
373 c.should have(2).items
375 it 'page=1なら末尾2件を返す' do
377 c = Sheet.himlist(@other_author, 1, 2)
378 c.should eq [@other_sheet5, @other_sheet4]
380 it 'page=2なら中間2件を返す' do
381 c = Sheet.himlist(@other_author, 2, 2)
382 c.should eq [@other_sheet3, @other_sheet2]
384 it 'page=3なら先頭1件を返す' do
385 c = Sheet.himlist(@other_author, 3, 2)
386 c.should eq [@other_sheet]
391 describe '用紙一覧ページ制御に於いて' do
393 Sheet.stub(:count).with(any_args).and_return(100)
396 r = Sheet.list_paginate
397 r.is_a?(Kaminari::PaginatableArray).should be_true
399 it '用紙一覧の取得条件を利用している' do
400 Sheet.stub(:list_where).with(any_args).and_return('')
401 Sheet.should_receive(:list_where).with(any_args).exactly(1)
402 r = Sheet.list_paginate
404 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
405 r = Sheet.list_paginate 3, 10
406 r.limit_value.should eq 10
407 r.offset_value.should eq 20
411 describe '自分の用紙一覧ページ制御に於いて' do
413 Sheet.stub(:count).with(any_args).and_return(100)
416 r = Sheet.mylist_paginate @author
417 r.is_a?(Kaminari::PaginatableArray).should be_true
419 it '自分の用紙一覧の取得条件を利用している' do
420 Sheet.stub(:mylist_where).with(any_args).and_return('')
421 Sheet.should_receive(:mylist_where).with(any_args).exactly(1)
422 r = Sheet.mylist_paginate @author
424 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
425 r = Sheet.mylist_paginate @author, 3, 10
426 r.limit_value.should eq 10
427 r.offset_value.should eq 20
431 describe '他作家の用紙一覧ページ制御に於いて' do
433 Sheet.stub(:count).with(any_args).and_return(100)
436 r = Sheet.himlist_paginate @other_author
437 r.is_a?(Kaminari::PaginatableArray).should be_true
439 it '他作家の用紙一覧の取得条件を利用している' do
440 Sheet.stub(:himlist_where).with(any_args).and_return('')
441 Sheet.should_receive(:himlist_where).with(any_args).exactly(1)
442 r = Sheet.himlist_paginate @other_author
444 it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
445 r = Sheet.himlist_paginate @other_author, 3, 10
446 r.limit_value.should eq 10
447 r.offset_value.should eq 20
451 describe '一覧取得オプションに於いて' do
454 r.should have(2).items
458 r.has_key?(:sheet_panels).should be_true
460 it 'スクコマはコマを含んでいる' do
462 r[:sheet_panels].has_key?(:panel).should be_true
466 r.has_key?(:author).should be_true
469 describe 'json一覧出力オプションに於いて' do
471 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
472 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
473 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
474 @sbt = FactoryGirl.create :speech_balloon_template
475 @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
476 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
477 @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
480 r = Sheet.list.to_json Sheet.list_json_opt
483 i.has_key?('sheet_panels').should be_true
485 it 'スクコマはコマを含んでいる' do
486 r = Sheet.list.to_json Sheet.list_json_opt
489 s = i['sheet_panels'].first
490 s.has_key?('panel').should be_true
493 r = Sheet.list.to_json Sheet.list_json_opt
496 i.has_key?('author').should be_true
500 describe '単体取得に於いて' do
502 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
504 context 'つつがなく終わるとき' do
505 it '単体取得オプションを利用している' do
506 Sheet.stub(:show_opt).with(any_args).and_return({})
507 Sheet.should_receive(:show_opt).with(any_args).exactly(1)
508 r = Sheet.show @sheet.id, @author
510 it '閲覧許可を問い合わせている' do
511 Sheet.any_instance.stub(:visible?).with(any_args).and_return(true)
512 Sheet.any_instance.should_receive(:visible?).with(any_args).exactly(1)
513 r = Sheet.show @sheet.id, @author
517 c = Sheet.show @sheet.id, @author
520 context '閲覧許可が出なかったとき' do
521 it '403Forbidden例外を返す' do
522 Sheet.any_instance.stub(:visible?).and_return(false)
524 Sheet.show @sheet.id, @author
525 }.should raise_error(ActiveRecord::Forbidden)
528 context '存在しない用紙を開こうとしたとき' do
529 it '404RecordNotFound例外を返す' do
531 Sheet.show 110, @author
532 }.should raise_error(ActiveRecord::RecordNotFound)
537 describe '編集取得に於いて' do
539 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
541 context 'つつがなく終わるとき' do
542 it '単体取得オプションを利用している' do
543 Sheet.stub(:show_opt).with(any_args).and_return({})
544 Sheet.should_receive(:show_opt).with(any_args).exactly(1)
545 r = Sheet.edit @sheet.id, @author
547 it '所持判定を問い合わせている' do
548 Sheet.any_instance.stub(:own?).with(any_args).and_return(true)
549 Sheet.any_instance.should_receive(:own?).with(any_args).exactly(1)
550 r = Sheet.edit @sheet.id, @author
554 Sheet.any_instance.stub(:own?).and_return(true)
555 c = Sheet.edit @sheet.id, @author.id
558 context '他人の用紙を開こうとしたとき' do
559 it '403Forbidden例外を返す' do
560 Sheet.any_instance.stub(:own?).and_return(false)
562 Sheet.edit @sheet.id, @author
563 }.should raise_error(ActiveRecord::Forbidden)
566 context '存在しない用紙を開こうとしたとき' do
567 it '404RecordNotFound例外を返す' do
569 Sheet.edit 110, @author
570 }.should raise_error(ActiveRecord::RecordNotFound)
574 describe '単体取得オプションに於いて' do
575 it 'includeキーを含んでいる' do
577 r.has_key?(:include).should be_true
580 r = Sheet.show_opt[:include]
581 r.should have(2).items
584 r = Sheet.show_opt[:include]
585 r.has_key?(:author).should be_true
588 r = Sheet.show_opt[:include]
589 r.has_key?(:sheet_panels).should be_true
591 it 'スクコマはコマを含んでいる' do
592 r = Sheet.show_opt[:include]
593 r[:sheet_panels].has_key?(:panel).should be_true
596 describe 'json単体出力オプションに於いて' do
598 @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
599 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
600 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
601 @sbt = FactoryGirl.create :speech_balloon_template
602 @sheet = FactoryGirl.create :sheet, :author_id => @author.id, :visible => 1
603 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
604 @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
607 r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
610 i.has_key?('sheet_panels').should be_true
612 it 'スクコマはコマを含んでいる' do
613 r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
616 s = i['sheet_panels'].first
617 s.has_key?('panel').should be_true
620 r = Sheet.show(@sheet.id, @author).to_json Sheet.show_json_opt
623 i.has_key?('author').should be_true
629 @sheet = FactoryGirl.create :sheet, :author_id => @author.id
630 @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
631 @sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @sheet.id, :panel_id => @panel.id
632 @other_sheet = FactoryGirl.create :sheet, :author_id => @author.id
633 @other_sheet_panel = FactoryGirl.create :sheet_panel, :author_id => @author.id, :sheet_id => @other_sheet.id, :panel_id => @panel.id
635 context 'つつがなく終わるとき' do
638 r = @sheet.destroy_with_sheet_panel
639 }.should change(Sheet, :count).by(-1)
641 r = Sheet.find @sheet.id
644 it '自身にリンクしているスクコマをすべて削除する' do
646 r = @sheet.destroy_with_sheet_panel
647 }.should change(SheetPanel, :count).by(-1)
649 r = SheetPanel.find @sheet_panel.id
653 r = @sheet.destroy_with_sheet_panel
657 context '削除に失敗したとき' do
659 SheetPanel.any_instance.stub(:destroy).with(any_args).and_return(false)
662 r = @sheet.destroy_with_sheet_panel
667 r = @sheet.destroy_with_sheet_panel
668 }.should_not change(Sheet, :count)
670 r = @sheet.destroy_with_sheet_panel
671 }.should_not change(SheetPanel, :count)