docbrown/spec/models/application_record_spec.rb
Jeremy Friesen 3253ba2c7a
Patching ERB rendering of the data-info JSON (#16067)
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.
2022-01-14 08:30:49 -05:00

66 lines
1.9 KiB
Ruby

require "rails_helper"
# ApplicationRecord is an abstract class, tests will use one of the core models
RSpec.describe ApplicationRecord, type: :model do
describe ".estimated_count" do
it "does not raise errors if there are no rows" do
expect { User.estimated_count }.not_to raise_error
end
end
describe "#class_name" do
it "is expected to be a string" do
user = User.new
expect(user.class_name).to eq("User")
end
end
describe "#decorate" do
it "decorates an object that has a decorator" do
sponsorship = build(:sponsorship)
expect(sponsorship.decorate).to be_a(SponsorshipDecorator)
end
it "raises an error if an object has no decorator" do
badge = build(:badge)
expect { badge.decorate }.to raise_error(UninferrableDecoratorError)
end
end
describe "#decorated?" do
it "returns false" do
sponsorship = build(:sponsorship)
expect(sponsorship.decorated?).to be(false)
end
end
describe ".decorate" do
before do
create(:sponsorship, level: :gold)
end
it "decorates a relation" do
decorated_collection = Sponsorship.gold.decorate
expect(decorated_collection.size).to eq(Sponsorship.gold.size)
expect(decorated_collection.first).to be_a(SponsorshipDecorator)
end
end
describe ".with_statement_timeout" do
it "sets the SQL statement timeout to the specified duration" do
original_timeout = described_class.statement_timeout
described_class.with_statement_timeout 1.second do
expect(described_class.statement_timeout).to eq 1.second
described_class.with_statement_timeout 10.seconds do
expect(described_class.statement_timeout).to eq 10.seconds
end
expect(described_class.statement_timeout).to eq 1.second
end
expect(described_class.statement_timeout).to eq original_timeout
end
end
end