models: remove belongs_to_creator macro.

The belongs_to_creator macro was used to initialize the creator_id field
to the CurrentUser. This made tests complicated because it meant you had
to create and set the current user every time you wanted to create an
object, when lead to the current user being set over and over again. It
also meant you had to constantly be aware of what the CurrentUser was in
many different contexts, which was often confusing. Setting creators
explicitly simplifies everything greatly.
This commit is contained in:
evazion
2020-01-18 15:52:01 -06:00
parent 77bf9ac7f3
commit b4ce2d83a6
86 changed files with 215 additions and 433 deletions

View File

@@ -5,6 +5,7 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
setup do
@user = create(:user)
@admin = create(:admin_user)
@bulk_update_request = create(:bulk_update_request, user: @user)
end
context "#new" do
@@ -23,12 +24,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end
context "#update" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
should "still handle enabled secondary validations correctly" do
put_auth bulk_update_request_path(@bulk_update_request.id), @user, params: {bulk_update_request: {script: "create alias zzz -> 222", skip_secondary_validations: "0"}}
@bulk_update_request.reload
@@ -43,12 +38,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end
context "#index" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
should "render" do
get bulk_update_requests_path
assert_response :success
@@ -56,12 +45,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end
context "#destroy" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for the creator" do
should "succeed" do
delete_auth bulk_update_request_path(@bulk_update_request), @user
@@ -92,12 +75,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end
context "#approve" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for a member" do
should "fail" do
post_auth approve_bulk_update_request_path(@bulk_update_request), @user

View File

@@ -188,7 +188,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
context "create action" do
should "create a comment" do
assert_difference("Comment.count", 1) do
post_auth comments_path, @user, params: {comment: FactoryBot.attributes_for(:comment, post_id: @post.id)}
post_auth comments_path, @user, params: { comment: { post_id: @post.id, body: "blah" } }
end
comment = Comment.last
assert_redirected_to post_path(comment.post)
@@ -196,7 +196,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
should "not allow commenting on nonexistent posts" do
assert_difference("Comment.count", 0) do
post_auth comments_path, @user, params: {comment: FactoryBot.attributes_for(:comment, post_id: -1)}
post_auth comments_path, @user, params: { comment: { post_id: -1, body: "blah" } }
end
assert_redirected_to comments_path
end

View File

@@ -4,9 +4,7 @@ class FavoriteGroupsControllerTest < ActionDispatch::IntegrationTest
context "The favorite groups controller" do
setup do
@user = create(:user)
as_user do
@favgroup = create(:favorite_group)
end
@favgroup = create(:favorite_group, creator: @user)
end
context "index action" do

View File

@@ -6,10 +6,8 @@ class ForumPostsControllerTest < ActionDispatch::IntegrationTest
@user = create(:user)
@other_user = create(:user)
@mod = create(:moderator_user)
as_user do
@forum_topic = create(:forum_topic, :title => "my forum topic")
@forum_post = create(:forum_post, :topic_id => @forum_topic.id, :body => "alias xxx -> yyy")
end
@forum_topic = as(@user) { create(:forum_topic, title: "my forum topic", creator: @user) }
@forum_post = as(@user) { create(:forum_post, creator: @user, topic: @forum_topic, body: "alias xxx -> yyy") }
end
context "with votes" do
@@ -78,10 +76,8 @@ class ForumPostsControllerTest < ActionDispatch::IntegrationTest
context "with private topics" do
setup do
as(@mod) do
@mod_topic = create(:mod_up_forum_topic)
@mod_posts = 2.times.map do
create(:forum_post, :topic_id => @mod_topic.id)
end
@mod_topic = create(:mod_up_forum_topic, creator: @mod)
@mod_posts = create_list(:forum_post, 2, topic: @mod_topic, creator: @mod)
end
@mod_post_ids = ([@forum_post] + @mod_posts).map(&:id).reverse
end

