2013-02-05 14:16:51 -05:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe ListController do
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# we need some data
|
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = Fabricate(:coding_horror)
|
2013-03-23 11:02:59 -04:00
|
|
|
@post = Fabricate(:post, user: @user)
|
2013-06-21 16:31:40 -04:00
|
|
|
|
|
|
|
# forces tests down some code paths
|
2015-01-06 19:03:45 -05:00
|
|
|
SiteSetting.stubs(:top_menu).returns('latest,-video|new|unread|categories|category/beer')
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-03-30 11:40:44 -04:00
|
|
|
describe 'titles for crawler layout' do
|
|
|
|
it 'has no title for the default URL' do
|
|
|
|
xhr :get, Discourse.anonymous_filters[0], _escaped_fragment_: 'true'
|
|
|
|
expect(assigns(:title)).to be_blank
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a title for non-default URLs' do
|
|
|
|
xhr :get, Discourse.anonymous_filters[1], _escaped_fragment_: 'true'
|
|
|
|
expect(assigns(:title)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-03-28 09:01:13 -04:00
|
|
|
describe 'indexes' do
|
|
|
|
|
2014-09-11 15:30:10 -04:00
|
|
|
(Discourse.anonymous_filters - [:categories]).each do |filter|
|
2013-06-21 16:31:40 -04:00
|
|
|
context "#{filter}" do
|
2013-03-28 09:01:13 -04:00
|
|
|
before { xhr :get, filter }
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-03-28 09:01:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-23 18:50:36 -05:00
|
|
|
Discourse.logged_in_filters.each do |filter|
|
2013-06-21 16:31:40 -04:00
|
|
|
context "#{filter}" do
|
2013-03-28 09:01:13 -04:00
|
|
|
it { expect { xhr :get, filter }.to raise_error(Discourse::NotLoggedIn) }
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-05-28 03:52:52 -04:00
|
|
|
it 'allows users to filter on a set of topic ids' do
|
2013-10-23 19:05:51 -04:00
|
|
|
p = create_post
|
|
|
|
|
2013-05-28 03:52:52 -04:00
|
|
|
xhr :get, :latest, format: :json, topic_ids: "#{p.topic_id}"
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-05-28 03:52:52 -04:00
|
|
|
parsed = JSON.parse(response.body)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(parsed["topic_list"]["topics"].length).to eq(1)
|
2013-05-28 03:52:52 -04:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-10-11 12:35:12 -04:00
|
|
|
describe 'RSS feeds' do
|
|
|
|
|
2014-09-11 15:30:10 -04:00
|
|
|
Discourse.feed_filters.each do |filter|
|
2013-10-11 12:35:12 -04:00
|
|
|
|
|
|
|
it 'renders RSS' do
|
|
|
|
get "#{filter}_feed", format: :rss
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
2013-10-11 12:35:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'category' do
|
|
|
|
|
|
|
|
context 'in a category' do
|
|
|
|
let(:category) { Fabricate(:category) }
|
|
|
|
|
2014-02-05 15:33:52 -05:00
|
|
|
context 'without access to see the category' do
|
|
|
|
before do
|
|
|
|
Guardian.any_instance.expects(:can_see?).with(category).returns(false)
|
|
|
|
xhr :get, :category_latest, category: category.slug
|
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.not_to respond_with(:success) }
|
2014-02-05 15:33:52 -05:00
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'with access to see the category' do
|
|
|
|
before do
|
2014-02-03 10:08:00 -05:00
|
|
|
xhr :get, :category_latest, category: category.slug
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2013-02-14 16:51:48 -05:00
|
|
|
|
|
|
|
context 'with a link that includes an id' do
|
|
|
|
before do
|
2014-02-03 10:08:00 -05:00
|
|
|
xhr :get, :category_latest, category: "#{category.id}-#{category.slug}"
|
2013-02-14 16:51:48 -05:00
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-02-14 16:51:48 -05:00
|
|
|
end
|
|
|
|
|
2013-08-22 15:46:17 -04:00
|
|
|
context 'another category exists with a number at the beginning of its name' do
|
|
|
|
# One category has another category's id at the beginning of its name
|
|
|
|
let!(:other_category) { Fabricate(:category, name: "#{category.id} name") }
|
|
|
|
|
|
|
|
before do
|
2014-02-03 10:08:00 -05:00
|
|
|
xhr :get, :category_latest, category: other_category.slug
|
2013-08-22 15:46:17 -04:00
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-08-22 15:46:17 -04:00
|
|
|
|
|
|
|
it 'uses the correct category' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(assigns(:category)).to eq(other_category)
|
2013-08-22 15:46:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-23 14:40:39 -04:00
|
|
|
context 'a child category' do
|
|
|
|
let(:sub_category) { Fabricate(:category, parent_category_id: category.id) }
|
|
|
|
|
|
|
|
context 'when parent and child are requested' do
|
|
|
|
before do
|
2014-02-03 10:08:00 -05:00
|
|
|
xhr :get, :category_latest, parent_category: category.slug, category: sub_category.slug
|
2013-10-23 14:40:39 -04:00
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-10-23 14:40:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when child is requested with the wrong parent' do
|
|
|
|
before do
|
2014-02-03 10:08:00 -05:00
|
|
|
xhr :get, :category_latest, parent_category: 'not_the_right_slug', category: sub_category.slug
|
2013-10-23 14:40:39 -04:00
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.not_to respond_with(:success) }
|
2013-10-23 14:40:39 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-27 22:36:12 -05:00
|
|
|
describe 'feed' do
|
|
|
|
it 'renders RSS' do
|
|
|
|
get :category_feed, category: category.slug, format: :rss
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
|
|
|
expect(response.content_type).to eq('application/rss+xml')
|
2013-02-27 22:36:12 -05:00
|
|
|
end
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-19 09:21:17 -04:00
|
|
|
describe "topics_by" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "should respond with a list" do
|
|
|
|
xhr :get, :topics_by, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-10-19 09:21:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-30 14:35:11 -04:00
|
|
|
context "private_messages" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "raises an error when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(false)
|
|
|
|
xhr :get, :private_messages, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(true)
|
|
|
|
xhr :get, :private_messages, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "private_messages_sent" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "raises an error when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(false)
|
|
|
|
xhr :get, :private_messages_sent, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(true)
|
|
|
|
xhr :get, :private_messages_sent, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "private_messages_unread" do
|
|
|
|
let!(:user) { log_in }
|
|
|
|
|
|
|
|
it "raises an error when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(false)
|
|
|
|
xhr :get, :private_messages_unread, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_forbidden
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds when can_see_private_messages? is false " do
|
|
|
|
Guardian.any_instance.expects(:can_see_private_messages?).returns(true)
|
|
|
|
xhr :get, :private_messages_unread, username: @user.username
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(response).to be_success
|
2013-09-30 14:35:11 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
context 'read' do
|
|
|
|
it 'raises an error when not logged in' do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect { xhr :get, :read }.to raise_error(Discourse::NotLoggedIn)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when logged in' do
|
|
|
|
before do
|
|
|
|
log_in_user(@user)
|
|
|
|
xhr :get, :read
|
|
|
|
end
|
|
|
|
|
2015-01-09 12:04:02 -05:00
|
|
|
it { is_expected.to respond_with(:success) }
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
describe "best_periods_for" do
|
2014-03-12 07:58:41 -04:00
|
|
|
|
|
|
|
it "returns yearly for more than 180 days" do
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(ListController.best_periods_for(nil)).to eq([:yearly])
|
|
|
|
expect(ListController.best_periods_for(180.days.ago)).to eq([:yearly])
|
2014-03-12 07:58:41 -04:00
|
|
|
end
|
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
it "includes monthly when less than 180 days and more than 35 days" do
|
2014-03-12 07:58:41 -04:00
|
|
|
(35...180).each do |date|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(ListController.best_periods_for(date.days.ago)).to eq([:monthly, :yearly])
|
2014-03-12 07:58:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
it "includes weekly when less than 35 days and more than 8 days" do
|
2014-03-12 07:58:41 -04:00
|
|
|
(8...35).each do |date|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(ListController.best_periods_for(date.days.ago)).to eq([:weekly, :monthly, :yearly])
|
2014-03-12 07:58:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-07 13:04:39 -04:00
|
|
|
it "includes daily when less than 8 days" do
|
2014-03-12 07:58:41 -04:00
|
|
|
(0...8).each do |date|
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(ListController.best_periods_for(date.days.ago)).to eq([:daily, :weekly, :monthly, :yearly])
|
2014-03-12 07:58:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|