2014-01-01 20:00:08 -05:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2014-01-01 20:00:08 -05:00
|
|
|
require_dependency 'post_creator'
|
|
|
|
|
|
|
|
describe CategoryUser do
|
2014-01-02 01:58:49 -05:00
|
|
|
|
|
|
|
it 'allows batch set' do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
category1 = Fabricate(:category)
|
|
|
|
category2 = Fabricate(:category)
|
|
|
|
|
|
|
|
watching = CategoryUser.where(user_id: user.id, notification_level: CategoryUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
CategoryUser.batch_set(user, :watching, [category1.id, category2.id])
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(watching.pluck(:category_id).sort).to eq [category1.id, category2.id]
|
2014-01-02 01:58:49 -05:00
|
|
|
|
|
|
|
CategoryUser.batch_set(user, :watching, [])
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(watching.count).to eq 0
|
2014-01-02 01:58:49 -05:00
|
|
|
|
|
|
|
CategoryUser.batch_set(user, :watching, [category2.id])
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(watching.count).to eq 1
|
2014-01-02 01:58:49 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-01-01 20:00:08 -05:00
|
|
|
context 'integration' do
|
|
|
|
before do
|
|
|
|
ActiveRecord::Base.observers.enable :all
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should operate correctly' do
|
|
|
|
watched_category = Fabricate(:category)
|
|
|
|
muted_category = Fabricate(:category)
|
2014-06-01 23:55:01 -04:00
|
|
|
tracked_category = Fabricate(:category)
|
|
|
|
|
2014-01-01 20:00:08 -05:00
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
CategoryUser.create!(user: user, category: watched_category, notification_level: CategoryUser.notification_levels[:watching])
|
|
|
|
CategoryUser.create!(user: user, category: muted_category, notification_level: CategoryUser.notification_levels[:muted])
|
2014-06-01 23:55:01 -04:00
|
|
|
CategoryUser.create!(user: user, category: tracked_category, notification_level: CategoryUser.notification_levels[:tracking])
|
2014-01-01 20:00:08 -05:00
|
|
|
|
|
|
|
watched_post = create_post(category: watched_category)
|
2015-11-30 01:03:47 -05:00
|
|
|
_muted_post = create_post(category: muted_category)
|
2014-06-01 23:55:01 -04:00
|
|
|
tracked_post = create_post(category: tracked_category)
|
2014-01-01 20:00:08 -05:00
|
|
|
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Notification.where(user_id: user.id, topic_id: watched_post.topic_id).count).to eq 1
|
|
|
|
expect(Notification.where(user_id: user.id, topic_id: tracked_post.topic_id).count).to eq 0
|
2014-06-01 23:55:01 -04:00
|
|
|
|
|
|
|
tu = TopicUser.get(tracked_post.topic, user)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(tu.notification_level).to eq TopicUser.notification_levels[:tracking]
|
|
|
|
expect(tu.notifications_reason_id).to eq TopicUser.notification_reasons[:auto_track_category]
|
2015-03-04 13:39:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "watches categories that have been changed" do
|
|
|
|
user = Fabricate(:user)
|
2015-08-19 16:40:20 -04:00
|
|
|
watched_category = Fabricate(:category)
|
2015-03-04 13:39:17 -05:00
|
|
|
CategoryUser.create!(user: user, category: watched_category, notification_level: CategoryUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
post = create_post
|
2015-04-25 11:18:35 -04:00
|
|
|
expect(TopicUser.get(post.topic, user)).to be_blank
|
2014-01-01 20:00:08 -05:00
|
|
|
|
2015-03-04 13:39:17 -05:00
|
|
|
# Now, change the topic's category
|
|
|
|
post.topic.change_category_to_id(watched_category.id)
|
|
|
|
tu = TopicUser.get(post.topic, user)
|
|
|
|
expect(tu.notification_level).to eq TopicUser.notification_levels[:watching]
|
2014-01-01 20:00:08 -05:00
|
|
|
end
|
|
|
|
|
2015-08-19 16:40:20 -04:00
|
|
|
it "unwatches categories that have been changed" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
watched_category = Fabricate(:category)
|
|
|
|
CategoryUser.create!(user: user, category: watched_category, notification_level: CategoryUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
post = create_post(category: watched_category)
|
|
|
|
tu = TopicUser.get(post.topic, user)
|
|
|
|
expect(tu.notification_level).to eq TopicUser.notification_levels[:watching]
|
|
|
|
|
|
|
|
# Now, change the topic's category
|
|
|
|
unwatched_category = Fabricate(:category)
|
|
|
|
post.topic.change_category_to_id(unwatched_category.id)
|
|
|
|
expect(TopicUser.get(post.topic, user)).to be_blank
|
|
|
|
end
|
|
|
|
|
2015-09-02 14:43:15 -04:00
|
|
|
it "is destroyed when a user is deleted" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
category = Fabricate(:category)
|
|
|
|
|
|
|
|
CategoryUser.create!(user: user, category: category, notification_level: CategoryUser.notification_levels[:watching])
|
|
|
|
|
|
|
|
expect(CategoryUser.where(user_id: user.id).count).to eq(1)
|
|
|
|
|
|
|
|
user.destroy!
|
|
|
|
|
|
|
|
expect(CategoryUser.where(user_id: user.id).count).to eq(0)
|
|
|
|
end
|
|
|
|
|
2014-01-01 20:00:08 -05:00
|
|
|
end
|
|
|
|
end
|
2014-12-31 09:55:03 -05:00
|
|
|
|