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 end
def ban def ban
@artist.ban! @artist.ban!(banner: CurrentUser.user)
redirect_to(artist_path(@artist), :notice => "Artist was banned") redirect_to(artist_path(@artist), :notice => "Artist was banned")
end end
@@ -48,7 +48,7 @@ class ArtistsController < ApplicationController
end end
def create def create
@artist = Artist.create(artist_params) @artist = Artist.create(artist_params.merge(creator: CurrentUser.user))
respond_with(@artist) respond_with(@artist)
end end

View File

@@ -10,7 +10,7 @@ class BulkUpdateRequestsController < ApplicationController
end end
def create 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) respond_with(@bulk_update_request, :location => bulk_update_requests_path)
end end

View File

@@ -33,7 +33,7 @@ class CommentsController < ApplicationController
end end
def create 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("; ") flash[:notice] = @comment.valid? ? "Comment posted" : @comment.errors.full_messages.join("; ")
respond_with(@comment) do |format| respond_with(@comment) do |format|
format.html do format.html do

View File

@@ -24,7 +24,7 @@ class FavoriteGroupsController < ApplicationController
end end
def create def create
@favorite_group = FavoriteGroup.create(favgroup_params) @favorite_group = CurrentUser.favorite_groups.create(favgroup_params)
respond_with(@favorite_group) respond_with(@favorite_group)
end end

View File

@@ -9,7 +9,7 @@ class ForumPostVotesController < ApplicationController
def create def create
@forum_post = ForumPost.find(params[:forum_post_id]) @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) respond_with(@forum_post_vote)
end end

View File

@@ -42,7 +42,7 @@ class ForumPostsController < ApplicationController
end end
def create 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 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)) respond_with(@forum_post, :location => forum_topic_path(@forum_post.topic, :page => page))
end end

View File

@@ -43,7 +43,11 @@ class ForumTopicsController < ApplicationController
end end
def create 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) respond_with(@forum_topic)
end end

View File

@@ -7,7 +7,7 @@ class IpBansController < ApplicationController
end end
def create 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) respond_with(@ip_ban, :location => ip_bans_path)
end end

View File

@@ -24,7 +24,7 @@ class NewsUpdatesController < ApplicationController
end end
def create 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) respond_with(@news_update, :location => news_updates_path)
end end

View File

@@ -19,7 +19,7 @@ class NotesController < ApplicationController
end end
def create def create
@note = Note.create(note_params(:create)) @note = Note.create(note_params(:create).merge(creator: CurrentUser.user))
respond_with(@note) do |fmt| respond_with(@note) do |fmt|
fmt.json do fmt.json do
if @note.errors.any? if @note.errors.any?

View File

@@ -43,7 +43,7 @@ class PoolsController < ApplicationController
end end
def create 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("; ") flash[:notice] = @pool.valid? ? "Pool created" : @pool.errors.full_messages.join("; ")
respond_with(@pool) respond_with(@pool)
end end

View File

@@ -13,7 +13,7 @@ class PostAppealsController < ApplicationController
end end
def create def create
@post_appeal = PostAppeal.create(post_appeal_params) @post_appeal = PostAppeal.create(post_appeal_params.merge(creator: CurrentUser.user))
respond_with(@post_appeal) respond_with(@post_appeal)
end end

View File

@@ -13,7 +13,7 @@ class PostFlagsController < ApplicationController
end end
def create def create
@post_flag = PostFlag.create(post_flag_params) @post_flag = PostFlag.create(post_flag_params.merge(creator: CurrentUser.user))
respond_with(@post_flag) respond_with(@post_flag)
end end

View File

@@ -24,7 +24,7 @@ class UserFeedbacksController < ApplicationController
end end
def create 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) respond_with(@user_feedback)
end end

View File

@@ -58,13 +58,13 @@ class AliasAndImplicationImporter
tokens.map do |token| tokens.map do |token|
case token[0] case token[0]
when :create_alias 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? unless tag_alias.valid?
raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})" raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})"
end end
when :create_implication 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? unless tag_implication.valid?
raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})" raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})"
end end
@@ -127,7 +127,7 @@ class AliasAndImplicationImporter
tokens.map do |token| tokens.map do |token|
case token[0] case token[0]
when :create_alias 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? unless tag_alias.valid?
raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})" raise Error, "Error: #{tag_alias.errors.full_messages.join("; ")} (create alias #{tag_alias.antecedent_name} -> #{tag_alias.consequent_name})"
end end
@@ -135,7 +135,7 @@ class AliasAndImplicationImporter
tag_alias.approve!(approver: approver, update_topic: false) tag_alias.approve!(approver: approver, update_topic: false)
when :create_implication 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? unless tag_implication.valid?
raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})" raise Error, "Error: #{tag_implication.errors.full_messages.join("; ")} (create implication #{tag_implication.antecedent_name} -> #{tag_implication.consequent_name})"
end end

View File

@@ -20,7 +20,7 @@ module ApproverPruner
inactive_approvers.each do |user| inactive_approvers.each do |user|
CurrentUser.scoped(User.system, "127.0.0.1") do CurrentUser.scoped(User.system, "127.0.0.1") do
user.update!(can_approve_posts: false) 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( Dmail.create_automated(
to_id: user.id, to_id: user.id,

View File

@@ -22,7 +22,7 @@ class ForumUpdater
end end
def create_response(body) 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 end
def update_title(title_tag) def update_title(title_tag)

View File

@@ -22,8 +22,9 @@ module TagRelationshipRetirementService
def forum_topic def forum_topic
topic = ForumTopic.where(title: forum_topic_title).first topic = ForumTopic.where(title: forum_topic_title).first
if topic.nil? if topic.nil?
topic = CurrentUser.as_system do CurrentUser.as(User.system) do
ForumTopic.create(title: forum_topic_title, category_id: 1, original_post_attributes: {body: forum_topic_body}) 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
end end
return topic return topic

View File

@@ -120,9 +120,7 @@ class UploadService
update_ugoira_frame_data(post, upload) update_ugoira_frame_data(post, upload)
if md5_changed if md5_changed
CurrentUser.as(User.system) do 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")
post.comments.create!(body: comment_replacement_message(post, replacement), do_not_bump_post: true)
end
else else
purge_cached_urls(post) purge_cached_urls(post)
end end

View File

@@ -96,10 +96,6 @@ class UserPromotion
end end
def create_user_feedback def create_user_feedback
user.feedback.create( UserFeedback.create(user: user, creator: promoter, category: "neutral", body: build_messages, disable_dmail_notification: true)
:category => "neutral",
:body => build_messages,
:disable_dmail_notification => true
)
end end
end end

View File

@@ -373,18 +373,6 @@ class ApplicationRecord < ActiveRecord::Base
concerning :UserMethods do concerning :UserMethods do
class_methods 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 = {}) def belongs_to_updater(options = {})
class_eval do class_eval do
belongs_to :updater, options.merge(class_name: "User") belongs_to :updater, options.merge(class_name: "User")

View File

@@ -13,7 +13,7 @@ class Artist < ApplicationRecord
after_save :clear_url_string_changed after_save :clear_url_string_changed
validate :validate_tag_category validate :validate_tag_category
validates :name, tag_name: true, uniqueness: true 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 :members, :class_name => "Artist", :foreign_key => "group_name", :primary_key => "name"
has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl", :autosave => true has_many :urls, :dependent => :destroy, :class_name => "ArtistUrl", :autosave => true
has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion" has_many :versions, -> {order("artist_versions.id ASC")}, :class_name => "ArtistVersion"
@@ -418,15 +418,15 @@ class Artist < ApplicationRecord
end end
end end
def ban! def ban!(banner: CurrentUser.user)
Post.transaction do Post.transaction do
CurrentUser.without_safe_mode do CurrentUser.without_safe_mode do
Post.tag_match(name).each(&:ban!) Post.tag_match(name).each(&:ban!)
# potential race condition but unlikely # potential race condition but unlikely
unless TagImplication.where(:antecedent_name => name, :consequent_name => "banned_artist").exists? 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 = TagImplication.create!(antecedent_name: name, consequent_name: "banned_artist", skip_secondary_validations: true, creator: banner)
tag_implication.approve!(approver: CurrentUser.user) tag_implication.approve!(approver: banner)
end end
update_column(:is_banned, true) update_column(:is_banned, true)

View File

@@ -15,7 +15,7 @@ class BulkUpdateRequest < ApplicationRecord
validate :validate_script, :on => :create validate :validate_script, :on => :create
before_validation :initialize_attributes, :on => :create before_validation :initialize_attributes, :on => :create
before_validation :normalize_text before_validation :normalize_text
after_create :create_forum_topic before_create :create_forum_topic
after_save :update_notice after_save :update_notice
scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) } 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 end
def create_forum_topic def create_forum_topic
if forum_topic_id CurrentUser.as(user) do
forum_post = forum_topic.posts.create(body: reason_with_link) self.forum_topic = ForumTopic.create(title: title, category_id: 1, creator: user) unless forum_topic.present?
update(forum_post_id: forum_post.id) self.forum_post = forum_topic.posts.create(body: reason_with_link, creator: user) unless forum_post.present?
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)
end end
end end

View File

@@ -5,7 +5,7 @@ class Comment < ApplicationRecord
validate :validate_comment_is_not_spam, on: :create validate :validate_comment_is_not_spam, on: :create
validates_presence_of :body, :message => "has no content" validates_presence_of :body, :message => "has no content"
belongs_to :post belongs_to :post
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to_updater belongs_to_updater
has_many :moderation_reports, as: :model has_many :moderation_reports, as: :model
has_many :votes, :class_name => "CommentVote", :dependent => :destroy has_many :votes, :class_name => "CommentVote", :dependent => :destroy

View File

@@ -1,7 +1,7 @@
class FavoriteGroup < ApplicationRecord class FavoriteGroup < ApplicationRecord
validates_uniqueness_of :name, :case_sensitive => false, :scope => :creator_id validates_uniqueness_of :name, :case_sensitive => false, :scope => :creator_id
validates_format_of :name, :with => /\A[^,]+\Z/, :message => "cannot have commas" 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 :normalize_name
before_validation :strip_name before_validation :strip_name
validate :creator_can_create_favorite_groups, :on => :create validate :creator_can_create_favorite_groups, :on => :create
@@ -45,9 +45,9 @@ class FavoriteGroup < ApplicationRecord
extend SearchMethods extend SearchMethods
def creator_can_create_favorite_groups 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." 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." error += " Upgrade your account to create more."
end end
self.errors.add(:base, error) self.errors.add(:base, error)

View File

@@ -2,7 +2,7 @@ class ForumPost < ApplicationRecord
include Mentionable include Mentionable
attr_readonly :topic_id attr_readonly :topic_id
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to_updater belongs_to_updater
belongs_to :topic, :class_name => "ForumTopic" belongs_to :topic, :class_name => "ForumTopic"
has_many :dtext_links, as: :model, dependent: :destroy has_many :dtext_links, as: :model, dependent: :destroy
@@ -113,7 +113,7 @@ class ForumPost < ApplicationRecord
end end
def validate_topic_is_unlocked def validate_topic_is_unlocked
return if CurrentUser.is_moderator? return if creator.is_moderator?
return if topic.nil? return if topic.nil?
if topic.is_locked? if topic.is_locked?
@@ -139,7 +139,7 @@ class ForumPost < ApplicationRecord
def update_topic_updated_at_on_create def update_topic_updated_at_on_create
if topic if topic
# need to do this to bypass the topic's original post from getting touched # 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 topic.response_count += 1
end end
end end

View File

@@ -1,5 +1,5 @@
class ForumPostVote < ApplicationRecord class ForumPostVote < ApplicationRecord
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to :forum_post belongs_to :forum_post
validates :creator_id, uniqueness: {scope: :forum_post_id} validates :creator_id, uniqueness: {scope: :forum_post_id}
validates :score, inclusion: {in: [-1, 0, 1]} validates :score, inclusion: {in: [-1, 0, 1]}

View File

@@ -11,7 +11,7 @@ class ForumTopic < ApplicationRecord
Admin: User::Levels::ADMIN Admin: User::Levels::ADMIN
} }
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to_updater belongs_to_updater
has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy has_many :posts, -> {order("forum_posts.id asc")}, :class_name => "ForumPost", :foreign_key => "topic_id", :dependent => :destroy
has_many :moderation_reports, through: :posts has_many :moderation_reports, through: :posts
@@ -180,7 +180,7 @@ class ForumTopic < ApplicationRecord
end end
def update_orignal_post 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 end
def viewable_moderation_reports def viewable_moderation_reports

View File

@@ -1,10 +1,10 @@
class IpBan < ApplicationRecord class IpBan < ApplicationRecord
belongs_to_creator belongs_to :creator, class_name: "User"
validate :validate_ip_addr validate :validate_ip_addr
validates_presence_of :reason validates_presence_of :reason
validates_uniqueness_of :ip_addr validates_uniqueness_of :ip_addr
after_create { ModAction.log("#{CurrentUser.name} created ip ban for #{ip_addr}", :ip_ban_create) } after_create { ModAction.log("#{creator.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_destroy { ModAction.log("#{creator.name} deleted ip ban for #{ip_addr}", :ip_ban_delete) }
def self.is_banned?(ip_addr) def self.is_banned?(ip_addr)
where("ip_addr >>= ?", ip_addr).exists? where("ip_addr >>= ?", ip_addr).exists?

View File

@@ -1,5 +1,5 @@
class NewsUpdate < ApplicationRecord class NewsUpdate < ApplicationRecord
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to_updater belongs_to_updater
scope :recent, -> {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)} scope :recent, -> {where("created_at >= ?", 2.weeks.ago).order("created_at desc").limit(5)}
end end

View File

@@ -3,7 +3,7 @@ class Note < ApplicationRecord
attr_accessor :html_id attr_accessor :html_id
belongs_to :post 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 has_many :versions, -> {order("note_versions.id ASC")}, :class_name => "NoteVersion", :dependent => :destroy
validates_presence_of :x, :y, :width, :height, :body validates_presence_of :x, :y, :width, :height, :body
validate :note_within_image validate :note_within_image
@@ -129,8 +129,9 @@ class Note < ApplicationRecord
save! save!
end end
def copy_to(new_post) def copy_to(new_post, creator: CurrentUser.user)
new_note = dup new_note = dup
new_note.creator = creator
new_note.post_id = new_post.id new_note.post_id = new_post.id
new_note.version = 0 new_note.version = 0

View File

@@ -3,7 +3,7 @@ class Pool < ApplicationRecord
POOL_ORDER_LIMIT = 1000 POOL_ORDER_LIMIT = 1000
array_attribute :post_ids, parse: /\d+/, cast: :to_i 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? validates_uniqueness_of :name, case_sensitive: false, if: :name_changed?
validate :validate_name, 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) !is_status_locked? && (is_pending? || is_flagged? || is_deleted?) && uploader != user && !approved_by?(user)
end end
def flag!(reason, options = {}) def flag!(reason, is_deletion: false)
flag = flags.create(:reason => reason, :is_resolved => false, :is_deletion => options[:is_deletion]) flag = flags.create(reason: reason, is_resolved: false, is_deletion: is_deletion, creator: CurrentUser.user)
if flag.errors.any? if flag.errors.any?
raise PostFlag::Error.new(flag.errors.full_messages.join("; ")) raise PostFlag::Error.new(flag.errors.full_messages.join("; "))
@@ -746,7 +746,7 @@ class Post < ApplicationRecord
when /^newpool:(.+)$/i when /^newpool:(.+)$/i
pool = Pool.find_by_name($1) pool = Pool.find_by_name($1)
if pool.nil? 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 end
end end

View File

@@ -8,7 +8,6 @@ class PostAppeal < ApplicationRecord
validates_presence_of :reason validates_presence_of :reason
validate :validate_post_is_inactive validate :validate_post_is_inactive
validate :validate_creator_is_not_limited 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" validates_uniqueness_of :creator_id, :scope => :post_id, :message => "have already appealed this post"
api_attributes including: [:is_resolved] api_attributes including: [:is_resolved]
@@ -60,10 +59,6 @@ class PostAppeal < ApplicationRecord
end end
end end
def initialize_creator
self.creator_id = CurrentUser.id
end
def appeal_count_for_creator def appeal_count_for_creator
creator.post_appeals.recent.count creator.post_appeals.recent.count
end end

View File

@@ -9,7 +9,7 @@ class PostFlag < ApplicationRecord
COOLDOWN_PERIOD = 3.days COOLDOWN_PERIOD = 3.days
belongs_to_creator :class_name => "User" belongs_to :creator, class_name: "User"
belongs_to :post belongs_to :post
validates_presence_of :reason validates_presence_of :reason
validate :validate_creator_is_not_limited, on: :create validate :validate_creator_is_not_limited, on: :create
@@ -124,7 +124,7 @@ class PostFlag < ApplicationRecord
def validate_creator_is_not_limited def validate_creator_is_not_limited
return if is_deletion return if is_deletion
if CurrentUser.can_approve_posts? if creator.can_approve_posts?
# do nothing # do nothing
elsif creator.created_at > 1.week.ago elsif creator.created_at > 1.week.ago
errors[:creator] << "cannot flag within the first week of sign up" 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 attr_accessor :skip_secondary_validations
belongs_to_creator belongs_to :creator, class_name: "User"
belongs_to :approver, class_name: "User", optional: true belongs_to :approver, class_name: "User", optional: true
belongs_to :forum_post, optional: true belongs_to :forum_post, optional: true
belongs_to :forum_topic, optional: true belongs_to :forum_topic, optional: true
@@ -24,7 +24,6 @@ class TagRelationship < ApplicationRecord
scope :pending, -> {where(status: "pending")} scope :pending, -> {where(status: "pending")}
scope :retired, -> {where(status: "retired")} scope :retired, -> {where(status: "retired")}
before_validation :initialize_creator, :on => :create
before_validation :normalize_names before_validation :normalize_names
validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/ validates_format_of :status, :with => /\A(active|deleted|pending|processing|queued|retired|error: .*)\Z/
validates_presence_of :antecedent_name, :consequent_name validates_presence_of :antecedent_name, :consequent_name
@@ -33,10 +32,6 @@ class TagRelationship < ApplicationRecord
validate :antecedent_and_consequent_are_different validate :antecedent_and_consequent_are_different
after_save :update_notice after_save :update_notice
def initialize_creator
self.creator_id = CurrentUser.user.id
end
def normalize_names def normalize_names
self.antecedent_name = antecedent_name.mb_chars.downcase.tr(" ", "_") self.antecedent_name = antecedent_name.mb_chars.downcase.tr(" ", "_")
self.consequent_name = consequent_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_update :encrypt_password_on_update
before_create :promote_to_admin_if_first_user before_create :promote_to_admin_if_first_user
before_create :customize_new_user before_create :customize_new_user
has_many :artists, foreign_key: :creator_id
has_many :artist_versions, foreign_key: :updater_id has_many :artist_versions, foreign_key: :updater_id
has_many :artist_commentary_versions, foreign_key: :updater_id has_many :artist_commentary_versions, foreign_key: :updater_id
has_many :comments, foreign_key: :creator_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 :feedback, :class_name => "UserFeedback", :dependent => :destroy
has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id has_many :forum_post_votes, dependent: :destroy, foreign_key: :creator_id
has_many :moderation_reports, as: :model has_many :moderation_reports, as: :model
has_many :pools, foreign_key: :creator_id
has_many :posts, :foreign_key => "uploader_id" has_many :posts, :foreign_key => "uploader_id"
has_many :post_appeals, foreign_key: :creator_id has_many :post_appeals, foreign_key: :creator_id
has_many :post_approvals, :dependent => :destroy has_many :post_approvals, :dependent => :destroy
@@ -108,6 +110,7 @@ class User < ApplicationRecord
has_one :dmail_filter has_one :dmail_filter
has_one :super_voter has_one :super_voter
has_one :token_bucket has_one :token_bucket
has_many :notes, foreign_key: :creator_id
has_many :note_versions, :foreign_key => "updater_id" has_many :note_versions, :foreign_key => "updater_id"
has_many :dmails, -> {order("dmails.id desc")}, :foreign_key => "owner_id" has_many :dmails, -> {order("dmails.id desc")}, :foreign_key => "owner_id"
has_many :saved_searches 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 :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 :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 :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 belongs_to :inviter, class_name: "User", optional: true
accepts_nested_attributes_for :dmail_filter accepts_nested_attributes_for :dmail_filter

