/
challenges_controller_spec.rb
227 lines (180 loc) · 5.15 KB
/
challenges_controller_spec.rb
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
require 'spec_helper'
describe ChallengesController do
describe "GET index" do
log_in_as :student
before do
Challenge.stub :visible
end
it "does not require a logged in user" do
controller.stub current_user: nil
get :index
response.should_not deny_access
end
it "assigns the visible challenges for non-admins" do
Challenge.stub visible: 'challenges'
get :index
assigns(:challenges).should eq 'challenges'
end
it "assigns all challenges for admins" do
current_user.stub admin?: true
Challenge.stub in_chronological_order: 'challenges'
get :index
assigns(:challenges).should eq 'challenges'
end
end
describe "GET new" do
log_in_as :admin
it "requires admin access" do
current_user.stub admin?: false
get :new
response.should deny_access
end
it "assigns a new challenge" do
Challenge.stub new: 'challenge'
get :new
assigns(:challenge).should eq 'challenge'
end
end
describe "POST create" do
log_in_as :admin
let(:challenge) { mock_model Challenge }
before do
Challenge.stub new: challenge
challenge.stub :save
end
it "requires admin access" do
current_user.stub admin?: false
post :create
response.should deny_access
end
it "initializes a new challenge with params[:challenge]" do
Challenge.should_receive(:new).with('challenge attributes')
post :create, challenge: 'challenge attributes'
end
it "assigns the new challenge" do
post :create
assigns(:challenge).should eq challenge
end
it "attempts to save the challenge" do
challenge.should_receive(:save)
post :create
end
it "redirects to the challenge on success" do
challenge.stub save: true
post :create
controller.should redirect_to challenge
end
it "rerenders the page on error" do
challenge.stub save: false
post :create
controller.should render_template :new
end
end
describe "GET show" do
log_in_as :student
let(:challenge) { double }
let(:solutions) { [double, double] }
before do
Challenge.stub find: challenge
ChallengeSolution.stub for_challenge_with_users: solutions
challenge.stub hidden?: false, closed?: false
end
it "does not requrie a logged in user" do
controller.stub current_user: nil
get :show, id: '1'
response.should_not deny_access
end
it "looks up the challenge by id" do
Challenge.should_receive(:find).with('42')
get :show, id: '42'
end
it "assigns the challenge" do
get :show, id: '1'
assigns(:challenge).should eq challenge
end
it "does not assign the solutions" do
get :show, id: '1'
assigns(:solutions).should be_nil
end
context "when closed" do
log_in_as :student
before do
challenge.stub closed?: true
end
it "assigns the solutions" do
get :show, id: '1'
assigns(:solutions).should eq solutions
end
end
context "when the user is admin" do
log_in_as :admin
it "assigns the solutions" do
get :show, id: '1'
assigns(:solutions).should eq solutions
end
end
context "when hidden" do
log_in_as :student
before do
challenge.stub hidden?: true
end
it "denies access to non-admins" do
get :show, id: '1'
response.should deny_access
end
it "allows admins to see the challenge" do
current_user.stub admin?: true
get :show, id: '1'
response.should be_success
end
end
end
describe "GET edit" do
log_in_as :admin
it "requires admin access" do
current_user.stub admin?: false
get :edit, id: '1'
response.should deny_access
end
it "finds the challenge by id and assigns it" do
Challenge.should_receive(:find).with('42').and_return('challenge')
get :edit, id: '42'
assigns(:challenge).should eq 'challenge'
end
end
describe "GET update" do
log_in_as :admin
let(:challenge) { mock_model Challenge }
before do
Challenge.stub find: challenge
challenge.stub :update_attributes
end
it "requires admin access" do
current_user.stub admin?: false
put :update, id: '1'
response.should deny_access
end
it "looks up the challenge by id" do
Challenge.should_receive(:find).with('42')
put :update, id: '42'
end
it "assigns the challenge" do
put :update, id: '1'
assigns(:challenge).should eq challenge
end
it "attempts to update the challenge" do
challenge.should_receive(:update_attributes).with('challenge attributes')
put :update, id: '1', challenge: 'challenge attributes'
end
it "redirects to the challenge on success" do
challenge.stub update_attributes: true
put :update, id: '1'
controller.should redirect_to challenge
end
it "rerenders the form on failure" do
challenge.stub update_attributes: false
put :update, id: '1'
controller.should render_template :edit
end
end
end