1 # -*- encoding: utf-8 -*-
5 describe SystemPicturesController do
7 @admin = FactoryGirl.create :admin
8 @user = FactoryGirl.create( :user_yas)
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 describe '一覧表示に於いて' do
20 SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
22 context 'パラメータpageについて' do
24 get :index, :page => 5
25 assigns(:page).should eq 5
27 it '省略されると@pageに1値が入る' do
29 assigns(:page).should eq 1
31 it '与えられたpage_sizeがセットされている' do
32 get :index, :page_size => 15
33 assigns(:page_size).should eq 15
35 it '省略されると@page_sizeにデフォルト値が入る' do
37 assigns(:page_size).should eq SystemPicture.default_page_size
39 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
40 get :index, :page_size => 1500
41 assigns(:page_size).should eq SystemPicture.max_page_size
43 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
44 get :index, :page_size => 0
45 assigns(:page_size).should eq SystemPicture.default_page_size
48 context 'つつがなく終わるとき' do
49 it 'システム画像モデルに一覧を問い合わせている' do
50 SystemPicture.should_receive(:list).exactly(1)
53 it '@system_picturesにリストを取得している' do
55 assigns(:system_pictures).should have_at_least(3).items
58 it 'ステータスコード200 OKを返す' do
60 response.should be_success
62 it 'indexテンプレートを描画する' do
64 response.should render_template("index")
68 it 'ステータスコード200 OKを返す' do
69 get :index, :format => :json
70 response.should be_success
73 get :index, :format => :json
74 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
76 it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
77 SystemPicture.should_receive(:list_json_opt).exactly(1)
78 get :index, :format => :json
80 it 'データがリスト構造になっている' do
81 get :index, :format => :json
82 json = JSON.parse response.body
83 json.should have_at_least(3).items
85 it 'リストの先頭くらいはシステム画像っぽいものであって欲しい' do
86 get :index, :format => :json
87 json = JSON.parse response.body
88 json.first.has_key?("ext").should be_true
89 json.first.has_key?("md5").should be_true
93 context '作家権限がないとき' do
98 it 'ステータスコード302 Foundを返す' do
100 response.status.should eq 302
102 it 'サインインページへ遷移する' do
104 response.should redirect_to '/users/sign_in'
108 it 'ステータスコード401 Unauthorizedを返す' do
109 get :index, :format => :json
110 response.status.should eq 401
112 it '応答メッセージにUnauthorizedを返す' do
113 get :index, :format => :json
114 response.message.should match(/Unauthorized/)
120 describe '単体表示に於いて' do
123 SystemPicture.stub(:show).and_return(@sp)
125 context 'つつがなく終わるとき' do
126 it 'ステータスコード200 OKを返す' do
127 get :show, :id => @sp.id
128 response.should be_success
130 it 'システム画像モデルに単体取得を問い合わせている' do
131 SystemPicture.should_receive(:show).exactly(1)
134 it '@system_pictureにアレを取得している' do
135 get :show, :id => @sp.id
136 assigns(:system_picture).should eq @sp
139 it 'showテンプレートを描画する' do
140 get :show, :id => @sp.id
141 response.should render_template("show")
146 get :show, :id => @sp.id, :format => :json
147 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
149 it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
150 SystemPicture.should_receive(:show_json_opt).exactly(1)
151 get :show, :id => @sp.id, :format => :json
154 get :show, :id => @sp.id, :format => :json
155 json = JSON.parse response.body
156 json["ext"].should match(/png/)
157 json["md5"].should be_true
158 json["width"].should be_true
161 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
162 #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
163 #スタブをおいてsend_dataがデータを返す体裁でテストする。
166 SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
167 SystemPicture.any_instance.stub(:restore).and_return('aaa')
169 it '画像モデルに画像データを問い合わせる' do
170 SystemPicture.any_instance.should_receive(:restore).exactly(1)
171 get :show, :id => @sp.id, :format => :png
173 it '画像モデルにMimeTypeを問い合わせる' do
174 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
175 get :show, :id => @sp.id, :format => :png
178 get :show, :id => @sp.id, :format => :png
179 response.body.should eq 'aaa'
184 SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
185 SystemPicture.any_instance.stub(:restore).and_return('bbb')
187 it '画像モデルに画像データを問い合わせる' do
188 SystemPicture.any_instance.should_receive(:restore).exactly(1)
189 get :show, :id => @sp.id, :format => :gif
191 it '画像モデルにMimeTypeを問い合わせる' do
192 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
193 get :show, :id => @sp.id, :format => :gif
196 get :show, :id => @sp.id, :format => :gif
197 response.body.should eq 'bbb'
202 SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
203 SystemPicture.any_instance.stub(:restore).and_return('ccc')
205 it '画像モデルに画像データを問い合わせる' do
206 SystemPicture.any_instance.should_receive(:restore).exactly(1)
207 get :show, :id => @sp.id, :format => :jpeg
209 it '画像モデルにMimeTypeを問い合わせる' do
210 SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
211 get :show, :id => @sp.id, :format => :jpeg
214 get :show, :id => @sp.id, :format => :jpeg
215 response.body.should eq 'ccc'
219 context '作家権限がないとき' do
224 it 'ステータスコード302 Foundを返す' do
225 get :show, :id => @sp.id
226 response.status.should eq 302
228 it 'サインインページへ遷移する' do
229 get :show, :id => @sp.id
230 response.body.should redirect_to '/users/sign_in'
234 it 'ステータスコード401 Unauthorizedを返す' do
235 get :show, :id => @sp.id, :format => :json
236 response.status.should eq 401
238 it '応答メッセージにUnauthorizedを返す' do
239 get :show, :id => @sp.id, :format => :json
240 response.message.should match(/Unauthorized/)
245 context '対象システム画像がないとき' do
247 SystemPicture.unstub(:show)
250 it '例外404 not_foundを返す' do
253 }.should raise_error(ActiveRecord::RecordNotFound)
257 it '例外404 not_foundを返す' do
259 get :show, :id => 0, :format => :json
260 }.should raise_error(ActiveRecord::RecordNotFound)
264 context '他人のシステム画像を見ようとしたとき' do
266 SystemPicture.stub(:show).and_return(@sp)
267 SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
270 it '例外403 forbiddenを返す' do
272 get :show, :id => @sp.id
273 }.should raise_error(ActiveRecord::Forbidden)
277 it '例外403 forbiddenを返す' do
279 get :show, :id => @sp.id, :format => :json
280 }.should raise_error(ActiveRecord::Forbidden)
287 describe '新規作成フォーム表示に於いて' do
292 context 'つつがなく終わるとき' do
294 it 'ステータスコード200 OKを返す' do
296 response.should be_success
298 it 'ページテンプレートnewを描画する' do
300 response.should render_template("new")
304 context '管理者権限がないとき' do
309 it 'ステータスコード302 Foundを返す' do
311 response.status.should eq 302
313 it 'サインインページへ遷移する' do
315 response.body.should redirect_to '/admins/sign_in'
321 describe '新規作成に於いて' do
325 @attr = {:system_picture => {:file => "abc\ndef\nghi"}}
326 @imager = ImagerTest.load("abc\ndef\nghi")
328 context '事前チェックしておく' do
330 PettanImager.stub(:load).with(any_args).and_return(@imager)
331 SystemPicture.any_instance.stub(:store).with(@imager).and_return(true)
333 it "画像ライブラリをロードしている" do
334 PettanImager.should_receive(:load).with(any_args).exactly(1)
337 it 'システム画像モデルに保存依頼する' do
338 SystemPicture.should_receive(:store).with(@imager).exactly(1)
342 context 'つつがなく終わるとき' do
344 PettanImager.stub(:load).with(any_args).and_return(@imager)
345 SystemPicture.stub(:store).with(@imager).and_return(@sp)
348 it 'ステータスコード302 Foundを返す' do
350 response.status.should eq 302
352 it '作成されたシステム画像の表示ページへ遷移する' do
354 response.should redirect_to(SystemPicture.last)
359 @attr.merge!({:format => :json})
361 it 'ステータスコード200 OKを返す' do
363 response.should be_success
365 it '作成されたシステム画像をjsonデータで返す' do
367 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
369 it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
370 SystemPicture.should_receive(:show_json_opt).exactly(1)
375 json = JSON.parse response.body
376 json["ext"].should match(/png/)
377 json["md5"].should be_true
378 json["width"].should be_true
382 context '管理者権限がないとき' do
387 it 'ステータスコード302 Foundを返す' do
389 response.status.should eq 302
391 it 'サインインページへ遷移する' do
393 response.body.should redirect_to '/admins/sign_in'
398 @attr.merge!({:format => :json})
400 it 'ステータスコード401 Unauthorizedを返す' do
402 response.status.should eq 401
404 it '応答メッセージにUnauthorizedを返す' do
406 response.message.should match(/Unauthorized/)
410 context '検証、保存に失敗した' do
412 SystemPicture.stub(:store).and_return(nil)
415 it 'ステータスコード200 OKを返す' do
417 response.status.should eq 200
421 response.should render_template("new")
426 @attr.merge!({:format => :json})
428 it 'ステータスコード422 unprocessable_entity を返す' do
430 response.status.should eq 422
432 it '応答メッセージUnprocessable Entityを返す' do
434 response.message.should match(/Unprocessable/)