OSDN Git Service

94eb80156809016a90f0489dd00d1ccbaf7bd1d4
[pettanr/pettanr.git] / spec / controllers / home_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #ユーザホーム
4
5 describe HomeController 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   end
19   
20   describe '自分のコミック一覧表示に於いて' do
21     before do
22       @comic = FactoryGirl.create :comic, :author_id => @author.id
23       sign_in @user
24       Comic.stub(:mylist).and_return([@comic, @comic, @comic])
25     end
26     context 'パラメータpageについて' do
27       it '@pageに値が入る' do
28         get :comic, :page => 5
29         assigns(:page).should eq 5
30       end
31       it '省略されると@pageに1値が入る' do
32         get :comic
33         assigns(:page).should eq 1
34       end
35       it '与えられたpage_sizeがセットされている' do
36         get :comic, :page_size => 15
37         assigns(:page_size).should eq 15
38       end
39       it '省略されると@page_sizeにデフォルト値が入る' do
40         get :comic
41         assigns(:page_size).should eq Author.default_comic_page_size
42       end
43       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
44         get :comic, :page_size => 1500
45         assigns(:page_size).should eq Author.comic_max_page_size
46       end
47       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
48         get :comic, :page_size => 0
49         assigns(:page_size).should eq Author.default_comic_page_size
50       end
51     end
52     context 'つつがなく終わるとき' do
53       it 'ステータスコード200 OKを返す' do
54         get :comic
55         response.should be_success 
56       end
57       it 'コミックモデルに一覧を問い合わせている' do
58         Comic.should_receive(:mylist).exactly(1)
59         get :comic
60       end
61       it '@comicsにリストを取得している' do
62         get :comic
63         assigns(:comics).should have_at_least(3).items
64       end
65       context 'html形式' do
66         it 'comicテンプレートを描画する' do
67           get :comic
68           response.should render_template("comic")
69         end
70       end
71       context 'json形式' do
72         it 'jsonデータを返す' do
73           get :comic, :format => :json
74           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
75         end
76         it 'データがリスト構造になっている' do
77           get :comic, :format => :json
78           json = JSON.parse response.body
79           json.should have_at_least(3).items
80         end
81         it 'リストの先頭くらいはコミックっぽいものであって欲しい' do
82           get :comic, :format => :json
83           json = JSON.parse response.body
84           json.first.has_key?("title").should be_true
85         end
86       end
87     end
88     context '作家権限がないとき' do
89       before do
90         sign_out @user
91       end
92       context 'html形式' do
93         it 'ステータスコード302 Foundを返す' do
94           get :comic
95           response.status.should eq 302
96         end
97         it 'サインインページへ遷移する' do
98           get :comic
99           response.should redirect_to '/users/sign_in'
100         end
101       end
102       context 'json形式' do
103         it 'ステータスコード401 Unauthorizedを返す' do
104           get :comic, :format => :json
105           response.status.should eq 401
106         end
107         it '応答メッセージにUnauthorizedを返す' do
108           get :comic, :format => :json
109           response.message.should match(/Unauthorized/)
110         end
111       end
112     end
113   end
114   
115   describe '自分のコマ一覧表示に於いて' do
116     before do
117       @panel = FactoryGirl.create :panel, :author_id => @author.id
118       sign_in @user
119       Panel.stub(:mylist).and_return([@panel, @panel, @panel])
120     end
121     context 'パラメータpageについて' do
122       it '@pageに値が入る' do
123         get :panel, :page => 5
124         assigns(:page).should eq 5
125       end
126       it '省略されると@pageに1値が入る' do
127         get :panel
128         assigns(:page).should eq 1
129       end
130       it '与えられたpage_sizeがセットされている' do
131         get :panel, :page_size => 15
132         assigns(:page_size).should eq 15
133       end
134       it '省略されると@page_sizeにデフォルト値が入る' do
135         get :panel
136         assigns(:page_size).should eq Author.default_panel_page_size
137       end
138       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
139         get :panel, :page_size => 1500
140         assigns(:page_size).should eq Author.panel_max_page_size
141       end
142       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
143         get :panel, :page_size => 0
144         assigns(:page_size).should eq Author.default_panel_page_size
145       end
146     end
147     context 'つつがなく終わるとき' do
148       it 'ステータスコード200 OKを返す' do
149         get :panel
150         response.should be_success 
151       end
152       it 'コマモデルに一覧を問い合わせている' do
153         Panel.should_receive(:mylist).exactly(1)
154         get :panel
155       end
156       it '@panelsにリストを取得している' do
157         get :panel
158         assigns(:panels).should have_at_least(3).items
159       end
160       context 'html形式' do
161         it 'panelテンプレートを描画する' do
162           get :panel
163           response.should render_template("panel")
164         end
165       end
166       context 'json形式' do
167         it 'jsonデータを返す' do
168           get :panel, :format => :json
169           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
170         end
171         it 'データがリスト構造になっている' do
172           get :panel, :format => :json
173           json = JSON.parse response.body
174           json.should have_at_least(3).items
175         end
176         it 'リストの先頭くらいはコマっぽいものであって欲しい' do
177           get :panel, :format => :json
178           json = JSON.parse response.body
179           json.first.has_key?("z").should be_true
180         end
181       end
182     end
183     context '作家権限がないとき' do
184       before do
185         sign_out @user
186       end
187       context 'html形式' do
188         it 'ステータスコード302 Foundを返す' do
189           get :panel
190           response.status.should eq 302
191         end
192         it 'サインインページへ遷移する' do
193           get :panel
194           response.should redirect_to '/users/sign_in'
195         end
196       end
197       context 'json形式' do
198         it 'ステータスコード401 Unauthorizedを返す' do
199           get :panel, :format => :json
200           response.status.should eq 401
201         end
202         it '応答メッセージにUnauthorizedを返す' do
203           get :panel, :format => :json
204           response.message.should match(/Unauthorized/)
205         end
206       end
207     end
208   end
209   
210   describe '自分のコマ絵一覧表示に於いて' do
211     before do
212       @pp = FactoryGirl.create :panel_picture, :panel_id => @panel.id, :picture_id => @p.id,
213         :width => @p.width, :height => @p.height
214       sign_in @user
215       PanelPicture.stub(:mylist).and_return([@pp, @pp, @pp])
216     end
217     context 'パラメータpageについて' do
218       it '@pageに値が入る' do
219         get :panel_picture, :page => 5
220         assigns(:page).should eq 5
221       end
222       it '省略されると@pageに1値が入る' do
223         get :panel_picture
224         assigns(:page).should eq 1
225       end
226       it '与えられたpage_sizeがセットされている' do
227         get :panel_picture, :page_size => 15
228         assigns(:page_size).should eq 15
229       end
230       it '省略されると@page_sizeにデフォルト値が入る' do
231         get :panel_picture
232         assigns(:page_size).should eq Author.default_panel_picture_page_size
233       end
234       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
235         get :panel_picture, :page_size => 1500
236         assigns(:page_size).should eq Author.panel_picture_max_page_size
237       end
238       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
239         get :panel_picture, :page_size => 0
240         assigns(:page_size).should eq Author.default_panel_picture_page_size
241       end
242     end
243     context 'つつがなく終わるとき' do
244       it 'ステータスコード200 OKを返す' do
245         get :panel_picture
246         response.should be_success 
247       end
248       it 'コマ絵モデルに一覧を問い合わせている' do
249         PanelPicture.should_receive(:mylist).exactly(1)
250         get :panel_picture
251       end
252       it '@panel_picturesにリストを取得している' do
253         get :panel_picture
254         assigns(:panel_pictures).should have_at_least(3).items
255       end
256       context 'html形式' do
257         it 'panel_pictureテンプレートを描画する' do
258           get :panel_picture
259           response.should render_template("panel_picture")
260         end
261       end
262       context 'json形式' do
263         it 'jsonデータを返す' do
264           get :panel_picture, :format => :json
265           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
266         end
267         it 'データがリスト構造になっている' do
268           get :panel_picture, :format => :json
269           json = JSON.parse response.body
270           json.should have_at_least(3).items
271         end
272         it 'リストの先頭くらいはコマ絵っぽいものであって欲しい' do
273           get :panel_picture, :format => :json
274           json = JSON.parse response.body
275           json.first.has_key?("link").should be_true
276         end
277       end
278     end
279     context '作家権限がないとき' do
280       before do
281         sign_out @user
282       end
283       context 'html形式' do
284         it 'ステータスコード302 Foundを返す' do
285           get :panel_picture
286           response.status.should eq 302
287         end
288         it 'サインインページへ遷移する' do
289           get :panel_picture
290           response.should redirect_to '/users/sign_in'
291         end
292       end
293       context 'json形式' do
294         it 'ステータスコード401 Unauthorizedを返す' do
295           get :panel_picture, :format => :json
296           response.status.should eq 401
297         end
298         it '応答メッセージにUnauthorizedを返す' do
299           get :panel_picture, :format => :json
300           response.message.should match(/Unauthorized/)
301         end
302       end
303     end
304   end
305   
306   describe '自分の原画一覧表示に於いて' do
307     before do
308       sign_in @user
309       OriginalPicture.stub(:list).and_return([@op, @op, @op])
310     end
311     context 'パラメータpageについて' do
312       it '@pageに値が入る' do
313         get :picture, :page => 5
314         assigns(:page).should eq 5
315       end
316       it '省略されると@pageに1値が入る' do
317         get :picture
318         assigns(:page).should eq 1
319       end
320       it '与えられたpage_sizeがセットされている' do
321         get :picture, :page_size => 15
322         assigns(:page_size).should eq 15
323       end
324       it '省略されると@page_sizeにデフォルト値が入る' do
325         get :picture
326         assigns(:page_size).should eq OriginalPicture.default_page_size
327       end
328       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
329         get :picture, :page_size => 1500
330         assigns(:page_size).should eq OriginalPicture.max_page_size
331       end
332       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
333         get :picture, :page_size => 0
334         assigns(:page_size).should eq OriginalPicture.default_page_size
335       end
336     end
337     context 'つつがなく終わるとき' do
338       it 'ステータスコード200 OKを返す' do
339         get :picture
340         response.should be_success 
341       end
342       it '原画モデルに一覧を問い合わせている' do
343         #原画は他人が使えないので「自分の」リストはない
344         OriginalPicture.should_receive(:list).exactly(1)
345         get :picture
346       end
347       it '@original_picturesにリストを取得している' do
348         get :picture
349         assigns(:original_pictures).should have_at_least(3).items
350       end
351       context 'html形式' do
352         it 'pictureテンプレートを描画する' do
353           get :picture
354           response.should render_template("picture")
355         end
356       end
357       context 'json形式' do
358         it 'jsonデータを返す' do
359           get :picture, :format => :json
360           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
361         end
362         it 'データがリスト構造になっている' do
363           get :picture, :format => :json
364           json = JSON.parse response.body
365           json.should have_at_least(3).items
366         end
367         it 'リストの先頭くらいは原画っぽいものであって欲しい' do
368           get :picture, :format => :json
369           json = JSON.parse response.body
370           json.first.has_key?("ext").should be_true
371         end
372       end
373     end
374     context '作家権限がないとき' do
375       before do
376         sign_out @user
377       end
378       context 'html形式' do
379         it 'ステータスコード302 Foundを返す' do
380           get :picture
381           response.status.should eq 302
382         end
383         it 'サインインページへ遷移する' do
384           get :picture
385           response.should redirect_to '/users/sign_in'
386         end
387       end
388       context 'json形式' do
389         it 'ステータスコード401 Unauthorizedを返す' do
390           get :picture, :format => :json
391           response.status.should eq 401
392         end
393         it '応答メッセージにUnauthorizedを返す' do
394           get :picture, :format => :json
395           response.message.should match(/Unauthorized/)
396         end
397       end
398     end
399     context '作家が絵師でないとき' do
400       before do
401         Author.any_instance.stub(:artist?).and_return(false)
402       end
403       context 'html形式' do
404         it 'ステータスコード302 Foundを返す' do
405           get :picture
406           response.status.should eq 302
407         end
408         it '絵師登録ページへ遷移する' do
409           get :picture
410           response.should redirect_to new_artist_path
411         end
412       end
413       context 'json形式' do
414         it '例外403 forbiddenを返す' do
415           lambda{
416             get :picture, :format => :json
417           }.should raise_error(ActiveRecord::Forbidden)
418         end
419       end
420     end
421   end
422   
423 end