OSDN Git Service

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