OSDN Git Service

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