discourse/spec/controllers/email_controller_spec.rb

113 lines
2.5 KiB
Ruby
Raw Normal View History

require 'rails_helper'
2013-02-06 03:16:51 +08:00
describe EmailController do
context '.preferences_redirect' do
it 'requires you to be logged in' do
2015-01-10 01:04:02 +08:00
expect { get :preferences_redirect }.to raise_error(Discourse::NotLoggedIn)
2013-02-06 03:16:51 +08:00
end
context 'when logged in' do
let!(:user) { log_in }
it 'redirects to your user preferences' do
get :preferences_redirect
2015-01-10 01:04:02 +08:00
expect(response).to redirect_to("/users/#{user.username}/preferences")
2013-02-06 03:16:51 +08:00
end
end
end
context '.resubscribe' do
let(:user) { Fabricate(:user, email_digests: false) }
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
2013-02-06 03:16:51 +08:00
context 'with a valid key' do
before do
get :resubscribe, key: key
2013-02-06 03:16:51 +08:00
user.reload
end
it 'subscribes the user' do
2015-01-10 01:04:02 +08:00
expect(user.email_digests).to eq(true)
2013-02-06 03:16:51 +08:00
end
end
end
context '.unsubscribe' do
let(:user) { Fabricate(:user) }
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
2013-02-06 03:16:51 +08:00
context 'with a valid key' do
before do
get :unsubscribe, key: key
2013-02-06 03:16:51 +08:00
user.reload
end
it 'unsubscribes the user' do
2015-01-10 01:04:02 +08:00
expect(user.email_digests).to eq(false)
2013-02-06 03:16:51 +08:00
end
it "sets the appropriate instance variables" do
2015-01-10 01:04:02 +08:00
expect(assigns(:success)).to be_present
end
end
context "with an expired key or invalid key" do
before do
get :unsubscribe, key: 'watwatwat'
end
it "sets the appropriate instance variables" do
2015-01-10 01:04:02 +08:00
expect(assigns(:success)).to be_blank
2013-02-06 03:16:51 +08:00
end
end
context 'when logged in as a different user' do
let!(:logged_in_user) { log_in(:coding_horror) }
before do
get :unsubscribe, key: key
2013-02-06 03:16:51 +08:00
user.reload
end
it 'does not unsubscribe the user' do
2015-01-10 01:04:02 +08:00
expect(user.email_digests).to eq(true)
2013-02-06 03:16:51 +08:00
end
it 'sets the appropriate instance variables' do
2015-01-10 01:04:02 +08:00
expect(assigns(:success)).to be_blank
expect(assigns(:different_user)).to be_present
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
context 'when logged in as the keyed user' do
before do
log_in_user(user)
get :unsubscribe, key: DigestUnsubscribeKey.create_key_for(user)
2013-02-06 03:16:51 +08:00
user.reload
end
it 'unsubscribes the user' do
2015-01-10 01:04:02 +08:00
expect(user.email_digests).to eq(false)
2013-02-06 03:16:51 +08:00
end
it 'sets the appropriate instance variables' do
2015-01-10 01:04:02 +08:00
expect(assigns(:success)).to be_present
2013-02-26 00:42:20 +08:00
end
2013-02-06 03:16:51 +08:00
end
it "sets not_found when the key didn't match anything" do
get :unsubscribe, key: 'asdfasdf'
2015-01-10 01:04:02 +08:00
expect(assigns(:not_found)).to eq(true)
2013-02-06 03:16:51 +08:00
end
end
end