FIX: tag input detects when a tag is not allowed and won't offer to create it anyway

This commit is contained in:
Neil Lalonde 2016-08-03 13:18:56 -04:00
parent e92f5e4fbf
commit 5f67cd7b45
3 changed files with 47 additions and 2 deletions

View file

@ -43,6 +43,8 @@ export default Ember.TextField.extend({
this.set('allowCreate', site.get('can_create_tag'));
}
this.set('termMatchesForbidden', false);
if (this.get('unlimitedTagCount')) {
limit = null;
} else if (this.get('limit')) {
@ -78,7 +80,7 @@ export default Ember.TextField.extend({
term = term.replace(filterRegexp, '').trim();
// No empty terms, make sure the user has permission to create the tag
if (!term.length || !self.get('allowCreate')) return;
if (!term.length || !self.get('allowCreate') || self.get('termMatchesForbidden')) return;
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
@ -119,6 +121,7 @@ export default Ember.TextField.extend({
if (self.siteSettings.tags_sort_alphabetically) {
data.results = data.results.sort(function(a,b) { return a.id > b.id; });
}
self.set('termMatchesForbidden', data.forbidden ? true : false);
return data;
}
},

View file

@ -156,7 +156,15 @@ class TagsController < ::ApplicationController
tags << { id: t.name, text: t.name, count: 0 }
end
render json: { results: tags }
json_response = { results: tags }
t = DiscourseTagging.clean_tag(params[:q])
if Tag.where(name: t).exists? && !tags.find { |h| h[:id] == t }
# filter_allowed_tags determined that the tag entered is not allowed
json_response[:forbidden] = t
end
render json: json_response
end
def notifications

View file

@ -39,4 +39,38 @@ describe TagsController do
end
end
end
describe 'search' do
context 'tagging disabled' do
it "returns 404" do
xhr :get, :search, q: 'stuff'
expect(response.status).to eq(404)
end
end
context 'tagging enabled' do
before do
SiteSetting.tagging_enabled = true
end
it "can return some tags" do
tag_names = ['stuff', 'stinky', 'stumped']
tag_names.each { |name| Fabricate(:tag, name: name) }
xhr :get, :search, q: 'stu'
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["results"].map{|j| j["id"]}.sort).to eq(['stuff', 'stumped'])
end
it "can say if given tag is not allowed" do
yup, nope = Fabricate(:tag, name: 'yup'), Fabricate(:tag, name: 'nope')
category = Fabricate(:category, tags: [yup])
xhr :get, :search, q: 'nope', categoryId: category.id
expect(response).to be_success
json = ::JSON.parse(response.body)
expect(json["results"].map{|j| j["id"]}.sort).to eq([])
expect(json["forbidden"]).to be_present
end
end
end
end