OSDN Git Service

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