2015-10-11 17:41:23 +08:00
|
|
|
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
|
2018-01-12 11:15:10 +08:00
|
|
|
get :preferences_redirect, format: :json
|
|
|
|
expect(response.status).to eq(403)
|
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
|
2017-08-31 12:06:56 +08:00
|
|
|
get :preferences_redirect, format: :json
|
2017-03-28 03:34:54 +08:00
|
|
|
expect(response).to redirect_to("/u/#{user.username}/preferences")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
context '.unsubscribe' do
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
render_views
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
it 'displays logo ut button if wrong user logged in' do
|
|
|
|
log_in_user Fabricate(:admin)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
key = UnsubscribeKey.create_key_for(user, "digest")
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
|
|
|
|
expect(response.body).to include(I18n.t("unsubscribe.log_out"))
|
|
|
|
expect(response.body).to include(I18n.t("unsubscribe.different_user_description"))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
it 'displays not found if key is not found' do
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: SecureRandom.hex }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).to include(CGI.escapeHTML(I18n.t("unsubscribe.not_found_description")))
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-06-17 09:27:52 +08:00
|
|
|
it 'correctly handles mailing list mode' do
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
key = UnsubscribeKey.create_key_for(user, "digest")
|
|
|
|
|
2016-06-17 09:57:23 +08:00
|
|
|
user.user_option.update_columns(mailing_list_mode: true)
|
2016-06-17 09:27:52 +08:00
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).to include(I18n.t("unsubscribe.mailing_list_mode"))
|
|
|
|
|
|
|
|
SiteSetting.disable_mailing_list_mode = true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).not_to include(I18n.t("unsubscribe.mailing_list_mode"))
|
|
|
|
|
2016-06-17 09:57:23 +08:00
|
|
|
user.user_option.update_columns(mailing_list_mode: false)
|
2016-06-17 09:27:52 +08:00
|
|
|
SiteSetting.disable_mailing_list_mode = false
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).not_to include(I18n.t("unsubscribe.mailing_list_mode"))
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly handles digest unsubscribe' do
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_option.update_columns(email_digests: false)
|
|
|
|
key = UnsubscribeKey.create_key_for(user, "digest")
|
|
|
|
|
|
|
|
# because we are type digest we will always show digest and it will be selected
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).to include(I18n.t("unsubscribe.disable_digest_emails"))
|
|
|
|
|
|
|
|
source = Nokogiri::HTML::fragment(response.body)
|
|
|
|
expect(source.css("#disable_digest_emails")[0]["checked"]).to eq("checked")
|
|
|
|
|
|
|
|
SiteSetting.disable_digest_emails = true
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).not_to include(I18n.t("unsubscribe.disable_digest_emails"))
|
|
|
|
|
|
|
|
SiteSetting.disable_digest_emails = false
|
|
|
|
key = UnsubscribeKey.create_key_for(user, "not_digest")
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).to include(I18n.t("unsubscribe.disable_digest_emails"))
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly handles watched categories' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user = post.user
|
|
|
|
cu = CategoryUser.create!(user_id: user.id,
|
2017-07-28 09:20:09 +08:00
|
|
|
category_id: post.topic.category_id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching])
|
2016-06-17 09:27:52 +08:00
|
|
|
|
|
|
|
key = UnsubscribeKey.create_key_for(user, post)
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).to include("unwatch_category")
|
|
|
|
|
|
|
|
cu.destroy!
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-06-17 09:27:52 +08:00
|
|
|
expect(response.body).not_to include("unwatch_category")
|
|
|
|
|
|
|
|
end
|
2016-07-06 03:16:32 +08:00
|
|
|
|
|
|
|
it 'correctly handles watched first post categories' do
|
|
|
|
post = Fabricate(:post)
|
|
|
|
user = post.user
|
|
|
|
cu = CategoryUser.create!(user_id: user.id,
|
2017-07-28 09:20:09 +08:00
|
|
|
category_id: post.topic.category_id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching_first_post])
|
2016-07-06 03:16:32 +08:00
|
|
|
|
|
|
|
key = UnsubscribeKey.create_key_for(user, post)
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-07-06 03:16:32 +08:00
|
|
|
expect(response.body).to include("unwatch_category")
|
|
|
|
|
|
|
|
cu.destroy!
|
|
|
|
|
2017-08-31 12:06:56 +08:00
|
|
|
get :unsubscribe, params: { key: key }
|
2016-07-06 03:16:32 +08:00
|
|
|
expect(response.body).not_to include("unwatch_category")
|
|
|
|
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|