OSDN Git Service

t#29400:update:itr2
[pettanr/pettanr.git] / spec / controllers / panel_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #コマ絵
3 require 'spec_helper'
4
5 describe PanelPicturesController do
6   before do
7     FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create( :user_yas)
12     @author = @user.author
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
15     @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
16     @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
17     @panel = FactoryGirl.create :panel, :author_id => @author.id
18     @attr = {:picture_id => @p.id, :panel_id => @panel.id, :width => @p.width, :height => @p.height}
19   end
20
21   describe '一覧表示に於いて' do
22     before do
23       @pp = FactoryGirl.create :panel_picture, @attr
24       sign_in @user
25       PanelPicture.stub(:list).and_return([@pp, @pp, @pp])
26     end
27     context 'パラメータpageについて' do
28       it '@pageに値が入る' do
29         get :index, :page => 5
30         assigns(:page).should eq 5
31       end
32       it '省略されると@pageに1値が入る' do
33         get :index
34         assigns(:page).should eq 1
35       end
36       it '与えられたpage_sizeがセットされている' do
37         get :index, :page_size => 15
38         assigns(:page_size).should eq 15
39       end
40       it '省略されると@page_sizeにデフォルト値が入る' do
41         get :index
42         assigns(:page_size).should eq PanelPicture.default_page_size
43       end
44       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 1500
46         assigns(:page_size).should eq PanelPicture.max_page_size
47       end
48       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
49         get :index, :page_size => 0
50         assigns(:page_size).should eq PanelPicture.default_page_size
51       end
52     end
53     context 'つつがなく終わるとき' do
54       it 'ステータスコード200 OKを返す' do
55         get :index
56         response.should be_success 
57       end
58       it 'コマ絵モデルに一覧を問い合わせている' do
59         PanelPicture.should_receive(:list).exactly(1)
60         get :index
61       end
62       it '@panel_picturesにリストを取得している' do
63         get :index
64         assigns(:panel_pictures).should have_at_least(3).items
65       end
66       context 'html形式' do
67         it 'indexテンプレートを描画する' do
68           get :index
69           response.should render_template("index")
70         end
71       end
72       context 'json形式' do
73         it 'jsonデータを返す' do
74           get :index, :format => :json
75           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
76         end
77         it 'コマ絵モデルにjson一覧出力オプションを問い合わせている' do
78           PanelPicture.should_receive(:list_json_opt).exactly(1)
79           get :index, :format => :json
80         end
81         it 'データがリスト構造になっている' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.should have_at_least(3).items
85         end
86         it 'リストの先頭くらいはコマ絵っぽいものであって欲しい' do
87           get :index, :format => :json
88           json = JSON.parse response.body
89           json.first.has_key?("link").should be_true
90           json.first.has_key?("x").should be_true
91           json.first.has_key?("y").should be_true
92         end
93       end
94     end
95     context '作家権限がないとき' do
96       before do
97         sign_out @user
98       end
99       context 'html形式' do
100         it 'ステータスコード302 Foundを返す' do
101           get :index
102           response.status.should eq 302
103         end
104         it 'サインインページへ遷移する' do
105           get :index
106           response.should redirect_to '/users/sign_in'
107         end
108       end
109       context 'json形式' do
110         it 'ステータスコード401 Unauthorizedを返す' do
111           get :index, :format => :json
112           response.status.should eq 401
113         end
114         it '応答メッセージにUnauthorizedを返す' do
115           get :index, :format => :json
116           response.message.should match(/Unauthorized/)
117         end
118       end
119     end
120   end
121   
122 end