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

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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?

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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,

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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]}

View File

@@ -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

View File

@@ -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?

View File

@@ -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

View File

@@ -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

View File

@@ -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?

View File

@@ -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

View File

@@ -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

View File

@@ -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"

View File

@@ -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(" ", "_")

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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}

View File

@@ -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

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory :favorite_group do
creator
name { FFaker::Lorem.word }
end
end

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:forum_post) do
creator
body {FFaker::Lorem.sentences.join(" ")}
end
end

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:forum_topic) do
creator
title {FFaker::Lorem.words.join(" ")}
is_sticky {false}
is_locked {false}

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:news_update) do
creator
message {"xxx"}
end
end

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:note) do
creator
post
x { 1 }
y { 1 }

View File

@@ -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

View File

@@ -1,5 +1,7 @@
FactoryBot.define do
factory(:post_appeal) do
creator
post
reason {"xxx"}
end
end

View File

@@ -1,5 +1,7 @@
FactoryBot.define do
factory(:post_flag) do
creator
post
reason {"xxx"}
is_resolved {false}
end

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory :tag_alias do
creator
antecedent_name {"aaa"}
consequent_name {"bbb"}
status {"active"}

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory :tag_implication do
creator
antecedent_name {"aaa"}
consequent_name {"bbb"}
status {"active"}

View File

@@ -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} }

View File

@@ -1,5 +1,6 @@
FactoryBot.define do
factory(:user_feedback) do
creator factory: :builder_user
user
category { "positive" }
body { FFaker::Lorem.words.join(" ") }

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

View File

@@ -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

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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)

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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")

View File

@@ -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

View File

@@ -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,38 +12,28 @@ 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
@post_appeal = build(:post_appeal, post: @post, creator: @alice)
assert_equal(false, @post_appeal.valid?)
assert_equal(["Post is active"], @post_appeal.errors.full_messages)
end
should "initialize its creator" do
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa")
assert_equal(@alice.id, @post_appeal.creator_id)
end
end
end
end

View File

@@ -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

View File

@@ -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

View File

@@ -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 = 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
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))

View File

@@ -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?)

View File

@@ -2030,7 +2030,7 @@ class PostTest < ActiveSupport::TestCase
should "return posts for the commenter:<name> 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:<any|none> 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

View File

@@ -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

View File

@@ -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")

View File

@@ -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

View File

@@ -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