|
3 | 3 | describe MailerController do |
4 | 4 | let(:user) { Fabricate(:user) } |
5 | 5 | 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 |
6 | 13 |
|
7 | 14 | 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' |
11 | 35 | end |
12 | 36 | end |
13 | 37 |
|
14 | 38 | 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' |
18 | 58 | end |
19 | 59 | end |
20 | 60 |
|
|
23 | 63 | @message = Fabricate.build(:message) |
24 | 64 | end |
25 | 65 |
|
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 |
30 | 91 | end |
31 | 92 |
|
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) |
34 | 96 | post :create, message: @message |
35 | 97 | end |
| 98 | + it_behaves_like 'unauthorized' |
| 99 | + end |
36 | 100 |
|
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 |
40 | 106 |
|
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) |
43 | 114 | end |
| 115 | + end |
44 | 116 |
|
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 |
47 | 121 | end |
| 122 | + it_behaves_like 'unauthorized' |
48 | 123 | end |
49 | | - end |
50 | 124 |
|
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' |
57 | 128 | end |
58 | 129 | end |
59 | 130 | end |
0 commit comments