OSDN Git Service

pass test
[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     Factory :admin
8     @license = Factory :license
9     @user = Factory( :user_yas)
10     @author = @user.author
11     @artist = Factory :artist_yas, :author_id => @author.id
12     @op = Factory :original_picture, :artist_id => @artist.id , :license_id => @license.id
13   end
14
15   describe '一覧表示に於いて' do
16     before do
17       @rp = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
18       sign_in @user
19       ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
20     end
21     context 'パラメータpageについて' do
22       it '@pageに値が入る' do
23         get :index, :page => 5
24         assigns(:page).should eq 5
25       end
26       it '省略されると@pageに1値が入る' do
27         get :index
28         assigns(:page).should eq 1
29       end
30       it '与えられたpage_sizeがセットされている' do
31         get :index, :page_size => 15
32         assigns(:page_size).should eq 15
33       end
34       it '省略されると@page_sizeにデフォルト値が入る' do
35         get :index
36         assigns(:page_size).should eq ResourcePicture.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq ResourcePicture.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq ResourcePicture.default_page_size
45       end
46     end
47     context 'つつがなく終わるとき' do
48       it 'ステータスコード200 OKを返す' do
49         get :index
50         response.should be_success 
51       end
52       it '素材モデルに一覧を問い合わせている' do
53         ResourcePicture.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@resource_picturesにリストを取得している' do
57         get :index
58         assigns(:resource_pictures).should have_at_least(3).items
59       end
60       context 'html形式' do
61         it 'indexテンプレートを描画する' do
62           get :index
63           response.should render_template("index")
64         end
65       end
66       context 'json形式' do
67         it 'jsonデータを返す' do
68           get :index, :format => :json
69           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
70         end
71         it 'データがリスト構造になっている' do
72           get :index, :format => :json
73           json = JSON.parse response.body
74           json.should have_at_least(3).items
75         end
76         it 'リストの先頭くらいは素材っぽいものであって欲しい' do
77           get :index, :format => :json
78           json = JSON.parse response.body
79           json.first.has_key?("ext").should be_true
80         end
81       end
82     end
83     context '作家権限がないとき' do
84       before do
85         sign_out @user
86       end
87       context 'html形式' do
88         it 'ステータスコード302 Foundを返す' do
89           get :index
90           response.status.should eq 302
91         end
92         it 'サインインページへ遷移する' do
93           get :index
94           response.should redirect_to '/users/sign_in'
95         end
96       end
97       context 'json形式' do
98         it 'ステータスコード401 Unauthorizedを返す' do
99           get :index, :format => :json
100           response.status.should eq 401
101         end
102         it '応答メッセージにUnauthorizedを返す' do
103           get :index, :format => :json
104           response.message.should match(/Unauthorized/)
105         end
106       end
107     end
108   end
109   
110   describe '単体表示に於いて' do
111     before do
112       @pic = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
113       sign_in @user
114       ResourcePicture.stub(:show).and_return(@pic)
115     end
116     context 'つつがなく終わるとき' do
117       it 'ステータスコード200 OKを返す' do
118         get :show, :id => @pic.id
119         response.should be_success
120       end
121       it '素材モデルに単体取得を問い合わせている' do
122         ResourcePicture.should_receive(:show).exactly(1)
123         get :show
124       end
125       it '@resource_pictureにアレを取得している' do
126         get :show, :id => @pic.id
127         assigns(:resource_picture).id.should eq(@pic.id)
128       end
129       context 'html形式' do
130         it 'showテンプレートを描画する' do
131           get :show, :id => @pic.id
132           response.should render_template("show")
133         end
134       end
135       context 'json形式' do
136         it 'jsonデータを返す' do
137           get :show, :id => @pic.id, :format => :json
138           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
139         end
140         it 'データがアレになっている' do
141           get :show, :id => @pic.id, :format => :json
142           json = JSON.parse response.body
143           json["ext"].should match(/png/)
144         end
145       end
146       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
147       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
148       #スタブをおいてsend_dataがデータを返す体裁でテストする。
149       context 'png形式' do
150         it '画像モデルに画像データを問い合わせる' do
151           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
152           get :show, :id => @pic.id, :format => :png
153         end
154         it '画像を送信する' do
155           ResourcePicture.any_instance.stub(:restore).and_return('aaa')
156           get :show, :id => @pic.id, :format => :png
157           response.body.should eq 'aaa'
158         end
159       end
160       context 'gif形式' do
161         it '画像モデルに画像データを問い合わせる' do
162           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
163           get :show, :id => @pic.id, :format => :gif
164         end
165         it '画像を送信する' do
166           ResourcePicture.any_instance.stub(:restore).and_return('bbb')
167           get :show, :id => @pic.id, :format => :gif
168           response.body.should eq 'bbb'
169         end
170       end
171       context 'jpeg形式' do
172         it '画像モデルに画像データを問い合わせる' do
173           ResourcePicture.any_instance.should_receive(:restore).exactly(1)
174           get :show, :id => @pic.id, :format => :jpeg
175         end
176         it '画像を送信する' do
177           ResourcePicture.any_instance.stub(:restore).and_return('ccc')
178           get :show, :id => @pic.id, :format => :jpeg
179           response.body.should eq 'ccc'
180         end
181       end
182     end
183     context '作家権限がないとき' do
184       before do
185         sign_out @user
186       end
187       context 'html形式' do
188         it 'ステータスコード302 Foundを返す' do
189           get :show, :id => @pic.id
190           response.status.should eq 302
191         end
192         it 'サインインページへ遷移する' do
193           get :show, :id => @pic.id
194           response.body.should redirect_to '/users/sign_in'
195         end
196       end
197       context 'json形式' do
198         it 'ステータスコード401 Unauthorizedを返す' do
199           get :show, :id => @pic.id, :format => :json
200           response.status.should eq 401
201         end
202         it '応答メッセージにUnauthorizedを返す' do
203           get :show, :id => @pic.id, :format => :json
204           response.message.should match(/Unauthorized/)
205         end
206       end
207     end
208 =begin
209     context '対象素材がないとき' do
210       before do
211         ResourcePicture.unstub(:show)
212       end
213       context 'html形式' do
214         it '例外404 not_foundを返す' do
215           lambda{
216             get :show, :id => 0
217           }.should raise_error(ActiveRecord::RecordNotFound)
218         end
219       end
220       context 'json形式' do
221         it '例外404 not_foundを返す' do
222           lambda{ 
223             get :show, :id => 0, :format => :json
224           }.should raise_error(ActiveRecord::RecordNotFound)
225         end
226       end
227     end
228     context '他人の素材を見ようとしたとき' do
229       before do
230         ResourcePicture.stub(:show).and_return(@pic)
231         ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
232       end
233       context 'html形式' do
234         it '例外403 forbiddenを返す' do
235           lambda{
236             get :show, :id => @pic.id
237           }.should raise_error(ActiveRecord::Forbidden)
238         end
239       end
240       context 'json形式' do
241         it '例外403 forbiddenを返す' do
242           lambda{
243             get :show, :id => @pic.id, :format => :json
244           }.should raise_error(ActiveRecord::Forbidden)
245         end
246       end
247     end
248 =end
249   end
250
251   describe '素材数取得に於いて' do
252     before do
253       ResourcePicture.should_receive(:visible_count).and_return(3)
254 #      sign_in @user
255     end
256     context 'つつがなく終わるとき' do
257       it 'ステータスコード200 OKを返す' do
258         get :count, :format => :json
259         response.should be_success 
260       end
261       context 'json形式' do
262         it 'jsonデータを返す' do
263           get :count, :format => :json
264           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
265         end
266         it 'データがHash構造になっていてコマ絵数が3である' do
267           get :count, :format => :json
268           json = JSON.parse response.body
269           json["count"].should == 3
270         end
271       end
272     end
273   end
274
275 end