Skip to content

Commit

Permalink
Fix QRCodesController spec and add spec for UUID validation
Browse files Browse the repository at this point in the history
  • Loading branch information
andresag4 committed Jul 22, 2024
1 parent 725b405 commit 0ff00fa
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
4 changes: 3 additions & 1 deletion app/models/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@
# index_profiles_on_uuid (uuid) UNIQUE
#
class Profile < ApplicationRecord
UUID_REGEX = /[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}/

belongs_to :profileable, polymorphic: true

has_one :self_ref, class_name: "Profile", foreign_key: :id, inverse_of: :self_ref, dependent: :destroy
has_one :user, through: :self_ref, source: :profileable, source_type: "User"
has_one :speaker, through: :self_ref, source: :profileable, source_type: "Speaker"

validates :uuid, uniqueness: true, presence: true
validates :uuid, uniqueness: true, presence: true, format: {with: UUID_REGEX}

before_validation :set_uuid

Expand Down
8 changes: 0 additions & 8 deletions spec/controllers/profiles_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,4 @@
end
end
end

describe "GET #qr_code" do
it "returns a PNG" do
get :qr_code, params: {uuid: profile.uuid}
expect(response.headers["Content-Type"]).to eq("image/png")
expect(response.headers["Content-Disposition"]).to include("#{profile.name}.png")
end
end
end
19 changes: 19 additions & 0 deletions spec/controllers/qr_codes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe QRCodesController, type: :controller do
let(:profile) { create(:profile, :with_user, :public) }

before do
sign_in(profile.user)
end

describe "GET #show" do
it "returns a PNG" do
get :show, params: {profile_uuid: profile.uuid}
expect(response.headers["Content-Type"]).to eq("image/png")
expect(response.headers["Content-Disposition"]).to include("#{profile.name}.png")
end
end
end
19 changes: 0 additions & 19 deletions spec/lib/qr_code_generator_spec.rb

This file was deleted.

16 changes: 16 additions & 0 deletions spec/models/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@
expect(profile.uuid).to be_present
end
end

describe "validations" do
context "with a valid UUID" do
it "is valid" do
profile.uuid = SecureRandom.uuid
expect(profile).to be_valid
end
end

context "with an invalid UUID" do
it "is invalid" do
profile.uuid = "invalid"
expect(profile).not_to be_valid
end
end
end
end

0 comments on commit 0ff00fa

Please sign in to comment.