mirror of
https://github.com/discourse/discourse.git
synced 2025-04-17 10:00:15 +08:00
FIX: Don't log a claimed topic database error during tests
We now test the uniqueness validation, but also rescue a DB exception in case the controller fails this check.
This commit is contained in:
parent
d043a4c6fe
commit
dcbe527a82
@ -6,7 +6,10 @@ class ReviewableClaimedTopicsController < ApplicationController
|
||||
def create
|
||||
topic = Topic.find_by(id: params[:reviewable_claimed_topic][:topic_id])
|
||||
guardian.ensure_can_claim_reviewable_topic!(topic)
|
||||
ReviewableClaimedTopic.create!(user_id: current_user.id, topic_id: topic.id)
|
||||
ReviewableClaimedTopic.create(user_id: current_user.id, topic_id: topic.id)
|
||||
render json: success_json
|
||||
rescue ActiveRecord::RecordNotUnique
|
||||
# This is just in case the validation fails under concurrency
|
||||
render json: success_json
|
||||
end
|
||||
|
||||
|
@ -3,6 +3,7 @@
|
||||
class ReviewableClaimedTopic < ActiveRecord::Base
|
||||
belongs_to :topic
|
||||
belongs_to :user
|
||||
validates_uniqueness_of :topic
|
||||
|
||||
def self.claimed_hash(topic_ids)
|
||||
result = {}
|
||||
|
@ -4,11 +4,14 @@ require 'rails_helper'
|
||||
|
||||
RSpec.describe ReviewableClaimedTopic, type: :model do
|
||||
|
||||
it "ensures uniqueness" do
|
||||
claimed = Fabricate(:reviewable_claimed_topic)
|
||||
expect(-> {
|
||||
ReviewableClaimedTopic.create!(topic_id: claimed.topic_id, user_id: Fabricate(:user).id)
|
||||
}).to raise_error(ActiveRecord::RecordNotUnique)
|
||||
it "respects the uniqueness constraint" do
|
||||
topic = Fabricate(:topic)
|
||||
|
||||
ct = ReviewableClaimedTopic.new(topic_id: topic.id, user_id: Fabricate(:user).id)
|
||||
expect(ct.save).to eq(true)
|
||||
|
||||
ct = ReviewableClaimedTopic.new(topic_id: topic.id, user_id: Fabricate(:user).id)
|
||||
expect(ct.save).to eq(false)
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -33,6 +33,14 @@ describe ReviewableClaimedTopicsController do
|
||||
expect(response.code).to eq("200")
|
||||
expect(ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?).to eq(true)
|
||||
end
|
||||
|
||||
it "won't an error if you claim twice" do
|
||||
SiteSetting.reviewable_claiming = 'optional'
|
||||
post "/reviewable_claimed_topics.json", params: params
|
||||
expect(ReviewableClaimedTopic.where(user_id: moderator.id, topic_id: topic.id).exists?).to eq(true)
|
||||
post "/reviewable_claimed_topics.json", params: params
|
||||
expect(response.code).to eq("200")
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user