2013-03-26 09:04:28 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2013-11-11 07:50:48 +08:00
|
|
|
describe TopicsController do
|
|
|
|
before do
|
|
|
|
TopicUser.stubs(:track_visit!)
|
|
|
|
end
|
|
|
|
|
|
|
|
let :topic do
|
|
|
|
Fabricate(:post).topic
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_referer(ref)
|
|
|
|
request.env['HTTP_REFERER'] = ref
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't store an incoming link when there's no referer" do
|
|
|
|
lambda {
|
|
|
|
get :show, id: topic.id
|
|
|
|
}.should_not change(IncomingLink, :count)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't raise an error on a very long link" do
|
|
|
|
set_referer("http://#{'a' * 2000}.com")
|
|
|
|
lambda { get :show, {id: topic.id} }.should_not raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "stores an incoming link when there is an off-site referer" do
|
|
|
|
lambda {
|
|
|
|
set_referer("http://google.com/search")
|
|
|
|
get :show, {id: topic.id}
|
|
|
|
}.should change(IncomingLink, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'after inserting an incoming link' do
|
|
|
|
|
|
|
|
it 'sets last link correctly' do
|
|
|
|
set_referer("http://google.com/search")
|
|
|
|
get :show, {topic_id: topic.id}
|
|
|
|
|
|
|
|
last_link = IncomingLink.last
|
|
|
|
last_link.topic_id.should == topic.id
|
|
|
|
last_link.post_number.should == 1
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-04-30 08:34:19 +08:00
|
|
|
describe 'api' do
|
2013-03-26 09:04:28 +08:00
|
|
|
describe PostsController do
|
|
|
|
let(:user) do
|
|
|
|
Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
2013-04-30 08:34:19 +08:00
|
|
|
let(:post) do
|
2013-03-26 09:04:28 +08:00
|
|
|
Fabricate(:post)
|
|
|
|
end
|
2013-04-30 08:34:19 +08:00
|
|
|
|
2013-10-23 03:53:08 +08:00
|
|
|
let(:api_key) { user.generate_api_key(user) }
|
|
|
|
let(:master_key) { ApiKey.create_master_key }
|
|
|
|
|
2013-03-26 09:04:28 +08:00
|
|
|
# choosing an arbitrarily easy to mock trusted activity
|
|
|
|
it 'allows users with api key to bookmark posts' do
|
2013-04-30 08:34:19 +08:00
|
|
|
PostAction.expects(:act).with(user, post, PostActionType.types[:bookmark]).once
|
2013-10-23 03:53:08 +08:00
|
|
|
put :bookmark, bookmarked: "true", post_id: post.id, api_key: api_key.key, format: :json
|
2013-10-23 23:05:49 +08:00
|
|
|
response.should be_success
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error with a user key that does not match an optionally specified username' do
|
|
|
|
PostAction.expects(:act).with(user, post, PostActionType.types[:bookmark]).never
|
|
|
|
put :bookmark, bookmarked: "true", post_id: post.id, api_key: api_key.key, api_username: 'made_up', format: :json
|
|
|
|
response.should_not be_success
|
2013-10-23 03:53:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows users with a master api key to bookmark posts' do
|
|
|
|
PostAction.expects(:act).with(user, post, PostActionType.types[:bookmark]).once
|
|
|
|
put :bookmark, bookmarked: "true", post_id: post.id, api_key: master_key.key, api_username: user.username, format: :json
|
2013-10-23 23:05:49 +08:00
|
|
|
response.should be_success
|
2013-03-26 09:04:28 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'disallows phonies to bookmark posts' do
|
2013-04-30 08:34:19 +08:00
|
|
|
PostAction.expects(:act).with(user, post, PostActionType.types[:bookmark]).never
|
|
|
|
lambda do
|
|
|
|
put :bookmark, bookmarked: "true", post_id: post.id, api_key: SecureRandom.hex(32), api_username: user.username, format: :json
|
2013-03-26 09:04:28 +08:00
|
|
|
end.should raise_error Discourse::NotLoggedIn
|
|
|
|
end
|
2013-04-30 08:34:19 +08:00
|
|
|
|
2013-03-26 09:04:28 +08:00
|
|
|
it 'disallows blank api' do
|
2013-04-30 08:34:19 +08:00
|
|
|
PostAction.expects(:act).with(user, post, PostActionType.types[:bookmark]).never
|
|
|
|
lambda do
|
|
|
|
put :bookmark, bookmarked: "true", post_id: post.id, api_key: "", api_username: user.username, format: :json
|
2013-03-26 09:04:28 +08:00
|
|
|
end.should raise_error Discourse::NotLoggedIn
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|