2013-02-15 05:51:48 +08:00
|
|
|
# encoding: utf-8
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
2013-10-17 14:44:56 +08:00
|
|
|
require_dependency 'post_creator'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Category do
|
2013-02-20 11:24:38 +08:00
|
|
|
it { should validate_presence_of :user_id }
|
2013-02-06 03:16:51 +08:00
|
|
|
it { should validate_presence_of :name }
|
|
|
|
|
|
|
|
it 'validates uniqueness of name' do
|
|
|
|
Fabricate(:category)
|
|
|
|
should validate_uniqueness_of(:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it { should belong_to :topic }
|
|
|
|
it { should belong_to :user }
|
|
|
|
|
|
|
|
it { should have_many :topics }
|
|
|
|
it { should have_many :category_featured_topics }
|
|
|
|
it { should have_many :featured_topics }
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
describe "resolve_permissions" do
|
|
|
|
it "can determine read_restricted" do
|
|
|
|
read_restricted, resolved = Category.resolve_permissions(:everyone => :full)
|
|
|
|
|
|
|
|
read_restricted.should be_false
|
|
|
|
resolved.should == []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "topic_create_allowed and post_create_allowed" do
|
|
|
|
it "works" do
|
|
|
|
default_category = Fabricate(:category)
|
|
|
|
full_category = Fabricate(:category)
|
|
|
|
can_post_category = Fabricate(:category)
|
|
|
|
can_read_category = Fabricate(:category)
|
|
|
|
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
|
|
|
admin = Fabricate(:admin)
|
|
|
|
|
|
|
|
full_category.set_permissions(group => :full)
|
|
|
|
full_category.save
|
|
|
|
|
|
|
|
can_post_category.set_permissions(group => :create_post)
|
|
|
|
can_post_category.save
|
|
|
|
|
|
|
|
can_read_category.set_permissions(group => :readonly)
|
|
|
|
can_read_category.save
|
|
|
|
|
|
|
|
guardian = Guardian.new(admin)
|
|
|
|
Category.topic_create_allowed(guardian).count.should == 4
|
|
|
|
Category.post_create_allowed(guardian).count.should == 4
|
|
|
|
Category.secured(guardian).count.should == 4
|
|
|
|
|
|
|
|
guardian = Guardian.new(user)
|
|
|
|
Category.secured(guardian).count.should == 4
|
|
|
|
Category.post_create_allowed(guardian).count.should == 3
|
|
|
|
Category.topic_create_allowed(guardian).count.should == 2 # explicitly allowed once, default allowed once
|
2013-07-16 13:44:07 +08:00
|
|
|
|
|
|
|
# everyone has special semantics, test it as well
|
|
|
|
can_post_category.set_permissions(:everyone => :create_post)
|
|
|
|
can_post_category.save
|
|
|
|
|
|
|
|
Category.post_create_allowed(guardian).count.should == 3
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2013-10-13 06:54:48 +08:00
|
|
|
# anonymous has permission to create no topics
|
|
|
|
guardian = Guardian.new(nil)
|
|
|
|
Category.post_create_allowed(guardian).count.should == 0
|
2013-07-14 09:24:16 +08:00
|
|
|
|
2013-10-13 06:54:48 +08:00
|
|
|
end
|
2013-07-14 09:24:16 +08:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
describe "security" do
|
2013-05-16 04:45:52 +08:00
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
let(:category_2) { Fabricate(:category) }
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:group) { Fabricate(:group) }
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-05-16 04:45:52 +08:00
|
|
|
it "secures categories correctly" do
|
2013-07-14 09:24:16 +08:00
|
|
|
category.read_restricted?.should be_false
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions({})
|
|
|
|
category.read_restricted?.should be_true
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(:everyone => :full)
|
|
|
|
category.read_restricted?.should be_false
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-05-16 04:45:52 +08:00
|
|
|
user.secure_categories.should be_empty
|
2013-04-29 14:33:24 +08:00
|
|
|
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(group.id => :full)
|
2013-04-29 14:33:24 +08:00
|
|
|
category.save
|
|
|
|
|
|
|
|
user.reload
|
2013-05-16 04:45:52 +08:00
|
|
|
user.secure_categories.should == [category]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "lists all secured categories correctly" do
|
|
|
|
group.add(user)
|
2013-07-14 09:24:16 +08:00
|
|
|
category.set_permissions(group.id => :full)
|
|
|
|
category.save
|
|
|
|
category_2.set_permissions(group.id => :full)
|
|
|
|
category_2.save
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-07-14 09:24:16 +08:00
|
|
|
Category.secured.should =~ []
|
|
|
|
Category.secured(Guardian.new(user)).should =~ [category, category_2]
|
2013-04-29 14:33:24 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "uncategorized name" do
|
|
|
|
let(:category) { Fabricate.build(:category, name: SiteSetting.uncategorized_name) }
|
|
|
|
|
|
|
|
it "is invalid to create a category with the reserved name" do
|
2013-02-26 00:42:20 +08:00
|
|
|
category.should_not be_valid
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-31 04:48:19 +08:00
|
|
|
it "strips leading blanks" do
|
|
|
|
Fabricate(:category, name: " music").name.should == "music"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "strips trailing blanks" do
|
|
|
|
Fabricate(:category, name: "bugs ").name.should == "bugs"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "strips leading and trailing blanks" do
|
|
|
|
Fabricate(:category, name: " blanks ").name.should == "blanks"
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "short name" do
|
|
|
|
let!(:category) { Fabricate(:category, name: 'xx') }
|
|
|
|
|
|
|
|
it "creates the category" do
|
|
|
|
category.should be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has one topic' do
|
|
|
|
Topic.where(category_id: category.id).count.should == 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'caching' do
|
|
|
|
it "invalidates the site cache on creation" do
|
|
|
|
Site.expects(:invalidate_cache).once
|
|
|
|
Fabricate(:category)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "invalidates the site cache on update" do
|
|
|
|
cat = Fabricate(:category)
|
|
|
|
Site.expects(:invalidate_cache).once
|
|
|
|
cat.update_attributes(name: 'new name')
|
|
|
|
end
|
|
|
|
|
|
|
|
it "invalidates the site cache on destroy" do
|
|
|
|
cat = Fabricate(:category)
|
|
|
|
Site.expects(:invalidate_cache).once
|
|
|
|
cat.destroy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-15 05:51:48 +08:00
|
|
|
describe 'non-english characters' do
|
|
|
|
let(:category) { Fabricate(:category, name: "電車男") }
|
|
|
|
|
|
|
|
it "creates a blank slug, this is OK." do
|
|
|
|
category.slug.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-30 23:09:09 +08:00
|
|
|
describe 'slug would be a number' do
|
|
|
|
let(:category) { Fabricate(:category, name: "電車男 2") }
|
|
|
|
|
|
|
|
it 'creates a blank slug' do
|
|
|
|
category.slug.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'after create' do
|
|
|
|
before do
|
2013-05-08 03:52:45 +08:00
|
|
|
@category = Fabricate(:category, name: 'Amazing Category')
|
2013-02-06 03:16:51 +08:00
|
|
|
@topic = @category.topic
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'is created correctly' do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category.slug.should == 'amazing-category'
|
|
|
|
|
2013-03-27 06:06:21 +08:00
|
|
|
@category.hotness.should == 5.0
|
|
|
|
|
2013-02-22 07:09:56 +08:00
|
|
|
@category.description.should be_blank
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
Topic.where(category_id: @category).count.should == 1
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
@topic.should be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
@topic.category.should == @category
|
|
|
|
|
2013-03-08 01:45:49 +08:00
|
|
|
@topic.should be_visible
|
|
|
|
|
|
|
|
@topic.pinned_at.should be_present
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
Guardian.new(@category.user).can_delete?(@topic).should be_false
|
|
|
|
|
|
|
|
@topic.posts.count.should == 1
|
|
|
|
|
|
|
|
@category.topic_url.should be_present
|
|
|
|
end
|
|
|
|
|
2013-04-02 00:26:51 +08:00
|
|
|
describe "creating a new category with the same slug" do
|
|
|
|
it "should have a blank slug" do
|
|
|
|
Fabricate(:category, name: "Amazing Categóry").slug.should be_blank
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe "trying to change the category topic's category" do
|
|
|
|
before do
|
|
|
|
@new_cat = Fabricate(:category, name: '2nd Category', user: @category.user)
|
|
|
|
@topic.change_category(@new_cat.name)
|
|
|
|
@topic.reload
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'does not cause changes' do
|
2013-02-06 03:16:51 +08:00
|
|
|
@category.topic_count.should == 0
|
|
|
|
@topic.category.should == @category
|
|
|
|
@category.topic.should == @topic
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'destroy' do
|
|
|
|
before do
|
|
|
|
@category = Fabricate(:category)
|
|
|
|
@category_id = @category.id
|
|
|
|
@topic_id = @category.topic_id
|
|
|
|
@category.destroy
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'is deleted correctly' do
|
2013-02-06 03:16:51 +08:00
|
|
|
Category.exists?(id: @category_id).should be_false
|
|
|
|
Topic.exists?(id: @topic_id).should be_false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-17 14:44:56 +08:00
|
|
|
describe 'latest' do
|
|
|
|
it 'should be updated correctly' do
|
|
|
|
category = Fabricate(:category)
|
|
|
|
post = create_post(category: category.name)
|
|
|
|
|
|
|
|
category.reload
|
|
|
|
category.latest_post_id.should == post.id
|
|
|
|
category.latest_topic_id.should == post.topic_id
|
|
|
|
|
|
|
|
post2 = create_post(category: category.name)
|
|
|
|
post3 = create_post(topic_id: post.topic_id, category: category.name)
|
|
|
|
|
|
|
|
category.reload
|
|
|
|
category.latest_post_id.should == post3.id
|
|
|
|
category.latest_topic_id.should == post2.topic_id
|
|
|
|
|
|
|
|
|
|
|
|
destroyer = PostDestroyer.new(Fabricate(:admin), post3)
|
|
|
|
destroyer.destroy
|
|
|
|
|
|
|
|
category.reload
|
|
|
|
category.latest_post_id.should == post2.id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe 'update_stats' do
|
|
|
|
before do
|
|
|
|
@category = Fabricate(:category)
|
2013-02-17 04:57:16 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-17 04:57:16 +08:00
|
|
|
context 'with regular topics' do
|
|
|
|
before do
|
2013-10-17 10:23:38 +08:00
|
|
|
create_post(user: @category.user, category: @category.name)
|
2013-02-17 04:57:16 +08:00
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'updates topic stats' do
|
2013-02-17 04:57:16 +08:00
|
|
|
@category.topics_week.should == 1
|
|
|
|
@category.topics_month.should == 1
|
|
|
|
@category.topics_year.should == 1
|
2013-03-31 19:22:05 +08:00
|
|
|
@category.topic_count.should == 1
|
2013-10-17 10:23:38 +08:00
|
|
|
@category.post_count.should == 1
|
2013-03-31 19:22:05 +08:00
|
|
|
end
|
2013-04-29 14:33:24 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-17 04:57:16 +08:00
|
|
|
context 'with deleted topics' do
|
|
|
|
before do
|
2013-02-26 00:42:20 +08:00
|
|
|
@category.topics << Fabricate(:deleted_topic,
|
2013-02-17 04:57:16 +08:00
|
|
|
user: @category.user)
|
|
|
|
Category.update_stats
|
|
|
|
@category.reload
|
|
|
|
end
|
|
|
|
|
2013-04-29 14:33:24 +08:00
|
|
|
it 'does not count deleted topics' do
|
2013-02-17 04:57:16 +08:00
|
|
|
@category.topics_week.should == 0
|
2013-04-29 14:33:24 +08:00
|
|
|
@category.topic_count.should == 0
|
2013-02-17 04:57:16 +08:00
|
|
|
@category.topics_month.should == 0
|
|
|
|
@category.topics_year.should == 0
|
2013-10-17 10:23:38 +08:00
|
|
|
@category.post_count.should == 0
|
2013-02-17 04:57:16 +08:00
|
|
|
end
|
2013-03-31 19:22:05 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|