fix searching for users with canvas ids 1-99

refs CNVS-6069

test plan: at courses/X/users , search by canvas id for a user with an
id < 100 (less than three chars). you should be able to successfully
find the user, and you shouldn't get an error message if you type
another number < 100

Change-Id: Id1655da622ee1dab3df7aad82abfce01f379d828
Reviewed-on: https://gerrit.instructure.com/22290
Tested-by: Jenkins <jenkins@instructure.com>
Reviewed-by: Jacob Fugal <jacob@instructure.com>
QA-Review: Jeremy Putnam <jeremyp@instructure.com>
Product-Review: Brian Palmer <brianp@instructure.com>
This commit is contained in:
Brian Palmer 2013-07-12 15:25:33 -06:00
parent b216585323
commit cd165575af
16 changed files with 69 additions and 49 deletions

View File

@ -23,6 +23,7 @@ define [
onInputDelay: 200
modelAttribute: 'filter'
minLength: 3
allowSmallerNumbers: true
onInput: =>
if @el.value isnt @lastValue

View File

@ -38,7 +38,7 @@ define [
{value} = @el
# TODO this needs to be refactored out into some validation
# rules or something
if value and value.length < @options.minLength
if value and value.length < @options.minLength and !(@options.allowSmallerNumbers && value > 0)
return unless @options.setParamOnInvalid
value = false
@setParam value

View File

@ -38,7 +38,7 @@ define [
onFail: (xhr) ->
return if xhr.statusText is 'abort'
parsed = $.parseJSON xhr.responseText
message = if parsed.message is "search_term of 3 or more characters is required"
message = if parsed?.errors?[0].message is "3 or more characters is required"
I18n.t('greater_than_three', 'Please enter a search term with three or more characters')
else
I18n.t('unknown_error', 'Something went wrong with your search, please try again.')

View File

@ -52,7 +52,7 @@ define [
onFail: (xhr) =>
return if xhr.statusText is 'abort'
parsed = $.parseJSON xhr.responseText
message = if parsed?.message is "search_term of 3 or more characters is required"
message = if parsed?.errors?[0].message is "3 or more characters is required"
I18n.t('greater_than_three', 'Please enter a search term with three or more characters')
else
I18n.t('unknown_error', 'Something went wrong with your search, please try again.')

View File

@ -397,13 +397,7 @@ class CoursesController < ApplicationController
params[:per_page] ||= params.delete(:limit)
search_params = params.slice(:search_term, :enrollment_role, :enrollment_type)
if (search_term = search_params[:search_term]) && search_term.size < 3
return render \
:json => {
"status" => "argument_error",
"message" => "search_term of 3 or more characters is required" },
:status => :bad_request
end
search_term = search_params[:search_term].presence
if search_term
users = UserSearch.for_user_in_context(search_term, @context, @current_user, search_params)
@ -440,6 +434,8 @@ class CoursesController < ApplicationController
user_json(u, @current_user, session, includes, @context, enrollments)
}
end
rescue UserSearch::SearchTermTooShort => e
render json: e.error_json, status: :bad_request
end
# @API List recently logged in students

View File

@ -258,15 +258,7 @@ class GroupCategoriesController < ApplicationController
return unless authorized_action(@context, @current_user, :read)
end
search_term = params[:search_term]
if search_term && search_term.size < 3
return render \
:json => {
"status" => "argument_error",
"message" => "search_term of 3 or more characters is required" },
:status => :bad_request
end
search_term = params[:search_term].presence
search_params = params.slice(:search_term)
search_params[:enrollment_role] = "StudentEnrollment" if @context.is_a? Course
@ -283,6 +275,8 @@ class GroupCategoriesController < ApplicationController
users = Api.paginate(users, self, api_v1_group_category_users_url)
render :json => users.map { |u| user_json(u, @current_user, session, [], @context) }
rescue UserSearch::SearchTermTooShort => e
render json: e.error_json, status: :bad_request
end
# @API Assign unassigned members

View File

@ -579,14 +579,7 @@ class GroupsController < ApplicationController
def users
return unless authorized_action(@context, @current_user, :read)
search_term = params[:search_term]
if search_term && search_term.size < 3
return render \
:json => {
"status" => "argument_error",
"message" => "search_term of 3 or more characters is required" },
:status => :bad_request
end
search_term = params[:search_term].presence
if search_term
users = UserSearch.for_user_in_context(search_term, @context, @current_user)
@ -596,6 +589,8 @@ class GroupsController < ApplicationController
users = Api.paginate(users, self, api_v1_group_users_url)
render :json => users.map { |u| user_json(u, @current_user, session) }
rescue UserSearch::SearchTermTooShort => e
render json: e.error_json, status: :bad_request
end
def edit

View File

@ -213,14 +213,7 @@ class UsersController < ApplicationController
end
if api_request?
search_term = params[:search_term]
if search_term && search_term.size < 3
return render \
:json => {
"status" => "argument_error",
"message" => "search_term of 3 or more characters is required" },
:status => :bad_request
end
search_term = params[:search_term].presence
if search_term
users = UserSearch.for_user_in_context(search_term, @context, @current_user)
@ -258,6 +251,8 @@ class UsersController < ApplicationController
end
end
end
rescue UserSearch::SearchTermTooShort => e
render json: e.error_json, status: :bad_request
end

View File

@ -26,6 +26,7 @@
<ul>
<li>{{#t "name"}}Name{{/t}}</li>
<li>{{#t "login_name_or_sis_id"}}Login / SIS ID{{/t}}</li>
<li>{{#t "canvas_user_id"}}Canvas User ID{{/t}}</li>
</ul>
</div>
{{else}}

View File

@ -1,13 +1,36 @@
module UserSearch
MIN_SEARCH_TERM_LENGTH = 3
class SearchTermTooShort < ArgumentError
def error_json
{
"errors" => [{
"field" => "search_term",
"code" => "invalid",
"message" => "#{UserSearch::MIN_SEARCH_TERM_LENGTH} or more characters is required",
}]
}
end
end
def self.for_user_in_context(search_term, context, searcher, options = {})
search_term = search_term.to_s
base_scope = scope_for(context, searcher, options.slice(:enrollment_type, :enrollment_role, :exclude_groups))
if search_term.to_s =~ Api::ID_REGEX
user = base_scope.find_by_id(search_term)
return [user] if user
if user
return [user]
elsif search_term.length < MIN_SEARCH_TERM_LENGTH
return []
end
# no user found by id, so lets go ahead with the regular search, maybe this person just has a ton of numbers in their name
end
if search_term.length < MIN_SEARCH_TERM_LENGTH
raise SearchTermTooShort
end
base_scope.where(conditions_statement(search_term))
end

View File

@ -159,3 +159,9 @@ def should_process_incoming_user_content(context)
saved_content = yield incoming_content
saved_content.should == "<p>content blahblahblah <a href=\"/#{context.class.to_s.underscore.pluralize}/#{context.id}/files/#{@attachment.id}/download?a=1&amp;b=3\">haha</a></p>"
end
def verify_json_error(error, field, code, message = nil)
error["field"].should == field
error["code"].should == code
error["message"].should == message if message
end

View File

@ -1070,9 +1070,9 @@ describe CoursesController, :type => :integration do
end
it "returns an error when search_term is fewer than 3 characters" do
json = api_call(:get, api_url, api_route, {:search_term => '12'}, {}, :expected_status => 400)
json["status"].should == "argument_error"
json["message"].should == "search_term of 3 or more characters is required"
json = api_call(:get, api_url, api_route, {:search_term => 'ab'}, {}, :expected_status => 400)
error = json["errors"].first
verify_json_error(error, "search_term", "invalid", "3 or more characters is required")
end
it "returns a list of users" do

View File

@ -96,9 +96,9 @@ describe "Group Categories API", :type => :integration do
end
it "returns an error when search_term is fewer than 3 characters" do
json = api_call(:get, api_url, api_route, {:search_term => '12'}, {}, :expected_status => 400)
json["status"].should == "argument_error"
json["message"].should == "search_term of 3 or more characters is required"
json = api_call(:get, api_url, api_route, {:search_term => 'ab'}, {}, :expected_status => 400)
error = json["errors"].first
verify_json_error(error, "search_term", "invalid", "3 or more characters is required")
end
it "returns a list of users" do

View File

@ -644,9 +644,9 @@ describe "Groups API", :type => :integration do
end
it "returns an error when search_term is fewer than 3 characters" do
json = api_call(:get, api_url, api_route, {:search_term => '12'}, {}, :expected_status => 400)
json["status"].should == "argument_error"
json["message"].should == "search_term of 3 or more characters is required"
json = api_call(:get, api_url, api_route, {:search_term => 'ab'}, {}, :expected_status => 400)
error = json["errors"].first
verify_json_error(error, "search_term", "invalid", "3 or more characters is required")
end
it "returns a list of users" do

View File

@ -347,9 +347,9 @@ describe "Users API", :type => :integration do
it "returns an error when search_term is fewer than 3 characters" do
@account = Account.default
json = api_call(:get, "/api/v1/accounts/#{@account.id}/users", { :controller => 'users', :action => "index", :format => 'json', :account_id => @account.id.to_param }, {:search_term => '12'}, {}, :expected_status => 400)
json["status"].should == "argument_error"
json["message"].should == "search_term of 3 or more characters is required"
json = api_call(:get, "/api/v1/accounts/#{@account.id}/users", { :controller => 'users', :action => "index", :format => 'json', :account_id => @account.id.to_param }, {:search_term => 'ab'}, {}, :expected_status => 400)
error = json["errors"].first
verify_json_error(error, "search_term", "invalid", "3 or more characters is required")
end
it "returns a list of users filtered by search_term" do

View File

@ -91,4 +91,13 @@ define [
simulateKeyup()
equal view.model.get('filter'), false, 'filter attribute is false'
test 'updates filter with small number', ->
view.model = new Backbone.Model filter: 'foo'
view.options.allowSmallerNumbers = false
setValue '1'
simulateKeyup()
equal view.model.get('filter'), 'foo', 'filter attribute did not change'
view.options.allowSmallerNumbers = true
setValue '2'
simulateKeyup()
equal view.model.get('filter'), '2', 'filter attribute did change'