2015-10-11 17:41:23 +08:00
require 'rails_helper'
2013-11-29 00:06:04 +08:00
describe TopicCreator do
2015-10-17 15:48:56 +08:00
let ( :user ) { Fabricate ( :user , trust_level : TrustLevel [ 2 ] ) }
2013-11-29 00:06:04 +08:00
let ( :moderator ) { Fabricate ( :moderator ) }
let ( :admin ) { Fabricate ( :admin ) }
let ( :valid_attrs ) { Fabricate . attributes_for ( :topic ) }
2017-07-28 09:20:09 +08:00
let ( :pm_valid_attrs ) { { raw : 'this is a new post' , title : 'this is a new title' , archetype : Archetype . private_message , target_usernames : moderator . username } }
2017-08-29 00:07:30 +08:00
let ( :pm_to_email_valid_attrs ) { { raw : 'this is a new email' , title : 'this is a new subject' , archetype : Archetype . private_message , target_emails : 'moderator@example.com' } }
2013-11-29 00:06:04 +08:00
describe '#create' do
2015-10-17 15:48:56 +08:00
context 'topic success cases' do
2013-11-29 00:06:04 +08:00
before do
TopicCreator . any_instance . expects ( :save_topic ) . returns ( true )
TopicCreator . any_instance . expects ( :watch_topic ) . returns ( true )
2017-07-07 14:09:14 +08:00
SiteSetting . allow_duplicate_topic_titles = true
2013-11-29 00:06:04 +08:00
end
it " should be possible for an admin to create a topic " do
2015-01-10 00:34:37 +08:00
expect ( TopicCreator . create ( admin , Guardian . new ( admin ) , valid_attrs ) ) . to be_valid
2013-11-29 00:06:04 +08:00
end
it " should be possible for a moderator to create a topic " do
2015-01-10 00:34:37 +08:00
expect ( TopicCreator . create ( moderator , Guardian . new ( moderator ) , valid_attrs ) ) . to be_valid
2013-11-29 00:06:04 +08:00
end
context 'regular user' do
2017-07-07 14:09:14 +08:00
before { SiteSetting . min_trust_to_create_topic = TrustLevel [ 0 ] }
2013-11-29 00:06:04 +08:00
it " should be possible for a regular user to create a topic " do
2015-01-10 00:34:37 +08:00
expect ( TopicCreator . create ( user , Guardian . new ( user ) , valid_attrs ) ) . to be_valid
2013-11-29 00:06:04 +08:00
end
it " should be possible for a regular user to create a topic with blank auto_close_time " do
2015-01-10 00:34:37 +08:00
expect ( TopicCreator . create ( user , Guardian . new ( user ) , valid_attrs . merge ( auto_close_time : '' ) ) ) . to be_valid
2013-11-29 00:06:04 +08:00
end
it " ignores auto_close_time without raising an error " do
topic = TopicCreator . create ( user , Guardian . new ( user ) , valid_attrs . merge ( auto_close_time : '24' ) )
2015-01-10 00:34:37 +08:00
expect ( topic ) . to be_valid
2017-05-17 02:49:42 +08:00
expect ( topic . public_topic_timer ) . to eq ( nil )
2013-11-29 00:06:04 +08:00
end
2014-08-18 23:07:32 +08:00
it " category name is case insensitive " do
category = Fabricate ( :category , name : " Neil's Blog " )
topic = TopicCreator . create ( user , Guardian . new ( user ) , valid_attrs . merge ( category : " neil's blog " ) )
2015-01-10 00:34:37 +08:00
expect ( topic ) . to be_valid
expect ( topic . category ) . to eq ( category )
2014-08-18 23:07:32 +08:00
end
2013-11-29 00:06:04 +08:00
end
end
2015-10-17 15:48:56 +08:00
context 'private message' do
context 'success cases' do
before do
TopicCreator . any_instance . expects ( :save_topic ) . returns ( true )
TopicCreator . any_instance . expects ( :watch_topic ) . returns ( true )
2017-07-07 14:09:14 +08:00
SiteSetting . allow_duplicate_topic_titles = true
2017-08-29 00:07:30 +08:00
SiteSetting . enable_staged_users = true
2015-10-17 15:48:56 +08:00
end
it " should be possible for a regular user to send private message " do
expect ( TopicCreator . create ( user , Guardian . new ( user ) , pm_valid_attrs ) ) . to be_valid
end
it " min_trust_to_create_topic setting should not be checked when sending private message " do
SiteSetting . min_trust_to_create_topic = TrustLevel [ 4 ]
expect ( TopicCreator . create ( user , Guardian . new ( user ) , pm_valid_attrs ) ) . to be_valid
end
2017-08-29 00:07:30 +08:00
it " should be possible for a trusted user to send private messages via email " do
SiteSetting . expects ( :enable_staged_users ) . returns ( true )
SiteSetting . expects ( :enable_staged_users ) . returns ( true )
SiteSetting . expects ( :enable_private_email_messages ) . returns ( true )
SiteSetting . min_trust_to_send_email_messages = TrustLevel [ 1 ]
expect ( TopicCreator . create ( user , Guardian . new ( user ) , pm_to_email_valid_attrs ) ) . to be_valid
end
2015-10-17 15:48:56 +08:00
end
context 'failure cases' do
it " min_trust_to_send_messages setting should be checked when sending private message " do
SiteSetting . min_trust_to_send_messages = TrustLevel [ 4 ]
2017-08-29 08:26:48 +08:00
expect do
TopicCreator . create ( user , Guardian . new ( user ) , pm_valid_attrs )
end . to raise_error ( ActiveRecord :: Rollback )
2015-10-17 15:48:56 +08:00
end
2017-08-29 00:07:30 +08:00
it " min_trust_to_send_email_messages should be checked when sending private messages via email " do
SiteSetting . min_trust_to_send_email_messages = TrustLevel [ 4 ]
2017-08-29 08:26:48 +08:00
expect do
TopicCreator . create ( user , Guardian . new ( user ) , pm_to_email_valid_attrs )
end . to raise_error ( ActiveRecord :: Rollback )
2017-08-29 00:07:30 +08:00
end
2015-10-17 15:48:56 +08:00
end
end
end
2013-11-29 00:06:04 +08:00
end