View File

@@ -1,7 +1,7 @@
class UserFeedback < ApplicationRecord class UserFeedback < ApplicationRecord
self.table_name = "user_feedback" self.table_name = "user_feedback"
belongs_to :user belongs_to :user
belongs_to_creator belongs_to :creator, class_name: "User"
attr_accessor :disable_dmail_notification attr_accessor :disable_dmail_notification
validates_presence_of :body, :category validates_presence_of :body, :category
validates_inclusion_of :category, :in => %w(positive negative neutral) validates_inclusion_of :category, :in => %w(positive negative neutral)

View File

@@ -1,7 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory(:artist) do factory(:artist) do
creator
name { rand(1_000_000).to_s } name { rand(1_000_000).to_s }
is_active { true } is_active { true }
association :creator, factory: :user
end end
end end

View File

@@ -1,5 +1,6 @@
FactoryBot.define do FactoryBot.define do
factory(:bulk_update_request) do |f| factory(:bulk_update_request) do |f|
user
title {"xxx"} title {"xxx"}
script {"create alias aaa -> bbb"} script {"create alias aaa -> bbb"}
skip_secondary_validations {true} skip_secondary_validations {true}

View File

@@ -1,6 +1,8 @@
FactoryBot.define do FactoryBot.define do
factory(:comment) do |f| factory(:comment) do |f|
creator
post post
creator_ip_addr { FFaker::Internet.ip_v4_address }
body {FFaker::Lorem.sentences.join(" ")} body {FFaker::Lorem.sentences.join(" ")}
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,7 +1,7 @@
FactoryBot.define do FactoryBot.define do
factory(:pool) do factory(:pool) do
creator
name {"pool_" + rand(100..1000099).to_s} name {"pool_" + rand(100..1000099).to_s}
association :creator, :factory => :user
description {FFaker::Lorem.sentences.join(" ")} description {FFaker::Lorem.sentences.join(" ")}
end end
end end

View File

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

View File

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

View File

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

View File

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

View File

@@ -11,6 +11,7 @@ FactoryBot.define do
last_logged_in_at {Time.now} last_logged_in_at {Time.now}
favorite_count {0} favorite_count {0}
bit_prefs {0} bit_prefs {0}
last_forum_read_at {nil}
factory(:banned_user) do factory(:banned_user) do
transient { ban_duration {3} } transient { ban_duration {3} }

View File

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

View File

@@ -5,6 +5,7 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
setup do setup do
@user = create(:user) @user = create(:user)
@admin = create(:admin_user) @admin = create(:admin_user)
@bulk_update_request = create(:bulk_update_request, user: @user)
end end
context "#new" do context "#new" do
@@ -23,12 +24,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end end
context "#update" do 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 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"}} 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 @bulk_update_request.reload
@@ -43,12 +38,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end end
context "#index" do context "#index" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
should "render" do should "render" do
get bulk_update_requests_path get bulk_update_requests_path
assert_response :success assert_response :success
@@ -56,12 +45,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end end
context "#destroy" do context "#destroy" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for the creator" do context "for the creator" do
should "succeed" do should "succeed" do
delete_auth bulk_update_request_path(@bulk_update_request), @user delete_auth bulk_update_request_path(@bulk_update_request), @user
@@ -92,12 +75,6 @@ class BulkUpdateRequestsControllerTest < ActionDispatch::IntegrationTest
end end
context "#approve" do context "#approve" do
setup do
as_user do
@bulk_update_request = create(:bulk_update_request)
end
end
context "for a member" do context "for a member" do
should "fail" do should "fail" do
post_auth approve_bulk_update_request_path(@bulk_update_request), @user 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 context "create action" do
should "create a comment" do should "create a comment" do
assert_difference("Comment.count", 1) 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 end
comment = Comment.last comment = Comment.last
assert_redirected_to post_path(comment.post) assert_redirected_to post_path(comment.post)
@@ -196,7 +196,7 @@ class CommentsControllerTest < ActionDispatch::IntegrationTest
should "not allow commenting on nonexistent posts" do should "not allow commenting on nonexistent posts" do
assert_difference("Comment.count", 0) 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 end
assert_redirected_to comments_path assert_redirected_to comments_path
end end

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -169,7 +169,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with only deleted comments" do context "with only deleted comments" do
setup do setup do
as(@user) { create(:comment, post: @post, is_deleted: true) } as(@user) { create(:comment, creator: @user, post: @post, is_deleted: true) }
end end
should "not show deleted comments to regular members" do should "not show deleted comments to regular members" do
@@ -194,7 +194,7 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with only downvoted comments" do context "with only downvoted comments" do
should "not show thresholded 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 } get_auth post_path(@post), @user, params: { id: @post.id }
assert_response :success assert_response :success
@@ -206,9 +206,9 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
context "with a mix of comments" do context "with a mix of comments" do
should "not show deleted or thresholded 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, creator: @user, 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, creator: @user, 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: "ugly", is_deleted: true) }
get_auth post_path(@post), @user, params: { id: @post.id } 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 context "The tag aliases controller" do
setup do setup do
@user = create(:admin_user) @user = create(:admin_user)
@tag_alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb")
end end
context "edit action" do 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 should "render" do
get_auth edit_tag_alias_path(@tag_alias), @user get_auth edit_tag_alias_path(@tag_alias), @user
assert_response :success assert_response :success
@@ -20,12 +15,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end end
context "update action" do 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 context "for a pending alias" do
setup do setup do
as_admin do as_admin do
@@ -63,12 +52,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end end
context "index action" do 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 should "list all tag alias" do
get_auth tag_aliases_path, @user get_auth tag_aliases_path, @user
assert_response :success assert_response :success
@@ -81,12 +64,6 @@ class TagAliasesControllerTest < ActionDispatch::IntegrationTest
end end
context "destroy action" do context "destroy action" do
setup do
as_admin do
@tag_alias = create(:tag_alias)
end
end
should "mark the alias as deleted" do should "mark the alias as deleted" do
assert_difference("TagAlias.count", 0) do assert_difference("TagAlias.count", 0) do
delete_auth tag_alias_path(@tag_alias), @user delete_auth tag_alias_path(@tag_alias), @user