View File

@@ -7,8 +7,9 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
@other_user = create(:user)
@mod = create(:moderator_user)
as_user do
@forum_topic = create(:forum_topic, :title => "my forum topic", :original_post_attributes => {:body => "xxx"})
as(@user) do
@forum_topic = create(:forum_topic, creator: @user, title: "my forum topic")
@forum_post = create(:forum_post, creator: @user, topic: @forum_topic, body: "xxx")
end
end
@@ -28,9 +29,7 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
@gold_user = create(:gold_user)
# An open topic should bump...
as(@gold_user) do
@open_topic = create(:forum_topic)
end
@open_topic = as(@gold_user) { create(:forum_topic, creator: @gold_user) }
@gold_user.reload
as(@gold_user) do
assert(@gold_user.has_forum_been_updated?)
@@ -47,9 +46,7 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
end
# Then adding an unread private topic should not bump.
as(@mod) do
create(:forum_post, :topic_id => @forum_topic.id)
end
as(@mod) { create(:forum_post, topic: @forum_topic, creator: @mod) }
@gold_user.reload
as(@gold_user) do
assert_equal(false, @gold_user.has_forum_been_updated?)
@@ -91,8 +88,10 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
context "index action" do
setup do
as_user do
@topic1 = create(:forum_topic, :is_sticky => true, :original_post_attributes => {:body => "xxx"})
@topic2 = create(:forum_topic, :original_post_attributes => {:body => "xxx"})
@topic1 = create(:forum_topic, is_sticky: true, creator: @user)
@topic2 = create(:forum_topic, creator: @user)
@post1 = create(:forum_post, topic: @topic1, creator: @user, body: "xxx")
@post2 = create(:forum_post, topic: @topic2, creator: @user, body: "xxx")
end
end
@@ -158,7 +157,7 @@ class ForumTopicsControllerTest < ActionDispatch::IntegrationTest
context "create action" do
should "create a new forum topic and post" do
assert_difference(["ForumPost.count", "ForumTopic.count"], 1) do
post_auth forum_topics_path, @user, params: {:forum_topic => {:title => "bababa", :original_post_attributes => {:body => "xaxaxa"}}}
post_auth forum_topics_path, @user, params: { forum_topic: { title: "bababa", original_post_attributes: { body: "xaxaxa" }}}
end
forum_topic = ForumTopic.last

View File

@@ -7,13 +7,8 @@ module Moderator
PoolArchive.delete_all
PostArchive.delete_all
travel_to(1.month.ago) do
@user = create(:moderator_user)
end
as_user do
create(:comment)
end
@user = create(:moderator_user, created_at: 1.month.ago)
as(@user) { create(:comment, creator: @user) }
end
should "find by ip addr" do

View File

@@ -29,9 +29,7 @@ module Moderator
end
should "work even if the deleter has flagged the post previously" do
as_user do
PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
end
create(:post_flag, post: @post, creator: @admin)
post_auth delete_moderator_post_post_path(@post), @admin, params: {:reason => "xxx", :format => "js", :commit => "Delete"}
assert(@post.reload.is_deleted?)
end

View File

@@ -5,7 +5,7 @@ class NewsUpdatesControllerTest < ActionDispatch::IntegrationTest
setup do
@admin = create(:admin_user)
as(@admin) do
@news_update = create(:news_update)
@news_update = create(:news_update, creator: @admin)
end
end

View File

@@ -3,9 +3,7 @@ require 'test_helper'
class PostFlagsControllerTest < ActionDispatch::IntegrationTest
context "The post flags controller" do
setup do
travel_to(2.weeks.ago) do
@user = create(:user)
end
@user = create(:user, created_at: 2.weeks.ago)
end
context "new action" do
@@ -17,10 +15,7 @@ class PostFlagsControllerTest < ActionDispatch::IntegrationTest
context "index action" do
setup do
@user.as_current do
@post = create(:post)
@post_flag = create(:post_flag, :post => @post)
end
@post_flag = create(:post_flag, creator: @user)
end
should "render" do

