From d4f1a5dbe8ba48e7bdcb973b1204c34053bc0484 Mon Sep 17 00:00:00 2001 From: Martha Date: Fri, 13 Mar 2015 10:56:32 -0700 Subject: [PATCH] added specs and validations for Role model - added shoulda-matchers gem for one-line specs - added FactoryGirl syntax methods --- Gemfile | 1 + Gemfile.lock | 3 +++ app/models/role.rb | 13 +++++++--- spec/models/role_spec.rb | 55 +++++++++++++++++++++++++++++++++++++--- spec/spec_helper.rb | 37 ++++++++++++++------------- 5 files changed, 85 insertions(+), 24 deletions(-) diff --git a/Gemfile b/Gemfile index 44bb174cc..2d984d526 100644 --- a/Gemfile +++ b/Gemfile @@ -254,6 +254,7 @@ group :test do gem "factory_girl_rails", "4.5.0" gem "timecop", "0.7.3" gem "webmock", "1.20.4", require: false + gem "shoulda-matchers", "2.8.0", require: false end group :development, :test do diff --git a/Gemfile.lock b/Gemfile.lock index 209f38680..321b97066 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -619,6 +619,8 @@ GEM rubyzip (~> 1.0) websocket (~> 1.0) shellany (0.0.1) + shoulda-matchers (2.8.0) + activesupport (>= 3.0.0) sidekiq (3.3.2) celluloid (>= 0.16.0) connection_pool (>= 2.1.1) @@ -803,6 +805,7 @@ DEPENDENCIES ruby-oembed (= 0.8.12) sass-rails (= 5.0.1) selenium-webdriver (= 2.45.0) + shoulda-matchers (= 2.8.0) sidekiq (= 3.3.2) sidetiq (= 0.6.3) simple_captcha2 (= 0.3.4) diff --git a/app/models/role.rb b/app/models/role.rb index 928035223..dafc33f37 100644 --- a/app/models/role.rb +++ b/app/models/role.rb @@ -1,18 +1,23 @@ #NOTE add the person object you want to attach role to... + class Role < ActiveRecord::Base belongs_to :person - scope :admins, -> { where(name: 'admin') } + validates :person, presence: true + validates :name, uniqueness: {scope: :person_id} + validates :name, inclusion: {in: %w(admin spotlight)} + + scope :admins, -> { where(name: "admin") } def self.is_admin?(person) - find_by_person_id_and_name(person.id, 'admin') + exists?(person_id: person.id, name: "admin") end def self.add_admin(person) - find_or_create_by(person_id: person.id, name: 'admin') + find_or_create_by(person_id: person.id, name: "admin") end def self.add_spotlight(person) - find_or_create_by(person_id: person.id, name: 'spotlight') + find_or_create_by(person_id: person.id, name: "spotlight") end end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb index 59ddaeb60..82a95a8ff 100644 --- a/spec/models/role_spec.rb +++ b/spec/models/role_spec.rb @@ -1,5 +1,54 @@ -require 'spec_helper' +require "spec_helper" -describe Role, :type => :model do - skip "add some examples to (or delete) #{__FILE__}" +describe Role do + let(:person) { create(:person) } + + describe "validations" do + it { should validate_presence_of(:person) } + it { should validate_uniqueness_of(:name).scoped_to(:person_id) } + it { should validate_inclusion_of(:name).in_array(%w(admin spotlight)) } + end + + describe "associations" do + it { should belong_to(:person) } + end + + describe "scopes" do + let!(:admin_role) { person.roles.create(name: "admin") } + let!(:spotlight_role) { person.roles.create(name: "spotlight") } + + describe ".admins" do + it "includes admin roles" do + expect(Role.admins).to match_array([admin_role]) + end + end + end + + describe ".is_admin?" do + it "defaults to false" do + expect(Role.is_admin?(person)).to be false + end + + context "when the person is an admin" do + before { person.roles.create(name: "admin") } + + it "is true" do + expect(Role.is_admin?(person)).to be true + end + end + end + + describe ".add_admin" do + it "creates the admin role" do + Role.add_admin(person) + expect(person.roles.where(name: "admin")).to exist + end + end + + describe ".add_spotlight" do + it "creates the spotlight role" do + Role.add_spotlight(person) + expect(person.roles.where(name: "spotlight")).to exist + end + end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 18a214680..afda99679 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,14 +2,15 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -ENV["RAILS_ENV"] ||= 'test' -require File.join(File.dirname(__FILE__), '..', 'config', 'environment') -require Rails.root.join('spec', 'helper_methods') -require Rails.root.join('spec', 'spec-doc') -require 'rspec/rails' -require 'webmock/rspec' -require 'factory_girl' -require 'sidekiq/testing' +ENV["RAILS_ENV"] ||= "test" +require File.join(File.dirname(__FILE__), "..", "config", "environment") +require Rails.root.join("spec", "helper_methods") +require Rails.root.join("spec", "spec-doc") +require "rspec/rails" +require "webmock/rspec" +require "factory_girl" +require "sidekiq/testing" +require "shoulda/matchers" include HelperMethods @@ -25,39 +26,39 @@ def set_up_friends end def alice - @alice ||= User.where(:username => 'alice').first + @alice ||= User.find_by(username: "alice") end def bob - @bob ||= User.where(:username => 'bob').first + @bob ||= User.find_by(username: "bob") end def eve - @eve ||= User.where(:username => 'eve').first + @eve ||= User.find_by(username: "eve") end def local_luke - @local_luke ||= User.where(:username => 'luke').first + @local_luke ||= User.find_by(username: "luke") end def local_leia - @local_leia ||= User.where(:username => 'leia').first + @local_leia ||= User.find_by(username: "leia") end def remote_raphael - @remote_raphael ||= Person.where(:diaspora_handle => 'raphael@remote.net').first + @remote_raphael ||= Person.find_by(diaspora_handle: "raphael@remote.net") end def peter - @peter ||= User.where(:username => 'peter').first + @peter ||= User.find_by(username: "peter") end def photo_fixture_name - @photo_fixture_name = File.join(File.dirname(__FILE__), 'fixtures', 'button.png') + @photo_fixture_name = File.join(File.dirname(__FILE__), "fixtures", "button.png") end # Force fixture rebuild -FileUtils.rm_f(Rails.root.join('tmp', 'fixture_builder.yml')) +FileUtils.rm_f(Rails.root.join("tmp", "fixture_builder.yml")) # Requires supporting files with custom matchers and macros, etc, # in ./support/ and its subdirectories. @@ -100,4 +101,6 @@ RSpec.configure do |config| config.after(:each) do ActionMailer::Base.deliveries.clear end + + config.include FactoryGirl::Syntax::Methods end