View File

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

View File

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

View File

@@ -23,14 +23,8 @@ class TagRelationshipRetirementServiceTest < ActiveSupport::TestCase
setup do setup do
subject.stubs(:is_unused?).returns(true) subject.stubs(:is_unused?).returns(true)
@user = FactoryBot.create(:user) @new_alias = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb")
as_user do @old_alias = create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd", created_at: 3.years.ago)
@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
end end
should "find old tag relationships" do should "find old tag relationships" do
@@ -50,17 +44,11 @@ class TagRelationshipRetirementServiceTest < ActiveSupport::TestCase
subject { TagRelationshipRetirementService } subject { TagRelationshipRetirementService }
setup do 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 @old_alias = create(:tag_alias, antecedent_name: "ccc", consequent_name: "ddd", created_at: 3.years.ago)
@new_alias = FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb") @old_post = create(:post, tag_string: "ddd", created_at: 3.years.ago)
@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
end end
should "return true if no recent post exists" do should "return true if no recent post exists" do

View File

@@ -8,6 +8,8 @@ class ApproverPrunerTest < ActiveSupport::TestCase
should "demote inactive approvers" do should "demote inactive approvers" do
assert_equal([@approver.id], ApproverPruner.inactive_approvers.map(&:id)) assert_equal([@approver.id], ApproverPruner.inactive_approvers.map(&:id))
assert_nothing_raised { ApproverPruner.prune! }
assert_equal(false, @approver.reload.can_approve_posts)
end end
should "not demote active approvers" do should "not demote active approvers" do

View File

@@ -30,27 +30,27 @@ class ArtistTest < ActiveSupport::TestCase
end end
should "parse inactive urls" do 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)) assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s))
refute(@artist.urls[0].is_active?) refute(@artist.urls[0].is_active?)
end end
should "not allow duplicate active+inactive urls" do 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(1, @artist.urls.count)
assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s)) assert_equal(["-http://monet.com"], @artist.urls.map(&:to_s))
refute(@artist.urls[0].is_active?) refute(@artist.urls[0].is_active?)
end end
should "allow deactivating a url" do 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") @artist.update(url_string: "-http://monet.com")
assert_equal(1, @artist.urls.count) assert_equal(1, @artist.urls.count)
refute(@artist.urls[0].is_active?) refute(@artist.urls[0].is_active?)
end end
should "allow activating a url" do 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") @artist.update(url_string: "http://monet.com")
assert_equal(1, @artist.urls.count) assert_equal(1, @artist.urls.count)
assert(@artist.urls[0].is_active?) assert(@artist.urls[0].is_active?)
@@ -69,7 +69,7 @@ class ArtistTest < ActiveSupport::TestCase
@post = FactoryBot.create(:post, :tag_string => "aaa") @post = FactoryBot.create(:post, :tag_string => "aaa")
@artist = FactoryBot.create(:artist, :name => "aaa") @artist = FactoryBot.create(:artist, :name => "aaa")
@admin = FactoryBot.create(:admin_user) @admin = FactoryBot.create(:admin_user)
CurrentUser.scoped(@admin) { @artist.ban! } @artist.ban!(banner: @admin)
@post.reload @post.reload
end end
@@ -542,7 +542,7 @@ class ArtistTest < ActiveSupport::TestCase
context "#new_with_defaults" do context "#new_with_defaults" do
should "fetch the defaults from the given source" 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" 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("niceandcool", artist.name)
assert_equal("nice_and_cool", artist.other_names_string) assert_equal("nice_and_cool", artist.other_names_string)

View File

@@ -37,7 +37,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
context "#update_notice" do context "#update_notice" do
setup do setup do
@forum_topic = FactoryBot.create(:forum_topic) @forum_topic = create(:forum_topic, creator: @admin)
end end
should "update the cache" do should "update the cache" do
@@ -70,7 +70,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
mass update aaa -> bbb mass update aaa -> bbb
' '
@bur = FactoryBot.create(:bulk_update_request, :script => @script) @bur = create(:bulk_update_request, script: @script, user: @admin)
@bur.approve!(@admin) @bur.approve!(@admin)
assert_enqueued_jobs(3) assert_enqueued_jobs(3)
@@ -111,7 +111,7 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
context "that has an invalid alias" do context "that has an invalid alias" do
setup 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") @req = FactoryBot.build(:bulk_update_request, :script => "create alias bbb -> aaa")
end end
@@ -172,8 +172,8 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
context "with an associated forum topic" do context "with an associated forum topic" do
setup do setup do
@topic = FactoryBot.create(:forum_topic, :title => "[bulk] hoge") @topic = create(:forum_topic, title: "[bulk] hoge", creator: @admin)
@post = FactoryBot.create(:forum_post, :topic_id => @topic.id) @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") @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 end

View File

@@ -72,7 +72,7 @@ class CommentTest < ActiveSupport::TestCase
dmail = Dmail.last dmail = Dmail.last
assert_equal(<<-EOS.strip_heredoc, dmail.body) 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] [quote]
Hey @#{@user2.name} check this out! Hey @#{@user2.name} check this out!
@@ -195,7 +195,7 @@ class CommentTest < ActiveSupport::TestCase
should "not allow upvotes by the creator" do should "not allow upvotes by the creator" do
user = FactoryBot.create(:user) user = FactoryBot.create(:user)
post = FactoryBot.create(:post) 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") } exception = assert_raises(ActiveRecord::RecordInvalid) { c1.vote!("up") }
assert_equal("Validation failed: You cannot upvote your own comments", exception.message) 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 class FavoriteTest < ActiveSupport::TestCase
def setup def setup
@user = create(:user) @fav_group = create(:favorite_group)
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
end end
context "searching by post id" do context "searching by post id" do

View File

