OSDN Git Service

t#30328:create op import ...and pull
[pettanr/pettanr.git] / spec / controllers / system_pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #システム画像
3 require 'spec_helper'
4
5 describe SystemPicturesController do
6   before 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
14   end
15   
16 if MagicNumber['run_mode'] == 1
17   describe '一覧表示に於いて' do
18     before do
19       sign_in @user
20       sign_in @admin
21       SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
22     end
23     context 'パラメータpageについて' do
24       it '@pageに値が入る' do
25         get :index, :page => 5
26         assigns(:page).should eq 5
27       end
28       it '省略されると@pageに1値が入る' do
29         get :index
30         assigns(:page).should eq 1
31       end
32       it '与えられたpage_sizeがセットされている' do
33         get :index, :page_size => 15
34         assigns(:page_size).should eq 15
35       end
36       it '省略されると@page_sizeにデフォルト値が入る' do
37         get :index
38         assigns(:page_size).should eq SystemPicture.default_page_size
39       end
40       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
41         get :index, :page_size => 1500
42         assigns(:page_size).should eq SystemPicture.max_page_size
43       end
44       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
45         get :index, :page_size => 0
46         assigns(:page_size).should eq SystemPicture.default_page_size
47       end
48     end
49     context 'つつがなく終わるとき' do
50       it 'システム画像モデルに一覧を問い合わせている' do
51         SystemPicture.should_receive(:list).exactly(1)
52         get :index
53       end
54       it '@system_picturesにリストを取得している' do
55         get :index
56         assigns(:system_pictures).should have_at_least(3).items
57       end
58       context 'html形式' do
59         it 'ステータスコード200 OKを返す' do
60           get :index
61           response.should be_success 
62         end
63         it 'indexテンプレートを描画する' do
64           get :index
65           response.should render_template("index")
66         end
67       end
68       context 'json形式' do
69         it 'ステータスコード200 OKを返す' do
70           get :index, :format => :json
71           response.should be_success 
72         end
73         it 'jsonデータを返す' do
74           get :index, :format => :json
75           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
76         end
77         it 'システム画像モデルにjson一覧出力オプションを問い合わせている' do
78           SystemPicture.should_receive(:list_json_opt).exactly(1)
79           get :index, :format => :json
80         end
81         it 'データがリスト構造になっている' do
82           get :index, :format => :json
83           json = JSON.parse response.body
84           json.should have_at_least(3).items
85         end
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
91         end
92       end
93     end
94     context '作家権限がないとき' do
95       before do
96         sign_out @user
97       end
98       context 'html形式' do
99         it 'ステータスコード302 Foundを返す' do
100           get :index
101           response.status.should eq 302
102         end
103         it 'サインインページへ遷移する' do
104           get :index
105           response.should redirect_to '/users/sign_in'
106         end
107       end
108       context 'json形式' do
109         it 'ステータスコード401 Unauthorizedを返す' do
110           get :index, :format => :json
111           response.status.should eq 401
112         end
113         it '応答メッセージにUnauthorizedを返す' do
114           get :index, :format => :json
115           response.message.should match(/Unauthorized/)
116         end
117       end
118     end
119   end
120   
121   describe '単体表示に於いて' do
122     before do
123       sign_in @user
124       SystemPicture.stub(:show).and_return(@sp)
125     end
126     context 'つつがなく終わるとき' do
127       it 'ステータスコード200 OKを返す' do
128         get :show, :id => @sp.id
129         response.should be_success
130       end
131       it 'システム画像モデルに単体取得を問い合わせている' do
132         SystemPicture.should_receive(:show).exactly(1)
133         get :show
134       end
135       it '@system_pictureにアレを取得している' do
136         get :show, :id => @sp.id
137         assigns(:system_picture).should eq @sp
138       end
139       context 'html形式' do
140         it 'showテンプレートを描画する' do
141           get :show, :id => @sp.id
142           response.should render_template("show")
143         end
144       end
145       context 'json形式' do
146         it 'jsonデータを返す' do
147           get :show, :id => @sp.id, :format => :json
148           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
149         end
150         it 'システム画像モデルにjson単体出力オプションを問い合わせている' do
151           SystemPicture.should_receive(:show_json_opt).exactly(1)
152           get :show, :id => @sp.id, :format => :json
153         end
154         it 'データがアレになっている' do
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
160         end
161       end
162       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
163       #renderが働かず、エラーとなってしまう。そこで、システム画像のファイル取得部分に
164       #スタブをおいてsend_dataがデータを返す体裁でテストする。
165       context 'png形式' do
166         before do
167           SystemPicture.any_instance.stub(:mime_type).and_return('image/png')
168           SystemPicture.any_instance.stub(:restore).and_return('aaa')
169         end
170         it '画像モデルに画像データを問い合わせる' do
171           SystemPicture.any_instance.should_receive(:restore).exactly(1)
172           get :show, :id => @sp.id, :format => :png
173         end
174         it '画像モデルにMimeTypeを問い合わせる' do
175           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
176           get :show, :id => @sp.id, :format => :png
177         end
178         it '画像を送信する' do
179           get :show, :id => @sp.id, :format => :png
180           response.body.should eq 'aaa'
181         end
182       end
183       context 'gif形式' do
184         before do
185           SystemPicture.any_instance.stub(:mime_type).and_return('image/gif')
186           SystemPicture.any_instance.stub(:restore).and_return('bbb')
187         end
188         it '画像モデルに画像データを問い合わせる' do
189           SystemPicture.any_instance.should_receive(:restore).exactly(1)
190           get :show, :id => @sp.id, :format => :gif
191         end
192         it '画像モデルにMimeTypeを問い合わせる' do
193           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
194           get :show, :id => @sp.id, :format => :gif
195         end
196         it '画像を送信する' do
197           get :show, :id => @sp.id, :format => :gif
198           response.body.should eq 'bbb'
199         end
200       end
201       context 'jpeg形式' do
202         before do
203           SystemPicture.any_instance.stub(:mime_type).and_return('image/jpeg')
204           SystemPicture.any_instance.stub(:restore).and_return('ccc')
205         end
206         it '画像モデルに画像データを問い合わせる' do
207           SystemPicture.any_instance.should_receive(:restore).exactly(1)
208           get :show, :id => @sp.id, :format => :jpeg
209         end
210         it '画像モデルにMimeTypeを問い合わせる' do
211           SystemPicture.any_instance.should_receive(:mime_type).exactly(1)
212           get :show, :id => @sp.id, :format => :jpeg
213         end
214         it '画像を送信する' do
215           get :show, :id => @sp.id, :format => :jpeg
216           response.body.should eq 'ccc'
217         end
218       end
219     end
220     context '作家権限がないとき' do
221       before do
222         sign_out @user
223       end
224       context 'html形式' do
225         it 'ステータスコード302 Foundを返す' do
226           get :show, :id => @sp.id
227           response.status.should eq 302
228         end
229         it 'サインインページへ遷移する' do
230           get :show, :id => @sp.id
231           response.body.should redirect_to '/users/sign_in'
232         end
233       end
234       context 'json形式' do
235         it 'ステータスコード401 Unauthorizedを返す' do
236           get :show, :id => @sp.id, :format => :json
237           response.status.should eq 401
238         end
239         it '応答メッセージにUnauthorizedを返す' do
240           get :show, :id => @sp.id, :format => :json
241           response.message.should match(/Unauthorized/)
242         end
243       end
244     end
245 =begin
246     context '対象システム画像がないとき' do
247       before do
248         SystemPicture.unstub(:show)
249       end
250       context 'html形式' do
251         it '例外404 not_foundを返す' do
252           lambda{
253             get :show, :id => 0
254           }.should raise_error(ActiveRecord::RecordNotFound)
255         end
256       end
257       context 'json形式' do
258         it '例外404 not_foundを返す' do
259           lambda{ 
260             get :show, :id => 0, :format => :json
261           }.should raise_error(ActiveRecord::RecordNotFound)
262         end
263       end
264     end
265     context '他人のシステム画像を見ようとしたとき' do
266       before do
267         SystemPicture.stub(:show).and_return(@sp)
268         SystemPicture.any_instance.stub(:own?).with(any_args()).and_return(false)
269       end
270       context 'html形式' do
271         it '例外403 forbiddenを返す' do
272           lambda{
273             get :show, :id => @sp.id
274           }.should raise_error(ActiveRecord::Forbidden)
275         end
276       end
277       context 'json形式' do
278         it '例外403 forbiddenを返す' do
279           lambda{
280             get :show, :id => @sp.id, :format => :json
281           }.should raise_error(ActiveRecord::Forbidden)
282         end
283       end
284     end
285 =end
286   end
287   
288 else
289   describe '一覧表示に於いて' do
290     before do
291       sign_in @user
292       sign_in @admin
293       SystemPicture.stub(:list).and_return([@sp, @sp, @sp])
294     end
295     context 'つつがなく終わるとき' do
296       context 'html形式' do
297         it 'ステータスコード200 OKを返す' do
298           get :index
299           response.should be_success 
300         end
301         it 'indexテンプレートを描画する' do
302           get :index
303           response.should render_template("index")
304         end
305       end
306       context 'json形式' do
307         it 'ステータスコード200 OKを返す' do
308           get :index, :format => :json
309           response.should be_success 
310         end
311         it 'jsonデータを返す' do
312           get :index, :format => :json
313           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
314         end
315       end
316     end
317     context '作家権限がないとき' do
318       before do
319         sign_out @user
320       end
321       context 'html形式' do
322         it 'ステータスコード200 OKを返す' do
323           get :index
324           response.should be_success 
325         end
326         it 'indexテンプレートを描画する' do
327           get :index
328           response.should render_template("index")
329         end
330       end
331       context 'json形式' do
332         it 'ステータスコード200 OKを返す' do
333           get :index, :format => :json
334           response.should be_success 
335         end
336         it 'jsonデータを返す' do
337           get :index, :format => :json
338           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
339         end
340       end
341     end
342   end
343   
344   describe '単体表示に於いて' do
345     before do
346       sign_in @user
347       SystemPicture.stub(:show).and_return(@sp)
348     end
349     context 'つつがなく終わるとき' do
350       it 'ステータスコード200 OKを返す' do
351         get :show, :id => @sp.id
352         response.should be_success
353       end
354       context 'html形式' do
355         it 'showテンプレートを描画する' do
356           get :show, :id => @sp.id
357           response.should render_template("show")
358         end
359       end
360       context 'json形式' do
361         it 'jsonデータを返す' do
362           get :show, :id => @sp.id, :format => :json
363           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
364         end
365       end
366     end
367     context '作家権限がないとき' do
368       before do
369         sign_out @user
370       end
371       it 'ステータスコード200 OKを返す' do
372         get :show, :id => @sp.id
373         response.should be_success
374       end
375       context 'html形式' do
376         it 'showテンプレートを描画する' do
377           get :show, :id => @sp.id
378           response.should render_template("show")
379         end
380       end
381       context 'json形式' do
382         it 'jsonデータを返す' do
383           get :show, :id => @sp.id, :format => :json
384           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
385         end
386       end
387     end
388   end
389 end
390 end