OSDN Git Service

merge v04
[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     Factory :admin
8     @sp = Factory :system_picture
9     @lg = Factory :license_group
10     @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = Factory( :user_yas)
12     @author = @user.author
13     @artist = Factory :artist_yas, :author_id => @author.id
14     @op = Factory :original_picture, :artist_id => @artist.id
15     @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
16     @rp = Factory :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
17     @panel = Factory :panel, :author_id => @author.id
18     @attr = {:resource_picture_id => @rp.id, :panel_id => @panel.id}
19   end
20
21   describe '一覧表示に於いて' do
22     before do
23       @pp = Factory :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 'データがリスト構造になっている' 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?("link").should be_true
86         end
87       end
88     end
89     context '作家権限がないとき' do
90       before do
91         sign_out @user
92       end
93       context 'html形式' do
94         it 'ステータスコード302 Foundを返す' do
95           get :index
96           response.status.should eq 302
97         end
98         it 'サインインページへ遷移する' do
99           get :index
100           response.should redirect_to '/users/sign_in'
101         end
102       end
103       context 'json形式' do
104         it 'ステータスコード401 Unauthorizedを返す' do
105           get :index, :format => :json
106           response.status.should eq 401
107         end
108         it '応答メッセージにUnauthorizedを返す' do
109           get :index, :format => :json
110           response.message.should match(/Unauthorized/)
111         end
112       end
113     end
114   end
115   
116 end