1 # -*- encoding: utf-8 -*-
5 describe PicturesController do
7 @admin = 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 = FactoryGirl.create :author, :user_id => @user.id
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
21 Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
23 context 'つつがなく終わるとき as json' do
24 it 'ステータスコード200 OKを返す' do
25 get :show, :id => @p.id, :format => :json
26 response.should be_success
28 it '実素材モデルに単体取得を問い合わせている' do
29 Picture.should_receive(:show).exactly(1)
30 get :show, :id => @p.id, :format => :json
32 it '@pictureにアレを取得している' do
33 get :show, :id => @p.id, :format => :json
34 assigns(:picture).id.should eq(@p.id)
37 it 'showテンプレートを描画する' do
38 get :show, :id => @p.id
39 response.should render_template("show")
44 get :show, :id => @p.id, :format => :json
45 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
48 get :show, :id => @p.id, :format => :json
49 json = JSON.parse response.body
50 json["revision"].should_not be_nil
51 json["ext"].should match(/png/)
54 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
55 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
56 #スタブをおいてsend_dataがデータを返す体裁でテストする。
58 it '画像モデルに画像データを問い合わせる' do
59 Picture.any_instance.should_receive(:restore).exactly(1)
60 get :show, :id => @p.id, :format => :png
63 Picture.any_instance.stub(:restore).and_return('aaa')
64 get :show, :id => @p.id, :format => :png
65 response.body.should eq 'aaa'
69 it '画像モデルに画像データを問い合わせる' do
70 Picture.any_instance.should_receive(:restore).exactly(1)
71 get :show, :id => @p.id, :format => :gif
74 Picture.any_instance.stub(:restore).and_return('bbb')
75 get :show, :id => @p.id, :format => :gif
76 response.body.should eq 'bbb'
80 it '画像モデルに画像データを問い合わせる' do
81 Picture.any_instance.should_receive(:restore).exactly(1)
82 get :show, :id => @p.id, :format => :jpeg
85 Picture.any_instance.stub(:restore).and_return('ccc')
86 get :show, :id => @p.id, :format => :jpeg
87 response.body.should eq 'ccc'
91 context '作家権限がないとき' do
96 it 'ステータスコード302 Foundを返す' do
97 get :show, :id => @p.id
98 response.status.should eq 302
100 it 'サインインページへ遷移する' do
101 get :show, :id => @p.id
102 response.body.should redirect_to '/users/sign_in'
106 it 'ステータスコード401 Unauthorizedを返す' do
107 get :show, :id => @p.id, :format => :json
108 response.status.should eq 401
110 it '応答メッセージにUnauthorizedを返す' do
111 get :show, :id => @p.id, :format => :json
112 response.message.should match(/Unauthorized/)
117 context '対象素材がないとき' do
119 Picture.unstub(:show)
122 it '例外404 not_foundを返す' do
125 }.should raise_error(ActiveRecord::RecordNotFound)
129 it '例外404 not_foundを返す' do
131 get :show, :id => 0, :format => :json
132 }.should raise_error(ActiveRecord::RecordNotFound)
136 context '他人の素材を見ようとしたとき' do
138 Picture.stub(:show).and_return(@p)
139 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
142 it '例外403 forbiddenを返す' do
144 get :show, :id => @p.id
145 }.should raise_error(ActiveRecord::Forbidden)
149 it '例外403 forbiddenを返す' do
151 get :show, :id => @p.id, :format => :json
152 }.should raise_error(ActiveRecord::Forbidden)
159 describe 'クレジット表示に於いて' do
161 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
163 Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
165 context 'つつがなく終わるとき' do
166 it 'ステータスコード200 OKを返す' do
167 get :credit, :id => @p.id
168 response.should be_success
170 it '実素材モデルに単体取得を問い合わせている' do
171 Picture.should_receive(:show).exactly(1)
172 get :credit, :id => @p.id
174 it '@pictureにアレを取得している' do
175 get :credit, :id => @p.id
176 assigns(:picture).id.should eq(@p.id)
179 it 'creditテンプレートを描画する' do
180 get :credit, :id => @p.id
181 response.should render_template("credit")
186 get :credit, :id => @p.id, :format => :json
187 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
190 get :credit, :id => @p.id, :format => :json
191 json = JSON.parse response.body
192 json["revision"].should_not be_nil
193 json["ext"].should match(/png/)
197 context '作家権限がないとき' do
202 it 'ステータスコード302 Foundを返す' do
203 get :credit, :id => @p.id
204 response.status.should eq 302
206 it 'サインインページへ遷移する' do
207 get :credit, :id => @p.id
208 response.body.should redirect_to '/users/sign_in'
212 it 'ステータスコード401 Unauthorizedを返す' do
213 get :credit, :id => @p.id, :format => :json
214 response.status.should eq 401
216 it '応答メッセージにUnauthorizedを返す' do
217 get :credit, :id => @p.id, :format => :json
218 response.message.should match(/Unauthorized/)
223 context '対象素材がないとき' do
225 Picture.unstub(:show)
228 it '例外404 not_foundを返す' do
231 }.should raise_error(ActiveRecord::RecordNotFound)
235 it '例外404 not_foundを返す' do
237 get :show, :id => 0, :format => :json
238 }.should raise_error(ActiveRecord::RecordNotFound)
245 describe 'md5検索の一覧に於いて' do
247 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
249 Picture.stub(:list_by_md5).with(any_args).and_return([@p, @p, @p])
251 context 'つつがなく終わるとき' do
252 it '実素材モデルにmd5検索を問い合わせている' do
253 Picture.should_receive(:list_by_md5).exactly(1)
254 get :search, :md5 => 'a'*32
256 it '@picturesにリストを取得している' do
257 get :search, :md5 => 'a'*32
258 assigns(:pictures).should have_at_least(3).items
261 it 'ステータスコード200 OKを返す' do
262 get :search, :md5 => 'a'*32
263 response.should be_success
265 it 'searchテンプレートを描画する' do
266 get :search, :md5 => 'a'*32
267 response.should render_template("search")
271 it 'ステータスコード200 OKを返す' do
272 get :search, :md5 => 'a'*32, :format => :json
273 response.should be_success
276 get :search, :md5 => 'a'*32, :format => :json
277 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
279 it 'データがリスト構造になっている' do
280 get :search, :md5 => 'a'*32, :format => :json
281 json = JSON.parse response.body
282 json.should have_at_least(3).items
284 it 'リストの先頭くらいは実素材っぽいものであって欲しい' do
285 get :search, :md5 => 'a'*32, :format => :json
286 json = JSON.parse response.body
287 json.first.has_key?("ext").should be_true
288 json.first.has_key?("md5").should be_true
289 json.first.has_key?("artist_id").should be_true
290 json.first.has_key?("width").should be_true
294 context '作家権限がないとき' do
299 it 'ステータスコード302 Foundを返す' do
300 get :search, :md5 => 'a'*32
301 response.status.should eq 302
303 it 'サインインページへ遷移する' do
304 get :search, :md5 => 'a'*32
305 response.should redirect_to '/users/sign_in'
309 it 'ステータスコード401 Unauthorizedを返す' do
310 get :search, :md5 => 'a'*32, :format => :json
311 response.status.should eq 401
313 it '応答メッセージにUnauthorizedを返す' do
314 get :search, :md5 => 'a'*32, :format => :json
315 response.message.should match(/Unauthorized/)