@@ -53,7 +53,7 @@ class ForumPostTest < ActiveSupport::TestCase
context "outside a quote block" do context "outside a quote block" do
setup do setup do
@user2 = FactoryBot.create(:user) @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 end
should "create a dmail" do should "create a dmail" do
@@ -63,7 +63,7 @@ class ForumPostTest < ActiveSupport::TestCase
dmail = Dmail.last dmail = Dmail.last
assert_equal(<<-EOS.strip_heredoc, dmail.body) 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] [quote]
Hey @#{@user2.name} check this out! Hey @#{@user2.name} check this out!
@@ -169,7 +169,7 @@ class ForumPostTest < ActiveSupport::TestCase
end end
should "initialize its creator" do 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) assert_equal(@user.id, post.creator_id)
end end

View File

@@ -7,7 +7,7 @@ class ForumTopicTest < ActiveSupport::TestCase
@user = FactoryBot.create(:user) @user = FactoryBot.create(:user)
CurrentUser.user = @user CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
@topic = FactoryBot.create(:forum_topic, :title => "xxx") @topic = create(:forum_topic, title: "xxx", creator: @user)
end end
teardown do teardown do
@@ -113,7 +113,7 @@ class ForumTopicTest < ActiveSupport::TestCase
context "#merge" do context "#merge" do
setup 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 => @topic.id, :body => "xxx")
FactoryBot.create(:forum_post, :topic_id => @topic2.id, :body => "xxx") FactoryBot.create(:forum_post, :topic_id => @topic2.id, :body => "xxx")
end end
@@ -127,7 +127,7 @@ class ForumTopicTest < ActiveSupport::TestCase
context "constructed with nested attributes for its original post" do context "constructed with nested attributes for its original post" do
should "create a matching forum post" do should "create a matching forum post" do
assert_difference(["ForumTopic.count", "ForumPost.count"], 1) 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 end
end end

View File

@@ -1,16 +1,6 @@
require 'test_helper' require 'test_helper'
class IpBanTest < ActiveSupport::TestCase 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 should "be able to ban a user" do
ip_ban = create(:ip_ban, ip_addr: "1.2.3.4") ip_ban = create(:ip_ban, ip_addr: "1.2.3.4")

View File

@@ -8,7 +8,7 @@ module Moderator
CurrentUser.user = @user CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
Danbooru.config.stubs(:member_comment_time_threshold).returns(1.week.from_now) 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) PoolArchive.stubs(:enabled?).returns(false)
PostArchive.stubs(:enabled?).returns(false) PostArchive.stubs(:enabled?).returns(false)
@user.reload @user.reload

View File

@@ -3,14 +3,7 @@ require 'test_helper'
class PostAppealTest < ActiveSupport::TestCase class PostAppealTest < ActiveSupport::TestCase
context "In all cases" do context "In all cases" do
setup do setup do
@alice = FactoryBot.create(:user) @alice = create(:user)
CurrentUser.user = @alice
CurrentUser.ip_addr = "127.0.0.1"
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end end
context "a user" do context "a user" do
@@ -19,37 +12,27 @@ class PostAppealTest < ActiveSupport::TestCase
end end
should "not be able to appeal a post more than twice" do should "not be able to appeal a post more than twice" do
assert_difference("PostAppeal.count", 1) do @post_appeal = create(:post_appeal, post: @post, creator: @alice)
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") @post_appeal = build(:post_appeal, post: @post, creator: @alice)
end
assert_difference("PostAppeal.count", 0) do
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa")
end
assert_equal(false, @post_appeal.valid?)
assert_includes(@post_appeal.errors.full_messages, "You have already appealed this post") assert_includes(@post_appeal.errors.full_messages, "You have already appealed this post")
end end
should "not be able to appeal more than 1 post in 24 hours" do should "not be able to appeal more than 1 post in 24 hours" do
@post_appeal = PostAppeal.new(:post => @post, :reason => "aaa") @post_appeal = create(:post_appeal, post: @post, creator: @alice)
@post_appeal.expects(:appeal_count_for_creator).returns(1) @post_appeal = build(:post_appeal, post: create(:post, is_deleted: true), creator: @alice)
assert_difference("PostAppeal.count", 0) do
@post_appeal.save assert_equal(false, @post_appeal.valid?)
end
assert_equal(["You can appeal at most 1 post a day"], @post_appeal.errors.full_messages) assert_equal(["You can appeal at most 1 post a day"], @post_appeal.errors.full_messages)
end end
should "not be able to appeal an active post" do should "not be able to appeal an active post" do
@post.update_attribute(:is_deleted, false) @post.update_attribute(:is_deleted, false)
assert_difference("PostAppeal.count", 0) do @post_appeal = build(:post_appeal, post: @post, creator: @alice)
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa")
end
assert_equal(["Post is active"], @post_appeal.errors.full_messages)
end
should "initialize its creator" do assert_equal(false, @post_appeal.valid?)
@post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") assert_equal(["Post is active"], @post_appeal.errors.full_messages)
assert_equal(@alice.id, @post_appeal.creator_id)
end end
end end
end end

View File

@@ -35,8 +35,8 @@ class PostApprovalTest < ActiveSupport::TestCase
context "that is then flagged" do context "that is then flagged" do
setup do setup do
@user2 = FactoryBot.create(:user) @user2 = create(:user, created_at: 2.weeks.ago)
@user3 = FactoryBot.create(:user) @user3 = create(:user, created_at: 2.weeks.ago)
@approver2 = FactoryBot.create(:user) @approver2 = FactoryBot.create(:user)
@approver2.can_approve_posts = true @approver2.can_approve_posts = true
@approver2.save @approver2.save

View File

@@ -2,22 +2,10 @@ require 'test_helper'
class PostEventTest < ActiveSupport::TestCase class PostEventTest < ActiveSupport::TestCase
def setup def setup
super @user = create(:user, created_at: 2.weeks.ago)
@post = create(:post)
travel_to(2.weeks.ago) do @post_flag = create(:post_flag, creator: @user, post: @post)
CurrentUser.user = FactoryBot.create(:user) @post_appeal = create(:post_appeal, creator: @user, post: @post)
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
end end
context "PostEvent.find_for_post" do context "PostEvent.find_for_post" do

View File

