1 # -*- encoding: utf-8 -*-
5 describe SystemPicturesController 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
16 if MagicNumber['run_mode'] == 1
17 describe '一覧表示に於いて' do
21 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
23 context 'パラメータpageについて' do
25 get :index, :page => 5
26 assigns(:page).should eq 5
28 it '省略されると@pageに1値が入る' do
30 assigns(:page).should eq 1
32 it '与えられたpage_sizeがセットされている' do
33 get :index, :page_size => 15
34 assigns(:page_size).should eq 15
36 it '省略されると@page_sizeにデフォルト値が入る' do
38 assigns(:page_size).should eq SystemPicture.default_page_size
40 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41 get :index, :page_size => 1500
42 assigns(:page_size).should eq SystemPicture.max_page_size
44 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45 get :index, :page_size => 0
46 assigns(:page_size).should eq SystemPicture.default_page_size
49 context 'つつがなく終わるとき' do
50 it 'システム画像モデルに一覧を問い合わせている' do
51 SystemPicture.should_receive(:list).exactly(1)
54 it '@system_picturesにリストを取得している' do
56 assigns(:system_pictures).should have_at_least(3).items
59 it 'ステータスコード200 OKを返す' do
61 response.should be_success
63 it 'indexテンプレートを描画する' do
65 response.should render_template("index")
69 it 'ステータスコード200 OKを返す' do
70 get :index, :format => :json
71 response.should be_success
74 get :index, :format => :json
75 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
77 it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
78 SystemPicture.should_receive(:list_json_opt).exactly(1)
79 get :index, :format => :json
81 it 'データがリスト構造になっている' do
82 get :index, :format => :json
83 json = JSON.parse response.body
84 json.should have_at_least(3).items
86 it 'リストの先頭くらいはシステム画像っぽいものであって欲しい' do
87 get :index, :format => :json
88 json = JSON.parse response.body
89 json.first.has_key?("ext").should be_true
90 json.first.has_key?("md5").should be_true
94 context '作家権限がないとき' do
99 it 'ステータスコード302 Foundを返す' do
101 response.status.should eq 302
103 it 'サインインページへ遷移する' do
105 response.should redirect_to '/users/sign_in'
109 it 'ステータスコード401 Unauthorizedを返す' do
110 get :index, :format => :json
111 response.status.should eq 401
113 it '応答メッセージにUnauthorizedを返す' do
114 get :index, :format => :json
115 response.message.should match(/Unauthorized/)
121 describe '単体表示に於いて' do
124 SystemPicture.stub(:show).and_return(@sp)
126 context 'つつがなく終わるとき' do
127 it 'ステータスコード200 OKを返す' do
128 get :show, :id => @sp.id
129 response.should be_success
131 it 'システム画像モデルに単体取得を問い合わせている' do
132 SystemPicture.should_receive(:show).exactly(1)
135 it '@system_pictureにアレを取得している' do
136 get :show, :id => @sp.id
137 assigns(:system_picture).should eq @sp
140 it 'showテンプレートを描画する' do
141 get :show, :id => @sp.id
142 response.should render_template("show")
147 get :show, :id => @sp.id, :format => :json
148 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
150 it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
151 SystemPicture.should_receive(:show_json_opt).exactly(1)
152 get :show, :id => @sp.id, :format => :json
155 get :show, :id => @sp.id, :format => :json
156 json = JSON.parse response.body
157 json["ext"].should match(/png/)
158 json["md5"].should be_true
159 json["width"].should be_true
162 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
163 #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
164 #スタブをおいてsend_dataがデータを返す体裁でテストする。
167 SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
168 SystemPicture.any_instance.stub(:restore).and_return('aaa')
170 it '画像モデルに画像データを問い合わせる' do
171 SystemPicture.any_instance.should_receive(:restore).exactly(1)
172 get :show, :id => @sp.id, :format => :png
174 it '画像モデルにMimeTypeを問い合わせる' do
175 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
176 get :show, :id => @sp.id, :format => :png
179 get :show, :id => @sp.id, :format => :png
180 response.body.should eq 'aaa'
185 SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
186 SystemPicture.any_instance.stub(:restore).and_return('bbb')
188 it '画像モデルに画像データを問い合わせる' do
189 SystemPicture.any_instance.should_receive(:restore).exactly(1)
190 get :show, :id => @sp.id, :format => :gif
192 it '画像モデルにMimeTypeを問い合わせる' do
193 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
194 get :show, :id => @sp.id, :format => :gif
197 get :show, :id => @sp.id, :format => :gif
198 response.body.should eq 'bbb'
203 SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
204 SystemPicture.any_instance.stub(:restore).and_return('ccc')
206 it '画像モデルに画像データを問い合わせる' do
207 SystemPicture.any_instance.should_receive(:restore).exactly(1)
208 get :show, :id => @sp.id, :format => :jpeg
210 it '画像モデルにMimeTypeを問い合わせる' do
211 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
212 get :show, :id => @sp.id, :format => :jpeg
215 get :show, :id => @sp.id, :format => :jpeg
216 response.body.should eq 'ccc'
220 context '作家権限がないとき' do
225 it 'ステータスコード302 Foundを返す' do
226 get :show, :id => @sp.id
227 response.status.should eq 302
229 it 'サインインページへ遷移する' do
230 get :show, :id => @sp.id
231 response.body.should redirect_to '/users/sign_in'
235 it 'ステータスコード401 Unauthorizedを返す' do
236 get :show, :id => @sp.id, :format => :json
237 response.status.should eq 401
239 it '応答メッセージにUnauthorizedを返す' do
240 get :show, :id => @sp.id, :format => :json
241 response.message.should match(/Unauthorized/)
246 context '対象システム画像がないとき' do
248 SystemPicture.unstub(:show)
251 it '例外404 not_foundを返す' do
254 }.should raise_error(ActiveRecord::RecordNotFound)
258 it '例外404 not_foundを返す' do
260 get :show, :id => 0, :format => :json
261 }.should raise_error(ActiveRecord::RecordNotFound)
265 context '他人のシステム画像を見ようとしたとき' do
267 SystemPicture.stub(:show).and_return(@sp)
268 SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
271 it '例外403 forbiddenを返す' do
273 get :show, :id => @sp.id
274 }.should raise_error(ActiveRecord::Forbidden)
278 it '例外403 forbiddenを返す' do
280 get :show, :id => @sp.id, :format => :json
281 }.should raise_error(ActiveRecord::Forbidden)
289 describe '一覧表示に於いて' do
293 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
295 context 'つつがなく終わるとき' do
297 it 'ステータスコード200 OKを返す' do
299 response.should be_success
301 it 'indexテンプレートを描画する' do
303 response.should render_template("index")
307 it 'ステータスコード200 OKを返す' do
308 get :index, :format => :json
309 response.should be_success
312 get :index, :format => :json
313 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
317 context '作家権限がないとき' do
322 it 'ステータスコード200 OKを返す' do
324 response.should be_success
326 it 'indexテンプレートを描画する' do
328 response.should render_template("index")
332 it 'ステータスコード200 OKを返す' do
333 get :index, :format => :json
334 response.should be_success
337 get :index, :format => :json
338 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
344 describe '単体表示に於いて' do
347 SystemPicture.stub(:show).and_return(@sp)
349 context 'つつがなく終わるとき' do
350 it 'ステータスコード200 OKを返す' do
351 get :show, :id => @sp.id
352 response.should be_success
355 it 'showテンプレートを描画する' do
356 get :show, :id => @sp.id
357 response.should render_template("show")
362 get :show, :id => @sp.id, :format => :json
363 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
367 context '作家権限がないとき' do
371 it 'ステータスコード200 OKを返す' do
372 get :show, :id => @sp.id
373 response.should be_success
376 it 'showテンプレートを描画する' do
377 get :show, :id => @sp.id
378 response.should render_template("show")
383 get :show, :id => @sp.id, :format => :json
384 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)