errs_controller_spec.rb
5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
require 'spec_helper'
describe ErrsController do
it_requires_authentication :for => {
:index => :get, :all => :get, :show => :get, :resolve => :put
},
:params => {:app_id => 'dummyid', :id => 'dummyid'}
let(:app) { Factory(:app) }
let(:err) { Factory(:err, :app => app) }
describe "GET /errs" do
render_views
context 'when logged in as an admin' do
before(:each) do
@user = Factory(:admin)
sign_in @user
@notice = Factory :notice
@err = @notice.err
end
it "should successfully list errs" do
get :index
response.should be_success
response.body.should match(@err.message)
end
it "should list atom feed successfully" do
get :index, :format => "atom"
response.should be_success
response.body.should match(@err.message)
end
it "should handle lots of errors" do
pending "Turning off long running spec"
1000.times { Factory :notice }
lambda { get :index }.should_not raise_error
end
context "pagination" do
before(:each) do
35.times { Factory :err }
end
it "should have default per_page value for user" do
get :index
assigns(:errs).size.should == User::PER_PAGE
end
it "should be able to override default per_page value" do
@user.update_attribute :per_page, 10
get :index
assigns(:errs).size.should == 10
end
end
end
context 'when logged in as a user' do
it 'gets a paginated list of unresolved errs for the users apps' do
sign_in(user = Factory(:user))
unwatched_err = Factory(:err)
watched_unresolved_err = Factory(:err, :app => Factory(:user_watcher, :user => user).app, :resolved => false)
watched_resolved_err = Factory(:err, :app => Factory(:user_watcher, :user => user).app, :resolved => true)
get :index
assigns(:errs).should include(watched_unresolved_err)
assigns(:errs).should_not include(unwatched_err, watched_resolved_err)
end
end
end
describe "GET /errs/all" do
context 'when logged in as an admin' do
it "gets a paginated list of all errs" do
sign_in Factory(:admin)
errs = WillPaginate::Collection.new(1,30)
3.times { errs << Factory(:err) }
3.times { errs << Factory(:err, :resolved => true)}
Err.should_receive(:ordered).and_return(
mock('proxy', :paginate => errs)
)
get :all
assigns(:errs).should == errs
end
end
context 'when logged in as a user' do
it 'gets a paginated list of all errs for the users apps' do
sign_in(user = Factory(:user))
unwatched_err = Factory(:err)
watched_unresolved_err = Factory(:err, :app => Factory(:user_watcher, :user => user).app, :resolved => false)
watched_resolved_err = Factory(:err, :app => Factory(:user_watcher, :user => user).app, :resolved => true)
get :all
assigns(:errs).should include(watched_resolved_err, watched_unresolved_err)
assigns(:errs).should_not include(unwatched_err)
end
end
end
describe "GET /apps/:app_id/errs/:id" do
render_views
before do
3.times { Factory(:notice, :err => err)}
end
context 'when logged in as an admin' do
before do
sign_in Factory(:admin)
end
it "finds the app" do
get :show, :app_id => app.id, :id => err.id
assigns(:app).should == app
end
it "finds the err" do
get :show, :app_id => app.id, :id => err.id
assigns(:err).should == err
end
it "successfully render page" do
get :show, :app_id => app.id, :id => err.id
response.should be_success
end
end
context 'when logged in as a user' do
before do
sign_in(@user = Factory(:user))
@unwatched_err = Factory(:err)
@watched_app = Factory(:app)
@watcher = Factory(:user_watcher, :user => @user, :app => @watched_app)
@watched_err = Factory(:err, :app => @watched_app)
end
it 'finds the err if the user is watching the app' do
get :show, :app_id => @watched_app.to_param, :id => @watched_err.id
assigns(:err).should == @watched_err
end
it 'raises a DocumentNotFound error if the user is not watching the app' do
lambda {
get :show, :app_id => @unwatched_err.app_id, :id => @unwatched_err.id
}.should raise_error(Mongoid::Errors::DocumentNotFound)
end
end
end
describe "PUT /apps/:app_id/errs/:id/resolve" do
before do
sign_in Factory(:admin)
@err = Factory(:err)
App.stub(:find).with(@err.app.id).and_return(@err.app)
@err.app.errs.stub(:find).and_return(@err)
@err.stub(:resolve!)
end
it 'finds the app and the err' do
App.should_receive(:find).with(@err.app.id).and_return(@err.app)
@err.app.errs.should_receive(:find).and_return(@err)
put :resolve, :app_id => @err.app.id, :id => @err.id
assigns(:app).should == @err.app
assigns(:err).should == @err
end
it "should resolve the issue" do
@err.should_receive(:resolve!).and_return(true)
put :resolve, :app_id => @err.app.id, :id => @err.id
end
it "should display a message" do
put :resolve, :app_id => @err.app.id, :id => @err.id
request.flash[:success].should match(/Great news/)
end
it "should redirect to the app page" do
put :resolve, :app_id => @err.app.id, :id => @err.id
response.should redirect_to(app_path(@err.app))
end
it "should redirect back to errs page" do
request.env["Referer"] = errs_path
put :resolve, :app_id => @err.app.id, :id => @err.id
response.should redirect_to(errs_path)
end
end
end