@@ -12,54 +12,37 @@ class PostFlagTest < ActiveSupport::TestCase
end end
context "a basic user" do 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 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) @post_flag.expects(:flag_count_for_creator).returns(1)
assert_difference("PostFlag.count", 0) do
as(@bob) { @post_flag.save } assert_equal(false, @post_flag.valid?)
end
assert_equal(["You can flag 1 post a day"], @post_flag.errors.full_messages) assert_equal(["You can flag 1 post a day"], @post_flag.errors.full_messages)
end end
end end
context "a gold user" do context "a gold user" do
setup do setup do
travel_to(2.weeks.ago) do @bob = create(:gold_user, created_at: 1.month.ago)
@bob = create(:gold_user)
end
end end
should "not be able to flag a post more than twice" do should "not be able to flag a post more than twice" do
assert_difference(-> { PostFlag.count }, 1) do @post_flag = create(:post_flag, post: @post, creator: @bob)
as(@bob) do @post_flag = build(:post_flag, post: @post, creator: @bob)
@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
assert_equal(false, @post_flag.valid?)
assert_equal(["have already flagged this post"], @post_flag.errors[:creator_id]) assert_equal(["have already flagged this post"], @post_flag.errors[:creator_id])
end end
should "not be able to flag more than 10 posts in 24 hours" do should "not be able to flag more than 10 posts in 24 hours" do
as(@bob) do @post_flag = build(:post_flag, post: @post, creator: @bob)
@post_flag = PostFlag.new(post: @post, reason: "aaa", is_resolved: false) @post_flag.expects(:flag_count_for_creator).returns(10)
@post_flag.expects(:flag_count_for_creator).returns(10)
assert_difference(-> { PostFlag.count }, 0) do assert_difference(-> { PostFlag.count }, 0) do
@post_flag.save @post_flag.save
end
end end
assert_equal(["You can flag 10 posts a day"], @post_flag.errors.full_messages) assert_equal(["You can flag 10 posts a day"], @post_flag.errors.full_messages)
end end
@@ -68,11 +51,8 @@ class PostFlagTest < ActiveSupport::TestCase
@post.update(is_deleted: true) @post.update(is_deleted: true)
end end
assert_difference(-> { PostFlag.count }, 0) do @post_flag = build(:post_flag, post: @post, creator: @bob)
as(@bob) do @post_flag.save
@post_flag = PostFlag.create(post: @post, reason: "aaa", is_resolved: false)
end
end
assert_equal(["Post is deleted"], @post_flag.errors.full_messages) assert_equal(["Post is deleted"], @post_flag.errors.full_messages)
end end
@@ -80,9 +60,7 @@ class PostFlagTest < ActiveSupport::TestCase
as(@alice) do as(@alice) do
@post.update(is_pending: true) @post.update(is_pending: true)
end end
as(@bob) do @flag = @post.flags.create(reason: "test", creator: @bob)
@flag = @post.flags.create(reason: "test")
end
assert_equal(["Post is pending and cannot be flagged"], @flag.errors.full_messages) assert_equal(["Post is pending and cannot be flagged"], @flag.errors.full_messages)
end end
@@ -94,49 +72,35 @@ class PostFlagTest < ActiveSupport::TestCase
@users = FactoryBot.create_list(:user, 2) @users = FactoryBot.create_list(:user, 2)
end end
as(@users.first) do @flag1 = create(:post_flag, post: @post, reason: "something", creator: @users.first)
@flag1 = PostFlag.create(post: @post, reason: "something")
end
as(@mod) do as(@mod) do
@post.approve! @post.approve!
end end
travel_to(PostFlag::COOLDOWN_PERIOD.from_now - 1.minute) do travel_to(PostFlag::COOLDOWN_PERIOD.from_now - 1.minute) do
as(@users.second) do @flag2 = build(:post_flag, post: @post, reason: "something", creator: @users.second)
@flag2 = PostFlag.create(post: @post, reason: "something") assert_equal(false, @flag2.valid?)
end
assert_match(/cannot be flagged more than once/, @flag2.errors[:post].join) assert_match(/cannot be flagged more than once/, @flag2.errors[:post].join)
end end
travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do travel_to(PostFlag::COOLDOWN_PERIOD.from_now + 1.minute) do
as(@users.second) do @flag3 = create(:post_flag, post: @post, reason: "something", creator: @users.second)
@flag3 = PostFlag.create(post: @post, reason: "something")
end
assert(@flag3.errors.empty?) assert(@flag3.errors.empty?)
end end
end end
should "initialize its creator" do should "initialize its creator" do
@post_flag = as(@alice) do @post_flag = create(:post_flag, creator: @alice)
PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false)
end
assert_equal(@alice.id, @post_flag.creator_id) assert_equal(@alice.id, @post_flag.creator_id)
end end
end end
context "a moderator user" do 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 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) @modpost = create(:post, :tag_string => "mmm", :uploader => @dave)
as(@alice) do @flag1 = create(:post_flag, post: @modpost, creator: @alice)
@flag1 = PostFlag.create(:post => @modpost, :reason => "aaa", :is_resolved => false)
end
assert_equal(false, @dave.can_view_flagger_on_post?(@flag1)) assert_equal(false, @dave.can_view_flagger_on_post?(@flag1))

View File

@@ -2,34 +2,17 @@ require 'test_helper'
class PostPrunerTest < ActiveSupport::TestCase class PostPrunerTest < ActiveSupport::TestCase
def setup 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) @old_post = FactoryBot.create(:post, :created_at => 5.days.ago, :is_pending => true)
@unresolved_flagged_post = FactoryBot.create(:post, :is_flagged => true) @unresolved_flagged_post = FactoryBot.create(:post, :is_flagged => true)
@resolved_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 @flagger = create(:gold_user, created_at: 2.weeks.ago)
@unresolved_post_flag = FactoryBot.create(:post_flag, :created_at => 5.days.ago, :is_resolved => false, :post_id => @unresolved_flagged_post.id) @unresolved_post_flag = create(:post_flag, creator: @flagger, created_at: 5.days.ago, is_resolved: false, post: @unresolved_flagged_post)
@resolved_post_flag = FactoryBot.create(:post_flag, :created_at => 5.days.ago, :is_resolved => true, :post_id => @resolved_flagged_post.id) @resolved_post_flag = create(:post_flag, creator: @flagger, created_at: 5.days.ago, is_resolved: true, post: @resolved_flagged_post)
end
PostPruner.new.prune! PostPruner.new.prune!
end end
def teardown
super
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
should "prune old pending posts" do should "prune old pending posts" do
@old_post.reload @old_post.reload
assert(@old_post.is_deleted?) assert(@old_post.is_deleted?)

View File

