2015-11-19 15:18:13 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-15 14:51:16 +00:00
|
|
|
describe Banzai::Filter::UserReferenceFilter, lib: true do
|
2015-11-19 15:18:13 +00:00
|
|
|
include FilterSpecHelper
|
|
|
|
|
|
|
|
let(:project) { create(:empty_project, :public) }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:reference) { user.to_reference }
|
|
|
|
|
|
|
|
it 'requires project context' do
|
|
|
|
expect { described_class.call('') }.to raise_error(ArgumentError, /:project/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'ignores invalid users' do
|
|
|
|
exp = act = "Hey #{invalidate_reference(reference)}"
|
2015-12-07 13:48:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to eq(exp)
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
%w(pre code a style).each do |elem|
|
|
|
|
it "ignores valid references contained inside '#{elem}' element" do
|
|
|
|
exp = act = "<#{elem}>Hey #{reference}</#{elem}>"
|
2015-12-07 13:48:53 +00:00
|
|
|
expect(reference_filter(act).to_html).to eq exp
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'mentioning @all' do
|
|
|
|
let(:reference) { User.reference_prefix + 'all' }
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.team << [project.creator, :developer]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports a special @all mention' do
|
2016-09-30 12:53:37 +00:00
|
|
|
project.team << [user, :developer]
|
2016-05-26 11:16:43 +00:00
|
|
|
doc = reference_filter("Hey #{reference}", author: user)
|
2016-09-30 12:53:37 +00:00
|
|
|
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.css('a').length).to eq 1
|
|
|
|
expect(doc.css('a').first.attr('href'))
|
|
|
|
.to eq urls.namespace_project_url(project.namespace, project)
|
|
|
|
end
|
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
it 'includes a data-author attribute when there is an author' do
|
2016-09-30 12:53:37 +00:00
|
|
|
project.team << [user, :developer]
|
2016-05-26 11:16:43 +00:00
|
|
|
doc = reference_filter(reference, author: user)
|
2015-12-24 14:33:51 +00:00
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
expect(doc.css('a').first.attr('data-author')).to eq(user.id.to_s)
|
2015-12-24 14:33:51 +00:00
|
|
|
end
|
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
it 'does not include a data-author attribute when there is no author' do
|
|
|
|
doc = reference_filter(reference)
|
2015-12-24 14:33:51 +00:00
|
|
|
|
2016-05-26 11:16:43 +00:00
|
|
|
expect(doc.css('a').first.has_attribute?('data-author')).to eq(false)
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
2016-09-30 12:53:37 +00:00
|
|
|
|
2016-10-01 09:17:56 +00:00
|
|
|
it 'ignores reference to all when the user is not a project member' do
|
2016-09-30 12:53:37 +00:00
|
|
|
doc = reference_filter("Hey #{reference}", author: user)
|
|
|
|
|
|
|
|
expect(doc.css('a').length).to eq 0
|
|
|
|
end
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'mentioning a user' do
|
|
|
|
it 'links to a User' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.css('a').first.attr('href')).to eq urls.user_url(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links to a User with a period' do
|
|
|
|
user = create(:user, name: 'alphA.Beta')
|
|
|
|
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{user.to_reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.css('a').length).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links to a User with an underscore' do
|
|
|
|
user = create(:user, name: 'ping_pong_king')
|
|
|
|
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{user.to_reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.css('a').length).to eq 1
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a data-user attribute' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first
|
|
|
|
|
|
|
|
expect(link).to have_attribute('data-user')
|
|
|
|
expect(link.attr('data-user')).to eq user.namespace.owner_id.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'mentioning a group' do
|
|
|
|
let(:group) { create(:group) }
|
|
|
|
let(:reference) { group.to_reference }
|
|
|
|
|
|
|
|
it 'links to the Group' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.css('a').first.attr('href')).to eq urls.group_url(group)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a data-group attribute' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}")
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first
|
|
|
|
|
|
|
|
expect(link).to have_attribute('data-group')
|
|
|
|
expect(link.attr('data-group')).to eq group.id.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links with adjacent text' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Mention me (#{reference}.)")
|
2015-11-19 15:18:13 +00:00
|
|
|
expect(doc.to_html).to match(/\(<a.+>#{reference}<\/a>\.\)/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes default classes' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}")
|
2016-07-17 00:03:56 +00:00
|
|
|
expect(doc.css('a').first.attr('class')).to eq 'gfm gfm-project_member has-tooltip'
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'supports an :only_path context' do
|
2015-12-07 13:48:53 +00:00
|
|
|
doc = reference_filter("Hey #{reference}", only_path: true)
|
2015-11-19 15:18:13 +00:00
|
|
|
link = doc.css('a').first.attr('href')
|
|
|
|
|
|
|
|
expect(link).not_to match %r(https?://)
|
|
|
|
expect(link).to eq urls.user_path(user)
|
|
|
|
end
|
2015-12-07 13:48:53 +00:00
|
|
|
|
|
|
|
context 'referencing a user in a link href' do
|
|
|
|
let(:reference) { %Q{<a href="#{user.to_reference}">User</a>} }
|
|
|
|
|
|
|
|
it 'links to a User' do
|
|
|
|
doc = reference_filter("Hey #{reference}")
|
|
|
|
expect(doc.css('a').first.attr('href')).to eq urls.user_url(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'links with adjacent text' do
|
|
|
|
doc = reference_filter("Mention me (#{reference}.)")
|
|
|
|
expect(doc.to_html).to match(/\(<a.+>User<\/a>\.\)/)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes a data-user attribute' do
|
|
|
|
doc = reference_filter("Hey #{reference}")
|
|
|
|
link = doc.css('a').first
|
|
|
|
|
|
|
|
expect(link).to have_attribute('data-user')
|
|
|
|
expect(link.attr('data-user')).to eq user.namespace.owner_id.to_s
|
|
|
|
end
|
|
|
|
end
|
2016-05-30 13:56:50 +00:00
|
|
|
|
|
|
|
describe '#namespaces' do
|
|
|
|
it 'returns a Hash containing all Namespaces' do
|
|
|
|
document = Nokogiri::HTML.fragment("<p>#{user.to_reference}</p>")
|
|
|
|
filter = described_class.new(document, project: project)
|
|
|
|
ns = user.namespace
|
|
|
|
|
|
|
|
expect(filter.namespaces).to eq({ ns.path => ns })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#usernames' do
|
|
|
|
it 'returns the usernames mentioned in a document' do
|
|
|
|
document = Nokogiri::HTML.fragment("<p>#{user.to_reference}</p>")
|
|
|
|
filter = described_class.new(document, project: project)
|
|
|
|
|
|
|
|
expect(filter.usernames).to eq([user.username])
|
|
|
|
end
|
|
|
|
end
|
2015-11-19 15:18:13 +00:00
|
|
|
end
|