1 # -*- encoding: utf-8 -*-
5 describe CommonLicensesController do
7 @admin = Factory :admin
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
15 describe '一覧表示に於いて' do
19 CommonLicense.stub(:list).and_return([@cl, @cl, @cl])
21 context 'パラメータpageについて' do
23 get :index, :page => 5
24 assigns(:page).should eq 5
26 it '省略されると@pageに1値が入る' do
28 assigns(:page).should eq 1
30 it '与えられたpage_sizeがセットされている' do
31 get :index, :page_size => 15
32 assigns(:page_size).should eq 15
34 it '省略されると@page_sizeにデフォルト値が入る' do
36 assigns(:page_size).should eq CommonLicense.default_page_size
38 it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39 get :index, :page_size => 1500
40 assigns(:page_size).should eq CommonLicense.max_page_size
42 it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43 get :index, :page_size => 0
44 assigns(:page_size).should eq CommonLicense.default_page_size
47 context 'つつがなく終わるとき' do
48 it 'ステータスコード200 OKを返す' do
50 response.should be_success
52 it 'コモンライセンスモデルに一覧を問い合わせている' do
53 CommonLicense.should_receive(:list).exactly(1)
56 it '@common_licensesにリストを取得している' do
58 assigns(:common_licenses).should have_at_least(3).items
61 it 'indexテンプレートを描画する' do
63 response.should render_template("index")
68 get :index, :format => :json
69 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
71 it 'データがリスト構造になっている' do
72 get :index, :format => :json
73 json = JSON.parse response.body
74 json.should have_at_least(3).items
76 it 'リストの先頭くらいはコモンライセンスっぽいものであって欲しい' do
77 get :index, :format => :json
78 json = JSON.parse response.body
79 json.first.has_key?("url").should be_true
83 context '作家権限がないとき' do
88 it 'ステータスコード302 Foundを返す' do
90 response.status.should eq 302
94 response.should redirect_to '/users/sign_in'
98 it 'ステータスコード401 Unauthorizedを返す' do
99 get :index, :format => :json
100 response.status.should eq 401
102 it '応答メッセージにUnauthorizedを返す' do
103 get :index, :format => :json
104 response.message.should match(/Unauthorized/)
108 context '管理者権限がないとき' do
113 it 'ステータスコード200 OKを返す' do
115 response.should be_success
119 it 'ステータスコード200 OKを返す' do
120 get :index, :format => :json
121 response.should be_success
127 describe '単体表示に於いて' do
131 CommonLicense.stub(:show).and_return(@cl)
133 context 'つつがなく終わるとき' do
134 it 'ステータスコード200 OKを返す' do
135 get :show, :id => @cl.id
136 response.should be_success
138 it 'コモンライセンスモデルに単体取得を問い合わせている' do
139 CommonLicense.should_receive(:show).exactly(1)
142 it '@common_licenseにアレを取得している' do
143 get :show, :id => @cl.id
144 assigns(:common_license).id.should eq(@cl.id)
147 it 'showテンプレートを描画する' do
148 get :show, :id => @cl.id
149 response.should render_template("show")
154 get :show, :id => @cl.id, :format => :json
155 lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
158 get :show, :id => @cl.id, :format => :json
159 json = JSON.parse response.body
160 json["name"].should match(/peta/)
164 context '作家権限がないとき' do
169 it 'ステータスコード302 Foundを返す' do
170 get :show, :id => @cl.id
171 response.status.should eq 302
173 it 'サインインページへ遷移する' do
174 get :show, :id => @cl.id
175 response.body.should redirect_to '/users/sign_in'
179 it 'ステータスコード401 Unauthorizedを返す' do
180 get :show, :id => @cl.id, :format => :json
181 response.status.should eq 401
183 it '応答メッセージにUnauthorizedを返す' do
184 get :show, :id => @cl.id, :format => :json
185 response.message.should match(/Unauthorized/)
189 context '管理者権限がないとき' do
194 it 'ステータスコード200 OKを返す' do
195 get :show, :id => @cl.id
196 response.should be_success
200 it 'ステータスコード200 OKを返す' do
201 get :show, :id => @cl.id, :format => :json
202 response.should be_success
209 describe 'インポートに於いて' do
214 @f = Rails.root + 'spec/json/common_license.json'
215 @t = File.open(@f, 'r').read
217 @fs = Rails.root + 'spec/json/common_licenses.json'
218 @ts = File.open(@fs, 'r').read
220 @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
221 @tes = File.open(@fes, 'r').read
222 @jes = JSON.parse @tes
224 context '事前チェックしておく' do
226 #異常な行を返すから、正常の意味で空を返す
227 CommonLicense.stub(:import).with(any_args()).and_return([])
229 it "@dataに渡したデータを保持している" do
230 post :import, :file => @t
231 assigns(:data).should_not be_nil
233 it 'モデルにインポート依頼する' do
234 CommonLicense.should_receive(:import).with(any_args()).exactly(1)
235 post :import, :file => @t
237 it "@errorsに結果を保持している" do
238 post :import, :file => @t
239 assigns(:errors).should eq []
242 context 'つつがなく終わるとき' do
246 it 'ステータスコード302 Foundを返す' do
247 post :import, :file => @t
248 response.status.should eq 302
250 it '管理者向けコモンライセンス一覧ページへ遷移する' do
251 post :import, :file => @t
252 response.should redirect_to('/common_licenses/list')
256 it 'ステータスコード200 OKを返す' do
257 post :import, :file => @t, :format => :json
258 response.should be_success
262 context '管理者権限がないとき' do
267 it 'ステータスコード302 Foundを返す' do
268 post :import, :file => @t
269 response.status.should eq 302
271 it '管理者サインインページへ遷移する' do
272 post :import, :file => @t
273 response.body.should redirect_to '/admins/sign_in'
277 it 'ステータスコード401 Unauthorizedを返す' do
278 post :import, :file => @t, :format => :json
279 response.status.should eq 401
281 it '応答メッセージにUnauthorizedを返す' do
282 post :import, :file => @t, :format => :json
283 response.message.should match(/Unauthorized/)
287 context '作家権限がないとき' do
288 #必要なのは管理者権限であって作家権限ではない。成功を見届ける
293 it 'ステータスコード302 Foundを返す' do
294 post :import, :file => @t
295 response.status.should eq 302
297 it '管理者向けコモンライセンス一覧ページへ遷移する' do
298 post :import, :file => @t
299 response.should redirect_to('/common_licenses/list')
303 it 'ステータスコード200 OKを返す' do
304 post :import, :file => @t, :format => :json
305 response.should be_success
309 context '検証、保存に失敗した' do
312 CommonLicense.stub(:import).with(any_args()).and_return(
313 [CommonLicense.new(Factory.attributes_for(:common_license))]
317 it 'ステータスコード200 OKを返す' do
318 post :import, :file => @t
319 response.status.should eq 200
321 it 'resultページを描画する' do
322 post :import, :file => @t
323 response.should render_template("result")
327 it 'ステータスコード422 unprocessable_entity を返す' do
328 post :import, :file => @t, :format => :json
329 response.status.should eq 422
331 it '応答メッセージUnprocessable Entityを返す' do
332 post :import, :file => @t, :format => :json
333 response.message.should match(/Unprocessable/)