OSDN Git Service

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