1 # -*- encoding: utf-8 -*-
5 describe ResourcePicturesController 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
17 describe '一覧表示に於いて' do
19 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
20 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
22 ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
24 context 'パラメータpageについて' do
26 get :index, :page => 5
27 assigns(:page).should eq 5
29 it '省略されると@pageに1値が入る' do
31 assigns(:page).should eq 1
33 it '与えられたpage_sizeがセットされている' do
34 get :index, :page_size => 15
35 assigns(:page_size).should eq 15
37 it '省略されると@page_sizeにデフォルト値が入る' do
39 assigns(:page_size).should eq ResourcePicture.default_page_size
41 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
42 get :index, :page_size => 1500
43 assigns(:page_size).should eq ResourcePicture.max_page_size
45 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
46 get :index, :page_size => 0
47 assigns(:page_size).should eq ResourcePicture.default_page_size
50 context 'つつがなく終わるとき' do
51 it 'ステータスコード200 OKを返す' do
53 response.should be_success
55 it '素材モデルに一覧を問い合わせている' do
56 ResourcePicture.should_receive(:list).exactly(1)
59 it '@resource_picturesにリストを取得している' do
61 assigns(:resource_pictures).should have_at_least(3).items
64 it 'indexテンプレートを描画する' do
66 response.should render_template("index")
71 get :index, :format => :json
72 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
74 it 'データがリスト構造になっている' do
75 get :index, :format => :json
76 json = JSON.parse response.body
77 json.should have_at_least(3).items
79 it 'リストの先頭くらいは素材っぽいものであって欲しい' do
80 get :index, :format => :json
81 json = JSON.parse response.body
82 json.first.has_key?("ext").should be_true
86 context '作家権限がないとき' do
91 it 'ステータスコード302 Foundを返す' do
93 response.status.should eq 302
97 response.should redirect_to '/users/sign_in'
101 it 'ステータスコード401 Unauthorizedを返す' do
102 get :index, :format => :json
103 response.status.should eq 401
105 it '応答メッセージにUnauthorizedを返す' do
106 get :index, :format => :json
107 response.message.should match(/Unauthorized/)
113 describe '単体表示に於いて' do
115 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
116 @pic = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
118 ResourcePicture.stub(:show).and_return(@pic)
120 context 'つつがなく終わるとき' do
121 it 'ステータスコード200 OKを返す' do
122 get :show, :id => @pic.id
123 response.should be_success
125 it '素材モデルに単体取得を問い合わせている' do
126 ResourcePicture.should_receive(:show).exactly(1)
129 it '@resource_pictureにアレを取得している' do
130 get :show, :id => @pic.id
131 assigns(:resource_picture).id.should eq(@pic.id)
134 it 'showテンプレートを描画する' do
135 get :show, :id => @pic.id
136 response.should render_template("show")
141 get :show, :id => @pic.id, :format => :json
142 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
145 get :show, :id => @pic.id, :format => :json
146 json = JSON.parse response.body
147 json["ext"].should match(/png/)
150 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
151 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
152 #スタブをおいてsend_dataがデータを返す体裁でテストする。
154 it '画像モデルに画像データを問い合わせる' do
155 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
156 get :show, :id => @pic.id, :format => :png
159 ResourcePicture.any_instance.stub(:restore).and_return('aaa')
160 get :show, :id => @pic.id, :format => :png
161 response.body.should eq 'aaa'
165 it '画像モデルに画像データを問い合わせる' do
166 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
167 get :show, :id => @pic.id, :format => :gif
170 ResourcePicture.any_instance.stub(:restore).and_return('bbb')
171 get :show, :id => @pic.id, :format => :gif
172 response.body.should eq 'bbb'
176 it '画像モデルに画像データを問い合わせる' do
177 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
178 get :show, :id => @pic.id, :format => :jpeg
181 ResourcePicture.any_instance.stub(:restore).and_return('ccc')
182 get :show, :id => @pic.id, :format => :jpeg
183 response.body.should eq 'ccc'
187 context '作家権限がないとき' do
192 it 'ステータスコード302 Foundを返す' do
193 get :show, :id => @pic.id
194 response.status.should eq 302
196 it 'サインインページへ遷移する' do
197 get :show, :id => @pic.id
198 response.body.should redirect_to '/users/sign_in'
202 it 'ステータスコード401 Unauthorizedを返す' do
203 get :show, :id => @pic.id, :format => :json
204 response.status.should eq 401
206 it '応答メッセージにUnauthorizedを返す' do
207 get :show, :id => @pic.id, :format => :json
208 response.message.should match(/Unauthorized/)
213 context '対象素材がないとき' do
215 ResourcePicture.unstub(:show)
218 it '例外404 not_foundを返す' do
221 }.should raise_error(ActiveRecord::RecordNotFound)
225 it '例外404 not_foundを返す' do
227 get :show, :id => 0, :format => :json
228 }.should raise_error(ActiveRecord::RecordNotFound)
232 context '他人の素材を見ようとしたとき' do
234 ResourcePicture.stub(:show).and_return(@pic)
235 ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
238 it '例外403 forbiddenを返す' do
240 get :show, :id => @pic.id
241 }.should raise_error(ActiveRecord::Forbidden)
245 it '例外403 forbiddenを返す' do
247 get :show, :id => @pic.id, :format => :json
248 }.should raise_error(ActiveRecord::Forbidden)
255 describe '素材数取得に於いて' do
257 ResourcePicture.should_receive(:visible_count).and_return(3)
260 context 'つつがなく終わるとき' do
261 it 'ステータスコード200 OKを返す' do
262 get :count, :format => :json
263 response.should be_success
267 get :count, :format => :json
268 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
270 it 'データがHash構造になっていてコマ絵数が3である' do
271 get :count, :format => :json
272 json = JSON.parse response.body
273 json["count"].should == 3
279 describe 'クレジット表示に於いて' do
\r
281 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
282 @pic = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
284 ResourcePicture.stub(:show).and_return(@pic)
286 context 'つつがなく終わるとき' do
\r
287 it 'ステータスコード200 OKを返す' do
\r
288 get :credit, :id => @pic.id
\r
289 response.should be_success
\r
291 it '素材モデルに単体取得を問い合わせている' do
\r
292 ResourcePicture.should_receive(:show).exactly(1)
\r
293 get :credit, :id => @pic.id
\r
295 it '@resource_pictureにアレを取得している' do
\r
296 get :credit, :id => @pic.id
\r
297 assigns(:resource_picture).id.should eq(@pic.id)
\r
299 context 'html形式' do
\r
300 it 'creditテンプレートを描画する' do
\r
301 get :credit, :id => @pic.id
\r
302 response.should render_template("credit")
\r
305 context 'json形式' do
\r
307 get :credit, :id => @pic.id, :format => :json
\r
308 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
310 it 'データがアレになっている' do
\r
311 get :credit, :id => @pic.id, :format => :json
\r
312 json = JSON.parse response.body
\r
313 json["ext"].should match(/png/)
\r
317 context '作家権限がないとき' do
\r
321 context 'html形式' do
\r
322 it 'ステータスコード302 Foundを返す' do
\r
323 get :credit, :id => @pic.id
\r
324 response.status.should eq 302
\r
326 it 'サインインページへ遷移する' do
\r
327 get :credit, :id => @pic.id
\r
328 response.body.should redirect_to '/users/sign_in'
\r
331 context 'json形式' do
\r
332 it 'ステータスコード401 Unauthorizedを返す' do
\r
333 get :credit, :id => @pic.id, :format => :json
\r
334 response.status.should eq 401
\r
336 it '応答メッセージにUnauthorizedを返す' do
\r
337 get :credit, :id => @pic.id, :format => :json
\r
338 response.message.should match(/Unauthorized/)
\r
343 context '対象素材がないとき' do
\r
345 ResourcePicture.unstub(:show)
\r
347 context 'html形式' do
\r
348 it '例外404 not_foundを返す' do
\r
350 get :show, :id => 0
\r
351 }.should raise_error(ActiveRecord::RecordNotFound)
\r
354 context 'json形式' do
\r
355 it '例外404 not_foundを返す' do
\r
357 get :show, :id => 0, :format => :json
\r
358 }.should raise_error(ActiveRecord::RecordNotFound)
\r