1 # -*- encoding: utf-8 -*-
5 describe PicturesController 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
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).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["ext"].should match(/png/)
53 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
54 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
55 #スタブをおいてsend_dataがデータを返す体裁でテストする。
57 it '画像モデルに画像データを問い合わせる' do
58 Picture.any_instance.should_receive(:restore).exactly(1)
59 get :show, :id => @p.id, :format => :png
62 Picture.any_instance.stub(:restore).and_return('aaa')
63 get :show, :id => @p.id, :format => :png
64 response.body.should eq 'aaa'
68 it '画像モデルに画像データを問い合わせる' do
69 Picture.any_instance.should_receive(:restore).exactly(1)
70 get :show, :id => @p.id, :format => :gif
73 Picture.any_instance.stub(:restore).and_return('bbb')
74 get :show, :id => @p.id, :format => :gif
75 response.body.should eq 'bbb'
79 it '画像モデルに画像データを問い合わせる' do
80 Picture.any_instance.should_receive(:restore).exactly(1)
81 get :show, :id => @p.id, :format => :jpeg
84 Picture.any_instance.stub(:restore).and_return('ccc')
85 get :show, :id => @p.id, :format => :jpeg
86 response.body.should eq 'ccc'
90 context '作家権限がないとき' do
95 it 'ステータスコード302 Foundを返す' do
96 get :show, :id => @p.id
97 response.status.should eq 302
100 get :show, :id => @p.id
101 response.body.should redirect_to '/users/sign_in'
105 it 'ステータスコード401 Unauthorizedを返す' do
106 get :show, :id => @p.id, :format => :json
107 response.status.should eq 401
109 it '応答メッセージにUnauthorizedを返す' do
110 get :show, :id => @p.id, :format => :json
111 response.message.should match(/Unauthorized/)
116 context '対象素材がないとき' do
118 Picture.unstub(:show)
121 it '例外404 not_foundを返す' do
124 }.should raise_error(ActiveRecord::RecordNotFound)
128 it '例外404 not_foundを返す' do
130 get :show, :id => 0, :format => :json
131 }.should raise_error(ActiveRecord::RecordNotFound)
135 context '他人の素材を見ようとしたとき' do
137 Picture.stub(:show).and_return(@p)
138 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
141 it '例外403 forbiddenを返す' do
143 get :show, :id => @p.id
144 }.should raise_error(ActiveRecord::Forbidden)
148 it '例外403 forbiddenを返す' do
150 get :show, :id => @p.id, :format => :json
151 }.should raise_error(ActiveRecord::Forbidden)
158 describe 'クレジット表示に於いて' do
160 @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
162 Picture.stub(:show).and_return(@p)
164 context 'つつがなく終わるとき' do
165 it 'ステータスコード200 OKを返す' do
166 get :credit, :id => @p.id
167 response.should be_success
169 it '実素材モデルに単体取得を問い合わせている' do
170 Picture.should_receive(:show).exactly(1)
171 get :credit, :id => @p.id
173 it '@pictureにアレを取得している' do
174 get :credit, :id => @p.id
175 assigns(:picture).id.should eq(@p.id)
178 it 'creditテンプレートを描画する' do
179 get :credit, :id => @p.id
180 response.should render_template("credit")
185 get :credit, :id => @p.id, :format => :json
186 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
189 get :credit, :id => @p.id, :format => :json
190 json = JSON.parse response.body
191 json["ext"].should match(/png/)
195 context '作家権限がないとき' do
200 it 'ステータスコード302 Foundを返す' do
201 get :credit, :id => @p.id
202 response.status.should eq 302
204 it 'サインインページへ遷移する' do
205 get :credit, :id => @p.id
206 response.body.should redirect_to '/users/sign_in'
210 it 'ステータスコード401 Unauthorizedを返す' do
211 get :credit, :id => @p.id, :format => :json
212 response.status.should eq 401
214 it '応答メッセージにUnauthorizedを返す' do
215 get :credit, :id => @p.id, :format => :json
216 response.message.should match(/Unauthorized/)
221 context '対象素材がないとき' do
223 Picture.unstub(:show)
226 it '例外404 not_foundを返す' do
229 }.should raise_error(ActiveRecord::RecordNotFound)
233 it '例外404 not_foundを返す' do
235 get :show, :id => 0, :format => :json
236 }.should raise_error(ActiveRecord::RecordNotFound)