apps_controller_spec.rb
4.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
require 'spec_helper'
describe AppsController do
it_requires_authentication
it_requires_admin_privileges :for => {:new => :get, :edit => :get, :create => :post, :update => :put, :destroy => :delete}
describe "GET /apps" do
context 'when logged in as an admin' do
it 'finds all apps' do
sign_in Factory(:admin)
3.times { Factory(:app) }
apps = App.all
get :index
assigns(:apps).should == apps
end
end
context 'when logged in as a regular user' do
it 'finds apps the user is watching' do
sign_in(user = Factory(:user))
unwatched_app = Factory(:app)
watched_app1 = Factory(:app)
watched_app2 = Factory(:app)
Factory(:user_watcher, :user => user, :app => watched_app1)
Factory(:user_watcher, :user => user, :app => watched_app2)
get :index
assigns(:apps).should include(watched_app1, watched_app2)
assigns(:apps).should_not include(unwatched_app)
end
end
end
describe "GET /apps/:id" do
render_views
context 'logged in as an admin' do
before(:each) do
sign_in Factory(:admin)
@app = Factory(:app)
end
it 'finds the app' do
get :show, :id => @app.id
assigns(:app).should == @app
end
it "should not raise errors for app with err without notices" do
Factory :err, :app => @app
lambda { get :show, :id => @app.id }.should_not raise_error
end
end
context 'logged in as a user' do
it 'finds the app if the user is watching it' do
pending
end
it 'does not find the app if the user is not watching it' do
sign_in Factory(:user)
app = Factory(:app)
lambda {
get :show, :id => app.id
}.should raise_error(Mongoid::Errors::DocumentNotFound)
end
end
end
context 'logged in as an admin' do
before do
sign_in Factory(:admin)
end
describe "GET /apps/new" do
it 'instantiates a new app with a prebuilt watcher' do
get :new
assigns(:app).should be_a(App)
assigns(:app).should be_new_record
assigns(:app).watchers.should_not be_empty
end
end
describe "GET /apps/:id/edit" do
it 'finds the correct app' do
app = Factory(:app)
get :edit, :id => app.id
assigns(:app).should == app
end
end
describe "POST /apps" do
before do
@app = Factory(:app)
App.stub(:new).and_return(@app)
end
context "when the create is successful" do
before do
@app.should_receive(:save).and_return(true)
end
it "should redirect to the app page" do
post :create, :app => {}
response.should redirect_to(app_path(@app))
end
it "should display a message" do
post :create, :app => {}
request.flash[:success].should match(/success/)
end
end
context "when the create is unsuccessful" do
it "should render the new page" do
@app.should_receive(:save).and_return(false)
post :create, :app => {}
response.should render_template(:new)
end
end
end
describe "PUT /apps/:id" do
before do
@app = Factory(:app)
App.stub(:find).with(@app.id).and_return(@app)
end
context "when the update is successful" do
before do
@app.should_receive(:update_attributes).and_return(true)
end
it "should redirect to the app page" do
put :update, :id => @app.id, :app => {}
response.should redirect_to(app_path(@app))
end
it "should display a message" do
put :update, :id => @app.id, :app => {}
request.flash[:success].should match(/success/)
end
end
context "when the update is unsuccessful" do
it "should render the edit page" do
@app.should_receive(:update_attributes).and_return(false)
put :update, :id => @app.id, :app => {}
response.should render_template(:edit)
end
end
end
describe "DELETE /apps/:id" do
before do
@app = Factory(:app)
App.stub(:find).with(@app.id).and_return(@app)
end
it "should find the app" do
delete :destroy, :id => @app.id
assigns(:app).should == @app
end
it "should destroy the app" do
@app.should_receive(:destroy)
delete :destroy, :id => @app.id
end
it "should display a message" do
delete :destroy, :id => @app.id
request.flash[:success].should match(/success/)
end
it "should redirect to the apps page" do
delete :destroy, :id => @app.id
response.should redirect_to(apps_path)
end
end
end
end