OSDN Git Service

033cb5cf6f84d23617b5033754dfe4d9481bd1a8
[pettanr/pettanr.git] / spec / controllers / resource_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #素材
3 require 'spec_helper'
4
5 describe ResourcePicturesController do
6   before 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
15   end
16
17   describe '一覧表示に於いて' do
18     before 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
21       sign_in @user
22       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
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 ResourcePicture.default_page_size
40       end
41       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
42         get :index, :page_size => 1500
43         assigns(:page_size).should eq ResourcePicture.max_page_size
44       end
45       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
46         get :index, :page_size => 0
47         assigns(:page_size).should eq ResourcePicture.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         ResourcePicture.should_receive(:list).exactly(1)
57         get :index
58       end
59       it '@resource_picturesにリストを取得している' do
60         get :index
61         assigns(:resource_pictures).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 'データがリスト構造になっている' do
75           get :index, :format => :json
76           json = JSON.parse response.body
77           json.should have_at_least(3).items
78         end
79         it 'リストの先頭くらいは素材っぽいものであって欲しい' do
80           get :index, :format => :json
81           json = JSON.parse response.body
82           json.first.has_key?("ext").should be_true
83         end
84       end
85     end
86     context '作家権限がないとき' do
87       before do
88         sign_out @user
89       end
90       context 'html形式' do
91         it 'ステータスコード302 Foundを返す' do
92           get :index
93           response.status.should eq 302
94         end
95         it 'サインインページへ遷移する' do
96           get :index
97           response.should redirect_to '/users/sign_in'
98         end
99       end
100       context 'json形式' do
101         it 'ステータスコード401 Unauthorizedを返す' do
102           get :index, :format => :json
103           response.status.should eq 401
104         end
105         it '応答メッセージにUnauthorizedを返す' do
106           get :index, :format => :json
107           response.message.should match(/Unauthorized/)
108         end
109       end
110     end
111   end
112   
113   describe '単体表示に於いて' do
114     before 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
117       sign_in @user
118       ResourcePicture.stub(:show).and_return(@pic)
119     end
120     context 'つつがなく終わるとき' do
121       it 'ステータスコード200 OKを返す' do
122         get :show, :id => @pic.id
123         response.should be_success
124       end
125       it '素材モデルに単体取得を問い合わせている' do
126         ResourcePicture.should_receive(:show).exactly(1)
127         get :show
128       end
129       it '@resource_pictureにアレを取得している' do
130         get :show, :id => @pic.id
131         assigns(:resource_picture).id.should eq(@pic.id)
132       end
133       context 'html形式' do
134         it 'showテンプレートを描画する' do
135           get :show, :id => @pic.id
136           response.should render_template("show")
137         end
138       end
139       context 'json形式' do
140         it 'jsonデータを返す' do
141           get :show, :id => @pic.id, :format => :json
142           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
143         end
144         it 'データがアレになっている' do
145           get :show, :id => @pic.id, :format => :json
146           json = JSON.parse response.body
147           json["ext"].should match(/png/)
148         end
149       end
150       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
151       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
152       #スタブをおいてsend_dataがデータを返す体裁でテストする。
153       context 'png形式' do
154         it '画像モデルに画像データを問い合わせる' do
155           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
156           get :show, :id => @pic.id, :format => :png
157         end
158         it '画像を送信する' do
159           ResourcePicture.any_instance.stub(:restore).and_return('aaa')
160           get :show, :id => @pic.id, :format => :png
161           response.body.should eq 'aaa'
162         end
163       end
164       context 'gif形式' do
165         it '画像モデルに画像データを問い合わせる' do
166           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
167           get :show, :id => @pic.id, :format => :gif
168         end
169         it '画像を送信する' do
170           ResourcePicture.any_instance.stub(:restore).and_return('bbb')
171           get :show, :id => @pic.id, :format => :gif
172           response.body.should eq 'bbb'
173         end
174       end
175       context 'jpeg形式' do
176         it '画像モデルに画像データを問い合わせる' do
177           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
178           get :show, :id => @pic.id, :format => :jpeg
179         end
180         it '画像を送信する' do
181           ResourcePicture.any_instance.stub(:restore).and_return('ccc')
182           get :show, :id => @pic.id, :format => :jpeg
183           response.body.should eq 'ccc'
184         end
185       end
186     end
187     context '作家権限がないとき' do
188       before do
189         sign_out @user
190       end
191       context 'html形式' do
192         it 'ステータスコード302 Foundを返す' do
193           get :show, :id => @pic.id
194           response.status.should eq 302
195         end
196         it 'サインインページへ遷移する' do
197           get :show, :id => @pic.id
198           response.body.should redirect_to '/users/sign_in'
199         end
200       end
201       context 'json形式' do
202         it 'ステータスコード401 Unauthorizedを返す' do
203           get :show, :id => @pic.id, :format => :json
204           response.status.should eq 401
205         end
206         it '応答メッセージにUnauthorizedを返す' do
207           get :show, :id => @pic.id, :format => :json
208           response.message.should match(/Unauthorized/)
209         end
210       end
211     end
212 =begin
213     context '対象素材がないとき' do
214       before do
215         ResourcePicture.unstub(:show)
216       end
217       context 'html形式' do
218         it '例外404 not_foundを返す' do
219           lambda{
220             get :show, :id => 0
221           }.should raise_error(ActiveRecord::RecordNotFound)
222         end
223       end
224       context 'json形式' do
225         it '例外404 not_foundを返す' do
226           lambda{ 
227             get :show, :id => 0, :format => :json
228           }.should raise_error(ActiveRecord::RecordNotFound)
229         end
230       end
231     end
232     context '他人の素材を見ようとしたとき' do
233       before do
234         ResourcePicture.stub(:show).and_return(@pic)
235         ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
236       end
237       context 'html形式' do
238         it '例外403 forbiddenを返す' do
239           lambda{
240             get :show, :id => @pic.id
241           }.should raise_error(ActiveRecord::Forbidden)
242         end
243       end
244       context 'json形式' do
245         it '例外403 forbiddenを返す' do
246           lambda{
247             get :show, :id => @pic.id, :format => :json
248           }.should raise_error(ActiveRecord::Forbidden)
249         end
250       end
251     end
252 =end
253   end
254
255   describe '素材数取得に於いて' do
256     before do
257       ResourcePicture.should_receive(:visible_count).and_return(3)
258 #      sign_in @user
259     end
260     context 'つつがなく終わるとき' do
261       it 'ステータスコード200 OKを返す' do
262         get :count, :format => :json
263         response.should be_success 
264       end
265       context 'json形式' do
266         it 'jsonデータを返す' do
267           get :count, :format => :json
268           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
269         end
270         it 'データがHash構造になっていてコマ絵数が3である' do
271           get :count, :format => :json
272           json = JSON.parse response.body
273           json["count"].should == 3
274         end
275       end
276     end
277   end
278
279   describe 'クレジット表示に於いて' do\r
280     before 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
283       sign_in @user
284       ResourcePicture.stub(:show).and_return(@pic)
285     end\r
286     context 'つつがなく終わるとき' do\r
287       it 'ステータスコード200 OKを返す' do\r
288         get :credit, :id => @pic.id\r
289         response.should be_success\r
290       end\r
291       it '素材モデルに単体取得を問い合わせている' do\r
292         ResourcePicture.should_receive(:show).exactly(1)\r
293         get :credit, :id => @pic.id\r
294       end\r
295       it '@resource_pictureにアレを取得している' do\r
296         get :credit, :id => @pic.id\r
297         assigns(:resource_picture).id.should eq(@pic.id)\r
298       end\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
303         end\r
304       end\r
305       context 'json形式' do\r
306         it '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
309         end\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
314         end\r
315       end\r
316     end\r
317     context '作家権限がないとき' do\r
318       before do\r
319         sign_out @user\r
320       end\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
325         end\r
326         it 'サインインページへ遷移する' do\r
327           get :credit, :id => @pic.id\r
328           response.body.should redirect_to '/users/sign_in'\r
329         end\r
330       end\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
335         end\r
336         it '応答メッセージにUnauthorizedを返す' do\r
337           get :credit, :id => @pic.id, :format => :json\r
338           response.message.should match(/Unauthorized/)\r
339         end\r
340       end\r
341     end\r
342 =begin\r
343     context '対象素材がないとき' do\r
344       before do\r
345         ResourcePicture.unstub(:show)\r
346       end\r
347       context 'html形式' do\r
348         it '例外404 not_foundを返す' do\r
349           lambda{\r
350             get :show, :id => 0\r
351           }.should raise_error(ActiveRecord::RecordNotFound)\r
352         end\r
353       end\r
354       context 'json形式' do\r
355         it '例外404 not_foundを返す' do\r
356           lambda{ \r
357             get :show, :id => 0, :format => :json\r
358           }.should raise_error(ActiveRecord::RecordNotFound)\r
359         end\r
360       end\r
361     end\r
362 =end\r
363   end\r
364 end