OSDN Git Service

11b47f843e97f6b53063afa88c4ce92875aba1e5
[pettanr/pettanr.git] / spec / controllers / panel_colors_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #指定色地
4
5 describe PanelColorsController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @sp = FactoryGirl.create :system_picture
10     @lg = FactoryGirl.create :license_group, :name => 'peta'
11     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
12     @author = FactoryGirl.create :author, :user_id => @user.id
13     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
14     @panel = FactoryGirl.create :panel, :author_id => @author.id
15   end
16
17   describe '一覧表示に於いて' do
18     before do
19       sign_in @user
20       @pc = FactoryGirl.create :panel_color, :panel_id => @panel.id
21       PanelColor.stub(:list).and_return([@pc, @pc, @pc])
22     end
23     context 'パラメータpageについて' do
24       it '@pageに値が入る' do
25         get :index, :page => 5
26         assigns(:page).should eq 5
27       end
28       it '省略されると@pageに1値が入る' do
29         get :index
30         assigns(:page).should eq 1
31       end
32       it '与えられたpage_sizeがセットされている' do
33         get :index, :page_size => 15
34         assigns(:page_size).should eq 15
35       end
36       it '省略されると@page_sizeにデフォルト値が入る' do
37         get :index
38         assigns(:page_size).should eq PanelColor.default_page_size
39       end
40       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 1500
42         assigns(:page_size).should eq PanelColor.max_page_size
43       end
44       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 0
46         assigns(:page_size).should eq PanelColor.default_page_size
47       end
48     end
49     context 'つつがなく終わるとき' do
50       it 'ステータスコード200 OKを返す' do
51         get :index
52         response.should be_success 
53       end
54       it '指定色地モデルに一覧を問い合わせている' do
55         PanelColor.should_receive(:list).exactly(1)
56         get :index
57       end
58       it '@panel_colorsにリストを取得している' do
59         get :index
60         assigns(:panel_colors).should have_at_least(3).items
61       end
62       context 'html形式' do
63         it 'indexテンプレートを描画する' do
64           get :index
65           response.should render_template("index")
66         end
67       end
68       context 'json形式' do
69         it 'jsonデータを返す' do
70           get :index, :format => :json
71           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
72         end
73         it '指定色地モデルにjson一覧出力オプションを問い合わせている' do
74           PanelColor.should_receive(:list_json_opt).exactly(1)
75           get :index, :format => :json
76         end
77         it 'データがリスト構造になっている' do
78           get :index, :format => :json
79           json = JSON.parse response.body
80           json.should have_at_least(3).items
81         end
82         it 'リストの先頭くらいは指定色地っぽいものであって欲しい' do
83           get :index, :format => :json
84           json = JSON.parse response.body
85           json.first.has_key?("panel_id").should be_true
86           json.first.has_key?("code").should be_true
87           json.first.has_key?("z").should be_true
88         end
89       end
90     end
91     context '作家権限がないとき' do
92       before do
93         sign_out @user
94       end
95       context 'html形式' do
96         it 'ステータスコード302 Foundを返す' do
97           get :index
98           response.status.should eq 302
99         end
100         it 'サインインページへ遷移する' do
101           get :index
102           response.should redirect_to '/users/sign_in'
103         end
104       end
105       context 'json形式' do
106         it 'ステータスコード401 Unauthorizedを返す' do
107           get :index, :format => :json
108           response.status.should eq 401
109         end
110         it '応答メッセージにUnauthorizedを返す' do
111           get :index, :format => :json
112           response.message.should match(/Unauthorized/)
113         end
114       end
115     end
116   end
117   
118   describe '単体表示に於いて' do
119     before do
120       sign_in @user
121       @pc = FactoryGirl.create :panel_color, :panel_id => @panel.id
122       PanelColor.stub(:show).and_return(@pc)
123     end
124     context 'つつがなく終わるとき' do
125       it 'ステータスコード200 OKを返す' do
126         get :show, :id => @pc.id
127         response.should be_success
128       end
129       it '指定色地モデルに単体取得を問い合わせている' do
130         PanelColor.should_receive(:show).exactly(1)
131         get :show
132       end
133       it '@panel_colorにアレを取得している' do
134         get :show, :id => @pc.id
135         assigns(:panel_color).should eq(@pc)
136       end
137       context 'html形式' do
138         it 'showテンプレートを描画する' do
139           get :show, :id => @pc.id
140           response.should render_template("show")
141         end
142       end
143       context 'json形式' do
144         it 'jsonデータを返す' do
145           get :show, :id => @pc.id, :format => :json
146           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
147         end
148         it '指定色地モデルにjson単体出力オプションを問い合わせている' do
149           PanelColor.should_receive(:show_json_opt).exactly(1)
150           get :show, :id => @pc.id, :format => :json
151         end
152         it 'データがアレになっている' do
153           get :show, :id => @pc.id, :format => :json
154           json = JSON.parse response.body
155           json["panel_id"].should_not be_nil
156           json["z"].should_not be_nil
157           json["code"].should_not be_nil
158         end
159       end
160     end
161     context '作家権限がないとき' do
162       before do
163         sign_out @user
164       end
165       context 'html形式' do
166         it 'ステータスコード302 Foundを返す' do
167           get :show, :id => @pc.id
168           response.status.should eq 302
169         end
170         it 'サインインページへ遷移する' do
171           get :show, :id => @pc.id
172           response.body.should redirect_to '/users/sign_in'
173         end
174       end
175       context 'json形式' do
176         it 'ステータスコード401 Unauthorizedを返す' do
177           get :show, :id => @pc.id, :format => :json
178           response.status.should eq 401
179         end
180         it '応答メッセージにUnauthorizedを返す' do
181           get :show, :id => @pc.id, :format => :json
182           response.message.should match(/Unauthorized/)
183         end
184       end
185     end
186   end
187   
188 end