OSDN Git Service

pass test
[pettanr/pettanr.git] / spec / controllers / common_licenses_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #コモンライセンス
3 require 'spec_helper'
4
5 describe CommonLicensesController do
6   before do
7     @admin = Factory :admin
8     @lc = Factory :license
9     @cl = Factory :common_license, :license_id => @lc.id
10     @user = Factory( :user_yas)
11     @author = @user.author
12     @artist = Factory :artist_yas, :author_id => @author.id
13   end
14 =begin
15   describe '一覧表示に於いて' do
16     before do
17       sign_in @admin
18       sign_in @user
19       CommonLicense.stub(:list).and_return([@cl, @cl, @cl])
20     end
21     context 'パラメータpageについて' do
22       it '@pageに値が入る' do
23         get :index, :page => 5
24         assigns(:page).should eq 5
25       end
26       it '省略されると@pageに1値が入る' do
27         get :index
28         assigns(:page).should eq 1
29       end
30       it '与えられたpage_sizeがセットされている' do
31         get :index, :page_size => 15
32         assigns(:page_size).should eq 15
33       end
34       it '省略されると@page_sizeにデフォルト値が入る' do
35         get :index
36         assigns(:page_size).should eq CommonLicense.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq CommonLicense.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq CommonLicense.default_page_size
45       end
46     end
47     context 'つつがなく終わるとき' do
48       it 'ステータスコード200 OKを返す' do
49         get :index
50         response.should be_success 
51       end
52       it 'コモンライセンスモデルに一覧を問い合わせている' do
53         CommonLicense.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@common_licensesにリストを取得している' do
57         get :index
58         assigns(:common_licenses).should have_at_least(3).items
59       end
60       context 'html形式' do
61         it 'indexテンプレートを描画する' do
62           get :index
63           response.should render_template("index")
64         end
65       end
66       context 'json形式' do
67         it 'jsonデータを返す' do
68           get :index, :format => :json
69           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
70         end
71         it 'データがリスト構造になっている' do
72           get :index, :format => :json
73           json = JSON.parse response.body
74           json.should have_at_least(3).items
75         end
76         it 'リストの先頭くらいはコモンライセンスっぽいものであって欲しい' do
77           get :index, :format => :json
78           json = JSON.parse response.body
79           json.first.has_key?("url").should be_true
80         end
81       end
82     end
83     context '作家権限がないとき' do
84       before do
85         sign_out @user
86       end
87       context 'html形式' do
88         it 'ステータスコード302 Foundを返す' do
89           get :index
90           response.status.should eq 302
91         end
92         it 'サインインページへ遷移する' do
93           get :index
94           response.should redirect_to '/users/sign_in'
95         end
96       end
97       context 'json形式' do
98         it 'ステータスコード401 Unauthorizedを返す' do
99           get :index, :format => :json
100           response.status.should eq 401
101         end
102         it '応答メッセージにUnauthorizedを返す' do
103           get :index, :format => :json
104           response.message.should match(/Unauthorized/)
105         end
106       end
107     end
108     context '管理者権限がないとき' do
109       before do
110         sign_out @admin
111       end
112       context 'html形式' do
113         it 'ステータスコード200 OKを返す' do
114           get :index
115           response.should be_success 
116         end
117       end
118       context 'json形式' do
119         it 'ステータスコード200 OKを返す' do
120           get :index, :format => :json
121           response.should be_success 
122         end
123       end
124     end
125   end
126   
127   describe '単体表示に於いて' do
128     before do
129       sign_in @admin
130       sign_in @user
131       CommonLicense.stub(:show).and_return(@cl)
132     end
133     context 'つつがなく終わるとき' do
134       it 'ステータスコード200 OKを返す' do
135         get :show, :id => @cl.id
136         response.should be_success
137       end
138       it 'コモンライセンスモデルに単体取得を問い合わせている' do
139         CommonLicense.should_receive(:show).exactly(1)
140         get :show
141       end
142       it '@common_licenseにアレを取得している' do
143         get :show, :id => @cl.id
144         assigns(:common_license).id.should eq(@cl.id)
145       end
146       context 'html形式' do
147         it 'showテンプレートを描画する' do
148           get :show, :id => @cl.id
149           response.should render_template("show")
150         end
151       end
152       context 'json形式' do
153         it 'jsonデータを返す' do
154           get :show, :id => @cl.id, :format => :json
155           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
156         end
157         it 'データがアレになっている' do
158           get :show, :id => @cl.id, :format => :json
159           json = JSON.parse response.body
160           json["name"].should match(/peta/)
161         end
162       end
163     end
164     context '作家権限がないとき' do
165       before do
166         sign_out @user
167       end
168       context 'html形式' do
169         it 'ステータスコード302 Foundを返す' do
170           get :show, :id => @cl.id
171           response.status.should eq 302
172         end
173         it 'サインインページへ遷移する' do
174           get :show, :id => @cl.id
175           response.body.should redirect_to '/users/sign_in'
176         end
177       end
178       context 'json形式' do
179         it 'ステータスコード401 Unauthorizedを返す' do
180           get :show, :id => @cl.id, :format => :json
181           response.status.should eq 401
182         end
183         it '応答メッセージにUnauthorizedを返す' do
184           get :show, :id => @cl.id, :format => :json
185           response.message.should match(/Unauthorized/)
186         end
187       end
188     end
189     context '管理者権限がないとき' do
190       before do
191         sign_out @admin
192       end
193       context 'html形式' do
194         it 'ステータスコード200 OKを返す' do
195           get :show, :id => @cl.id
196           response.should be_success
197         end
198       end
199       context 'json形式' do
200         it 'ステータスコード200 OKを返す' do
201           get :show, :id => @cl.id, :format => :json
202           response.should be_success
203         end
204       end
205     end
206   end
207 =end
208   
209   describe 'インポートに於いて' do
210     before do
211       sign_in @admin
212       sign_in @user
213       #テストデータを用意してね
214       @f = Rails.root + 'spec/json/common_license.json'
215       @t = File.open(@f, 'r').read
216       @j = JSON.parse @t
217       @fs = Rails.root + 'spec/json/common_licenses.json'
218       @ts = File.open(@fs, 'r').read
219       @js = JSON.parse @ts
220       @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
221       @tes = File.open(@fes, 'r').read
222       @jes = JSON.parse @tes
223     end
224     context '事前チェックしておく' do
225       before do
226         #異常な行を返すから、正常の意味で空を返す
227         CommonLicense.stub(:import).with(any_args()).and_return([])
228       end
229       it "@dataに渡したデータを保持している" do
230         post :import, :file => @t
231         assigns(:data).should_not be_nil
232       end
233       it 'モデルにインポート依頼する' do
234         CommonLicense.should_receive(:import).with(any_args()).exactly(1)
235         post :import, :file => @t
236       end
237       it "@errorsに結果を保持している" do
238         post :import, :file => @t
239         assigns(:errors).should eq []
240       end
241     end
242     context 'つつがなく終わるとき' do
243       before do
244       end
245       context 'html形式' do
246         it 'ステータスコード302 Foundを返す' do
247           post :import, :file => @t
248           response.status.should eq 302
249         end
250         it '管理者向けコモンライセンス一覧ページへ遷移する' do
251           post :import, :file => @t
252           response.should redirect_to('/common_licenses/list')
253         end
254       end
255       context 'json形式' do
256         it 'ステータスコード200 OKを返す' do
257           post :import, :file => @t, :format => :json
258           response.should be_success 
259         end
260       end
261     end
262     context '管理者権限がないとき' do
263       before do
264         sign_out @admin
265       end
266       context 'html形式' do
267         it 'ステータスコード302 Foundを返す' do
268           post :import, :file => @t
269           response.status.should eq 302
270         end
271         it '管理者サインインページへ遷移する' do
272           post :import, :file => @t
273           response.body.should redirect_to '/admins/sign_in'
274         end
275       end
276       context 'json形式' do
277         it 'ステータスコード401 Unauthorizedを返す' do
278           post :import, :file => @t, :format => :json
279           response.status.should eq 401
280         end
281         it '応答メッセージにUnauthorizedを返す' do
282           post :import, :file => @t, :format => :json
283           response.message.should match(/Unauthorized/)
284         end
285       end
286     end
287     context '作家権限がないとき' do
288       #必要なのは管理者権限であって作家権限ではない。成功を見届ける
289       before do
290         sign_out @user
291       end
292       context 'html形式' do
293         it 'ステータスコード302 Foundを返す' do
294           post :import, :file => @t
295           response.status.should eq 302
296         end
297         it '管理者向けコモンライセンス一覧ページへ遷移する' do
298           post :import, :file => @t
299           response.should redirect_to('/common_licenses/list')
300         end
301       end
302       context 'json形式' do
303         it 'ステータスコード200 OKを返す' do
304           post :import, :file => @t, :format => :json
305           response.should be_success 
306         end
307       end
308     end
309     context '検証、保存に失敗した' do
310       before do
311         #異常な行を返す
312         CommonLicense.stub(:import).with(any_args()).and_return(
313           [CommonLicense.new(Factory.attributes_for(:common_license))]
314         )
315       end
316       context 'html形式' do
317         it 'ステータスコード200 OKを返す' do
318           post :import, :file => @t
319           response.status.should eq 200
320         end
321         it 'resultページを描画する' do
322           post :import, :file => @t
323           response.should render_template("result")
324         end
325       end
326       context 'json形式' do
327         it 'ステータスコード422 unprocessable_entity を返す' do
328           post :import, :file => @t, :format => :json
329           response.status.should eq 422
330         end
331         it '応答メッセージUnprocessable Entityを返す' do
332           post :import, :file => @t, :format => :json
333           response.message.should match(/Unprocessable/)
334         end
335       end
336     end
337   end
338 end