From d5f66487b7008acb7e4daba77cacfaa89791c9a9 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 17 Dec 2015 09:44:48 +0800 Subject: [PATCH] update map spec - add authorize_to_delete. TODO: add other map authorize specs --- app/models/map.rb | 1 - spec/factories/maps.rb | 1 + spec/models/map_spec.rb | 13 ++++++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/app/models/map.rb b/app/models/map.rb index 48dcae3b..8bca3b30 100644 --- a/app/models/map.rb +++ b/app/models/map.rb @@ -14,7 +14,6 @@ class Map < ActiveRecord::Base }, :default_url => 'https://s3.amazonaws.com/metamaps-assets/site/missing-map.png' validates :name, presence: true - validates :arranged, presence: true validates :arranged, inclusion: { in: [true, false] } validates :permission, presence: true validates :permission, inclusion: { in: Perm::ISSIONS.map(&:to_s) } diff --git a/spec/factories/maps.rb b/spec/factories/maps.rb index 74058605..bfda5a9a 100644 --- a/spec/factories/maps.rb +++ b/spec/factories/maps.rb @@ -2,6 +2,7 @@ FactoryGirl.define do factory :map do name { random_string(10) } permission :commons + arranged { false } user end end diff --git a/spec/models/map_spec.rb b/spec/models/map_spec.rb index b0590edb..688c4252 100644 --- a/spec/models/map_spec.rb +++ b/spec/models/map_spec.rb @@ -4,8 +4,19 @@ RSpec.describe Map, type: :model do # TODO: what is it important to be sure about when working with a Map? it { is_expected.to belong_to :user } it { is_expected.to validate_presence_of :name } - it { is_expected.to validate_presence_of :arranged } it { is_expected.to validate_presence_of :permission } it { is_expected.to validate_inclusion_of(:permission).in_array Perm::ISSIONS.map(&:to_s) } + it { is_expected.to validate_inclusion_of(:arranged).in_array [true, false] } + + context 'permissions' do + let(:owner) { create :user } + let(:other_user) { create :user } + let(:map) { create :map, user: owner, permission: :commons } + + it 'prevents deletion by non-owner' do + expect(map.authorize_to_delete(other_user)).to eq false + expect(map.authorize_to_delete(owner)).to eq map + end + end end