added specs and validations for Role model
- added shoulda-matchers gem for one-line specs - added FactoryGirl syntax methods
This commit is contained in:
parent
0856eb8f86
commit
d4f1a5dbe8
5 changed files with 85 additions and 24 deletions
1
Gemfile
1
Gemfile
|
|
@ -254,6 +254,7 @@ group :test do
|
||||||
gem "factory_girl_rails", "4.5.0"
|
gem "factory_girl_rails", "4.5.0"
|
||||||
gem "timecop", "0.7.3"
|
gem "timecop", "0.7.3"
|
||||||
gem "webmock", "1.20.4", require: false
|
gem "webmock", "1.20.4", require: false
|
||||||
|
gem "shoulda-matchers", "2.8.0", require: false
|
||||||
end
|
end
|
||||||
|
|
||||||
group :development, :test do
|
group :development, :test do
|
||||||
|
|
|
||||||
|
|
@ -619,6 +619,8 @@ GEM
|
||||||
rubyzip (~> 1.0)
|
rubyzip (~> 1.0)
|
||||||
websocket (~> 1.0)
|
websocket (~> 1.0)
|
||||||
shellany (0.0.1)
|
shellany (0.0.1)
|
||||||
|
shoulda-matchers (2.8.0)
|
||||||
|
activesupport (>= 3.0.0)
|
||||||
sidekiq (3.3.2)
|
sidekiq (3.3.2)
|
||||||
celluloid (>= 0.16.0)
|
celluloid (>= 0.16.0)
|
||||||
connection_pool (>= 2.1.1)
|
connection_pool (>= 2.1.1)
|
||||||
|
|
@ -803,6 +805,7 @@ DEPENDENCIES
|
||||||
ruby-oembed (= 0.8.12)
|
ruby-oembed (= 0.8.12)
|
||||||
sass-rails (= 5.0.1)
|
sass-rails (= 5.0.1)
|
||||||
selenium-webdriver (= 2.45.0)
|
selenium-webdriver (= 2.45.0)
|
||||||
|
shoulda-matchers (= 2.8.0)
|
||||||
sidekiq (= 3.3.2)
|
sidekiq (= 3.3.2)
|
||||||
sidetiq (= 0.6.3)
|
sidetiq (= 0.6.3)
|
||||||
simple_captcha2 (= 0.3.4)
|
simple_captcha2 (= 0.3.4)
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,23 @@
|
||||||
#NOTE add the person object you want to attach role to...
|
#NOTE add the person object you want to attach role to...
|
||||||
|
|
||||||
class Role < ActiveRecord::Base
|
class Role < ActiveRecord::Base
|
||||||
belongs_to :person
|
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)
|
def self.is_admin?(person)
|
||||||
find_by_person_id_and_name(person.id, 'admin')
|
exists?(person_id: person.id, name: "admin")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.add_admin(person)
|
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
|
end
|
||||||
|
|
||||||
def self.add_spotlight(person)
|
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
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,54 @@
|
||||||
require 'spec_helper'
|
require "spec_helper"
|
||||||
|
|
||||||
describe Role, :type => :model do
|
describe Role do
|
||||||
skip "add some examples to (or delete) #{__FILE__}"
|
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
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,15 @@
|
||||||
# licensed under the Affero General Public License version 3 or later. See
|
# licensed under the Affero General Public License version 3 or later. See
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
ENV["RAILS_ENV"] ||= 'test'
|
ENV["RAILS_ENV"] ||= "test"
|
||||||
require File.join(File.dirname(__FILE__), '..', 'config', 'environment')
|
require File.join(File.dirname(__FILE__), "..", "config", "environment")
|
||||||
require Rails.root.join('spec', 'helper_methods')
|
require Rails.root.join("spec", "helper_methods")
|
||||||
require Rails.root.join('spec', 'spec-doc')
|
require Rails.root.join("spec", "spec-doc")
|
||||||
require 'rspec/rails'
|
require "rspec/rails"
|
||||||
require 'webmock/rspec'
|
require "webmock/rspec"
|
||||||
require 'factory_girl'
|
require "factory_girl"
|
||||||
require 'sidekiq/testing'
|
require "sidekiq/testing"
|
||||||
|
require "shoulda/matchers"
|
||||||
|
|
||||||
include HelperMethods
|
include HelperMethods
|
||||||
|
|
||||||
|
|
@ -25,39 +26,39 @@ def set_up_friends
|
||||||
end
|
end
|
||||||
|
|
||||||
def alice
|
def alice
|
||||||
@alice ||= User.where(:username => 'alice').first
|
@alice ||= User.find_by(username: "alice")
|
||||||
end
|
end
|
||||||
|
|
||||||
def bob
|
def bob
|
||||||
@bob ||= User.where(:username => 'bob').first
|
@bob ||= User.find_by(username: "bob")
|
||||||
end
|
end
|
||||||
|
|
||||||
def eve
|
def eve
|
||||||
@eve ||= User.where(:username => 'eve').first
|
@eve ||= User.find_by(username: "eve")
|
||||||
end
|
end
|
||||||
|
|
||||||
def local_luke
|
def local_luke
|
||||||
@local_luke ||= User.where(:username => 'luke').first
|
@local_luke ||= User.find_by(username: "luke")
|
||||||
end
|
end
|
||||||
|
|
||||||
def local_leia
|
def local_leia
|
||||||
@local_leia ||= User.where(:username => 'leia').first
|
@local_leia ||= User.find_by(username: "leia")
|
||||||
end
|
end
|
||||||
|
|
||||||
def remote_raphael
|
def remote_raphael
|
||||||
@remote_raphael ||= Person.where(:diaspora_handle => 'raphael@remote.net').first
|
@remote_raphael ||= Person.find_by(diaspora_handle: "raphael@remote.net")
|
||||||
end
|
end
|
||||||
|
|
||||||
def peter
|
def peter
|
||||||
@peter ||= User.where(:username => 'peter').first
|
@peter ||= User.find_by(username: "peter")
|
||||||
end
|
end
|
||||||
|
|
||||||
def photo_fixture_name
|
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
|
end
|
||||||
|
|
||||||
# Force fixture rebuild
|
# 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,
|
# Requires supporting files with custom matchers and macros, etc,
|
||||||
# in ./support/ and its subdirectories.
|
# in ./support/ and its subdirectories.
|
||||||
|
|
@ -100,4 +101,6 @@ RSpec.configure do |config|
|
||||||
config.after(:each) do
|
config.after(:each) do
|
||||||
ActionMailer::Base.deliveries.clear
|
ActionMailer::Base.deliveries.clear
|
||||||
end
|
end
|
||||||
|
|
||||||
|
config.include FactoryGirl::Syntax::Methods
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue