OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / spec / controllers / pictures_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #実素材
3 require 'spec_helper'
4
5 describe PicturesController do
6   before 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
15   end
16
17   describe '単体表示に於いて' do
18     before do
19       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
20       sign_in @user
21       Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
22     end
23     context 'つつがなく終わるとき as json' do
24       it 'ステータスコード200 OKを返す' do
25         get :show, :id => @p.id, :format => :json
26         response.should be_success
27       end
28       it '実素材モデルに単体取得を問い合わせている' do
29         Picture.should_receive(:show).exactly(1)
30         get :show, :id => @p.id, :format => :json
31       end
32       it '@pictureにアレを取得している' do
33         get :show, :id => @p.id, :format => :json
34         assigns(:picture).id.should eq(@p.id)
35       end
36       context 'html形式' do
37         it 'showテンプレートを描画する' do
38           get :show, :id => @p.id
39           response.should render_template("show")
40         end
41       end
42       context 'json形式' do
43         it 'jsonデータを返す' do
44           get :show, :id => @p.id, :format => :json
45           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
46         end
47         it 'データがアレになっている' do
48           get :show, :id => @p.id, :format => :json
49           json = JSON.parse response.body
50           json["revision"].should_not be_nil
51           json["ext"].should match(/png/)
52         end
53       end
54       #画像送信では、send_dataにスタブをおいてテストしたいが、ここに噛ませると
55       #renderが働かず、エラーとなってしまう。そこで、素材のファイル取得部分に
56       #スタブをおいてsend_dataがデータを返す体裁でテストする。
57       context 'png形式' do
58         it '画像モデルに画像データを問い合わせる' do
59           Picture.any_instance.should_receive(:restore).exactly(1)
60           get :show, :id => @p.id, :format => :png
61         end
62         it '画像を送信する' do
63           Picture.any_instance.stub(:restore).and_return('aaa')
64           get :show, :id => @p.id, :format => :png
65           response.body.should eq 'aaa'
66         end
67       end
68       context 'gif形式' do
69         it '画像モデルに画像データを問い合わせる' do
70           Picture.any_instance.should_receive(:restore).exactly(1)
71           get :show, :id => @p.id, :format => :gif
72         end
73         it '画像を送信する' do
74           Picture.any_instance.stub(:restore).and_return('bbb')
75           get :show, :id => @p.id, :format => :gif
76           response.body.should eq 'bbb'
77         end
78       end
79       context 'jpeg形式' do
80         it '画像モデルに画像データを問い合わせる' do
81           Picture.any_instance.should_receive(:restore).exactly(1)
82           get :show, :id => @p.id, :format => :jpeg
83         end
84         it '画像を送信する' do
85           Picture.any_instance.stub(:restore).and_return('ccc')
86           get :show, :id => @p.id, :format => :jpeg
87           response.body.should eq 'ccc'
88         end
89       end
90     end
91     context '作家権限がないとき' do
92       before do
93         sign_out @user
94       end
95       context 'html形式' do
96         it 'ステータスコード302 Foundを返す' do
97           get :show, :id => @p.id
98           response.status.should eq 302
99         end
100         it 'サインインページへ遷移する' do
101           get :show, :id => @p.id
102           response.body.should redirect_to '/users/sign_in'
103         end
104       end
105       context 'json形式' do
106         it 'ステータスコード401 Unauthorizedを返す' do
107           get :show, :id => @p.id, :format => :json
108           response.status.should eq 401
109         end
110         it '応答メッセージにUnauthorizedを返す' do
111           get :show, :id => @p.id, :format => :json
112           response.message.should match(/Unauthorized/)
113         end
114       end
115     end
116 =begin
117     context '対象素材がないとき' do
118       before do
119         Picture.unstub(:show)
120       end
121       context 'html形式' do
122         it '例外404 not_foundを返す' do
123           lambda{
124             get :show, :id => 0
125           }.should raise_error(ActiveRecord::RecordNotFound)
126         end
127       end
128       context 'json形式' do
129         it '例外404 not_foundを返す' do
130           lambda{ 
131             get :show, :id => 0, :format => :json
132           }.should raise_error(ActiveRecord::RecordNotFound)
133         end
134       end
135     end
136     context '他人の素材を見ようとしたとき' do
137       before do
138         Picture.stub(:show).and_return(@p)
139         Picture.any_instance.stub(:own?).with(any_args()).and_return(false)
140       end
141       context 'html形式' do
142         it '例外403 forbiddenを返す' do
143           lambda{
144             get :show, :id => @p.id
145           }.should raise_error(ActiveRecord::Forbidden)
146         end
147       end
148       context 'json形式' do
149         it '例外403 forbiddenを返す' do
150           lambda{
151             get :show, :id => @p.id, :format => :json
152           }.should raise_error(ActiveRecord::Forbidden)
153         end
154       end
155     end
156 =end
157   end
158
159   describe 'クレジット表示に於いて' do
160     before do
161       @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
162       sign_in @user
163       Picture.stub(:show).with(@p.id.to_s, @author).and_return(@p)
164     end
165     context 'つつがなく終わるとき' do
166       it 'ステータスコード200 OKを返す' do
167         get :credit, :id => @p.id
168         response.should be_success
169       end
170       it '実素材モデルに単体取得を問い合わせている' do
171         Picture.should_receive(:show).exactly(1)
172         get :credit, :id => @p.id
173       end
174       it '@pictureにアレを取得している' do
175         get :credit, :id => @p.id
176         assigns(:picture).id.should eq(@p.id)
177       end
178       context 'html形式' do
179         it 'creditテンプレートを描画する' do
180           get :credit, :id => @p.id
181           response.should render_template("credit")
182         end
183       end
184       context 'json形式' do
185         it 'jsonデータを返す' do
186           get :credit, :id => @p.id, :format => :json
187           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
188         end
189         it 'データがアレになっている' do
190           get :credit, :id => @p.id, :format => :json
191           json = JSON.parse response.body
192           json["revision"].should_not be_nil
193           json["ext"].should match(/png/)
194         end
195       end
196     end
197     context '作家権限がないとき' do
198       before do
199         sign_out @user
200       end
201       context 'html形式' do
202         it 'ステータスコード302 Foundを返す' do
203           get :credit, :id => @p.id
204           response.status.should eq 302
205         end
206         it 'サインインページへ遷移する' do
207           get :credit, :id => @p.id
208           response.body.should redirect_to '/users/sign_in'
209         end
210       end
211       context 'json形式' do
212         it 'ステータスコード401 Unauthorizedを返す' do
213           get :credit, :id => @p.id, :format => :json
214           response.status.should eq 401
215         end
216         it '応答メッセージにUnauthorizedを返す' do
217           get :credit, :id => @p.id, :format => :json
218           response.message.should match(/Unauthorized/)
219         end
220       end
221     end
222 =begin
223     context '対象素材がないとき' do
224       before do
225         Picture.unstub(:show)
226       end
227       context 'html形式' do
228         it '例外404 not_foundを返す' do
229           lambda{
230             get :show, :id => 0
231           }.should raise_error(ActiveRecord::RecordNotFound)
232         end
233       end
234       context 'json形式' do
235         it '例外404 not_foundを返す' do
236           lambda{ 
237             get :show, :id => 0, :format => :json
238           }.should raise_error(ActiveRecord::RecordNotFound)
239         end
240       end
241     end
242 =end
243   end
244
245 end