1 # -*- encoding: utf-8 -*-
5 describe PicturesController do
8 @sp = Factory :system_picture
9 @lg = Factory :license_group
10 @license = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11 @user = Factory( :user_yas)
12 @author = @user.author
13 @artist = Factory :artist_yas, :author_id => @author.id
14 @op = Factory :original_picture, :artist_id => @artist.id
17 describe '単体表示に於いて' do
19 @p = Factory :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)
38 get :show, :id => @p.id, :format => :json
39 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
42 get :show, :id => @p.id, :format => :json
43 json = JSON.parse response.body
44 json["ext"].should match(/png/)
47 #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
48 #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
49 #スタブをおいてsend_dataがデータを返す体裁でテストする。
51 it '画像モデルに画像データを問い合わせる' do
52 Picture.any_instance.should_receive(:restore).exactly(1)
53 get :show, :id => @p.id, :format => :png
56 Picture.any_instance.stub(:restore).and_return('aaa')
57 get :show, :id => @p.id, :format => :png
58 response.body.should eq 'aaa'
62 it '画像モデルに画像データを問い合わせる' do
63 Picture.any_instance.should_receive(:restore).exactly(1)
64 get :show, :id => @p.id, :format => :gif
67 Picture.any_instance.stub(:restore).and_return('bbb')
68 get :show, :id => @p.id, :format => :gif
69 response.body.should eq 'bbb'
73 it '画像モデルに画像データを問い合わせる' do
74 Picture.any_instance.should_receive(:restore).exactly(1)
75 get :show, :id => @p.id, :format => :jpeg
78 Picture.any_instance.stub(:restore).and_return('ccc')
79 get :show, :id => @p.id, :format => :jpeg
80 response.body.should eq 'ccc'
84 context '作家権限がないとき' do
89 it 'ステータスコード302 Foundを返す' do
90 get :show, :id => @p.id
91 response.status.should eq 302
94 get :show, :id => @p.id
95 response.body.should redirect_to '/users/sign_in'
99 it 'ステータスコード401 Unauthorizedを返す' do
100 get :show, :id => @p.id, :format => :json
101 response.status.should eq 401
103 it '応答メッセージにUnauthorizedを返す' do
104 get :show, :id => @p.id, :format => :json
105 response.message.should match(/Unauthorized/)
110 context '対象素材がないとき' do
112 Picture.unstub(:show)
115 it '例外404 not_foundを返す' do
118 }.should raise_error(ActiveRecord::RecordNotFound)
122 it '例外404 not_foundを返す' do
124 get :show, :id => 0, :format => :json
125 }.should raise_error(ActiveRecord::RecordNotFound)
129 context '他人の素材を見ようとしたとき' do
131 Picture.stub(:show).and_return(@p)
132 Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
135 it '例外403 forbiddenを返す' do
137 get :show, :id => @p.id
138 }.should raise_error(ActiveRecord::Forbidden)
142 it '例外403 forbiddenを返す' do
144 get :show, :id => @p.id, :format => :json
145 }.should raise_error(ActiveRecord::Forbidden)
152 describe 'クレジット表示に於いて' do
\r
154 @p = Factory :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
156 Picture.stub(:show).and_return(@p)
158 context 'つつがなく終わるとき' do
\r
159 it 'ステータスコード200 OKを返す' do
\r
160 get :credit, :id => @p.id
\r
161 response.should be_success
\r
163 it '実素材モデルに単体取得を問い合わせている' do
\r
164 Picture.should_receive(:show).exactly(1)
\r
165 get :credit, :id => @p.id
\r
167 it '@pictureにアレを取得している' do
\r
168 get :credit, :id => @p.id
\r
169 assigns(:picture).id.should eq(@p.id)
\r
171 context 'html形式' do
\r
172 it 'creditテンプレートを描画する' do
\r
173 get :credit, :id => @p.id
\r
174 response.should render_template("credit")
\r
177 context 'json形式' do
\r
179 get :credit, :id => @p.id, :format => :json
\r
180 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
\r
182 it 'データがアレになっている' do
\r
183 get :credit, :id => @p.id, :format => :json
\r
184 json = JSON.parse response.body
\r
185 json["ext"].should match(/png/)
\r
189 context '作家権限がないとき' do
\r
193 context 'html形式' do
\r
194 it 'ステータスコード302 Foundを返す' do
\r
195 get :credit, :id => @p.id
\r
196 response.status.should eq 302
\r
198 it 'サインインページへ遷移する' do
\r
199 get :credit, :id => @p.id
\r
200 response.body.should redirect_to '/users/sign_in'
\r
203 context 'json形式' do
\r
204 it 'ステータスコード401 Unauthorizedを返す' do
\r
205 get :credit, :id => @p.id, :format => :json
\r
206 response.status.should eq 401
\r
208 it '応答メッセージにUnauthorizedを返す' do
\r
209 get :credit, :id => @p.id, :format => :json
\r
210 response.message.should match(/Unauthorized/)
\r
215 context '対象素材がないとき' do
\r
217 Picture.unstub(:show)
\r
219 context 'html形式' do
\r
220 it '例外404 not_foundを返す' do
\r
222 get :show, :id => 0
\r
223 }.should raise_error(ActiveRecord::RecordNotFound)
\r
226 context 'json形式' do
\r
227 it '例外404 not_foundを返す' do
\r
229 get :show, :id => 0, :format => :json
\r
230 }.should raise_error(ActiveRecord::RecordNotFound)
\r