Repository navigation
Protect featured posts and hidden-post indexing #75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3db5de2
Protect featured posts across account and publication boundaries
philipithomas 5c6c199
Allow legacy account edits and guard marketing featured posts
philipithomas 4b600d1
Sync privacy safeguards with current main
philipithomas da3e145
Preserve privacy guards alongside restored image safety
philipithomas 6a5297e
Read current featured content instead of cached account records
philipithomas d6774a3
Keep featured-content assertions independent of typography
philipithomas 05e2ee2
Assert featured privacy across both homepage designs
philipithomas 6d89aeb
Merge main into content privacy and resolve integration conflicts
philipithomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
| require 'minitest/mock' | ||
|
|
||
| class MarketingFeaturedCacheTest < ActionDispatch::IntegrationTest | ||
| setup do | ||
| @original_solo_mode = Rails.configuration.solo_mode | ||
| @original_multiuser_mode = Rails.configuration.multiuser_mode | ||
| @original_cache = Rails.cache | ||
| Rails.configuration.solo_mode = false | ||
| Rails.configuration.multiuser_mode = true | ||
| Rails.cache = ActiveSupport::Cache::MemoryStore.new | ||
| @account = accounts(:grandfathered_user) | ||
| @account.update!(slug: 'philipithomas') | ||
| File.open(Rails.root.join('app/assets/images/logo/icon.png')) do |photo| | ||
| @account.photo.attach(io: photo, filename: 'profile.png', content_type: 'image/png') | ||
| end | ||
| @post = @account.posts.create!( | ||
| subject: 'Cached feature original title', body: 'Cached feature original body', | ||
| published_at: Time.current | ||
| ) | ||
| @account.update!(pinned_post: @post) | ||
| host! Rails.configuration.base_host | ||
| end | ||
|
|
||
| teardown do | ||
| @account.photo.purge | ||
| Rails.configuration.solo_mode = @original_solo_mode | ||
| Rails.configuration.multiuser_mode = @original_multiuser_mode | ||
| Rails.cache = @original_cache | ||
| end | ||
|
|
||
| { | ||
| 'hidden' => { visibility: :hidden }, | ||
| 'unpublished' => { published_at: nil }, | ||
| 'archived' => { archived: true } | ||
| }.each do |state, attributes| | ||
| test "warm marketing cache stops exposing a post immediately after it becomes #{state}" do | ||
| with_production_cache do | ||
| get '/' | ||
| assert_featured_content(@post) | ||
|
|
||
| # Exercise real persistence and pin-removal callbacks after warming the | ||
| # cache. Neither the test nor production code clears the cache. | ||
| @post.update!(attributes) | ||
|
|
||
| get '/' | ||
| assert_feature_absent(@post) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| test 'warm marketing cache immediately reflects removal of the featured pin' do | ||
| with_production_cache do | ||
| get '/' | ||
| assert_featured_content(@post) | ||
|
|
||
| @account.update!(pinned_post: nil) | ||
|
|
||
| get '/' | ||
| # The public "Read newest post" link can remain after unpinning; the | ||
| # hero card and social preview must stop featuring the post. | ||
| assert_feature_absent(@post, check_url: false) | ||
| end | ||
| end | ||
|
|
||
| test 'warm marketing cache shows a replacement pin instead of the previous post' do | ||
| with_production_cache do | ||
| get '/' | ||
| assert_featured_content(@post) | ||
|
|
||
| replacement = @account.posts.create!( | ||
| subject: 'Replacement feature title', body: 'Replacement feature body', published_at: Time.current | ||
| ) | ||
| @account.update!(pinned_post: replacement) | ||
|
|
||
| get '/' | ||
| assert_feature_absent(@post) | ||
| assert_featured_content(replacement) | ||
| end | ||
| end | ||
|
|
||
| test 'a legacy cached account cannot disclose a post hidden after the entry was written' do | ||
| with_production_cache do | ||
| Rails.cache.write('homepage-featured-account', Account.includes(:pinned_post).find(@account.id)) | ||
| @post.update!(visibility: :hidden) | ||
|
|
||
| get '/' | ||
| assert_feature_absent(@post) | ||
| end | ||
| end | ||
|
|
||
| test 'warm marketing cache stops featuring an account after it is locked' do | ||
| with_production_cache do | ||
| get '/' | ||
| assert_featured_content(@post) | ||
| @account.update!(locked_at: Time.current) | ||
|
|
||
| get '/' | ||
| assert_feature_absent(@post) | ||
| assert_not_includes response.body, @account.name | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def with_production_cache(&block) | ||
| # Keep the test environment and its offline mail/job adapters. Only select | ||
| # the production cache path after the app has already booted normally. | ||
| Rails.env.stub(:production?, true, &block) | ||
| end | ||
|
|
||
| def assert_featured_content(post) | ||
| assert_response :success | ||
| assert_select 'p', text: "I just published \"#{post.subject}\" on my website - check it out:", count: 1 | ||
| assert_select 'img[alt=?]', post.subject, count: 1 | ||
| assert_includes response.body, post.slug | ||
| end | ||
|
|
||
| def assert_feature_absent(post, check_url: true) | ||
| assert_response :success | ||
| assert_not_includes response.body, post.subject | ||
| assert_not_includes response.body, post.body.to_plain_text | ||
| assert_not_includes response.body, post.slug if check_url | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'test_helper' | ||
|
|
||
| class PinnedPostsControllerTest < ActionDispatch::IntegrationTest | ||
| include Devise::Test::IntegrationHelpers | ||
|
|
||
| setup do | ||
| @original_solo_mode = Rails.configuration.solo_mode | ||
| @original_multiuser_mode = Rails.configuration.multiuser_mode | ||
| Rails.configuration.solo_mode = false | ||
| Rails.configuration.multiuser_mode = true | ||
| @account = accounts(:new_user) | ||
| @other_account = accounts(:grandfathered_user) | ||
| host! Rails.configuration.base_host | ||
| sign_in @account | ||
| end | ||
|
|
||
| teardown do | ||
| Rails.configuration.solo_mode = @original_solo_mode | ||
| Rails.configuration.multiuser_mode = @original_multiuser_mode | ||
| end | ||
|
|
||
| test 'can feature an owned public post and remove the feature' do | ||
| post = create_post(@account) | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: post.id } } | ||
|
|
||
| assert_redirected_to page_posts_path(@account) | ||
| assert_equal post.id, @account.reload.pinned_post_id | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: '' } } | ||
|
|
||
| assert_redirected_to page_posts_path(@account) | ||
| assert_nil @account.reload.pinned_post_id | ||
| end | ||
|
|
||
| test 'can continue to feature an owned unlisted post' do | ||
| post = create_post(@account, visibility: :unlisted) | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: post.id } } | ||
|
|
||
| assert_redirected_to page_posts_path(@account) | ||
| assert_equal post.id, @account.reload.pinned_post_id | ||
| end | ||
|
|
||
| test 'cannot feature another account published post or draft' do | ||
| [Time.current, nil].each do |published_at| | ||
| post = create_post(@other_account, published_at: published_at) | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: post.id } } | ||
|
|
||
| assert_response :unprocessable_entity | ||
| assert_nil @account.reload.pinned_post_id | ||
| end | ||
| end | ||
|
|
||
| test 'cannot feature a draft hidden or archived post' do | ||
| [{ published_at: nil }, { visibility: :hidden }, { archived: true }].each do |attributes| | ||
| post = create_post(@account, **attributes) | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: post.id } } | ||
|
|
||
| assert_response :unprocessable_entity | ||
| assert_nil @account.reload.pinned_post_id | ||
| end | ||
| end | ||
|
|
||
| test 'invalid pin leaves the existing feature unchanged' do | ||
| existing_post = create_post(@account) | ||
| @account.update!(pinned_post: existing_post) | ||
|
|
||
| put page_account_path(@account), params: { account: { pinned_post_id: -1 } } | ||
|
|
||
| assert_response :unprocessable_entity | ||
| assert_equal existing_post.id, @account.reload.pinned_post_id | ||
| end | ||
|
|
||
| test 'cannot update the feature on another account' do | ||
| post = create_post(@other_account) | ||
|
|
||
| put page_account_path(@other_account), params: { account: { pinned_post_id: post.id } } | ||
|
|
||
| assert_redirected_to page_path(@account) | ||
| assert_nil @other_account.reload.pinned_post_id | ||
| end | ||
|
|
||
| test 'public homepage does not disclose a foreign draft from a historical invalid pin' do | ||
| post = create_post(@other_account, published_at: nil, subject: 'Confidential draft', body: 'Private draft content') | ||
| @account.update_column(:pinned_post_id, post.id) | ||
| sign_out @account | ||
| host! @account.postcard_host | ||
|
|
||
| get root_path | ||
|
|
||
| assert_response :success | ||
| assert_no_match 'Confidential draft', response.body | ||
| assert_no_match 'Private draft content', response.body | ||
| end | ||
|
|
||
| test 'marketing homepage excludes historical invalid featured pins' do | ||
| @account.update!(slug: 'philipithomas') | ||
| post = create_post(@other_account, published_at: nil, subject: 'Confidential draft', body: 'Private draft content') | ||
| @account.update_column(:pinned_post_id, post.id) | ||
| sign_out @account | ||
|
|
||
| get '/' | ||
|
|
||
| assert_response :success | ||
| assert_no_match 'Confidential draft', response.body | ||
| assert_no_match 'Private draft content', response.body | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_post(account, **attributes) | ||
| account.posts.create!({ subject: 'A post', body: 'Post content', published_at: Time.current }.merge(attributes)) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.