diff --git a/Gemfile b/Gemfile index e20ff191..f81ee66b 100644 --- a/Gemfile +++ b/Gemfile @@ -92,6 +92,7 @@ group :development, :test do gem 'rake' gem 'puma' gem 'rspec-rails', '~> 3.9' + gem 'rspec-its', '~> 1.3' gem 'factory_bot_rails', require: false gem 'faker' gem 'capybara' diff --git a/Gemfile.lock b/Gemfile.lock index c70c9ee6..bc9c4bd1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -388,6 +388,9 @@ GEM rspec-expectations (3.9.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) + rspec-its (1.3.0) + rspec-core (>= 3.0.0) + rspec-expectations (>= 3.0.0) rspec-mocks (3.9.1) diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) @@ -556,6 +559,7 @@ DEPENDENCIES redcarpet redis rolify (~> 5.2) + rspec-its (~> 1.3) rspec-rails (~> 3.9) ruby-progressbar sanitize diff --git a/app/models/group.rb b/app/models/group.rb index c7fbb9ad..0f04893f 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,7 +1,10 @@ +# frozen_string_literal: true + class Group < ApplicationRecord belongs_to :user has_many :group_members, dependent: :destroy + validates :name, length: { minimum: 1 } validates :display_name, length: { maximum: 30 } before_validation do diff --git a/spec/models/group_spec.rb b/spec/models/group_spec.rb new file mode 100644 index 00000000..bec7560c --- /dev/null +++ b/spec/models/group_spec.rb @@ -0,0 +1,66 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe(Group, type: :model) do + let(:user) { FactoryBot.build(:user) } + + describe 'name mangling' do + subject do + Group.new(user: user, display_name: display_name).tap(&:validate) + end + + { + 'great group' => 'great-group', + 'followers' => '-followers-', + ' followers ' => '-followers-', + " the game \t\nyes" => 'the-game-yes', + + # not nice, but this is just the way it is: + "\u{1f98a} :3" => '3', + "\u{1f98a}" => '' + }.each do |display_name, expected_name| + context "when display name is #{display_name.inspect}" do + let(:display_name) { display_name } + + its(:name) { should eq expected_name } + end + end + end + + describe 'validations' do + subject do + Group.new(user: user, display_name: display_name).validate + end + + context "when display name is 'great group' (valid)" do + let(:display_name) { 'great group' } + + it { is_expected.to be true } + end + + context "when display name is '1' (valid)" do + let(:display_name) { '1' } + + it { is_expected.to be true } + end + + context 'when display name is the letter E 621 times (invalid, too long)' do + let(:display_name) { 'E' * 621 } + + it { is_expected.to be false } + end + + context 'when display name is an empty string (invalid, as `name` would be empty)' do + let(:display_name) { '' } + + it { is_expected.to be false } + end + + context "when display name is \u{1f98a} (invalid, as `name` would be empty)" do + let(:display_name) { "\u{1f98a}" } + + it { is_expected.to be false } + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 7517cd89..5fc46546 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -8,6 +8,7 @@ abort("The Rails environment is running in production mode!") if Rails.env.produ require "spec_helper" require "rspec/rails" # Add additional requires below this line. Rails is not loaded until this point! +require "rspec/its" require "devise" require "capybara/rails" require "capybara/rspec"