docbrown/spec/services/mailchimp/bot_spec.rb
Molly Struve e87dead7ad
Use new UserSetting and UserNotificationSettings and Ignore Related User Table Fields (#14121)
* schema file undelete description

* feat: v1 of the script

* Flesh out remaining enums under their categories

* complete UsersSettings data update script

* complete DUS for relevant attributes in users and profiles tables

* complete DUS for users_notification_settings

* alphabetize user_settings sql file

* safeguard against null values for "null: false" settings

* Set up actual UsersSettings DUS and specs files

* fix broken DUS script

* complete specs for UsersSetting DUS

* Address QA of specs

* complete specs for users_notification_settings DUS

* fix the typos (thanks Julianna!)

* begin implementation

* still building

* add missing attribute "email_membership_newsletter"

* complete sync code (except race condition for user profile)

* complete implementation, remains tests

* Address PR review and fix Travis fails

* remove superfluous Profile.new

* fix travis fails

* feat: update the users_notification_setting attributes from the user model

* feat: use the config fonts enums to display the fonts

* feat: loop through the keys

* fix profile = nil blowing up; add specs for notification_setting model

* remove unneeded spec

* remove feed validation until after sync code removed; fixes feed_import spec failures

* remove spec associated with feed_url validation in user_setting model

* fix failing spec 😅

* add TODO

* feat: set the user settings in the user controller  and use it in the customization form

* feat: move some update logic to the users settings controller thats being used from customization

* feat: show the updated values form the users_settingd and not the user instance

* Generalize redirect back to current tab

* still trying to reflect changed theme upon refresh

* customizations take effect on refresh

* remove 'with_feed' scope from user model

Co-authored-by: Jamie Gaskins <jamie@forem.com>

* start with takeover for fields previously in profiles table

* Takeover code for `publishing_from_rss` section in Settings (#13914)

* implement takeover code part 1

* implement takeover code

* fix feed fetch

* need rhymes help

* complete implementation; specs pending

* fix STUPID omission that caused so many headaches 😫

* implement profile fields pointing to users_settings 🎉

* run migrations

* implement inbox type & guidelines takeover code; specs pending (#13911)

* Point changes in notification settings to `users_notification_settings` table (#13910)

* implement takeover code; remains specs

* address PR feedback; remove related sync code

* address PR review feedback

* need help with routing and specs

* address pr review

* addressing pr review

* Treat implementation edge cases and omissions 😅

* fix uncommented comment

* fixing implementation cases

* address more PR review feedback

* fixing notifications use-cases

* refactor settings controller

* more pr review changes

* solving bugs

* fix broken onboarding

* handle eperience_level calls

* more fixes

* remove unneeded mappings

* add To-dos for quety updates

* remove done TODO

* purge done TODOs

* update notification_settings-related queries

* start fixing specs

* fixing specs

* fix notification and lrg_forem specs

* fixing broken specs

* still fixing

* fix line dif and remove reloads from user.rb

* run specs

* silence bullet and other fixes

* remove setting migration scripts and specs, fix more settings for specs

* handle missing user for article builder and fix notification specs

* fix some final controller specs and re-add incorrectly removed specs

* remove deprecated data update scripts and related workers, put travis back

* refactor admin tags mods controller, write/move specs for users notifications settings controller

* schema cleanup and other small refactors for consistency

* set field we can invalidate in spec via active record instead of at the db level

* remove I think an uneccessary hook call from subscribe_to_mailchimp_newsletter

* use bnefore_create to setup settings, please dont blow up the test suite

* mailchimp bot fix

* remove decorator in favor of single model method

Co-authored-by: Arit Amana <msarit@gmail.com>
Co-authored-by: Ridhwana <ridhwana.khan16@gmail.com>
Co-authored-by: Arit Amana <32520970+msarit@users.noreply.github.com>
Co-authored-by: Jamie Gaskins <jamie@forem.com>
2021-07-08 09:31:34 -05:00

151 lines
5.3 KiB
Ruby

require "rails_helper"
RSpec.describe Mailchimp::Bot, type: :service do
let(:user) { create(:user, :with_newsletters, :ignore_mailchimp_subscribe_callback) }
let(:article) { create(:article, user_id: user.id) }
let(:my_gibbon_client) { class_double Gibbon::Request }
let(:tag) do
create(:tag,
name: "tagname",
bg_color_hex: Faker::Color.hex_color,
text_color_hex: Faker::Color.hex_color,
supported: true)
end
before do
allow(Gibbon::Request).to receive(:new) { my_gibbon_client }
allow(my_gibbon_client).to receive(:lists) { my_gibbon_client }
allow(my_gibbon_client).to receive(:tag_mods) { my_gibbon_client }
allow(my_gibbon_client).to receive(:community_mods) { my_gibbon_client }
allow(my_gibbon_client).to receive(:members) { my_gibbon_client }
allow(my_gibbon_client).to receive(:upsert).and_return(true)
end
def matcher
{
body: {
email_address: user.email,
status: "subscribed",
merge_fields: {
NAME: user.name.to_s,
USERNAME: user.username.to_s,
TWITTER: user.twitter_username.to_s,
GITHUB: user.github_username.to_s,
IMAGE_URL: user.profile_image_url.to_s,
ARTICLES: user.articles.size,
COMMENTS: user.comments.size,
ONBOARD_PK: user.onboarding_package_requested.to_s,
EXPERIENCE: user.setting.experience_level || 666
}
}
}
end
describe "#upsert" do
it "works" do
described_class.new(user).upsert
expect(my_gibbon_client).to have_received(:upsert)
end
end
describe "#upsert_to_newsletter" do
it "sends proper information" do
user.notification_setting.update(email_newsletter: true)
described_class.new(user).upsert_to_newsletter
expect(my_gibbon_client).to have_received(:upsert).with(matcher)
end
it "unsubscribes properly" do
user.notification_setting.update(email_newsletter: false)
described_class.new(user).upsert_to_newsletter
expect(my_gibbon_client).to have_received(:upsert)
.with(hash_including(body: hash_including(status: "unsubscribed")))
end
it "subscribes properly" do
user.notification_setting.update(email_newsletter: false)
user.notification_setting.update(email_newsletter: true)
described_class.new(user).upsert_to_newsletter
expect(my_gibbon_client).to have_received(:upsert)
.with(hash_including(body: hash_including(status: "subscribed")))
end
it "updates email properly" do
user.update(email: Faker::Internet.email)
user.confirm
described_class.new(user).upsert_to_newsletter
expect(my_gibbon_client).to have_received(:upsert)
.with(hash_including(body: hash_including(email_address: user.email)))
end
it "tries to resubscribe the user if the user has previously been subscribed" do
user.notification_setting.update(email_newsletter: false)
mailchimp_bot = described_class.new(user)
mc_error =
Gibbon::MailChimpError.new("Error", status_code: 400, title: "Member In Compliance State")
allow(mailchimp_bot.gibbon).to receive(:upsert).and_raise(mc_error)
allow(mailchimp_bot).to receive(:resubscribe_to_newsletter)
mailchimp_bot.upsert_to_newsletter
expect(mailchimp_bot).to have_received(:resubscribe_to_newsletter)
end
end
describe "manage community moderator list" do
before { Settings::General.mailchimp_community_moderators_id = "something" }
after { Settings::General.mailchimp_community_moderators_id = nil }
it "returns false if user isn't a community moderator" do
expect(described_class.new(user).manage_community_moderator_list).to be(false)
end
it "sends proper information" do
user.notification_setting.update(email_community_mod_newsletter: true)
user.add_role(:trusted)
Settings::General.mailchimp_community_moderators_id = "something"
described_class.new(user).manage_community_moderator_list
expect(my_gibbon_client).to have_received(:upsert)
.with(hash_including(
body: hash_including(
status: "subscribed",
),
))
end
end
describe "manage tag moderator list" do
before { Settings::General.mailchimp_tag_moderators_id = "something" }
after { Settings::General.mailchimp_tag_moderators_id = nil }
it "returns false if user isn't a tag moderator" do
expect(described_class.new(user).manage_tag_moderator_list).to be(false)
end
it "sends proper information" do
user.notification_setting.update(email_tag_mod_newsletter: true)
user.add_role(:tag_moderator, tag)
described_class.new(user).manage_tag_moderator_list
expect(my_gibbon_client).to have_received(:upsert)
.with(hash_including(
body: hash_including(
status: "subscribed",
),
))
end
end
describe "#unsubscribe_all_newsletters" do
context "when called" do
before { allow(my_gibbon_client).to receive(:update).and_return(true) }
it "unsubscribes the user from the weekly newsletter" do
described_class.new(user).unsubscribe_all_newsletters
expect(my_gibbon_client).to have_received(:update)
.with(hash_including(body: hash_including(status: "unsubscribed")))
end
end
end
end