@@ -2030,7 +2030,7 @@ class PostTest < ActiveSupport::TestCase
should "return posts for the commenter:<name> metatag" do should "return posts for the commenter:<name> metatag" do
users = FactoryBot.create_list(:user, 2, created_at: 2.weeks.ago) users = FactoryBot.create_list(:user, 2, created_at: 2.weeks.ago)
posts = FactoryBot.create_list(:post, 2) 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[0]], "commenter:#{users[0].name}")
assert_tag_match([posts[1]], "commenter:#{users[1].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 should "return posts for the commenter:<any|none> metatag" do
posts = FactoryBot.create_list(:post, 2) posts = FactoryBot.create_list(:post, 2)
FactoryBot.create(:comment, post: posts[0], is_deleted: false) create(:comment, creator: create(:user, created_at: 2.weeks.ago), 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[1], is_deleted: true)
assert_tag_match([posts[0]], "commenter:any") assert_tag_match([posts[0]], "commenter:any")
assert_tag_match([posts[1]], "commenter:none") assert_tag_match([posts[1]], "commenter:none")
@@ -2143,7 +2143,7 @@ class PostTest < ActiveSupport::TestCase
pending = FactoryBot.create(:post, is_pending: true) pending = FactoryBot.create(:post, is_pending: true)
disapproved = 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") FactoryBot.create(:post_disapproval, post: disapproved, reason: "disinterest")
assert_tag_match([pending, flagged], "status:unmoderated") assert_tag_match([pending, flagged], "status:unmoderated")
@@ -2352,9 +2352,10 @@ class PostTest < ActiveSupport::TestCase
tag_string: tags[n - 1] tag_string: tags[n - 1]
) )
FactoryBot.create(:artist_commentary, post: p) u = create(:user, created_at: 2.weeks.ago)
FactoryBot.create(:comment, post: p, do_not_bump_post: false) create(:artist_commentary, post: p)
FactoryBot.create(:note, post: p) create(:comment, post: p, creator: u, do_not_bump_post: false)
create(:note, post: p, creator: u)
p p
end end
@@ -2406,11 +2407,12 @@ class PostTest < ActiveSupport::TestCase
post1 = FactoryBot.create(:post) post1 = FactoryBot.create(:post)
post2 = FactoryBot.create(:post) post2 = FactoryBot.create(:post)
post3 = FactoryBot.create(:post) post3 = FactoryBot.create(:post)
user = create(:gold_user)
CurrentUser.scoped(FactoryBot.create(:gold_user), "127.0.0.1") do as(user) do
comment1 = FactoryBot.create(:comment, :post => post1) comment1 = create(:comment, creator: user, post: post1)
comment2 = FactoryBot.create(:comment, :post => post2, :do_not_bump_post => true) comment2 = create(:comment, creator: user, post: post2, do_not_bump_post: true)
comment3 = FactoryBot.create(:comment, :post => post3) comment3 = create(:comment, creator: user, post: post3)
end end
assert_tag_match([post3, post1, post2], "order:comment_bumped") 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) @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") @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") create(:note, post: @src, 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) create(:note, post: @src, x: 10, y: 10, width: 10, height: 10, body: "deleted", is_active: false)
@src.reload
@src.copy_notes_to(@dst) @src.reload.copy_notes_to(@dst)
end end
should "copy notes and tags" do should "copy notes and tags" do

View File

@@ -108,7 +108,7 @@ class TagAliasTest < ActiveSupport::TestCase
end end
should "populate the creator information" do 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) assert_equal(CurrentUser.user.id, ta.creator_id)
end end

View File

@@ -6,7 +6,6 @@ class TagImplicationTest < ActiveSupport::TestCase
user = FactoryBot.create(:admin_user) user = FactoryBot.create(:admin_user)
CurrentUser.user = user CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
@user = FactoryBot.create(:user)
end end
teardown do teardown do
@@ -104,7 +103,7 @@ class TagImplicationTest < ActiveSupport::TestCase
end end
should "populate the creator information" do 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) assert_equal(CurrentUser.user.id, ti.creator_id)
end end
@@ -263,7 +262,6 @@ class TagImplicationTest < ActiveSupport::TestCase
context "with an associated forum topic" do context "with an associated forum topic" do
setup do setup do
@admin = FactoryBot.create(:admin_user)
@topic = FactoryBot.create(:forum_topic, :title => "Tag implication: aaa -> bbb") @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")) @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") @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 class UserFeedbackTest < ActiveSupport::TestCase
context "A user's feedback" do 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 should "create a dmail" do
user = FactoryBot.create(:user) user = FactoryBot.create(:user)
gold = FactoryBot.create(:gold_user) gold = FactoryBot.create(:gold_user)
member = FactoryBot.create(:user) member = FactoryBot.create(:user)
dmail = <<~EOS.chomp dmail = <<~EOS.chomp
@#{gold.name} created a "positive record":/user_feedbacks?search[user_id]=#{user.id} for your account: @#{gold.name} created a "positive record":/user_feedbacks?search[user_id]=#{user.id} for your account:
good job! good job!
EOS EOS
CurrentUser.user = gold
assert_difference("Dmail.count", 1) do 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) assert_equal(dmail, user.dmails.last.body)
end end
end end
should "not validate if the creator is the user" do should "not validate if the creator is the user" do
gold_user = FactoryBot.create(:gold_user) user = FactoryBot.create(:gold_user)
CurrentUser.user = gold_user feedback = build(:user_feedback, creator: user, user: user)
feedback = FactoryBot.build(:user_feedback, :user => gold_user)
feedback.save feedback.save
assert_equal(["You cannot submit feedback for yourself"], feedback.errors.full_messages) assert_equal(["You cannot submit feedback for yourself"], feedback.errors.full_messages)
end end
@@ -42,12 +30,10 @@ class UserFeedbackTest < ActiveSupport::TestCase
gold = FactoryBot.create(:gold_user) gold = FactoryBot.create(:gold_user)
member = FactoryBot.create(:user) member = FactoryBot.create(:user)
CurrentUser.user = gold feedback = FactoryBot.create(:user_feedback, creator: gold, user: user)
feedback = FactoryBot.create(:user_feedback, :user => user)
assert(feedback.errors.empty?) assert(feedback.errors.empty?)
CurrentUser.user = member feedback = build(:user_feedback, creator: member, user: user)
feedback = FactoryBot.build(:user_feedback, :user => user)
feedback.save feedback.save
assert_equal(["You must be gold"], feedback.errors.full_messages) assert_equal(["You must be gold"], feedback.errors.full_messages)
end end

View File

@@ -88,9 +88,7 @@ class UserTest < ActiveSupport::TestCase
@user.update_column(:created_at, 1.year.ago) @user.update_column(:created_at, 1.year.ago)
assert(@user.can_comment?) assert(@user.can_comment?)
assert(!@user.is_comment_limited?) assert(!@user.is_comment_limited?)
Danbooru.config.member_comment_limit.times do create_list(:comment, Danbooru.config.member_comment_limit, creator: @user)
FactoryBot.create(:comment)
end
assert(@user.is_comment_limited?) assert(@user.is_comment_limited?)
end end