-# -*- encoding: utf-8 -*-
-#コモンライセンス
-require 'spec_helper'
-
-describe CommonLicensesController do
- before do
- @admin = Factory :admin
- @lc = Factory :license
- @cl = Factory :common_license, :license_id => @lc.id
- @user = Factory( :user_yas)
- @author = @user.author
- @artist = Factory :artist_yas, :author_id => @author.id
- end
-=begin
- describe '一覧表示に於いて' do
- before do
- sign_in @admin
- sign_in @user
- CommonLicense.stub(:list).and_return([@cl, @cl, @cl])
- end
- context 'パラメータpageについて' do
- it '@pageに値が入る' do
- get :index, :page => 5
- assigns(:page).should eq 5
- end
- it '省略されると@pageに1値が入る' do
- get :index
- assigns(:page).should eq 1
- end
- it '与えられたpage_sizeがセットされている' do
- get :index, :page_size => 15
- assigns(:page_size).should eq 15
- end
- it '省略されると@page_sizeにデフォルト値が入る' do
- get :index
- assigns(:page_size).should eq CommonLicense.default_page_size
- end
- it '最大を超えると@page_sizeにデフォルト最大値が入る' do
- get :index, :page_size => 1500
- assigns(:page_size).should eq CommonLicense.max_page_size
- end
- it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
- get :index, :page_size => 0
- assigns(:page_size).should eq CommonLicense.default_page_size
- end
- end
- context 'つつがなく終わるとき' do
- it 'ステータスコード200 OKを返す' do
- get :index
- response.should be_success
- end
- it 'コモンライセンスモデルに一覧を問い合わせている' do
- CommonLicense.should_receive(:list).exactly(1)
- get :index
- end
- it '@common_licensesにリストを取得している' do
- get :index
- assigns(:common_licenses).should have_at_least(3).items
- end
- context 'html形式' do
- it 'indexテンプレートを描画する' do
- get :index
- response.should render_template("index")
- end
- end
- context 'json形式' do
- it 'jsonデータを返す' do
- get :index, :format => :json
- lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
- end
- it 'データがリスト構造になっている' do
- get :index, :format => :json
- json = JSON.parse response.body
- json.should have_at_least(3).items
- end
- it 'リストの先頭くらいはコモンライセンスっぽいものであって欲しい' do
- get :index, :format => :json
- json = JSON.parse response.body
- json.first.has_key?("url").should be_true
- end
- end
- end
- context '作家権限がないとき' do
- before do
- sign_out @user
- end
- context 'html形式' do
- it 'ステータスコード302 Foundを返す' do
- get :index
- response.status.should eq 302
- end
- it 'サインインページへ遷移する' do
- get :index
- response.should redirect_to '/users/sign_in'
- end
- end
- context 'json形式' do
- it 'ステータスコード401 Unauthorizedを返す' do
- get :index, :format => :json
- response.status.should eq 401
- end
- it '応答メッセージにUnauthorizedを返す' do
- get :index, :format => :json
- response.message.should match(/Unauthorized/)
- end
- end
- end
- context '管理者権限がないとき' do
- before do
- sign_out @admin
- end
- context 'html形式' do
- it 'ステータスコード200 OKを返す' do
- get :index
- response.should be_success
- end
- end
- context 'json形式' do
- it 'ステータスコード200 OKを返す' do
- get :index, :format => :json
- response.should be_success
- end
- end
- end
- end
-
- describe '単体表示に於いて' do
- before do
- sign_in @admin
- sign_in @user
- CommonLicense.stub(:show).and_return(@cl)
- end
- context 'つつがなく終わるとき' do
- it 'ステータスコード200 OKを返す' do
- get :show, :id => @cl.id
- response.should be_success
- end
- it 'コモンライセンスモデルに単体取得を問い合わせている' do
- CommonLicense.should_receive(:show).exactly(1)
- get :show
- end
- it '@common_licenseにアレを取得している' do
- get :show, :id => @cl.id
- assigns(:common_license).id.should eq(@cl.id)
- end
- context 'html形式' do
- it 'showテンプレートを描画する' do
- get :show, :id => @cl.id
- response.should render_template("show")
- end
- end
- context 'json形式' do
- it 'jsonデータを返す' do
- get :show, :id => @cl.id, :format => :json
- lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
- end
- it 'データがアレになっている' do
- get :show, :id => @cl.id, :format => :json
- json = JSON.parse response.body
- json["name"].should match(/peta/)
- end
- end
- end
- context '作家権限がないとき' do
- before do
- sign_out @user
- end
- context 'html形式' do
- it 'ステータスコード302 Foundを返す' do
- get :show, :id => @cl.id
- response.status.should eq 302
- end
- it 'サインインページへ遷移する' do
- get :show, :id => @cl.id
- response.body.should redirect_to '/users/sign_in'
- end
- end
- context 'json形式' do
- it 'ステータスコード401 Unauthorizedを返す' do
- get :show, :id => @cl.id, :format => :json
- response.status.should eq 401
- end
- it '応答メッセージにUnauthorizedを返す' do
- get :show, :id => @cl.id, :format => :json
- response.message.should match(/Unauthorized/)
- end
- end
- end
- context '管理者権限がないとき' do
- before do
- sign_out @admin
- end
- context 'html形式' do
- it 'ステータスコード200 OKを返す' do
- get :show, :id => @cl.id
- response.should be_success
- end
- end
- context 'json形式' do
- it 'ステータスコード200 OKを返す' do
- get :show, :id => @cl.id, :format => :json
- response.should be_success
- end
- end
- end
- end
-=end
-
- describe 'インポートに於いて' do
- before do
- sign_in @admin
- sign_in @user
- #テストデータを用意してね
- @f = Rails.root + 'spec/json/common_license.json'
- @t = File.open(@f, 'r').read
- @j = JSON.parse @t
- @fs = Rails.root + 'spec/json/common_licenses.json'
- @ts = File.open(@fs, 'r').read
- @js = JSON.parse @ts
- @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
- @tes = File.open(@fes, 'r').read
- @jes = JSON.parse @tes
- end
- context '事前チェックしておく' do
- before do
- #異常な行を返すから、正常の意味で空を返す
- CommonLicense.stub(:import).with(any_args()).and_return([])
- end
- it "@dataに渡したデータを保持している" do
- post :import, :file => @t
- assigns(:data).should_not be_nil
- end
- it 'モデルにインポート依頼する' do
- CommonLicense.should_receive(:import).with(any_args()).exactly(1)
- post :import, :file => @t
- end
- it "@errorsに結果を保持している" do
- post :import, :file => @t
- assigns(:errors).should eq []
- end
- end
- context 'つつがなく終わるとき' do
- before do
- end
- context 'html形式' do
- it 'ステータスコード302 Foundを返す' do
- post :import, :file => @t
- response.status.should eq 302
- end
- it '管理者向けコモンライセンス一覧ページへ遷移する' do
- post :import, :file => @t
- response.should redirect_to('/common_licenses/list')
- end
- end
- context 'json形式' do
- it 'ステータスコード200 OKを返す' do
- post :import, :file => @t, :format => :json
- response.should be_success
- end
- end
- end
- context '管理者権限がないとき' do
- before do
- sign_out @admin
- end
- context 'html形式' do
- it 'ステータスコード302 Foundを返す' do
- post :import, :file => @t
- response.status.should eq 302
- end
- it '管理者サインインページへ遷移する' do
- post :import, :file => @t
- response.body.should redirect_to '/admins/sign_in'
- end
- end
- context 'json形式' do
- it 'ステータスコード401 Unauthorizedを返す' do
- post :import, :file => @t, :format => :json
- response.status.should eq 401
- end
- it '応答メッセージにUnauthorizedを返す' do
- post :import, :file => @t, :format => :json
- response.message.should match(/Unauthorized/)
- end
- end
- end
- context '作家権限がないとき' do
- #必要なのは管理者権限であって作家権限ではない。成功を見届ける
- before do
- sign_out @user
- end
- context 'html形式' do
- it 'ステータスコード302 Foundを返す' do
- post :import, :file => @t
- response.status.should eq 302
- end
- it '管理者向けコモンライセンス一覧ページへ遷移する' do
- post :import, :file => @t
- response.should redirect_to('/common_licenses/list')
- end
- end
- context 'json形式' do
- it 'ステータスコード200 OKを返す' do
- post :import, :file => @t, :format => :json
- response.should be_success
- end
- end
- end
- context '検証、保存に失敗した' do
- before do
- #異常な行を返す
- CommonLicense.stub(:import).with(any_args()).and_return(
- [CommonLicense.new(Factory.attributes_for(:common_license))]
- )
- end
- context 'html形式' do
- it 'ステータスコード200 OKを返す' do
- post :import, :file => @t
- response.status.should eq 200
- end
- it 'resultページを描画する' do
- post :import, :file => @t
- response.should render_template("result")
- end
- end
- context 'json形式' do
- it 'ステータスコード422 unprocessable_entity を返す' do
- post :import, :file => @t, :format => :json
- response.status.should eq 422
- end
- it '応答メッセージUnprocessable Entityを返す' do
- post :import, :file => @t, :format => :json
- response.message.should match(/Unprocessable/)
- end
- end
- end
- end
-end