OSDN Git Service

215c20b782aa1956803a1323dea3213431a039a1
[pettanr/pettanr.git] / spec / controllers / licenses_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ライセンス
3 require 'spec_helper'
4
5 describe LicensesController do
6   before do
7     @admin = FactoryGirl.create :admin
8     @lg = FactoryGirl.create :license_group
9     @sp = FactoryGirl.create :system_picture
10     @user = FactoryGirl.create( :user_yas)
11     @author = FactoryGirl.create :author, :user_id => @user.id
12     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
13   end
14
15 if MagicNumber['run_mode'] == 1
16   describe '一覧表示に於いて' do
17     before do
18       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
19       sign_in @user
20       License.stub(:list).and_return([@l, @l, @l])
21     end
22     context 'つつがなく終わるとき' do
23       it 'ステータスコード200 OKを返す' do
24         get :index
25         response.should be_success 
26       end
27       it 'ライセンスモデルに一覧を問い合わせている' do
28         License.should_receive(:list).exactly(1)
29         get :index
30       end
31       it '@licensesにリストを取得している' do
32         get :index
33         assigns(:licenses).should have_at_least(3).items
34       end
35       context 'html形式' do
36         it 'indexテンプレートを描画する' do
37           get :index
38           response.should render_template("index")
39         end
40       end
41       context 'json形式' do
42         it 'jsonデータを返す' do
43           get :index, :format => :json
44           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
45         end
46         it 'ライセンスモデルにjson一覧出力オプションを問い合わせている' do
47           License.should_receive(:list_json_opt).exactly(1)
48           get :index, :format => :json
49         end
50         it 'データがリスト構造になっている' do
51           get :index, :format => :json
52           json = JSON.parse response.body
53           json.should have_at_least(3).items
54         end
55         it 'リストの先頭くらいはライセンスっぽいものであって欲しい' do
56           get :index, :format => :json
57           json = JSON.parse response.body
58           json.first.has_key?("name").should be_true
59           json.first.has_key?("caption").should be_true
60           json.first.has_key?("url").should be_true
61         end
62       end
63     end
64     context '作家権限がないとき' do
65       before do
66         sign_out @user
67       end
68       it 'ステータスコード200 okを返す' do
69         get :index
70         response.status.should eq 200
71       end
72     end
73   end
74   
75   describe '単体表示に於いて' do
76     before do
77       sign_in @user
78       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
79       License.stub(:show).and_return(@l)
80     end
81     context 'つつがなく終わるとき' do
82       it 'ステータスコード200 OKを返す' do
83         get :show, :id => @l.id
84         response.should be_success
85       end
86       it 'ライセンスモデルに単体取得を問い合わせている' do
87         License.should_receive(:show).exactly(1)
88         get :show
89       end
90       it '@licenseにアレを取得している' do
91         get :show, :id => @l.id
92         assigns(:license).id.should eq(@l.id)
93       end
94       context 'html形式' do
95         it 'showテンプレートを描画する' do
96           get :show, :id => @l.id
97           response.should render_template("show")
98         end
99       end
100       context 'json形式' do
101         it 'jsonデータを返す' do
102           get :show, :id => @l.id, :format => :json
103           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
104         end
105         it 'ライセンスモデルにjson単体出力オプションを問い合わせている' do
106           License.should_receive(:show_json_opt).exactly(1)
107           get :show, :id => @l.id, :format => :json
108         end
109         it 'データがアレになっている' do
110           get :show, :id => @l.id, :format => :json
111           json = JSON.parse response.body
112           json["name"].should match(/peta/)
113           json["caption"].should_not be_nil
114           json["url"].should_not be_nil
115         end
116       end
117     end
118     context '作家権限がないとき' do
119       before do
120         sign_out @user
121       end
122       it 'ステータスコード200 okを返す' do
123         get :show, :id => @l.id
124         response.status.should eq 200
125       end
126     end
127   end
128   
129 else
130   describe '一覧表示に於いて' do
131     before do
132       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
133       sign_in @user
134       License.stub(:list).and_return([@l, @l, @l])
135     end
136       context 'つつがなく終わるとき' do
137       it 'ステータスコード200 OKを返す' do
138         get :index
139         response.should be_success 
140       end
141       context 'html形式' do
142         it 'indexテンプレートを描画する' do
143           get :index
144           response.should render_template("index")
145         end
146       end
147       context 'json形式' do
148         it 'jsonデータを返す' do
149           get :index, :format => :json
150           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
151         end
152       end
153     end
154     context '作家権限がないとき' do
155       before do
156         sign_out @user
157       end
158       it 'ステータスコード200 okを返す' do
159         get :index
160         response.status.should eq 200
161       end
162       context 'html形式' do
163         it 'indexテンプレートを描画する' do
164           get :index
165           response.should render_template("index")
166         end
167       end
168       context 'json形式' do
169         it 'jsonデータを返す' do
170           get :index, :format => :json
171           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
172         end
173       end
174     end
175   end
176   
177   describe '単体表示に於いて' do
178     before do
179       sign_in @user
180       @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
181       License.stub(:show).and_return(@l)
182     end
183     context 'つつがなく終わるとき' do
184       it 'ステータスコード200 OKを返す' do
185         get :show, :id => @l.id
186         response.should be_success
187       end
188       context 'html形式' do
189         it 'showテンプレートを描画する' do
190           get :show, :id => @l.id
191           response.should render_template("show")
192         end
193       end
194       context 'json形式' do
195         it 'jsonデータを返す' do
196           get :show, :id => @l.id, :format => :json
197           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
198         end
199       end
200     end
201     context '作家権限がないとき' do
202       before do
203         sign_out @user
204       end
205       it 'ステータスコード200 okを返す' do
206         get :show, :id => @l.id
207         response.status.should eq 200
208       end
209       context 'html形式' do
210         it 'showテンプレートを描画する' do
211           get :show, :id => @l.id
212           response.should render_template("show")
213         end
214       end
215       context 'json形式' do
216         it 'jsonデータを返す' do
217           get :show, :id => @l.id, :format => :json
218           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
219         end
220       end
221     end
222   end
223   
224 end
225 end