1 # -*- encoding: utf-8 -*-
5 describe ResourcePicturesController do
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
15 describe '一覧表示に於いて' do
17 @rp = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
19 ResourcePicture.stub(:list).and_return([@rp, @rp, @rp])
21 context 'パラメータpageについて' do
23 get :index, :page => 5
24 assigns(:page).should eq 5
26 it '省略されると@pageに1値が入る' do
28 assigns(:page).should eq 1
30 it '与えられたpage_sizeがセットされている' do
31 get :index, :page_size => 15
32 assigns(:page_size).should eq 15
34 it '省略されると@page_sizeにデフォルト値が入る' do
36 assigns(:page_size).should eq ResourcePicture.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq ResourcePicture.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq ResourcePicture.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it '素材モデルに一覧を問い合わせている' do
53 ResourcePicture.should_receive(:list).exactly(1)
56 it '@resource_picturesにリストを取得している' do
58 assigns(:resource_pictures).should have_at_least(3).items
61 it 'indexテンプレートを描画する' do
63 response.should render_template("index")
68 get :index, :format => :json
69 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71 it 'データがリスト構造になっている' do
72 get :index, :format => :json
73 json = JSON.parse response.body
74 json.should have_at_least(3).items
76 it 'リストの先頭くらいは素材っぽいものであって欲しい' do
77 get :index, :format => :json
78 json = JSON.parse response.body
79 json.first.has_key?("ext").should be_true
83 context '作家権限がないとき' do
88 it 'ステータスコード302 Foundを返す' do
90 response.status.should eq 302
94 response.should redirect_to '/users/sign_in'
98 it 'ステータスコード401 Unauthorizedを返す' do
99 get :index, :format => :json
100 response.status.should eq 401
102 it '応答メッセージにUnauthorizedを返す' do
103 get :index, :format => :json
104 response.message.should match(/Unauthorized/)
110 describe '単体表示に於いて' do
112 @pic = Factory :resource_picture, :artist_id => @artist.id , :license_id => @license.id, :original_picture_id => @op.id
114 ResourcePicture.stub(:show).and_return(@pic)
116 context 'つつがなく終わるとき' do
117 it 'ステータスコード200 OKを返す' do
118 get :show, :id => @pic.id
119 response.should be_success
121 it '素材モデルに単体取得を問い合わせている' do
122 ResourcePicture.should_receive(:show).exactly(1)
125 it '@resource_pictureにアレを取得している' do
126 get :show, :id => @pic.id
127 assigns(:resource_picture).id.should eq(@pic.id)
130 it 'showテンプレートを描画する' do
131 get :show, :id => @pic.id
132 response.should render_template("show")
137 get :show, :id => @pic.id, :format => :json
138 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
141 get :show, :id => @pic.id, :format => :json
142 json = JSON.parse response.body
143 json["ext"].should match(/png/)
146 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
147 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
148 #スタブをおいてsend_dataがデータを返す体裁でテストする。
150 it '画像モデルに画像データを問い合わせる' do
151 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
152 get :show, :id => @pic.id, :format => :png
155 ResourcePicture.any_instance.stub(:restore).and_return('aaa')
156 get :show, :id => @pic.id, :format => :png
157 response.body.should eq 'aaa'
161 it '画像モデルに画像データを問い合わせる' do
162 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
163 get :show, :id => @pic.id, :format => :gif
166 ResourcePicture.any_instance.stub(:restore).and_return('bbb')
167 get :show, :id => @pic.id, :format => :gif
168 response.body.should eq 'bbb'
172 it '画像モデルに画像データを問い合わせる' do
173 ResourcePicture.any_instance.should_receive(:restore).exactly(1)
174 get :show, :id => @pic.id, :format => :jpeg
177 ResourcePicture.any_instance.stub(:restore).and_return('ccc')
178 get :show, :id => @pic.id, :format => :jpeg
179 response.body.should eq 'ccc'
183 context '作家権限がないとき' do
188 it 'ステータスコード302 Foundを返す' do
189 get :show, :id => @pic.id
190 response.status.should eq 302
192 it 'サインインページへ遷移する' do
193 get :show, :id => @pic.id
194 response.body.should redirect_to '/users/sign_in'
198 it 'ステータスコード401 Unauthorizedを返す' do
199 get :show, :id => @pic.id, :format => :json
200 response.status.should eq 401
202 it '応答メッセージにUnauthorizedを返す' do
203 get :show, :id => @pic.id, :format => :json
204 response.message.should match(/Unauthorized/)
209 context '対象素材がないとき' do
211 ResourcePicture.unstub(:show)
214 it '例外404 not_foundを返す' do
217 }.should raise_error(ActiveRecord::RecordNotFound)
221 it '例外404 not_foundを返す' do
223 get :show, :id => 0, :format => :json
224 }.should raise_error(ActiveRecord::RecordNotFound)
228 context '他人の素材を見ようとしたとき' do
230 ResourcePicture.stub(:show).and_return(@pic)
231 ResourcePicture.any_instance.stub(:own?).with(any_args()).and_return(false)
234 it '例外403 forbiddenを返す' do
236 get :show, :id => @pic.id
237 }.should raise_error(ActiveRecord::Forbidden)
241 it '例外403 forbiddenを返す' do
243 get :show, :id => @pic.id, :format => :json
244 }.should raise_error(ActiveRecord::Forbidden)
251 describe '素材数取得に於いて' do
253 ResourcePicture.should_receive(:visible_count).and_return(3)
256 context 'つつがなく終わるとき' do
257 it 'ステータスコード200 OKを返す' do
258 get :count, :format => :json
259 response.should be_success
263 get :count, :format => :json
264 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
266 it 'データがHash構造になっていてコマ絵数が3である' do
267 get :count, :format => :json
268 json = JSON.parse response.body
269 json["count"].should == 3