2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe EmailController do
|
|
|
|
|
|
|
|
context '.preferences_redirect' do
|
|
|
|
|
|
|
|
it 'requires you to be logged in' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect { get :preferences_redirect }.to raise_error(Discourse::NotLoggedIn)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it 'redirects to your user preferences' do
|
|
|
|
get :preferences_redirect
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to redirect_to("/users/#{user.username}/preferences")
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.resubscribe' do
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
let(:user) {
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_option.update_columns(email_digests: false)
|
|
|
|
user
|
|
|
|
}
|
2015-02-13 14:15:49 -05:00
|
|
|
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
context 'with a valid key' do
|
|
|
|
before do
|
2015-02-13 14:15:49 -05:00
|
|
|
get :resubscribe, key: key
|
2013-02-05 14:16:51 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'subscribes the user' do
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.email_digests).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context '.unsubscribe' do
|
|
|
|
|
2016-02-16 23:46:19 -05:00
|
|
|
let(:user) {
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_option.update_columns(email_always: true, email_digests: true, email_direct: true, email_private_messages: true)
|
|
|
|
user
|
|
|
|
}
|
2015-02-13 14:15:49 -05:00
|
|
|
let(:key) { DigestUnsubscribeKey.create_key_for(user) }
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2016-01-20 04:25:25 -05:00
|
|
|
context 'from confirm unsubscribe email' do
|
|
|
|
before do
|
|
|
|
get :unsubscribe, key: key, from_all: true
|
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unsubscribes from all emails' do
|
2016-02-16 23:46:19 -05:00
|
|
|
options = user.user_option
|
|
|
|
expect(options.email_digests).to eq false
|
|
|
|
expect(options.email_direct).to eq false
|
|
|
|
expect(options.email_private_messages).to eq false
|
|
|
|
expect(options.email_always).to eq false
|
2016-01-20 04:25:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'with a valid key' do
|
|
|
|
before do
|
2015-02-13 14:15:49 -05:00
|
|
|
get :unsubscribe, key: key
|
2013-02-05 14:16:51 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unsubscribes the user' do
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.email_digests).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:19:45 -04:00
|
|
|
it "sets the appropriate instance variables" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:success)).to be_present
|
2014-07-15 17:19:45 -04:00
|
|
|
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-09 12:04:02 -05:00
|
|
|
expect(assigns(:success)).to be_blank
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in as a different user' do
|
|
|
|
let!(:logged_in_user) { log_in(:coding_horror) }
|
|
|
|
|
|
|
|
before do
|
2015-02-13 14:15:49 -05:00
|
|
|
get :unsubscribe, key: key
|
2013-02-05 14:16:51 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not unsubscribe the user' do
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.email_digests).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:19:45 -04:00
|
|
|
it 'sets the appropriate instance variables' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:success)).to be_blank
|
|
|
|
expect(assigns(:different_user)).to be_present
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in as the keyed user' do
|
|
|
|
|
|
|
|
before do
|
|
|
|
log_in_user(user)
|
2015-02-13 14:15:49 -05:00
|
|
|
get :unsubscribe, key: DigestUnsubscribeKey.create_key_for(user)
|
2013-02-05 14:16:51 -05:00
|
|
|
user.reload
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'unsubscribes the user' do
|
2016-02-16 23:46:19 -05:00
|
|
|
expect(user.user_option.email_digests).to eq(false)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2014-07-15 17:19:45 -04:00
|
|
|
it 'sets the appropriate instance variables' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:success)).to be_present
|
2013-02-25 11:42:20 -05:00
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "sets not_found when the key didn't match anything" do
|
|
|
|
get :unsubscribe, key: 'asdfasdf'
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:not_found)).to eq(true)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|