docbrown/spec/services/users/username_generator_spec.rb
Joshua Wehner a20cf3e02b
Unique cross-model-slug check is likely misconfigured (#19263)
* Page unique_cross_model_slug seems misconfigured

* Update validator test

* More verbose invalid attribute message

* Try dynamic model/attribute registration

* Rubocop

* Use the cross-model-slug existence check

* Register with the dynamic cross-model-slug checker

* Cleanup

* Add missing keys to fr

* Update format regex

* Adjust validation: always presence, unique if changed

* Tests can create reserved-word pages when they need to

* Slugs can be mixed case?

* Case-sensitive is better, actually

* Refactor to use CrossModel check

* Refactor, rename for clarity

* Refactor, avoid mocking oneself

* Refactor, injectable everything

* Add reservedword check to extracted exists? checker

* Move to concerns

* Without dynamic registration

* extend when needed

* Cleanup comments, remove registration references

---------

Co-authored-by: Goran <gorang.pub@gmail.com>
2023-04-07 17:04:45 +02:00

64 lines
1.6 KiB
Ruby

require "rails_helper"
RSpec.describe Users::UsernameGenerator, type: :service do
let(:does_not_exist) do
detector = Object.new
def detector.exists?(_username)
false
end
detector
end
def result_from(usernames)
described_class.call(usernames, detector: does_not_exist)
end
it "returns randomly generated username if empty list is passed" do
expect(result_from([""])).to match(%([a-z]+{12}))
expect(result_from([])).to match(%([a-z]+{12}))
end
it "returns randomly generated username if bad list is passed" do
expect(result_from([nil, nil, 123, User])).to match(%([a-z]+{12}))
end
it "returns supplied username if does not exist" do
expect(result_from(["username"])).to eq("username")
end
it "returns normalized username" do
expect(result_from(["user.name"])).to eq("username")
end
context "when username already exists" do
subject(:result) { described_class.call ["username"], detector: username_exists }
let(:username_exists) do
detector = Object.new
def detector.exists?(username)
username == "username"
end
detector
end
it "returns supplied username with suffix" do
expect(result).to match(/username_\d+/)
end
context "when all generation methods are exhausted" do
subject(:result) do
described_class.call [],
detector: username_exists,
generator: pseudo_random
end
let(:pseudo_random) do
-> { "username" }
end
it "returns nil" do
expect(result).to be_nil
end
end
end
end