Fix tags request specs

This commit is contained in:
Eugene Burmakin 2025-11-19 20:35:43 +01:00
parent e02b397b87
commit 01df22d080
3 changed files with 31 additions and 9 deletions

View file

@ -6,6 +6,8 @@ class Tag < ApplicationRecord
has_many :places, through: :taggings, source: :taggable, source_type: 'Place'
validates :name, presence: true, uniqueness: { scope: :user_id }
validates :icon, length: { maximum: 10, allow_blank: true }
validate :icon_is_not_ascii_letter
validates :color, format: { with: /\A#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\z/, allow_blank: true }
validates :privacy_radius_meters, numericality: {
greater_than: 0,
@ -20,4 +22,13 @@ class Tag < ApplicationRecord
def privacy_zone?
privacy_radius_meters.present?
end
private
def icon_is_not_ascii_letter
return if icon.blank?
return unless icon.match?(/\A[a-zA-Z]+\z/)
errors.add(:icon, 'must be an emoji or symbol, not a letter')
end
end

View file

@ -50,9 +50,8 @@ RSpec.describe "Tags", type: :request do
it "prevents editing other users' tags" do
other_tag = create(:tag, user: create(:user))
expect {
get edit_tag_path(other_tag)
}.to raise_error(ActiveRecord::RecordNotFound)
get edit_tag_path(other_tag)
expect(response).to have_http_status(:not_found)
end
end
@ -116,9 +115,8 @@ RSpec.describe "Tags", type: :request do
it "prevents updating other users' tags" do
other_tag = create(:tag, user: create(:user))
expect {
patch tag_path(other_tag), params: { tag: { name: 'Hacked' } }
}.to raise_error(ActiveRecord::RecordNotFound)
patch tag_path(other_tag), params: { tag: { name: 'Hacked' } }
expect(response).to have_http_status(:not_found)
end
end
@ -139,9 +137,8 @@ RSpec.describe "Tags", type: :request do
it "prevents deleting other users' tags" do
other_tag = create(:tag, user: create(:user))
expect {
delete tag_path(other_tag)
}.to raise_error(ActiveRecord::RecordNotFound)
delete tag_path(other_tag)
expect(response).to have_http_status(:not_found)
end
end

View file

@ -0,0 +1,14 @@
# frozen_string_literal: true
# Stub GitHub API requests in tests
RSpec.configure do |config|
config.before(:each) do
# Stub GitHub API version checking
stub_request(:get, "https://api.github.com/repos/Freika/dawarich/tags")
.to_return(
status: 200,
body: [{ name: "v0.1.0" }].to_json,
headers: { 'Content-Type' => 'application/json' }
)
end
end