OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / spec / controllers / speeches_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #セリフ
3 require 'spec_helper'
4
5 describe SpeechesController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @user = FactoryGirl.create( :user_yas)
9     @author = FactoryGirl.create :author, :user_id => @user.id
10     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
11     @sp = FactoryGirl.create :system_picture
12     @lg = FactoryGirl.create :license_group
13     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
14
15     @speech_balloon_template = FactoryGirl.create :speech_balloon_template
16     @writing_format = FactoryGirl.create :writing_format
17     @panel = FactoryGirl.create :panel, :author_id => @author.id
18   end
19
20 if MagicNumber['run_mode'] == 1
21   describe '一覧表示に於いて' do
22     before do
23       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
24       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
25       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
26       sign_in @user
27       Speech.stub(:list).and_return([@speech, @speech, @speech])
28     end
29     context 'パラメータpageについて' do
30       it '@pageに値が入る' do
31         get :index, :page => 5
32         assigns(:page).should eq 5
33       end
34       it '省略されると@pageに1値が入る' do
35         get :index
36         assigns(:page).should eq 1
37       end
38       it '与えられたpage_sizeがセットされている' do
39         get :index, :page_size => 15
40         assigns(:page_size).should eq 15
41       end
42       it '省略されると@page_sizeにデフォルト値が入る' do
43         get :index
44         assigns(:page_size).should eq Speech.default_page_size
45       end
46       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
47         get :index, :page_size => 1500
48         assigns(:page_size).should eq Speech.max_page_size
49       end
50       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
51         get :index, :page_size => 0
52         assigns(:page_size).should eq Speech.default_page_size
53       end
54     end
55     context 'つつがなく終わるとき' do
56       it 'ステータスコード200 OKを返す' do
57         get :index
58         response.should be_success 
59       end
60       it 'セリフモデルに一覧を問い合わせている' do
61         Speech.should_receive(:list).exactly(1)
62         get :index
63       end
64       it '@speechesにリストを取得している' do
65         get :index
66         assigns(:speeches).should have_at_least(3).items
67       end
68       context 'html形式' do
69         it 'indexテンプレートを描画する' do
70           get :index
71           response.should render_template("index")
72         end
73       end
74       context 'json形式' do
75         it 'jsonデータを返す' do
76           get :index, :format => :json
77           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
78         end
79         it 'セリフモデルにjson一覧出力オプションを問い合わせている' do
80           Speech.should_receive(:list_json_opt).exactly(1)
81           get :index, :format => :json
82         end
83         it 'データがリスト構造になっている' do
84           get :index, :format => :json
85           json = JSON.parse response.body
86           json.should have_at_least(3).items
87         end
88         it 'リストの先頭くらいはセリフっぽいものであって欲しい' do
89           get :index, :format => :json
90           json = JSON.parse response.body
91           json.first.has_key?("speech_balloon_id").should be_true
92           json.first.has_key?("x").should be_true
93           json.first.has_key?("content").should be_true
94         end
95       end
96     end
97     context 'ユーザ権限がないとき' do
98       before do
99         sign_out @user
100       end
101       context 'html形式' do
102         it 'ステータスコード302 Foundを返す' do
103           get :index
104           response.status.should eq 302
105         end
106         it 'サインインページへ遷移する' do
107           get :index
108           response.should redirect_to '/users/sign_in'
109         end
110       end
111       context 'json形式' do
112         it 'ステータスコード401 Unauthorizedを返す' do
113           get :index, :format => :json
114           response.status.should eq 401
115         end
116         it '応答メッセージにUnauthorizedを返す' do
117           get :index, :format => :json
118           response.message.should match(/Unauthorized/)
119         end
120       end
121     end
122     context 'ユーザ権限はないが管理者権限があるとき' do
123       before do
124         sign_out @user
125         sign_in @admin
126       end
127       it 'ステータスコード200 OKを返す' do
128         get :index
129         response.should be_success 
130       end
131     end
132     context 'ユーザだが作家登録していないとき' do
133       before do
134         @author.destroy
135       end
136       it 'ステータスコード200 OKを返す' do
137         get :index
138         response.should be_success 
139       end
140     end
141   end
142   
143   describe '単体表示に於いて' do
144     before do
145       sign_in @user
146       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
147       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
148       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
149       Speech.stub(:show).and_return(@speech)
150     end
151     context 'つつがなく終わるとき' do
152       it 'ステータスコード200 OKを返す' do
153         get :show, :id => @speech.id
154         response.should be_success
155       end
156       it 'セリフモデルに単体取得を問い合わせている' do
157         Speech.should_receive(:show).exactly(1)
158         get :show
159       end
160       it '@speechにアレを取得している' do
161         get :show, :id => @speech.id
162         assigns(:speech).should eq(@speech)
163       end
164       context 'html形式' do
165         it 'showテンプレートを描画する' do
166           get :show, :id => @speech.id
167           response.should render_template("show")
168         end
169       end
170       context 'json形式' do
171         it 'jsonデータを返す' do
172           get :show, :id => @speech.id, :format => :json
173           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
174         end
175         it 'セリフモデルにjson単体出力オプションを問い合わせている' do
176           Speech.should_receive(:show_json_opt).exactly(1)
177           get :show, :id => @speech.id, :format => :json
178         end
179         it 'データがアレになっている' do
180           get :show, :id => @speech.id, :format => :json
181           json = JSON.parse response.body
182           json["speech_balloon_id"].should_not be_nil
183           json["x"].should_not be_nil
184           json["content"].should_not be_nil
185         end
186       end
187     end
188     context 'ユーザ権限がないとき' do
189       before do
190         sign_out @user
191       end
192       context 'html形式' do
193         it 'ステータスコード302 Foundを返す' do
194           get :show, :id => @speech.id
195           response.status.should eq 302
196         end
197         it 'サインインページへ遷移する' do
198           get :show, :id => @speech.id
199           response.body.should redirect_to '/users/sign_in'
200         end
201       end
202       context 'json形式' do
203         it 'ステータスコード401 Unauthorizedを返す' do
204           get :show, :id => @speech.id, :format => :json
205           response.status.should eq 401
206         end
207         it '応答メッセージにUnauthorizedを返す' do
208           get :show, :id => @speech.id, :format => :json
209           response.message.should match(/Unauthorized/)
210         end
211       end
212     end
213     context 'ユーザ権限はないが管理者権限があるとき' do
214       before do
215         sign_out @user
216         sign_in @admin
217       end
218       it 'ステータスコード200 OKを返す' do
219         get :show, :id => @speech.id
220         response.should be_success 
221       end
222     end
223     context 'ユーザだが作家登録していないとき' do
224       before do
225         @author.destroy
226       end
227       it 'ステータスコード200 OKを返す' do
228         get :show, :id => @speech.id
229         response.should be_success 
230       end
231     end
232   end
233   
234 else
235   describe '一覧表示に於いて' do
236     before do
237       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
238       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
239       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
240       sign_in @user
241       Speech.stub(:list).and_return([@speech, @speech, @speech])
242     end
243     context 'つつがなく終わるとき' do
244       it 'ステータスコード200 OKを返す' do
245         get :index
246         response.should be_success 
247       end
248       context 'html形式' do
249         it 'indexテンプレートを描画する' do
250           get :index
251           response.should render_template("index")
252         end
253       end
254       context 'json形式' do
255         it 'jsonデータを返す' do
256           get :index, :format => :json
257           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
258         end
259       end
260     end
261     context 'ユーザ権限がないとき' do
262       before do
263         sign_out @user
264       end
265       it 'ステータスコード200 OKを返す' do
266         get :index
267         response.should be_success 
268       end
269       context 'html形式' do
270         it 'indexテンプレートを描画する' do
271           get :index
272           response.should render_template("index")
273         end
274       end
275       context 'json形式' do
276         it 'jsonデータを返す' do
277           get :index, :format => :json
278           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
279         end
280       end
281     end
282   end
283   
284   describe '単体表示に於いて' do
285     before do
286       sign_in @user
287       @sb = FactoryGirl.create :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id
288       @balloon = FactoryGirl.create :balloon, :speech_balloon_id => @sb.id, :system_picture_id => @sp.id
289       @speech = FactoryGirl.create :speech, :speech_balloon_id => @sb.id, :writing_format_id => @writing_format.id
290       Speech.stub(:show).and_return(@speech)
291     end
292     context 'つつがなく終わるとき' do
293       it 'ステータスコード200 OKを返す' do
294         get :show, :id => @speech.id
295         response.should be_success
296       end
297       context 'html形式' do
298         it 'showテンプレートを描画する' do
299           get :show, :id => @speech.id
300           response.should render_template("show")
301         end
302       end
303       context 'json形式' do
304         it 'jsonデータを返す' do
305           get :show, :id => @speech.id, :format => :json
306           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
307         end
308       end
309     end
310     context 'ユーザ権限がないとき' do
311       before do
312         sign_out @user
313       end
314       it 'ステータスコード200 OKを返す' do
315         get :show, :id => @speech.id
316         response.should be_success
317       end
318       context 'html形式' do
319         it 'showテンプレートを描画する' do
320           get :show, :id => @speech.id
321           response.should render_template("show")
322         end
323       end
324       context 'json形式' do
325         it 'jsonデータを返す' do
326           get :show, :id => @speech.id, :format => :json
327           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
328         end
329       end
330     end
331   end
332   
333 end
334 end