Merge pull request #5792 from MothOnMars/4020-role-specs

added specs and validations for Role model
This commit is contained in:
Jonne Haß 2015-03-20 01:53:27 +01:00
commit 644fc46dee
6 changed files with 86 additions and 24 deletions

View file

@ -104,6 +104,7 @@ diaspora.yml file**. The existing settings from 0.4.x and before will not work a
* Add tests for liking and unliking posts [#5741](https://github.com/diaspora/diaspora/pull/5741)
* Rewrite slide effect in conversations as css transition for better performance [#5776](https://github.com/diaspora/diaspora/pull/5776)
* Various cleanups and improvements in the frontend code [#5781](https://github.com/diaspora/diaspora/pull/5781) [#5769](https://github.com/diaspora/diaspora/pull/5769) [#5763](https://github.com/diaspora/diaspora/pull/5763) [#5762](https://github.com/diaspora/diaspora/pull/5762) [#5758](https://github.com/diaspora/diaspora/pull/5758) [#5755](https://github.com/diaspora/diaspora/pull/5755) [#5747](https://github.com/diaspora/diaspora/pull/5747) [#5734](https://github.com/diaspora/diaspora/pull/5734) [#5786](https://github.com/diaspora/diaspora/pull/5786) [#5768](https://github.com/diaspora/diaspora/pull/5798)
* Add specs and validations to the role model [#5792](https://github.com/diaspora/diaspora/pull/5792)
## Bug fixes
* orca cannot see 'Add Contact' button [#5158](https://github.com/diaspora/diaspora/pull/5158)

View file

@ -255,6 +255,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

View file

@ -620,6 +620,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)
@ -802,6 +804,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)

View file

@ -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

View file

@ -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

View file

@ -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