From b4ce2d83a66c993ff71f307816d6b54ddd5039ee Mon Sep 17 00:00:00 2001 From: evazion Date: Sat, 18 Jan 2020 15:52:01 -0600 Subject: [PATCH] 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. --- app/controllers/artists_controller.rb | 4 +- .../bulk_update_requests_controller.rb | 2 +- app/controllers/comments_controller.rb | 2 +- app/controllers/favorite_groups_controller.rb | 2 +- .../forum_post_votes_controller.rb | 2 +- app/controllers/forum_posts_controller.rb | 2 +- app/controllers/forum_topics_controller.rb | 6 +- app/controllers/ip_bans_controller.rb | 2 +- app/controllers/news_updates_controller.rb | 2 +- app/controllers/notes_controller.rb | 2 +- app/controllers/pools_controller.rb | 2 +- app/controllers/post_appeals_controller.rb | 2 +- app/controllers/post_flags_controller.rb | 2 +- app/controllers/user_feedbacks_controller.rb | 2 +- app/logical/alias_and_implication_importer.rb | 8 +- app/logical/approver_pruner.rb | 2 +- app/logical/forum_updater.rb | 2 +- .../tag_relationship_retirement_service.rb | 5 +- app/logical/upload_service/replacer.rb | 4 +- app/logical/user_promotion.rb | 6 +- app/models/application_record.rb | 12 --- app/models/artist.rb | 8 +- app/models/bulk_update_request.rb | 11 +-- app/models/comment.rb | 2 +- app/models/favorite_group.rb | 6 +- app/models/forum_post.rb | 6 +- app/models/forum_post_vote.rb | 2 +- app/models/forum_topic.rb | 4 +- app/models/ip_ban.rb | 6 +- app/models/news_update.rb | 2 +- app/models/note.rb | 5 +- app/models/pool.rb | 2 +- app/models/post.rb | 6 +- app/models/post_appeal.rb | 5 -- app/models/post_flag.rb | 4 +- app/models/tag_relationship.rb | 7 +- app/models/user.rb | 6 ++ app/models/user_feedback.rb | 2 +- test/factories/artist.rb | 2 +- test/factories/bulk_update_request.rb | 1 + test/factories/comment.rb | 2 + test/factories/favorite_group.rb | 1 + test/factories/forum_post.rb | 1 + test/factories/forum_topic.rb | 1 + test/factories/news_update.rb | 1 + test/factories/note.rb | 1 + test/factories/pool.rb | 2 +- test/factories/post_appeal.rb | 2 + test/factories/post_flag.rb | 2 + test/factories/tag_alias.rb | 1 + test/factories/tag_implication.rb | 1 + test/factories/user.rb | 1 + test/factories/user_feedback.rb | 1 + .../bulk_update_requests_controller_test.rb | 25 +----- test/functional/comments_controller_test.rb | 4 +- .../favorite_groups_controller_test.rb | 4 +- .../functional/forum_posts_controller_test.rb | 12 +-- .../forum_topics_controller_test.rb | 21 +++-- .../moderator/ip_addrs_controller_test.rb | 9 +- .../moderator/post/posts_controller_test.rb | 4 +- .../news_updates_controller_test.rb | 2 +- test/functional/post_flags_controller_test.rb | 9 +- test/functional/posts_controller_test.rb | 10 +-- .../functional/tag_aliases_controller_test.rb | 25 +----- .../tag_implications_controller_test.rb | 25 +----- .../user_feedbacks_controller_test.rb | 2 +- ...ag_relationship_retirement_service_test.rb | 24 ++---- test/unit/approver_pruner_test.rb | 2 + test/unit/artist_test.rb | 12 +-- test/unit/bulk_update_request_test.rb | 10 +-- test/unit/comment_test.rb | 4 +- test/unit/favorite_group_test.rb | 10 +-- test/unit/forum_post_test.rb | 6 +- test/unit/forum_topic_test.rb | 6 +- test/unit/ip_ban_test.rb | 10 --- test/unit/moderator/ip_addr_search_test.rb | 2 +- test/unit/post_appeal_test.rb | 39 +++------ test/unit/post_approval_test.rb | 4 +- test/unit/post_event_test.rb | 20 +---- test/unit/post_flag_test.rb | 82 ++++++------------- test/unit/post_pruner_test.rb | 23 +----- test/unit/post_test.rb | 31 +++---- test/unit/tag_alias_test.rb | 2 +- test/unit/tag_implication_test.rb | 4 +- test/unit/user_feedback_test.rb | 24 ++---- test/unit/user_test.rb | 4 +- 86 files changed, 215 insertions(+), 433 deletions(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 383c0cb7b..030913614 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -18,7 +18,7 @@ class ArtistsController < ApplicationController end def ban - @artist.ban! + @artist.ban!(banner: CurrentUser.user) redirect_to(artist_path(@artist), :notice => "Artist was banned") end @@ -48,7 +48,7 @@ class ArtistsController < ApplicationController end def create - @artist = Artist.create(artist_params) + @artist = Artist.create(artist_params.merge(creator: CurrentUser.user)) respond_with(@artist) end diff --git a/app/controllers/bulk_update_requests_controller.rb b/app/controllers/bulk_update_requests_controller.rb index 0b982152a..740b38c8c 100644 --- a/app/controllers/bulk_update_requests_controller.rb +++ b/app/controllers/bulk_update_requests_controller.rb @@ -10,7 +10,7 @@ class BulkUpdateRequestsController < ApplicationController end def create - @bulk_update_request = BulkUpdateRequest.create(bur_params(:create)) + @bulk_update_request = BulkUpdateRequest.create(bur_params(:create).merge(user: CurrentUser.user)) respond_with(@bulk_update_request, :location => bulk_update_requests_path) end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 488622c04..42f72367c 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -33,7 +33,7 @@ class CommentsController < ApplicationController end def create - @comment = Comment.create(comment_params(:create)) + @comment = Comment.create(comment_params(:create).merge(creator: CurrentUser.user, creator_ip_addr: CurrentUser.ip_addr)) flash[:notice] = @comment.valid? ? "Comment posted" : @comment.errors.full_messages.join("; ") respond_with(@comment) do |format| format.html do diff --git a/app/controllers/favorite_groups_controller.rb b/app/controllers/favorite_groups_controller.rb index c6d77ca97..c1c7f213f 100644 --- a/app/controllers/favorite_groups_controller.rb +++ b/app/controllers/favorite_groups_controller.rb @@ -24,7 +24,7 @@ class FavoriteGroupsController < ApplicationController end def create - @favorite_group = FavoriteGroup.create(favgroup_params) + @favorite_group = CurrentUser.favorite_groups.create(favgroup_params) respond_with(@favorite_group) end diff --git a/app/controllers/forum_post_votes_controller.rb b/app/controllers/forum_post_votes_controller.rb index 59fedd82e..c00a40ca5 100644 --- a/app/controllers/forum_post_votes_controller.rb +++ b/app/controllers/forum_post_votes_controller.rb @@ -9,7 +9,7 @@ class ForumPostVotesController < ApplicationController def create @forum_post = ForumPost.find(params[:forum_post_id]) - @forum_post_vote = @forum_post.votes.create(forum_post_vote_params) + @forum_post_vote = @forum_post.votes.create(forum_post_vote_params.merge(creator: CurrentUser.user)) respond_with(@forum_post_vote) end diff --git a/app/controllers/forum_posts_controller.rb b/app/controllers/forum_posts_controller.rb index 282e35358..9d44bb969 100644 --- a/app/controllers/forum_posts_controller.rb +++ b/app/controllers/forum_posts_controller.rb @@ -42,7 +42,7 @@ class ForumPostsController < ApplicationController end def create - @forum_post = ForumPost.create(forum_post_params(:create)) + @forum_post = ForumPost.create(forum_post_params(:create).merge(creator: CurrentUser.user)) page = @forum_post.topic.last_page if @forum_post.topic.last_page > 1 respond_with(@forum_post, :location => forum_topic_path(@forum_post.topic, :page => page)) end diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb index 248734d2b..ce434e9fe 100644 --- a/app/controllers/forum_topics_controller.rb +++ b/app/controllers/forum_topics_controller.rb @@ -43,7 +43,11 @@ class ForumTopicsController < ApplicationController end def create - @forum_topic = ForumTopic.create(forum_topic_params(:create)) + @forum_topic = ForumTopic.new(forum_topic_params(:create)) + @forum_topic.creator = CurrentUser.user + @forum_topic.original_post.creator = CurrentUser.user + @forum_topic.save + respond_with(@forum_topic) end diff --git a/app/controllers/ip_bans_controller.rb b/app/controllers/ip_bans_controller.rb index 57f3d774f..7854c718e 100644 --- a/app/controllers/ip_bans_controller.rb +++ b/app/controllers/ip_bans_controller.rb @@ -7,7 +7,7 @@ class IpBansController < ApplicationController end def create - @ip_ban = IpBan.create(ip_ban_params) + @ip_ban = CurrentUser.ip_bans.create(ip_ban_params) respond_with(@ip_ban, :location => ip_bans_path) end diff --git a/app/controllers/news_updates_controller.rb b/app/controllers/news_updates_controller.rb index a00389ea3..8616bb0d3 100644 --- a/app/controllers/news_updates_controller.rb +++ b/app/controllers/news_updates_controller.rb @@ -24,7 +24,7 @@ class NewsUpdatesController < ApplicationController end def create - @news_update = NewsUpdate.create(news_update_params) + @news_update = NewsUpdate.create(news_update_params.merge(creator: CurrentUser.user)) respond_with(@news_update, :location => news_updates_path) end diff --git a/app/controllers/notes_controller.rb b/app/controllers/notes_controller.rb index ef2ff5a02..5f45be061 100644 --- a/app/controllers/notes_controller.rb +++ b/app/controllers/notes_controller.rb @@ -19,7 +19,7 @@ class NotesController < ApplicationController end def create - @note = Note.create(note_params(:create)) + @note = Note.create(note_params(:create).merge(creator: CurrentUser.user)) respond_with(@note) do |fmt| fmt.json do if @note.errors.any? diff --git a/app/controllers/pools_controller.rb b/app/controllers/pools_controller.rb index 32e4c551d..b6eb0c2ae 100644 --- a/app/controllers/pools_controller.rb +++ b/app/controllers/pools_controller.rb @@ -43,7 +43,7 @@ class PoolsController < ApplicationController end def create - @pool = Pool.create(pool_params) + @pool = Pool.create(pool_params.merge(creator: CurrentUser.user)) flash[:notice] = @pool.valid? ? "Pool created" : @pool.errors.full_messages.join("; ") respond_with(@pool) end diff --git a/app/controllers/post_appeals_controller.rb b/app/controllers/post_appeals_controller.rb index 9116899e5..0f7058a72 100644 --- a/app/controllers/post_appeals_controller.rb +++ b/app/controllers/post_appeals_controller.rb @@ -13,7 +13,7 @@ class PostAppealsController < ApplicationController end def create - @post_appeal = PostAppeal.create(post_appeal_params) + @post_appeal = PostAppeal.create(post_appeal_params.merge(creator: CurrentUser.user)) respond_with(@post_appeal) end diff --git a/app/controllers/post_flags_controller.rb b/app/controllers/post_flags_controller.rb index 456e4e304..eefb6c8ca 100644 --- a/app/controllers/post_flags_controller.rb +++ b/app/controllers/post_flags_controller.rb @@ -13,7 +13,7 @@ class PostFlagsController < ApplicationController end def create - @post_flag = PostFlag.create(post_flag_params) + @post_flag = PostFlag.create(post_flag_params.merge(creator: CurrentUser.user)) respond_with(@post_flag) end diff --git a/app/controllers/user_feedbacks_controller.rb b/app/controllers/user_feedbacks_controller.rb index 3f77f00e1..0d5ae4d10 100644 --- a/app/controllers/user_feedbacks_controller.rb +++ b/app/controllers/user_feedbacks_controller.rb @@ -24,7 +24,7 @@ class UserFeedbacksController < ApplicationController end def create - @user_feedback = UserFeedback.create(user_feedback_params(:create)) + @user_feedback = UserFeedback.create(user_feedback_params(:create).merge(creator: CurrentUser.user)) respond_with(@user_feedback) end diff --git a/app/logical/alias_and_implication_importer.rb b/app/logical/alias_and_implication_importer.rb index bbfdc0441..eda5daa05 100644 --- a/app/logical/alias_and_implication_importer.rb +++ b/app/logical/alias_and_implication_importer.rb @@ -58,13 +58,13 @@ class AliasAndImplicationImporter tokens.map do |token| case token[0] when :create_alias - tag_alias = TagAlias.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations) + tag_alias = TagAlias.new(creator: User.system, forum_topic_id: forum_id, status: "pending", antecedent_name: token[1], consequent_name: token[2], skip_secondary_validations: skip_secondary_validations) unless tag_alias.valid? raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})" end when :create_implication - tag_implication = TagImplication.new(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations) + tag_implication = TagImplication.new(creator: User.system, forum_topic_id: forum_id, status: "pending", antecedent_name: token[1], consequent_name: token[2], skip_secondary_validations: skip_secondary_validations) unless tag_implication.valid? raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})" end @@ -127,7 +127,7 @@ class AliasAndImplicationImporter tokens.map do |token| case token[0] when :create_alias - tag_alias = TagAlias.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations) + tag_alias = TagAlias.create(creator: approver, forum_topic_id: forum_id, status: "pending", antecedent_name: token[1], consequent_name: token[2], skip_secondary_validations: skip_secondary_validations) unless tag_alias.valid? raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})" end @@ -135,7 +135,7 @@ class AliasAndImplicationImporter tag_alias.approve!(approver: approver, update_topic: false) when :create_implication - tag_implication = TagImplication.create(:forum_topic_id => forum_id, :status => "pending", :antecedent_name => token[1], :consequent_name => token[2], :skip_secondary_validations => skip_secondary_validations) + tag_implication = TagImplication.create(creator: approver, forum_topic_id: forum_id, status: "pending", antecedent_name: token[1], consequent_name: token[2], skip_secondary_validations: skip_secondary_validations) unless tag_implication.valid? raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})" end diff --git a/app/logical/approver_pruner.rb b/app/logical/approver_pruner.rb index 0fc6cda6e..7421eb4c3 100644 --- a/app/logical/approver_pruner.rb +++ b/app/logical/approver_pruner.rb @@ -20,7 +20,7 @@ module ApproverPruner inactive_approvers.each do |user| CurrentUser.scoped(User.system, "127.0.0.1") do user.update!(can_approve_posts: false) - user.feedback.create(category: "neutral", body: "Lost approval privileges") + user.feedback.create(category: "neutral", body: "Lost approval privileges", creator: User.system) Dmail.create_automated( to_id: user.id, diff --git a/app/logical/forum_updater.rb b/app/logical/forum_updater.rb index 36122e08f..0f274dec5 100644 --- a/app/logical/forum_updater.rb +++ b/app/logical/forum_updater.rb @@ -22,7 +22,7 @@ class ForumUpdater end def create_response(body) - forum_topic.posts.create(body: body, skip_mention_notifications: true) + forum_topic.posts.create(body: body, skip_mention_notifications: true, creator: User.system) end def update_title(title_tag) diff --git a/app/logical/tag_relationship_retirement_service.rb b/app/logical/tag_relationship_retirement_service.rb index 2dbd4304b..fe7bbb00b 100644 --- a/app/logical/tag_relationship_retirement_service.rb +++ b/app/logical/tag_relationship_retirement_service.rb @@ -22,8 +22,9 @@ module TagRelationshipRetirementService def forum_topic topic = ForumTopic.where(title: forum_topic_title).first if topic.nil? - topic = CurrentUser.as_system do - ForumTopic.create(title: forum_topic_title, category_id: 1, original_post_attributes: {body: forum_topic_body}) + CurrentUser.as(User.system) do + topic = ForumTopic.create!(creator: User.system, title: forum_topic_title, category_id: 1) + forum_post = ForumPost.create!(creator: User.system, body: forum_topic_body, topic: topic) end end return topic diff --git a/app/logical/upload_service/replacer.rb b/app/logical/upload_service/replacer.rb index 8fe001d91..b1ac345d6 100644 --- a/app/logical/upload_service/replacer.rb +++ b/app/logical/upload_service/replacer.rb @@ -120,9 +120,7 @@ class UploadService update_ugoira_frame_data(post, upload) if md5_changed - CurrentUser.as(User.system) do - post.comments.create!(body: comment_replacement_message(post, replacement), do_not_bump_post: true) - end + Comment.create!(post: post, creator: User.system, body: comment_replacement_message(post, replacement), do_not_bump_post: true, creator_ip_addr: "127.0.0.1") else purge_cached_urls(post) end diff --git a/app/logical/user_promotion.rb b/app/logical/user_promotion.rb index 06695fa6a..8f46ba0f0 100644 --- a/app/logical/user_promotion.rb +++ b/app/logical/user_promotion.rb @@ -96,10 +96,6 @@ class UserPromotion end def create_user_feedback - user.feedback.create( - :category => "neutral", - :body => build_messages, - :disable_dmail_notification => true - ) + UserFeedback.create(user: user, creator: promoter, category: "neutral", body: build_messages, disable_dmail_notification: true) end end diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 5c91aa78b..a9b289a04 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -373,18 +373,6 @@ class ApplicationRecord < ActiveRecord::Base concerning :UserMethods do class_methods do - def belongs_to_creator(options = {}) - class_eval do - belongs_to :creator, options.merge(class_name: "User") - before_validation(on: :create) do |rec| - if rec.creator_id.nil? - rec.creator_id = CurrentUser.id - rec.creator_ip_addr = CurrentUser.ip_addr if rec.respond_to?(:creator_ip_addr=) - end - end - end - end - def belongs_to_updater(options = {}) class_eval do belongs_to :updater, options.merge(class_name: "User") diff --git a/app/models/artist.rb b/app/models/artist.rb index 4324e8641..67716dbea 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -13,7 +13,7 @@ class Artist < ApplicationRecord after_save :clear_url_string_changed validate :validate_tag_category validates :name, tag_name: true, uniqueness: true - belongs_to_creator + belongs_to :creator, class_name: "User" has_many :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name" has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl", :autosave => true has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion" @@ -418,15 +418,15 @@ class Artist < ApplicationRecord end end - def ban! + def ban!(banner: CurrentUser.user) Post.transaction do CurrentUser.without_safe_mode do Post.tag_match(name).each(&:ban!) # potential race condition but unlikely unless TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").exists? - tag_implication = TagImplication.create!(:antecedent_name => name, :consequent_name => "banned_artist", :skip_secondary_validations => true) - tag_implication.approve!(approver: CurrentUser.user) + tag_implication = TagImplication.create!(antecedent_name: name, consequent_name: "banned_artist", skip_secondary_validations: true, creator: banner) + tag_implication.approve!(approver: banner) end update_column(:is_banned, true) diff --git a/app/models/bulk_update_request.rb b/app/models/bulk_update_request.rb index 04f1216ba..a42a951f6 100644 --- a/app/models/bulk_update_request.rb +++ b/app/models/bulk_update_request.rb @@ -15,7 +15,7 @@ class BulkUpdateRequest < ApplicationRecord validate :validate_script, :on => :create before_validation :initialize_attributes, :on => :create before_validation :normalize_text - after_create :create_forum_topic + before_create :create_forum_topic after_save :update_notice scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) } @@ -94,12 +94,9 @@ class BulkUpdateRequest < ApplicationRecord end def create_forum_topic - if forum_topic_id - forum_post = forum_topic.posts.create(body: reason_with_link) - update(forum_post_id: forum_post.id) - else - forum_topic = ForumTopic.create(title: title, category_id: 1, original_post_attributes: {body: reason_with_link}) - update(forum_topic_id: forum_topic.id, forum_post_id: forum_topic.posts.first.id) + CurrentUser.as(user) do + self.forum_topic = ForumTopic.create(title: title, category_id: 1, creator: user) unless forum_topic.present? + self.forum_post = forum_topic.posts.create(body: reason_with_link, creator: user) unless forum_post.present? end end diff --git a/app/models/comment.rb b/app/models/comment.rb index 005e3c68d..5f19a0b4d 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -5,7 +5,7 @@ class Comment < ApplicationRecord validate :validate_comment_is_not_spam, on: :create validates_presence_of :body, :message => "has no content" belongs_to :post - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to_updater has_many :moderation_reports, as: :model has_many :votes, :class_name => "CommentVote", :dependent => :destroy diff --git a/app/models/favorite_group.rb b/app/models/favorite_group.rb index 67cdbf578..19ba08b40 100644 --- a/app/models/favorite_group.rb +++ b/app/models/favorite_group.rb @@ -1,7 +1,7 @@ class FavoriteGroup < ApplicationRecord validates_uniqueness_of :name, :case_sensitive => false, :scope => :creator_id validates_format_of :name, :with => /\A[^,]+\Z/, :message => "cannot have commas" - belongs_to_creator + belongs_to :creator, class_name: "User" before_validation :normalize_name before_validation :strip_name validate :creator_can_create_favorite_groups, :on => :create @@ -45,9 +45,9 @@ class FavoriteGroup < ApplicationRecord extend SearchMethods def creator_can_create_favorite_groups - if creator.favorite_group_count >= creator.favorite_group_limit + if creator.favorite_groups.count >= creator.favorite_group_limit error = "You can only keep up to #{creator.favorite_group_limit} favorite groups." - if !CurrentUser.user.is_platinum? + if !creator.is_platinum? error += " Upgrade your account to create more." end self.errors.add(:base, error) diff --git a/app/models/forum_post.rb b/app/models/forum_post.rb index 860f3791c..24df11067 100644 --- a/app/models/forum_post.rb +++ b/app/models/forum_post.rb @@ -2,7 +2,7 @@ class ForumPost < ApplicationRecord include Mentionable attr_readonly :topic_id - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to_updater belongs_to :topic, :class_name => "ForumTopic" has_many :dtext_links, as: :model, dependent: :destroy @@ -113,7 +113,7 @@ class ForumPost < ApplicationRecord end def validate_topic_is_unlocked - return if CurrentUser.is_moderator? + return if creator.is_moderator? return if topic.nil? if topic.is_locked? @@ -139,7 +139,7 @@ class ForumPost < ApplicationRecord def update_topic_updated_at_on_create if topic # need to do this to bypass the topic's original post from getting touched - ForumTopic.where(:id => topic.id).update_all(["updater_id = ?, response_count = response_count + 1, updated_at = ?", CurrentUser.id, Time.now]) + ForumTopic.where(:id => topic.id).update_all(["updater_id = ?, response_count = response_count + 1, updated_at = ?", creator.id, Time.now]) topic.response_count += 1 end end diff --git a/app/models/forum_post_vote.rb b/app/models/forum_post_vote.rb index 9637aca8f..b7aae81d0 100644 --- a/app/models/forum_post_vote.rb +++ b/app/models/forum_post_vote.rb @@ -1,5 +1,5 @@ class ForumPostVote < ApplicationRecord - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to :forum_post validates :creator_id, uniqueness: {scope: :forum_post_id} validates :score, inclusion: {in: [-1, 0, 1]} diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 48aa8dd4d..680001305 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -11,7 +11,7 @@ class ForumTopic < ApplicationRecord Admin: User::Levels::ADMIN } - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to_updater has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy has_many :moderation_reports, through: :posts @@ -180,7 +180,7 @@ class ForumTopic < ApplicationRecord end def update_orignal_post - original_post&.update_columns(:updater_id => CurrentUser.id, :updated_at => Time.now) + original_post&.update_columns(:updater_id => updater.id, :updated_at => Time.now) end def viewable_moderation_reports diff --git a/app/models/ip_ban.rb b/app/models/ip_ban.rb index 0da1a8838..cb6fd96ee 100644 --- a/app/models/ip_ban.rb +++ b/app/models/ip_ban.rb @@ -1,10 +1,10 @@ class IpBan < ApplicationRecord - belongs_to_creator + belongs_to :creator, class_name: "User" validate :validate_ip_addr validates_presence_of :reason validates_uniqueness_of :ip_addr - after_create { ModAction.log("#{CurrentUser.name} created ip ban for #{ip_addr}", :ip_ban_create) } - after_destroy { ModAction.log("#{CurrentUser.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) } + after_create { ModAction.log("#{creator.name} created ip ban for #{ip_addr}", :ip_ban_create) } + after_destroy { ModAction.log("#{creator.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) } def self.is_banned?(ip_addr) where("ip_addr >>= ?", ip_addr).exists? diff --git a/app/models/news_update.rb b/app/models/news_update.rb index 5e0fecb09..bbc68de60 100644 --- a/app/models/news_update.rb +++ b/app/models/news_update.rb @@ -1,5 +1,5 @@ class NewsUpdate < ApplicationRecord - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to_updater scope :recent, -> {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)} end diff --git a/app/models/note.rb b/app/models/note.rb index 2f03bb97d..65f7c9c34 100644 --- a/app/models/note.rb +++ b/app/models/note.rb @@ -3,7 +3,7 @@ class Note < ApplicationRecord attr_accessor :html_id belongs_to :post - belongs_to_creator + belongs_to :creator, class_name: "User" has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy validates_presence_of :x, :y, :width, :height, :body validate :note_within_image @@ -129,8 +129,9 @@ class Note < ApplicationRecord save! end - def copy_to(new_post) + def copy_to(new_post, creator: CurrentUser.user) new_note = dup + new_note.creator = creator new_note.post_id = new_post.id new_note.version = 0 diff --git a/app/models/pool.rb b/app/models/pool.rb index e1873542d..96a3822ac 100644 --- a/app/models/pool.rb +++ b/app/models/pool.rb @@ -3,7 +3,7 @@ class Pool < ApplicationRecord POOL_ORDER_LIMIT = 1000 array_attribute :post_ids, parse: /\d+/, cast: :to_i - belongs_to_creator + belongs_to :creator, class_name: "User" validates_uniqueness_of :name, case_sensitive: false, if: :name_changed? validate :validate_name, if: :name_changed? diff --git a/app/models/post.rb b/app/models/post.rb index cf7b5c254..d4d44de5a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -268,8 +268,8 @@ class Post < ApplicationRecord !is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && uploader != user && !approved_by?(user) end - def flag!(reason, options = {}) - flag = flags.create(:reason => reason, :is_resolved => false, :is_deletion => options[:is_deletion]) + def flag!(reason, is_deletion: false) + flag = flags.create(reason: reason, is_resolved: false, is_deletion: is_deletion, creator: CurrentUser.user) if flag.errors.any? raise PostFlag::Error.new(flag.errors.full_messages.join("; ")) @@ -746,7 +746,7 @@ class Post < ApplicationRecord when /^newpool:(.+)$/i pool = Pool.find_by_name($1) if pool.nil? - pool = Pool.create(:name => $1, :description => "This pool was automatically generated") + pool = Pool.create(name: $1, creator: CurrentUser.user, description: "This pool was automatically generated") end end end diff --git a/app/models/post_appeal.rb b/app/models/post_appeal.rb index 0b7247c9d..29e7f8102 100644 --- a/app/models/post_appeal.rb +++ b/app/models/post_appeal.rb @@ -8,7 +8,6 @@ class PostAppeal < ApplicationRecord validates_presence_of :reason validate :validate_post_is_inactive validate :validate_creator_is_not_limited - before_validation :initialize_creator, :on => :create validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post" api_attributes including: [:is_resolved] @@ -60,10 +59,6 @@ class PostAppeal < ApplicationRecord end end - def initialize_creator - self.creator_id = CurrentUser.id - end - def appeal_count_for_creator creator.post_appeals.recent.count end diff --git a/app/models/post_flag.rb b/app/models/post_flag.rb index c7784a68b..709eb5f54 100644 --- a/app/models/post_flag.rb +++ b/app/models/post_flag.rb @@ -9,7 +9,7 @@ class PostFlag < ApplicationRecord COOLDOWN_PERIOD = 3.days - belongs_to_creator :class_name => "User" + belongs_to :creator, class_name: "User" belongs_to :post validates_presence_of :reason validate :validate_creator_is_not_limited, on: :create @@ -124,7 +124,7 @@ class PostFlag < ApplicationRecord def validate_creator_is_not_limited return if is_deletion - if CurrentUser.can_approve_posts? + if creator.can_approve_posts? # do nothing elsif creator.created_at > 1.week.ago errors[:creator] << "cannot flag within the first week of sign up" diff --git a/app/models/tag_relationship.rb b/app/models/tag_relationship.rb index 5c4bbb27e..fffea1dd9 100644 --- a/app/models/tag_relationship.rb +++ b/app/models/tag_relationship.rb @@ -7,7 +7,7 @@ class TagRelationship < ApplicationRecord attr_accessor :skip_secondary_validations - belongs_to_creator + belongs_to :creator, class_name: "User" belongs_to :approver, class_name: "User", optional: true belongs_to :forum_post, optional: true belongs_to :forum_topic, optional: true @@ -24,7 +24,6 @@ class TagRelationship < ApplicationRecord scope :pending, -> {where(status: "pending")} scope :retired, -> {where(status: "retired")} - before_validation :initialize_creator, :on => :create before_validation :normalize_names validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/ validates_presence_of :antecedent_name, :consequent_name @@ -33,10 +32,6 @@ class TagRelationship < ApplicationRecord validate :antecedent_and_consequent_are_different after_save :update_notice - def initialize_creator - self.creator_id = CurrentUser.user.id - end - def normalize_names self.antecedent_name = antecedent_name.mb_chars.downcase.tr(" ", "_") self.consequent_name = consequent_name.mb_chars.downcase.tr(" ", "_") diff --git a/app/models/user.rb b/app/models/user.rb index e325e5304..63cf7974e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -86,6 +86,7 @@ class User < ApplicationRecord before_update :encrypt_password_on_update before_create :promote_to_admin_if_first_user before_create :customize_new_user + has_many :artists, foreign_key: :creator_id has_many :artist_versions, foreign_key: :updater_id has_many :artist_commentary_versions, foreign_key: :updater_id has_many :comments, foreign_key: :creator_id @@ -94,6 +95,7 @@ class User < ApplicationRecord has_many :feedback, :class_name => "UserFeedback", :dependent => :destroy has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id has_many :moderation_reports, as: :model + has_many :pools, foreign_key: :creator_id has_many :posts, :foreign_key => "uploader_id" has_many :post_appeals, foreign_key: :creator_id has_many :post_approvals, :dependent => :destroy @@ -108,6 +110,7 @@ class User < ApplicationRecord has_one :dmail_filter has_one :super_voter has_one :token_bucket + has_many :notes, foreign_key: :creator_id has_many :note_versions, :foreign_key => "updater_id" has_many :dmails, -> {order("dmails.id desc")}, :foreign_key => "owner_id" has_many :saved_searches @@ -115,6 +118,9 @@ class User < ApplicationRecord has_many :user_name_change_requests, -> {visible.order("user_name_change_requests.created_at desc")} has_many :favorite_groups, -> {order(name: :asc)}, foreign_key: :creator_id has_many :favorites, ->(rec) {where("user_id % 100 = #{rec.id % 100} and user_id = #{rec.id}").order("id desc")} + has_many :ip_bans, foreign_key: :creator_id + has_many :tag_aliases, foreign_key: :creator_id + has_many :tag_implications, foreign_key: :creator_id belongs_to :inviter, class_name: "User", optional: true accepts_nested_attributes_for :dmail_filter diff --git a/app/models/user_feedback.rb b/app/models/user_feedback.rb index f52a9045a..be2a3f609 100644 --- a/app/models/user_feedback.rb +++ b/app/models/user_feedback.rb @@ -1,7 +1,7 @@ class UserFeedback < ApplicationRecord self.table_name = "user_feedback" belongs_to :user - belongs_to_creator + belongs_to :creator, class_name: "User" attr_accessor :disable_dmail_notification validates_presence_of :body, :category validates_inclusion_of :category, :in => %w(positive negative neutral) diff --git a/test/factories/artist.rb b/test/factories/artist.rb index 7137e3845..5556f2e03 100644 --- a/test/factories/artist.rb +++ b/test/factories/artist.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory(:artist) do + creator name { rand(1_000_000).to_s } is_active { true } - association :creator, factory: :user end end diff --git a/test/factories/bulk_update_request.rb b/test/factories/bulk_update_request.rb index e9b72f5e0..4e987a920 100644 --- a/test/factories/bulk_update_request.rb +++ b/test/factories/bulk_update_request.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:bulk_update_request) do |f| + user title {"xxx"} script {"create alias aaa -> bbb"} skip_secondary_validations {true} diff --git a/test/factories/comment.rb b/test/factories/comment.rb index 184191c4e..f9e7d6764 100644 --- a/test/factories/comment.rb +++ b/test/factories/comment.rb @@ -1,6 +1,8 @@ FactoryBot.define do factory(:comment) do |f| + creator post + creator_ip_addr { FFaker::Internet.ip_v4_address } body {FFaker::Lorem.sentences.join(" ")} end end diff --git a/test/factories/favorite_group.rb b/test/factories/favorite_group.rb index 060d83af8..b14b9d57c 100644 --- a/test/factories/favorite_group.rb +++ b/test/factories/favorite_group.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :favorite_group do + creator name { FFaker::Lorem.word } end end diff --git a/test/factories/forum_post.rb b/test/factories/forum_post.rb index a1af4ae70..acc151a70 100644 --- a/test/factories/forum_post.rb +++ b/test/factories/forum_post.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:forum_post) do + creator body {FFaker::Lorem.sentences.join(" ")} end end diff --git a/test/factories/forum_topic.rb b/test/factories/forum_topic.rb index 8f2a1d331..5a59e45a0 100644 --- a/test/factories/forum_topic.rb +++ b/test/factories/forum_topic.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:forum_topic) do + creator title {FFaker::Lorem.words.join(" ")} is_sticky {false} is_locked {false} diff --git a/test/factories/news_update.rb b/test/factories/news_update.rb index 3923185e2..370a99a09 100644 --- a/test/factories/news_update.rb +++ b/test/factories/news_update.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:news_update) do + creator message {"xxx"} end end diff --git a/test/factories/note.rb b/test/factories/note.rb index 6fbcd8a7e..14c6a8093 100644 --- a/test/factories/note.rb +++ b/test/factories/note.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:note) do + creator post x { 1 } y { 1 } diff --git a/test/factories/pool.rb b/test/factories/pool.rb index cff7ea112..63308fc91 100644 --- a/test/factories/pool.rb +++ b/test/factories/pool.rb @@ -1,7 +1,7 @@ FactoryBot.define do factory(:pool) do + creator name {"pool_" + rand(100..1000099).to_s} - association :creator, :factory => :user description {FFaker::Lorem.sentences.join(" ")} end end diff --git a/test/factories/post_appeal.rb b/test/factories/post_appeal.rb index 1c2c93ca6..8bddbf093 100644 --- a/test/factories/post_appeal.rb +++ b/test/factories/post_appeal.rb @@ -1,5 +1,7 @@ FactoryBot.define do factory(:post_appeal) do + creator + post reason {"xxx"} end end diff --git a/test/factories/post_flag.rb b/test/factories/post_flag.rb index e2c49c5bb..ed7c3d942 100644 --- a/test/factories/post_flag.rb +++ b/test/factories/post_flag.rb @@ -1,5 +1,7 @@ FactoryBot.define do factory(:post_flag) do + creator + post reason {"xxx"} is_resolved {false} end diff --git a/test/factories/tag_alias.rb b/test/factories/tag_alias.rb index aa879071d..3ab1efb46 100644 --- a/test/factories/tag_alias.rb +++ b/test/factories/tag_alias.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :tag_alias do + creator antecedent_name {"aaa"} consequent_name {"bbb"} status {"active"} diff --git a/test/factories/tag_implication.rb b/test/factories/tag_implication.rb index 79965b1b7..c1f0d7fd5 100644 --- a/test/factories/tag_implication.rb +++ b/test/factories/tag_implication.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :tag_implication do + creator antecedent_name {"aaa"} consequent_name {"bbb"} status {"active"} diff --git a/test/factories/user.rb b/test/factories/user.rb index 3f9153cde..6a966a7af 100644 --- a/test/factories/user.rb +++ b/test/factories/user.rb @@ -11,6 +11,7 @@ FactoryBot.define do last_logged_in_at {Time.now} favorite_count {0} bit_prefs {0} + last_forum_read_at {nil} factory(:banned_user) do transient { ban_duration {3} } diff --git a/test/factories/user_feedback.rb b/test/factories/user_feedback.rb index 516ce9fa4..7db0a48f6 100644 --- a/test/factories/user_feedback.rb +++ b/test/factories/user_feedback.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory(:user_feedback) do + creator factory: :builder_user user category { "positive" } body { FFaker::Lorem.words.join(" ") } diff --git a/test/functional/bulk_update_requests_controller_test.rb b/test/functional/bulk_update_requests_controller_test.rb index 6f540f8ea..57c2a255b 100644 --- a/test/functional/bulk_update_requests_controller_test.rb +++ b/test/functional/bulk_update_requests_controller_test.rb @@ -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 diff --git a/test/functional/comments_controller_test.rb b/test/functional/comments_controller_test.rb index aa7729ad3..4040a4088 100644 --- a/test/functional/comments_controller_test.rb +++ b/test/functional/comments_controller_test.rb @@ -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 diff --git a/test/functional/favorite_groups_controller_test.rb b/test/functional/favorite_groups_controller_test.rb index c81ad37e6..284b49f47 100644 --- a/test/functional/favorite_groups_controller_test.rb +++ b/test/functional/favorite_groups_controller_test.rb @@ -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 diff --git a/test/functional/forum_posts_controller_test.rb b/test/functional/forum_posts_controller_test.rb index 41014b978..18b53f7fa 100644 --- a/test/functional/forum_posts_controller_test.rb +++ b/test/functional/forum_posts_controller_test.rb @@ -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 diff --git a/test/functional/forum_topics_controller_test.rb b/test/functional/forum_topics_controller_test.rb index a8b35b79d..0eb4faa14 100644 --- a/test/functional/forum_topics_controller_test.rb +++ b/test/functional/forum_topics_controller_test.rb @@ -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 diff --git a/test/functional/moderator/ip_addrs_controller_test.rb b/test/functional/moderator/ip_addrs_controller_test.rb index 76d62d284..80a963980 100644 --- a/test/functional/moderator/ip_addrs_controller_test.rb +++ b/test/functional/moderator/ip_addrs_controller_test.rb @@ -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 diff --git a/test/functional/moderator/post/posts_controller_test.rb b/test/functional/moderator/post/posts_controller_test.rb index 165ec3146..7041d61e0 100644 --- a/test/functional/moderator/post/posts_controller_test.rb +++ b/test/functional/moderator/post/posts_controller_test.rb @@ -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 diff --git a/test/functional/news_updates_controller_test.rb b/test/functional/news_updates_controller_test.rb index 4c26d109d..2a6f0c280 100644 --- a/test/functional/news_updates_controller_test.rb +++ b/test/functional/news_updates_controller_test.rb @@ -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 diff --git a/test/functional/post_flags_controller_test.rb b/test/functional/post_flags_controller_test.rb index 2125180ec..fad71fcfc 100644 --- a/test/functional/post_flags_controller_test.rb +++ b/test/functional/post_flags_controller_test.rb @@ -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 diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index 16ab3d9d7..ccfdb0d27 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -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 } diff --git a/test/functional/tag_aliases_controller_test.rb b/test/functional/tag_aliases_controller_test.rb index b6852fc1a..1805ca893 100644 --- a/test/functional/tag_aliases_controller_test.rb +++ b/test/functional/tag_aliases_controller_test.rb @@ -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 diff --git a/test/functional/tag_implications_controller_test.rb b/test/functional/tag_implications_controller_test.rb index 60e748594..81b024837 100644 --- a/test/functional/tag_implications_controller_test.rb +++ b/test/functional/tag_implications_controller_test.rb @@ -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 diff --git a/test/functional/user_feedbacks_controller_test.rb b/test/functional/user_feedbacks_controller_test.rb index f3049cbc5..bfef3a94b 100644 --- a/test/functional/user_feedbacks_controller_test.rb +++ b/test/functional/user_feedbacks_controller_test.rb @@ -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 diff --git a/test/models/tag_relationship_retirement_service_test.rb b/test/models/tag_relationship_retirement_service_test.rb index 13d34d4bb..a8c9432be 100644 --- a/test/models/tag_relationship_retirement_service_test.rb +++ b/test/models/tag_relationship_retirement_service_test.rb @@ -23,14 +23,8 @@ class TagRelationshipRetirementServiceTest < ActiveSupport::TestCase setup do subject.stubs(:is_unused?).returns(true) - @user = FactoryBot.create(:user) - as_user do - @new_alias = FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") - - travel_to(3.years.ago) do - @old_alias = FactoryBot.create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd") - end - end + @new_alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") + @old_alias = create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd", created_at: 3.years.ago) end should "find old tag relationships" do @@ -50,17 +44,11 @@ class TagRelationshipRetirementServiceTest < ActiveSupport::TestCase subject { TagRelationshipRetirementService } setup do - @user = FactoryBot.create(:user) + @new_alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") + @new_post = create(:post, tag_string: "bbb") - as_user do - @new_alias = FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") - @new_post = FactoryBot.create(:post, tag_string: "bbb") - - travel_to(3.years.ago) do - @old_alias = FactoryBot.create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd") - @old_post = FactoryBot.create(:post, tag_string: "ddd") - end - end + @old_alias = create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd", created_at: 3.years.ago) + @old_post = create(:post, tag_string: "ddd", created_at: 3.years.ago) end should "return true if no recent post exists" do diff --git a/test/unit/approver_pruner_test.rb b/test/unit/approver_pruner_test.rb index 1289678a2..444ae498e 100644 --- a/test/unit/approver_pruner_test.rb +++ b/test/unit/approver_pruner_test.rb @@ -8,6 +8,8 @@ class ApproverPrunerTest < ActiveSupport::TestCase should "demote inactive approvers" do assert_equal([@approver.id], ApproverPruner.inactive_approvers.map(&:id)) + assert_nothing_raised { ApproverPruner.prune! } + assert_equal(false, @approver.reload.can_approve_posts) end should "not demote active approvers" do diff --git a/test/unit/artist_test.rb b/test/unit/artist_test.rb index 37d137b75..d83068088 100644 --- a/test/unit/artist_test.rb +++ b/test/unit/artist_test.rb @@ -30,27 +30,27 @@ class ArtistTest < ActiveSupport::TestCase end should "parse inactive urls" do - @artist = Artist.create(name: "blah", url_string: "-http://monet.com") + @artist = create(:artist, name: "blah", url_string: "-http://monet.com") assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s)) refute(@artist.urls[0].is_active?) end should "not allow duplicate active+inactive urls" do - @artist = Artist.create(name: "blah", url_string: "-http://monet.com\nhttp://monet.com") + @artist = create(:artist, name: "blah", url_string: "-http://monet.com\nhttp://monet.com") assert_equal(1, @artist.urls.count) assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s)) refute(@artist.urls[0].is_active?) end should "allow deactivating a url" do - @artist = Artist.create(name: "blah", url_string: "http://monet.com") + @artist = create(:artist, name: "blah", url_string: "http://monet.com") @artist.update(url_string: "-http://monet.com") assert_equal(1, @artist.urls.count) refute(@artist.urls[0].is_active?) end should "allow activating a url" do - @artist = Artist.create(name: "blah", url_string: "-http://monet.com") + @artist = create(:artist, name: "blah", url_string: "-http://monet.com") @artist.update(url_string: "http://monet.com") assert_equal(1, @artist.urls.count) assert(@artist.urls[0].is_active?) @@ -69,7 +69,7 @@ class ArtistTest < ActiveSupport::TestCase @post = FactoryBot.create(:post, :tag_string => "aaa") @artist = FactoryBot.create(:artist, :name => "aaa") @admin = FactoryBot.create(:admin_user) - CurrentUser.scoped(@admin) { @artist.ban! } + @artist.ban!(banner: @admin) @post.reload end @@ -542,7 +542,7 @@ class ArtistTest < ActiveSupport::TestCase context "#new_with_defaults" do should "fetch the defaults from the given source" do source = "https://i.pximg.net/img-original/img/2018/01/28/23/56/50/67014762_p0.jpg" - artist = Artist.new_with_defaults(source: source) + artist = Artist.new_with_defaults(source: source, creator: create(:user)) assert_equal("niceandcool", artist.name) assert_equal("nice_and_cool", artist.other_names_string) diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index 19b14f6d7..9be6cd2da 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -37,7 +37,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase context "#update_notice" do setup do - @forum_topic = FactoryBot.create(:forum_topic) + @forum_topic = create(:forum_topic, creator: @admin) end should "update the cache" do @@ -70,7 +70,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase mass update aaa -> bbb ' - @bur = FactoryBot.create(:bulk_update_request, :script => @script) + @bur = create(:bulk_update_request, script: @script, user: @admin) @bur.approve!(@admin) assert_enqueued_jobs(3) @@ -111,7 +111,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase context "that has an invalid alias" do setup do - @alias1 = FactoryBot.create(:tag_alias) + @alias1 = create(:tag_alias, creator: @admin) @req = FactoryBot.build(:bulk_update_request, :script => "create alias bbb -> aaa") end @@ -172,8 +172,8 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase context "with an associated forum topic" do setup do - @topic = FactoryBot.create(:forum_topic, :title => "[bulk] hoge") - @post = FactoryBot.create(:forum_post, :topic_id => @topic.id) + @topic = create(:forum_topic, title: "[bulk] hoge", creator: @admin) + @post = create(:forum_post, topic: @topic, creator: @admin) @req = FactoryBot.create(:bulk_update_request, :script => "create alias AAA -> BBB", :forum_topic_id => @topic.id, :forum_post_id => @post.id, :title => "[bulk] hoge") end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index bb64db132..c8345d87b 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -72,7 +72,7 @@ class CommentTest < ActiveSupport::TestCase dmail = Dmail.last assert_equal(<<-EOS.strip_heredoc, dmail.body) - @#{CurrentUser.name} mentioned you in a \"comment\":/posts/#{@comment.post_id}#comment-#{@comment.id} on post ##{@comment.post_id}: + @#{@comment.creator.name} mentioned you in a \"comment\":/posts/#{@comment.post_id}#comment-#{@comment.id} on post ##{@comment.post_id}: [quote] Hey @#{@user2.name} check this out! @@ -195,7 +195,7 @@ class CommentTest < ActiveSupport::TestCase should "not allow upvotes by the creator" do user = FactoryBot.create(:user) post = FactoryBot.create(:post) - c1 = FactoryBot.create(:comment, :post => post) + c1 = create(:comment, post: post, creator: CurrentUser.user) exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("up") } assert_equal("Validation failed: You cannot upvote your own comments", exception.message) diff --git a/test/unit/favorite_group_test.rb b/test/unit/favorite_group_test.rb index 7bca4a706..21285d57c 100644 --- a/test/unit/favorite_group_test.rb +++ b/test/unit/favorite_group_test.rb @@ -2,15 +2,7 @@ require 'test_helper' class FavoriteTest < ActiveSupport::TestCase def setup - @user = create(:user) - CurrentUser.user = @user - CurrentUser.ip_addr = "127.0.0.1" - @fav_group = create(:favorite_group, creator: @user, name: "blah") - end - - def teardown - CurrentUser.user = nil - CurrentUser.ip_addr = nil + @fav_group = create(:favorite_group) end context "searching by post id" do diff --git a/test/unit/forum_post_test.rb b/test/unit/forum_post_test.rb index 6f21dbc3f..df7240529 100644 --- a/test/unit/forum_post_test.rb +++ b/test/unit/forum_post_test.rb @@ -53,7 +53,7 @@ class ForumPostTest < ActiveSupport::TestCase context "outside a quote block" do setup do @user2 = FactoryBot.create(:user) - @post = FactoryBot.build(:forum_post, :topic_id => @topic.id, :body => "Hey @#{@user2.name} check this out!") + @post = build(:forum_post, creator: @user, topic: @topic, body: "Hey @#{@user2.name} check this out!") end should "create a dmail" do @@ -63,7 +63,7 @@ class ForumPostTest < ActiveSupport::TestCase dmail = Dmail.last assert_equal(<<-EOS.strip_heredoc, dmail.body) - @#{CurrentUser.name} mentioned you in topic ##{@topic.id} (\"#{@topic.title}\":[/forum_topics/#{@topic.id}?page=1]): + @#{@user.name} mentioned you in topic ##{@topic.id} (\"#{@topic.title}\":[/forum_topics/#{@topic.id}?page=1]): [quote] Hey @#{@user2.name} check this out! @@ -169,7 +169,7 @@ class ForumPostTest < ActiveSupport::TestCase end should "initialize its creator" do - post = FactoryBot.create(:forum_post, :topic_id => @topic.id) + post = create(:forum_post, topic: @topic, creator: @user) assert_equal(@user.id, post.creator_id) end diff --git a/test/unit/forum_topic_test.rb b/test/unit/forum_topic_test.rb index a5e491774..46fd4c97d 100644 --- a/test/unit/forum_topic_test.rb +++ b/test/unit/forum_topic_test.rb @@ -7,7 +7,7 @@ class ForumTopicTest < ActiveSupport::TestCase @user = FactoryBot.create(:user) CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" - @topic = FactoryBot.create(:forum_topic, :title => "xxx") + @topic = create(:forum_topic, title: "xxx", creator: @user) end teardown do @@ -113,7 +113,7 @@ class ForumTopicTest < ActiveSupport::TestCase context "#merge" do setup do - @topic2 = FactoryBot.create(:forum_topic, :title => "yyy") + @topic2 = create(:forum_topic, title: "yyy", creator: @user) FactoryBot.create(:forum_post, :topic_id => @topic.id, :body => "xxx") FactoryBot.create(:forum_post, :topic_id => @topic2.id, :body => "xxx") end @@ -127,7 +127,7 @@ class ForumTopicTest < ActiveSupport::TestCase context "constructed with nested attributes for its original post" do should "create a matching forum post" do assert_difference(["ForumTopic.count", "ForumPost.count"], 1) do - @topic = FactoryBot.create(:forum_topic, :title => "abc", :original_post_attributes => {:body => "abc"}) + @topic = create(:forum_topic, title: "abc", original_post_attributes: { body: "abc", creator: @user }) end end end diff --git a/test/unit/ip_ban_test.rb b/test/unit/ip_ban_test.rb index b05e0ccf1..6a0e3deb6 100644 --- a/test/unit/ip_ban_test.rb +++ b/test/unit/ip_ban_test.rb @@ -1,16 +1,6 @@ require 'test_helper' class IpBanTest < ActiveSupport::TestCase - setup do - CurrentUser.user = FactoryBot.create(:mod_user) - CurrentUser.ip_addr = "127.0.0.1" - end - - teardown do - CurrentUser.user = nil - CurrentUser.ip_addr = nil - end - should "be able to ban a user" do ip_ban = create(:ip_ban, ip_addr: "1.2.3.4") diff --git a/test/unit/moderator/ip_addr_search_test.rb b/test/unit/moderator/ip_addr_search_test.rb index bb1626e7a..9ef49617e 100644 --- a/test/unit/moderator/ip_addr_search_test.rb +++ b/test/unit/moderator/ip_addr_search_test.rb @@ -8,7 +8,7 @@ module Moderator CurrentUser.user = @user CurrentUser.ip_addr = "127.0.0.1" Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now) - @comment = FactoryBot.create(:comment) + @comment = create(:comment, creator: @user, creator_ip_addr: "127.0.0.1") PoolArchive.stubs(:enabled?).returns(false) PostArchive.stubs(:enabled?).returns(false) @user.reload diff --git a/test/unit/post_appeal_test.rb b/test/unit/post_appeal_test.rb index da3db59a5..ef0b9c27f 100644 --- a/test/unit/post_appeal_test.rb +++ b/test/unit/post_appeal_test.rb @@ -3,14 +3,7 @@ require 'test_helper' class PostAppealTest < ActiveSupport::TestCase context "In all cases" do setup do - @alice = FactoryBot.create(:user) - CurrentUser.user = @alice - CurrentUser.ip_addr = "127.0.0.1" - end - - teardown do - CurrentUser.user = nil - CurrentUser.ip_addr = nil + @alice = create(:user) end context "a user" do @@ -19,37 +12,27 @@ class PostAppealTest < ActiveSupport::TestCase end should "not be able to appeal a post more than twice" do - assert_difference("PostAppeal.count", 1) do - @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") - end - - assert_difference("PostAppeal.count", 0) do - @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") - end + @post_appeal = create(:post_appeal, post: @post, creator: @alice) + @post_appeal = build(:post_appeal, post: @post, creator: @alice) + assert_equal(false, @post_appeal.valid?) assert_includes(@post_appeal.errors.full_messages, "You have already appealed this post") end should "not be able to appeal more than 1 post in 24 hours" do - @post_appeal = PostAppeal.new(:post => @post, :reason => "aaa") - @post_appeal.expects(:appeal_count_for_creator).returns(1) - assert_difference("PostAppeal.count", 0) do - @post_appeal.save - end + @post_appeal = create(:post_appeal, post: @post, creator: @alice) + @post_appeal = build(:post_appeal, post: create(:post, is_deleted: true), creator: @alice) + + assert_equal(false, @post_appeal.valid?) assert_equal(["You can appeal at most 1 post a day"], @post_appeal.errors.full_messages) end should "not be able to appeal an active post" do @post.update_attribute(:is_deleted, false) - assert_difference("PostAppeal.count", 0) do - @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") - end - assert_equal(["Post is active"], @post_appeal.errors.full_messages) - end + @post_appeal = build(:post_appeal, post: @post, creator: @alice) - should "initialize its creator" do - @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") - assert_equal(@alice.id, @post_appeal.creator_id) + assert_equal(false, @post_appeal.valid?) + assert_equal(["Post is active"], @post_appeal.errors.full_messages) end end end diff --git a/test/unit/post_approval_test.rb b/test/unit/post_approval_test.rb index c6603a918..765ebb31e 100644 --- a/test/unit/post_approval_test.rb +++ b/test/unit/post_approval_test.rb @@ -35,8 +35,8 @@ class PostApprovalTest < ActiveSupport::TestCase context "that is then flagged" do setup do - @user2 = FactoryBot.create(:user) - @user3 = FactoryBot.create(:user) + @user2 = create(:user, created_at: 2.weeks.ago) + @user3 = create(:user, created_at: 2.weeks.ago) @approver2 = FactoryBot.create(:user) @approver2.can_approve_posts = true @approver2.save diff --git a/test/unit/post_event_test.rb b/test/unit/post_event_test.rb index 372f6d193..460414504 100644 --- a/test/unit/post_event_test.rb +++ b/test/unit/post_event_test.rb @@ -2,22 +2,10 @@ require 'test_helper' class PostEventTest < ActiveSupport::TestCase def setup - super - - travel_to(2.weeks.ago) do - CurrentUser.user = FactoryBot.create(:user) - CurrentUser.ip_addr = "127.0.0.1" - end - - @post = FactoryBot.create(:post) - @post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false) - @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") - end - - def teardown - super - CurrentUser.user = nil - CurrentUser.ip_addr = nil + @user = create(:user, created_at: 2.weeks.ago) + @post = create(:post) + @post_flag = create(:post_flag, creator: @user, post: @post) + @post_appeal = create(:post_appeal, creator: @user, post: @post) end context "PostEvent.find_for_post" do diff --git a/test/unit/post_flag_test.rb b/test/unit/post_flag_test.rb index 0b288d223..6ddfd212f 100644 --- a/test/unit/post_flag_test.rb +++ b/test/unit/post_flag_test.rb @@ -12,54 +12,37 @@ class PostFlagTest < ActiveSupport::TestCase end context "a basic user" do - setup do - travel_to(2.weeks.ago) do - @bob = create(:user) - end - end - should "not be able to flag more than 1 post in 24 hours" do - @post_flag = PostFlag.new(post: @post, reason: "aaa", is_resolved: false) + @bob = create(:user, created_at: 2.weeks.ago) + @post_flag = build(:post_flag, creator: @bob) @post_flag.expects(:flag_count_for_creator).returns(1) - assert_difference("PostFlag.count", 0) do - as(@bob) { @post_flag.save } - end + + assert_equal(false, @post_flag.valid?) assert_equal(["You can flag 1 post a day"], @post_flag.errors.full_messages) end end context "a gold user" do setup do - travel_to(2.weeks.ago) do - @bob = create(:gold_user) - end + @bob = create(:gold_user, created_at: 1.month.ago) end should "not be able to flag a post more than twice" do - assert_difference(-> { PostFlag.count }, 1) do - as(@bob) do - @post_flag = PostFlag.create(post: @post, reason: "aaa", is_resolved: false) - end - end - - assert_difference(-> { PostFlag.count }, 0) do - as(@bob) do - @post_flag = PostFlag.create(post: @post, reason: "aaa", is_resolved: false) - end - end + @post_flag = create(:post_flag, post: @post, creator: @bob) + @post_flag = build(:post_flag, post: @post, creator: @bob) + assert_equal(false, @post_flag.valid?) assert_equal(["have already flagged this post"], @post_flag.errors[:creator_id]) end should "not be able to flag more than 10 posts in 24 hours" do - as(@bob) do - @post_flag = PostFlag.new(post: @post, reason: "aaa", is_resolved: false) - @post_flag.expects(:flag_count_for_creator).returns(10) + @post_flag = build(:post_flag, post: @post, creator: @bob) + @post_flag.expects(:flag_count_for_creator).returns(10) - assert_difference(-> { PostFlag.count }, 0) do - @post_flag.save - end + assert_difference(-> { PostFlag.count }, 0) do + @post_flag.save end + assert_equal(["You can flag 10 posts a day"], @post_flag.errors.full_messages) end @@ -68,11 +51,8 @@ class PostFlagTest < ActiveSupport::TestCase @post.update(is_deleted: true) end - assert_difference(-> { PostFlag.count }, 0) do - as(@bob) do - @post_flag = PostFlag.create(post: @post, reason: "aaa", is_resolved: false) - end - end + @post_flag = build(:post_flag, post: @post, creator: @bob) + @post_flag.save assert_equal(["Post is deleted"], @post_flag.errors.full_messages) end @@ -80,9 +60,7 @@ class PostFlagTest < ActiveSupport::TestCase as(@alice) do @post.update(is_pending: true) end - as(@bob) do - @flag = @post.flags.create(reason: "test") - end + @flag = @post.flags.create(reason: "test", creator: @bob) assert_equal(["Post is pending and cannot be flagged"], @flag.errors.full_messages) end @@ -94,49 +72,35 @@ class PostFlagTest < ActiveSupport::TestCase @users = FactoryBot.create_list(:user, 2) end - as(@users.first) do - @flag1 = PostFlag.create(post: @post, reason: "something") - end + @flag1 = create(:post_flag, post: @post, reason: "something", creator: @users.first) as(@mod) do @post.approve! end travel_to(PostFlag::COOLDOWN_PERIOD.from_now - 1.minute) do - as(@users.second) do - @flag2 = PostFlag.create(post: @post, reason: "something") - end + @flag2 = build(:post_flag, post: @post, reason: "something", creator: @users.second) + assert_equal(false, @flag2.valid?) assert_match(/cannot be flagged more than once/, @flag2.errors[:post].join) end travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do - as(@users.second) do - @flag3 = PostFlag.create(post: @post, reason: "something") - end + @flag3 = create(:post_flag, post: @post, reason: "something", creator: @users.second) assert(@flag3.errors.empty?) end end should "initialize its creator" do - @post_flag = as(@alice) do - PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false) - end + @post_flag = create(:post_flag, creator: @alice) assert_equal(@alice.id, @post_flag.creator_id) end end context "a moderator user" do - setup do - travel_to(2.weeks.ago) do - @dave = create(:moderator_user) - end - end - should "not be able to view flags on their own uploads" do + @dave = create(:moderator_user, created_at: 1.month.ago) @modpost = create(:post, :tag_string => "mmm", :uploader => @dave) - as(@alice) do - @flag1 = PostFlag.create(:post => @modpost, :reason => "aaa", :is_resolved => false) - end + @flag1 = create(:post_flag, post: @modpost, creator: @alice) assert_equal(false, @dave.can_view_flagger_on_post?(@flag1)) diff --git a/test/unit/post_pruner_test.rb b/test/unit/post_pruner_test.rb index 5ba413419..09e491fa7 100644 --- a/test/unit/post_pruner_test.rb +++ b/test/unit/post_pruner_test.rb @@ -2,34 +2,17 @@ require 'test_helper' class PostPrunerTest < ActiveSupport::TestCase def setup - super - - @user = FactoryBot.create(:admin_user) - CurrentUser.user = @user - CurrentUser.ip_addr = "127.0.0.1" - - travel_to(2.weeks.ago) do - @flagger = FactoryBot.create(:gold_user) - end @old_post = FactoryBot.create(:post, :created_at => 5.days.ago, :is_pending => true) @unresolved_flagged_post = FactoryBot.create(:post, :is_flagged => true) @resolved_flagged_post = FactoryBot.create(:post, :is_flagged => true) - CurrentUser.scoped(@flagger, "127.0.0.2") do - @unresolved_post_flag = FactoryBot.create(:post_flag, :created_at => 5.days.ago, :is_resolved => false, :post_id => @unresolved_flagged_post.id) - @resolved_post_flag = FactoryBot.create(:post_flag, :created_at => 5.days.ago, :is_resolved => true, :post_id => @resolved_flagged_post.id) - end + @flagger = create(:gold_user, created_at: 2.weeks.ago) + @unresolved_post_flag = create(:post_flag, creator: @flagger, created_at: 5.days.ago, is_resolved: false, post: @unresolved_flagged_post) + @resolved_post_flag = create(:post_flag, creator: @flagger, created_at: 5.days.ago, is_resolved: true, post: @resolved_flagged_post) PostPruner.new.prune! end - def teardown - super - - CurrentUser.user = nil - CurrentUser.ip_addr = nil - end - should "prune old pending posts" do @old_post.reload assert(@old_post.is_deleted?) diff --git a/test/unit/post_test.rb b/test/unit/post_test.rb index a8a73e3d9..703888d59 100644 --- a/test/unit/post_test.rb +++ b/test/unit/post_test.rb @@ -2030,7 +2030,7 @@ class PostTest < ActiveSupport::TestCase should "return posts for the commenter: metatag" do users = FactoryBot.create_list(:user, 2, created_at: 2.weeks.ago) posts = FactoryBot.create_list(:post, 2) - comms = users.zip(posts).map { |u, p| as(u) { FactoryBot.create(:comment, post: p) } } + comms = users.zip(posts).map { |u, p| as(u) { FactoryBot.create(:comment, creator: u, post: p) } } assert_tag_match([posts[0]], "commenter:#{users[0].name}") assert_tag_match([posts[1]], "commenter:#{users[1].name}") @@ -2038,8 +2038,8 @@ class PostTest < ActiveSupport::TestCase should "return posts for the commenter: metatag" do posts = FactoryBot.create_list(:post, 2) - FactoryBot.create(:comment, post: posts[0], is_deleted: false) - FactoryBot.create(:comment, post: posts[1], is_deleted: true) + create(:comment, creator: create(:user, created_at: 2.weeks.ago), post: posts[0], is_deleted: false) + create(:comment, creator: create(:user, created_at: 2.weeks.ago), post: posts[1], is_deleted: true) assert_tag_match([posts[0]], "commenter:any") assert_tag_match([posts[1]], "commenter:none") @@ -2143,7 +2143,7 @@ class PostTest < ActiveSupport::TestCase pending = FactoryBot.create(:post, is_pending: true) disapproved = FactoryBot.create(:post, is_pending: true) - FactoryBot.create(:post_flag, post: flagged) + create(:post_flag, post: flagged, creator: create(:user, created_at: 2.weeks.ago)) FactoryBot.create(:post_disapproval, post: disapproved, reason: "disinterest") assert_tag_match([pending, flagged], "status:unmoderated") @@ -2352,9 +2352,10 @@ class PostTest < ActiveSupport::TestCase tag_string: tags[n - 1] ) - FactoryBot.create(:artist_commentary, post: p) - FactoryBot.create(:comment, post: p, do_not_bump_post: false) - FactoryBot.create(:note, post: p) + u = create(:user, created_at: 2.weeks.ago) + create(:artist_commentary, post: p) + create(:comment, post: p, creator: u, do_not_bump_post: false) + create(:note, post: p, creator: u) p end @@ -2406,11 +2407,12 @@ class PostTest < ActiveSupport::TestCase post1 = FactoryBot.create(:post) post2 = FactoryBot.create(:post) post3 = FactoryBot.create(:post) + user = create(:gold_user) - CurrentUser.scoped(FactoryBot.create(:gold_user), "127.0.0.1") do - comment1 = FactoryBot.create(:comment, :post => post1) - comment2 = FactoryBot.create(:comment, :post => post2, :do_not_bump_post => true) - comment3 = FactoryBot.create(:comment, :post => post3) + as(user) do + comment1 = create(:comment, creator: user, post: post1) + comment2 = create(:comment, creator: user, post: post2, do_not_bump_post: true) + comment3 = create(:comment, creator: user, post: post3) end assert_tag_match([post3, post1, post2], "order:comment_bumped") @@ -2749,11 +2751,10 @@ class PostTest < ActiveSupport::TestCase @src = FactoryBot.create(:post, image_width: 100, image_height: 100, tag_string: "translated partially_translated", has_embedded_notes: true) @dst = FactoryBot.create(:post, image_width: 200, image_height: 200, tag_string: "translation_request") - @src.notes.create(x: 10, y: 10, width: 10, height: 10, body: "test") - @src.notes.create(x: 10, y: 10, width: 10, height: 10, body: "deleted", is_active: false) - @src.reload + create(:note, post: @src, x: 10, y: 10, width: 10, height: 10, body: "test") + create(:note, post: @src, x: 10, y: 10, width: 10, height: 10, body: "deleted", is_active: false) - @src.copy_notes_to(@dst) + @src.reload.copy_notes_to(@dst) end should "copy notes and tags" do diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 114b6bff9..7c963c9b0 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -108,7 +108,7 @@ class TagAliasTest < ActiveSupport::TestCase end should "populate the creator information" do - ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb") + ta = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", creator: CurrentUser.user) assert_equal(CurrentUser.user.id, ta.creator_id) end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index 4f5bdc069..279dcd68f 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -6,7 +6,6 @@ class TagImplicationTest < ActiveSupport::TestCase user = FactoryBot.create(:admin_user) CurrentUser.user = user CurrentUser.ip_addr = "127.0.0.1" - @user = FactoryBot.create(:user) end teardown do @@ -104,7 +103,7 @@ class TagImplicationTest < ActiveSupport::TestCase end should "populate the creator information" do - ti = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb") + ti = create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", creator: CurrentUser.user) assert_equal(CurrentUser.user.id, ti.creator_id) end @@ -263,7 +262,6 @@ class TagImplicationTest < ActiveSupport::TestCase context "with an associated forum topic" do setup do - @admin = FactoryBot.create(:admin_user) @topic = FactoryBot.create(:forum_topic, :title => "Tag implication: aaa -> bbb") @post = FactoryBot.create(:forum_post, topic_id: @topic.id, :body => TagImplicationRequest.command_string("aaa", "bbb")) @implication = FactoryBot.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "bbb", :forum_topic => @topic, :forum_post => @post, :status => "pending") diff --git a/test/unit/user_feedback_test.rb b/test/unit/user_feedback_test.rb index 86463f3a4..85ea65b31 100644 --- a/test/unit/user_feedback_test.rb +++ b/test/unit/user_feedback_test.rb @@ -2,37 +2,25 @@ require 'test_helper' class UserFeedbackTest < ActiveSupport::TestCase context "A user's feedback" do - setup do - CurrentUser.ip_addr = "127.0.0.1" - end - - teardown do - CurrentUser.user = nil - CurrentUser.ip_addr = nil - end - should "create a dmail" do user = FactoryBot.create(:user) gold = FactoryBot.create(:gold_user) member = FactoryBot.create(:user) - dmail = <<~EOS.chomp @#{gold.name} created a "positive record":/user_feedbacks?search[user_id]=#{user.id} for your account: good job! EOS - CurrentUser.user = gold assert_difference("Dmail.count", 1) do - FactoryBot.create(:user_feedback, :user => user, :body => "good job!") + create(:user_feedback, creator: gold, user: user, body: "good job!") assert_equal(dmail, user.dmails.last.body) end end should "not validate if the creator is the user" do - gold_user = FactoryBot.create(:gold_user) - CurrentUser.user = gold_user - feedback = FactoryBot.build(:user_feedback, :user => gold_user) + user = FactoryBot.create(:gold_user) + feedback = build(:user_feedback, creator: user, user: user) feedback.save assert_equal(["You cannot submit feedback for yourself"], feedback.errors.full_messages) end @@ -42,12 +30,10 @@ class UserFeedbackTest < ActiveSupport::TestCase gold = FactoryBot.create(:gold_user) member = FactoryBot.create(:user) - CurrentUser.user = gold - feedback = FactoryBot.create(:user_feedback, :user => user) + feedback = FactoryBot.create(:user_feedback, creator: gold, user: user) assert(feedback.errors.empty?) - CurrentUser.user = member - feedback = FactoryBot.build(:user_feedback, :user => user) + feedback = build(:user_feedback, creator: member, user: user) feedback.save assert_equal(["You must be gold"], feedback.errors.full_messages) end diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index c00804bcb..4d58a81f4 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -88,9 +88,7 @@ class UserTest < ActiveSupport::TestCase @user.update_column(:created_at, 1.year.ago) assert(@user.can_comment?) assert(!@user.is_comment_limited?) - Danbooru.config.member_comment_limit.times do - FactoryBot.create(:comment) - end + create_list(:comment, Danbooru.config.member_comment_limit, creator: @user) assert(@user.is_comment_limited?) end