View File

@@ -169,7 +169,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with only deleted comments" do
setup do
as(@user) { create(:comment, post: @post, is_deleted: true) }
as(@user) { create(:comment, creator: @user, post: @post, is_deleted: true) }
end
should "not show deleted comments to regular members" do
@@ -194,7 +194,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with only downvoted comments" do
should "not show thresholded comments" do
comment = as(@user) { create(:comment, post: @post, score: -10) }
comment = as(@user) { create(:comment, creator: @user, post: @post, score: -10) }
get_auth post_path(@post), @user, params: { id: @post.id }
assert_response :success
@@ -206,9 +206,9 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with a mix of comments" do
should "not show deleted or thresholded comments " do
as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "good") }
as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "bad", score: -10) }
as(@user) { create(:comment, post: @post, do_not_bump_post: true, body: "ugly", is_deleted: true) }
as(@user) { create(:comment, creator: @user, post: @post, do_not_bump_post: true, body: "good") }
as(@user) { create(:comment, creator: @user, post: @post, do_not_bump_post: true, body: "bad", score: -10) }
as(@user) { create(:comment, creator: @user, post: @post, do_not_bump_post: true, body: "ugly", is_deleted: true) }
get_auth post_path(@post), @user, params: { id: @post.id }

View File

@@ -4,15 +4,10 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
context "The tag aliases controller" do
setup do
@user = create(:admin_user)
@tag_alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb")
end
context "edit action" do
setup do
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "render" do
get_auth edit_tag_alias_path(@tag_alias), @user
assert_response :success
@@ -20,12 +15,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end
context "update action" do
setup do
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
context "for a pending alias" do
setup do
as_admin do
@@ -63,12 +52,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end
context "index action" do
setup do
as_admin do
@tag_alias = create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "list all tag alias" do
get_auth tag_aliases_path, @user
assert_response :success
@@ -81,12 +64,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end
context "destroy action" do
setup do
as_admin do
@tag_alias = create(:tag_alias)
end
end
should "mark the alias as deleted" do
assert_difference("TagAlias.count", 0) do
delete_auth tag_alias_path(@tag_alias), @user

View File

@@ -4,15 +4,10 @@ class TagImplicationsControllerTest < ActionDispatch::IntegrationTest
context "The tag implications controller" do
setup do
@user = create(:admin_user)
@tag_implication = create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb")
end
context "edit action" do
setup do
as_admin do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "render" do
get_auth tag_implication_path(@tag_implication), @user
assert_response :success
@@ -20,12 +15,6 @@ class TagImplicationsControllerTest < ActionDispatch::IntegrationTest
end
context "update action" do
setup do
as_admin do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
context "for a pending implication" do
setup do
as_admin do
@@ -60,12 +49,6 @@ class TagImplicationsControllerTest < ActionDispatch::IntegrationTest
end
context "index action" do
setup do
as_user do
@tag_implication = create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb")
end
end
should "list all tag implications" do
get tag_implications_path
assert_response :success
@@ -78,12 +61,6 @@ class TagImplicationsControllerTest < ActionDispatch::IntegrationTest
end
context "destroy action" do
setup do
as_user do
@tag_implication = create(:tag_implication)
end
end
should "mark the implication as deleted" do
assert_difference("TagImplication.count", 0) do
delete_auth tag_implication_path(@tag_implication), @user

View File

@@ -6,7 +6,7 @@ class UserFeedbacksControllerTest < ActionDispatch::IntegrationTest
@user = create(:user)
@critic = create(:gold_user)
@mod = create(:moderator_user)
@user_feedback = as(@critic) { create(:user_feedback, user: @user) }
@user_feedback = create(:user_feedback, user: @user, creator: @critic)
end
context "new action" do