From cf6411988d1f57d020ea636d87963b163e79b9e6 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 17 Dec 2015 23:45:38 +0800 Subject: [PATCH] topic spec - need to add permissions and fix 2 failing tests --- spec/factories/metacodes.rb | 4 +++ spec/factories/synapses.rb | 4 +-- spec/factories/topics.rb | 2 ++ spec/models/topic_spec.rb | 50 ++++++++++++++++++++++++++++++++++++- 4 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 spec/factories/metacodes.rb diff --git a/spec/factories/metacodes.rb b/spec/factories/metacodes.rb new file mode 100644 index 00000000..5cfb38f6 --- /dev/null +++ b/spec/factories/metacodes.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :metacode do + end +end diff --git a/spec/factories/synapses.rb b/spec/factories/synapses.rb index f39a7dd6..10fd74da 100644 --- a/spec/factories/synapses.rb +++ b/spec/factories/synapses.rb @@ -3,7 +3,7 @@ FactoryGirl.define do desc { random_string(10) } category :to permission :commons - association :node1, factory: :topic - association :node2, factory: :topic + association :topic1, factory: :topic + association :topic2, factory: :topic end end diff --git a/spec/factories/topics.rb b/spec/factories/topics.rb index ecec31a3..c54301c1 100644 --- a/spec/factories/topics.rb +++ b/spec/factories/topics.rb @@ -2,5 +2,7 @@ FactoryGirl.define do factory :topic do name { random_string(10) } permission :commons + user + metacode end end diff --git a/spec/models/topic_spec.rb b/spec/models/topic_spec.rb index b8c5a8e0..17485803 100644 --- a/spec/models/topic_spec.rb +++ b/spec/models/topic_spec.rb @@ -1,5 +1,53 @@ require 'rails_helper' RSpec.describe Topic, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + it { is_expected.to belong_to :user } + it { is_expected.to belong_to :metacode } + it { is_expected.to have_many :maps } + + context 'has_viewable_synapses function' do + let (:user) { create(:user) } + let (:other_user) { create(:user) } + + context 'topic with no synapses' do + let (:topic) { create(:topic) } + + it 'returns false' do + expect(topic.has_viewable_synapses(user)).to eq false + end + end + + context 'topic with one unpermitted synapse' do + let (:topic) { create(:topic) } + let (:synapse) { create(:synapse, permission: :private, topic1: topic, user: other_user) } + + it 'returns false' do + expect(topic.has_viewable_synapses(user)).to eq false + end + end + + context 'topic with one permitted synapse' do + let (:topic) { create(:topic) } + let (:synapse) { create(:synapse, permission: :private, topic1: topic, user: user) } + + it 'returns true' do + expect(topic.has_viewable_synapses(user)).to eq true + end + end + + context 'topic with one unpermitted, one permitted synapse' do + let (:topic) { create(:topic) } + let (:synapse1) { create(:synapse, permission: :private, topic1: topic, user: other_user) } + let (:synapse2) { create(:synapse, permission: :private, topic1: topic, user: user) } + + it 'returns true' do + expect(topic.synapses.count).to eq 2 + expect(topic.has_viewable_synapses(user)).to eq true + end + end + end + + context 'permssions' do + pending "TODO" + end end