OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / spec / controllers / speech_balloon_templates_controller_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #フキダシテンプレート
4 describe SpeechBalloonTemplatesController do
5
6   before do
7     @admin = FactoryGirl.create :admin
8     @sp = FactoryGirl.create :system_picture
9     @lg = FactoryGirl.create :license_group
10     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
11     @user = FactoryGirl.create( :user_yas)
12     @author = @user.author
13     @sbt = FactoryGirl.create :speech_balloon_template
14   end
15
16   describe '一覧表示に於いて' do
17     before do
18       sign_in @user
19       SpeechBalloonTemplate.stub(:list).and_return([@sbt, @sbt, @sbt])
20     end
21     context 'つつがなく終わるとき' do
22       it 'フキダシテンプレートモデルに一覧を問い合わせている' do
23         SpeechBalloonTemplate.should_receive(:list).exactly(1)
24         get :index
25       end
26       it '@speech_balloon_templatesにリストを取得している' do
27         get :index
28         assigns(:speech_balloon_templates).should have_at_least(3).items
29       end
30       context 'html形式' do
31         it 'ステータスコード200 OKを返す' do
32           get :index
33           response.should be_success 
34         end
35         it 'indexテンプレートを描画する' do
36           get :index
37           response.should render_template("index")
38         end
39       end
40       context 'json形式' do
41         it 'ステータスコード200 OKを返す' do
42           get :index, :format => :json
43           response.should be_success 
44         end
45         it 'jsonデータを返す' do
46           get :index, :format => :json
47           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
48         end
49         it 'フキダシテンプレートモデルにjson一覧出力オプションを問い合わせている' do
50           SpeechBalloonTemplate.should_receive(:list_json_opt).exactly(1)
51           get :index, :format => :json
52         end
53         it 'データがリスト構造になっている' do
54           get :index, :format => :json
55           json = JSON.parse response.body
56           json.should have_at_least(3).items
57         end
58         it 'リストの先頭くらいはフキダシテンプレートっぽいものであって欲しい' do
59           get :index, :format => :json
60           json = JSON.parse response.body
61           json.first.has_key?("name").should be_true
62           json.first.has_key?("classname").should be_true
63           json.first.has_key?("t").should be_true
64         end
65       end
66     end
67     context '作家権限がないとき' do
68       before do
69         sign_out @user
70       end
71       context 'html形式' do
72         it 'ステータスコード302 Foundを返す' do
73           get :index
74           response.status.should eq 302
75         end
76         it 'サインインページへ遷移する' do
77           get :index
78           response.should redirect_to '/users/sign_in'
79         end
80       end
81       context 'json形式' do
82         it 'ステータスコード401 Unauthorizedを返す' do
83           get :index, :format => :json
84           response.status.should eq 401
85         end
86         it '応答メッセージにUnauthorizedを返す' do
87           get :index, :format => :json
88           response.message.should match(/Unauthorized/)
89         end
90       end
91     end
92   end
93   
94   describe '単体表示に於いて' do
95     before do
96       sign_in @user
97       SpeechBalloonTemplate.stub(:show).with(@sbt.id.to_s, @author).and_return(@sbt)
98     end
99     context 'つつがなく終わるとき' do
100       it 'フキダシテンプレートモデルに単体取得を問い合わせている' do
101         SpeechBalloonTemplate.should_receive(:show).exactly(1)
102         get :show
103       end
104       it '@speech_balloon_templateにアレを取得している' do
105         get :show, :id => @sbt.id
106         assigns(:speech_balloon_template).should eq(@sbt)
107       end
108       context 'html形式' do
109         it 'ステータスコード200 OKを返す' do
110           get :show, :id => @sbt.id
111           response.should be_success
112         end
113         it 'showテンプレートを描画する' do
114           get :show, :id => @sbt.id
115           response.should render_template("show")
116         end
117       end
118       context 'json形式' do
119         it 'ステータスコード200 OKを返す' do
120           get :show, :id => @sbt.id, :format => :json
121           response.should be_success
122         end
123         it 'jsonデータを返す' do
124           get :show, :id => @sbt.id, :format => :json
125           lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError)
126         end
127         it 'フキダシテンプレートモデルにjson単体出力オプションを問い合わせている' do
128           SpeechBalloonTemplate.should_receive(:show_json_opt).exactly(1)
129           get :show, :id => @sbt.id, :format => :json
130         end
131         it 'データがアレになっている' do
132           get :show, :id => @sbt.id, :format => :json
133           json = JSON.parse response.body
134           json["classname"].should match(/Plain/)
135         end
136       end
137     end
138     context '作家権限がないとき' do
139       before do
140         sign_out @user
141       end
142       context 'html形式' do
143         it 'ステータスコード302 Foundを返す' do
144           get :index
145           response.status.should eq 302
146         end
147         it 'サインインページへ遷移する' do
148           get :index
149           response.should redirect_to '/users/sign_in'
150         end
151       end
152       context 'json形式' do
153         it 'ステータスコード401 Unauthorizedを返す' do
154           get :index, :format => :json
155           response.status.should eq 401
156         end
157         it '応答メッセージにUnauthorizedを返す' do
158           get :index, :format => :json
159           response.message.should match(/Unauthorized/)
160         end
161       end
162     end
163   end
164   
165
166 end