Skip to content

Commit fbd7385

Browse files
committed
Restrict access to MailerController actions
1 parent 18c8c5f commit fbd7385

2 files changed

Lines changed: 99 additions & 26 deletions

File tree

app/controllers/mailer_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
class MailerController < ApplicationController
2+
load_and_authorize_resource class: Message
3+
24
def new
35
@users = Array(params[:user])
46
@emails = User.where(:username => @users).all.map(&:email)

spec/controllers/mailer_controller_spec.rb

Lines changed: 97 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,58 @@
33
describe MailerController do
44
let(:user) { Fabricate(:user) }
55
let(:users) { Fabricate.sequence(:user, 5)}
6+
let(:diffusion) { Fabricate.build(:diffusion) }
7+
8+
shared_examples 'unauthorized' do
9+
it 'redirects to the login page' do
10+
response.should redirect_to(login_path)
11+
end
12+
end
613

714
describe "GET 'new' for a single user email" do
8-
it "returns http success" do
9-
get :new, user: Array(user)
10-
response.should be_success
15+
16+
context 'when user is a moderator' do
17+
before { sign_in Fabricate(:user, moderator: true) }
18+
it "returns http success" do
19+
get :new, user: Array(user)
20+
response.should be_success
21+
end
22+
end
23+
24+
context 'when user is not a moderator' do
25+
before do
26+
sign_in Fabricate(:user)
27+
get :new, user: Array(user)
28+
end
29+
it_behaves_like 'unauthorized'
30+
end
31+
32+
context 'when user is a guest' do
33+
before { get :new, user: Array(user) }
34+
it_behaves_like 'unauthorized'
1135
end
1236
end
1337

1438
describe "GET 'new' for a diffusion" do
15-
it "returns http success" do
16-
get :new, user: Array(users)
17-
response.should be_success
39+
context 'when user is a moderator' do
40+
before { sign_in Fabricate(:user, moderator: true) }
41+
it "returns http success" do
42+
get :new, user: Array(users)
43+
response.should be_success
44+
end
45+
end
46+
47+
context 'when user is not a moderator' do
48+
before do
49+
sign_in Fabricate(:user)
50+
get :new, user: Array(users)
51+
end
52+
it_behaves_like 'unauthorized'
53+
end
54+
55+
context 'when user is a guest' do
56+
before { get :new, user: Array(users) }
57+
it_behaves_like 'unauthorized'
1858
end
1959
end
2060

@@ -23,37 +63,68 @@
2363
@message = Fabricate.build(:message)
2464
end
2565

26-
it 'delivers the email' do
27-
expect {
28-
post :create, message: @message
29-
}.to change {ActionMailer::Base.deliveries.size}.by(1)
66+
context 'when user is a moderator' do
67+
before { sign_in Fabricate(:user, moderator: true) }
68+
it 'delivers the email' do
69+
expect {
70+
post :create, message: @message
71+
}.to change {ActionMailer::Base.deliveries.size}.by(1)
72+
end
73+
74+
describe 'delivered message' do
75+
before :each do
76+
post :create, message: @message
77+
end
78+
79+
it "returns http success" do
80+
response.should be_redirect
81+
end
82+
83+
it 'delivers the mail with the subject that we wanted to' do
84+
ActionMailer::Base.deliveries.last.subject.should == @message.subject
85+
end
86+
87+
it 'delivers the mail with the body that we wanted to' do
88+
ActionMailer::Base.deliveries.last.body.to_s.should match @message.body
89+
end
90+
end
3091
end
3192

32-
describe 'delivered message' do
33-
before :each do
93+
context 'when user is not a moderator' do
94+
before do
95+
sign_in Fabricate(:user)
3496
post :create, message: @message
3597
end
98+
it_behaves_like 'unauthorized'
99+
end
36100

37-
it "returns http success" do
38-
response.should be_redirect
39-
end
101+
context 'when user is a guest' do
102+
before { post :create, message: @message }
103+
it_behaves_like 'unauthorized'
104+
end
105+
end
40106

41-
it 'delivers the mail with the subject that we wanted to' do
42-
ActionMailer::Base.deliveries.last.subject.should == @message.subject
107+
describe "POST 'create' for a diffusion" do
108+
context 'when user is a moderator' do
109+
before { sign_in Fabricate(:user, moderator: true) }
110+
it "returns http success" do
111+
expect {
112+
post 'create', message: diffusion
113+
}.to change {ActionMailer::Base.deliveries.size}.by(diffusion.email.size)
43114
end
115+
end
44116

45-
it 'delivers the mail with the body that we wanted to' do
46-
ActionMailer::Base.deliveries.last.body.to_s.should match @message.body
117+
context 'when user is not a moderator' do
118+
before do
119+
sign_in Fabricate(:user)
120+
post :create, message: diffusion
47121
end
122+
it_behaves_like 'unauthorized'
48123
end
49-
end
50124

51-
describe "POST 'create' for a diffusion" do
52-
it "returns http success" do
53-
@diffusion = Fabricate.build(:diffusion)
54-
expect {
55-
post 'create', message: @diffusion
56-
}.to change {ActionMailer::Base.deliveries.size}.by(@diffusion.email.size)
125+
context 'when user is a guest' do
126+
before { post :create, message: diffusion }
127+
it_behaves_like 'unauthorized'
57128
end
58129
end
59130
end

0 commit comments

Comments
 (0)