OSDN Git Service

repair test
[pettanr/pettanr.git] / spec / controllers / authors_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #作家
4
5 describe AuthorsController 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   end
15
16   describe '一覧表示に於いて' do
17     before do
18       Author.stub(:list).and_return([@author, @author, @author])
19       sign_in @user
20     end
21     context '事前チェックする' 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 Author.default_page_size
37       end
38       it '最大を超えると@page_sizeにデフォルト最大値が入る' do
39         get :index, :page_size => 1500
40         assigns(:page_size).should eq Author.max_page_size
41       end
42       it '不正な値が入ると@page_sizeにデフォルト最大値が入る' do
43         get :index, :page_size => 0
44         assigns(:page_size).should eq Author.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         Author.should_receive(:list).exactly(1)
54         get :index
55       end
56       it '@authorsにリストを取得している' do
57         get :index
58         assigns(:authors).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?("name").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   end
109   
110   describe '閲覧に於いて' do
111     before do
112       Author.stub(:show).and_return(@author)
113       sign_in @user
114     end
115     context 'つつがなく終わるとき' do
116       it 'ステータスコード200 OKを返す' do
117         get :show, :id => @author.id
118         response.should be_success
119       end
120       it '作家モデルに単体取得を問い合わせている' do
121         Author.should_receive(:show).exactly(1)
122         get :show
123       end
124       it '@authorにアレを取得している' do
125         get :show, :id => @author.id
126         assigns(:author).should eq(@author)
127       end
128       context 'html形式' do
129         it 'showテンプレートを描画する' do
130           get :show, :id => @author.id
131           response.should render_template("show")
132         end
133       end
134       context 'json形式' do
135         it 'jsonデータを返す' do
136           get :show, :id => @author.id, :format => :json
137           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
138         end
139         it 'データがアレになっている' do
140           get :show, :id => @author.id, :format => :json
141           json = JSON.parse response.body
142           json["name"].should match(/name/)
143         end
144       end
145     end
146     context '作家権限がないとき' do
147       before do
148         sign_out @user
149       end
150       context 'html形式' do
151         it 'ステータスコード302 Foundを返す' do
152           get :show, :id => @author.id
153           response.status.should eq 302
154         end
155         it 'サインインページへ遷移する' do
156           get :show, :id => @author.id
157           response.body.should redirect_to '/users/sign_in'
158         end
159       end
160       context 'json形式' do
161         it 'ステータスコード401 Unauthorizedを返す' do
162           get :show, :id => @author.id, :format => :json
163           response.status.should eq 401
164         end
165         it '応答メッセージにUnauthorizedを返す' do
166           get :show, :id => @author.id, :format => :json
167           response.message.should match(/Unauthorized/)
168         end
169       end
170     end
171 =begin
172     context '対象作家がないとき' do
173       context 'html形式' do
174         it '例外404 not_foundを返す' do
175           lambda{
176             get :show, :id => 0
177           }.should raise_error(ActiveRecord::RecordNotFound)
178         end
179       end
180       context 'json形式' do
181         it '例外404 not_foundを返す' do
182           lambda{ 
183             get :show, :id => 0, :format => :json
184           }.should raise_error(ActiveRecord::RecordNotFound)
185         end
186       end
187     end
188 =end
189   end
190   
191   describe '作家数取得に於いて' do
192     before do
193       Author.should_receive(:visible_count).and_return(3)
194 #      sign_in @user
195     end
196     context 'つつがなく終わるとき' do
197       it 'ステータスコード200 OKを返す' do
198         get :count, :format => :json
199         response.should be_success 
200       end
201       context 'json形式' do
202         it 'jsonデータを返す' do
203           get :count, :format => :json
204           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
205         end
206         it 'データがHash構造になっていてコミック数が1である' do
207           get :count, :format => :json
208           json = JSON.parse response.body
209           json["count"].should == 3
210         end
211       end
212     end
213   end
214
215 end