Prior to this commit, we were somewhat naively rendering Hash style data attributes in our ERB templates. By rendering each hash attribute separately, we were rendering characters that could break the javascript (e.g. double hack or backslash `"` or `\`). By moving to this view_object rendering, we leverage Rails's `to_json` behavior to ensure properly escaped values. As part of this exercise, I generalized the method to allow for other places to benefit from this behavior. This generalization also helps ensure that we have a more conformant rendering (e.g. we should always have an :id, :className, and :name value in our data-info hash). _Note: I've updated the user's names for Cypress tests as they are more likely to catch the particular issue than anything else. I assume that I'm going to break some cypress tests and will need some help fixing them._ Closes #15916, #14704 Supersedes #15983 How to test locally: Assuming you have seeded database (e.g. `rails db:seed`), checkout the "main" branch. Then in `rails console` find a user that's written articles: ```ruby user = Article.last.user user.update(name: "\\: #{user.name}") user.articles.each(&:save) ``` Now, again on the "main" branch, start your application (e.g., `bin/startup`). Then get a logged in and a logged out browser session going. Open your web inspector and open console. Then go to the local instances homepage (e.g., http://localhost:3000) and look for JS errors. On the main branch, you should see an exception around `JSON.parse(button.data.info)` (assuming that the `user`'s article is rendered on the homepage). Then go to the user's page (e.g. https://localhost:3000/:user-slug) and look for JS parse errors. On this PR's branch (e.g., `jeremyf/take-two-at-resolving-gh-15916`) you shouldn't see those console errors. More importantly, the Follow buttons should work.
182 lines
5.2 KiB
Ruby
182 lines
5.2 KiB
Ruby
FactoryBot.define do
|
|
sequence(:email) { |n| "person#{n}@example.com" }
|
|
sequence(:username) { |n| "username#{n}" }
|
|
sequence(:twitter_username) { |n| "twitter#{n}" }
|
|
sequence(:github_username) { |n| "github#{n}" }
|
|
|
|
image_path = Rails.root.join("spec/support/fixtures/images/image1.jpeg")
|
|
|
|
factory :user do
|
|
# Creating a name that has includes double quotes and backslashes.
|
|
# This way we can see if things are parsing correctly.
|
|
name do
|
|
"#{Faker::Name.first_name} \"#{Faker::Name.first_name}\" \\:/ #{Faker::Name.last_name}"
|
|
end
|
|
email { generate :email }
|
|
username { generate :username }
|
|
profile_image { Rack::Test::UploadedFile.new(image_path, "image/jpeg") }
|
|
twitter_username { generate :twitter_username }
|
|
github_username { generate :github_username }
|
|
confirmed_at { Time.current }
|
|
saw_onboarding { true }
|
|
checked_code_of_conduct { true }
|
|
checked_terms_and_conditions { true }
|
|
registered_at { Time.current }
|
|
signup_cta_variant { "navbar_basic" }
|
|
|
|
trait :with_identity do
|
|
transient { identities { Authentication::Providers.available } }
|
|
|
|
after(:create) do |user, options|
|
|
options.identities.each do |provider|
|
|
auth = OmniAuth.config.mock_auth.fetch(provider.to_sym)
|
|
create(
|
|
:identity,
|
|
user: user, provider: provider, uid: auth.uid, auth_data_dump: auth,
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
trait :with_broken_identity do
|
|
# Mimics a situation that can occur in production
|
|
transient { identities { Authentication::Providers.available } }
|
|
|
|
after(:create) do |user, options|
|
|
options.identities.each do |provider|
|
|
auth = OmniAuth.config.mock_auth.fetch(provider.to_sym)
|
|
create(
|
|
:identity,
|
|
user: user, provider: provider, uid: auth.uid, auth_data_dump: nil,
|
|
)
|
|
end
|
|
end
|
|
end
|
|
|
|
trait :super_admin do
|
|
after(:build) { |user| user.add_role(:super_admin) }
|
|
end
|
|
|
|
trait :creator do
|
|
after(:build) do |user|
|
|
user.add_role(:super_admin)
|
|
user.add_role(:creator)
|
|
end
|
|
end
|
|
|
|
trait :admin do
|
|
after(:build) { |user| user.add_role(:admin) }
|
|
end
|
|
|
|
trait :single_resource_admin do
|
|
transient do
|
|
resource { nil }
|
|
end
|
|
|
|
after(:build) { |user, options| user.add_role(:single_resource_admin, options.resource) }
|
|
end
|
|
|
|
trait :tech_admin do
|
|
after(:build) { |user| user.add_role(:tech_admin) }
|
|
end
|
|
|
|
trait :restricted_liquid_tag do
|
|
transient do
|
|
resource { nil }
|
|
end
|
|
|
|
after(:build) { |user, options| user.add_role(:restricted_liquid_tag, options.resource) }
|
|
end
|
|
|
|
trait :super_plus_single_resource_admin do
|
|
transient do
|
|
resource { nil }
|
|
end
|
|
|
|
after(:build) do |user, options|
|
|
user.add_role(:super_admin)
|
|
user.add_role(:single_resource_admin, options.resource)
|
|
end
|
|
end
|
|
|
|
trait :trusted do
|
|
after(:build) { |user| user.add_role(:trusted) }
|
|
end
|
|
|
|
trait :suspended do
|
|
after(:build) { |user| user.add_role(:suspended) }
|
|
end
|
|
|
|
trait :invited do
|
|
after(:build) do |user|
|
|
user.registered = false
|
|
user.registered_at = nil
|
|
end
|
|
end
|
|
|
|
trait :ignore_mailchimp_subscribe_callback do
|
|
after(:build) do |user|
|
|
# rubocop:disable Lint/EmptyBlock
|
|
user.define_singleton_method(:subscribe_to_mailchimp_newsletter) {}
|
|
# rubocop:enable Lint/EmptyBlock
|
|
# user.class.skip_callback(:validates, :after_create)
|
|
end
|
|
end
|
|
|
|
trait :org_member do
|
|
after(:create) do |user|
|
|
org = create(:organization)
|
|
create(:organization_membership, user_id: user.id, organization_id: org.id, type_of_user: "member")
|
|
end
|
|
end
|
|
|
|
trait :org_admin do
|
|
after(:create) do |user|
|
|
org = create(:organization)
|
|
create(:organization_membership, user_id: user.id, organization_id: org.id, type_of_user: "admin")
|
|
end
|
|
end
|
|
|
|
trait :with_article do
|
|
after(:create) do |user|
|
|
create(:article, user_id: user.id)
|
|
user.update(articles_count: 1)
|
|
end
|
|
end
|
|
|
|
trait :with_only_comment do
|
|
after(:create) do |user|
|
|
other_user = create(:user)
|
|
article = create(:article, user_id: other_user.id)
|
|
create(:comment, user_id: user.id, commentable: article)
|
|
user.update(comments_count: 1)
|
|
end
|
|
end
|
|
|
|
trait :with_article_and_comment do
|
|
after(:create) do |user|
|
|
article = create(:article, user_id: user.id)
|
|
create(:comment, user_id: user.id, commentable: article)
|
|
user.update(articles_count: 1, comments_count: 1)
|
|
end
|
|
end
|
|
|
|
trait :tag_moderator do
|
|
after(:create) do |user|
|
|
tag = create(:tag)
|
|
user.add_role(:tag_moderator, tag)
|
|
end
|
|
end
|
|
|
|
trait :without_profile do
|
|
_skip_creating_profile { true }
|
|
end
|
|
|
|
trait :with_newsletters do
|
|
after(:create) do |user|
|
|
Users::NotificationSetting.find_by(user_id: user.id)
|
|
.update_columns(email_newsletter: true, email_digest_periodic: true)
|
|
end
|
|
end
|
|
end
|
|
end
|