fix slug generation exception

refs VICE-739
flag=none

test plan:
  - tests pass

qa risk: low

Change-Id: I05265f2b28e4c635b96783f27c1ff588c5831162
Reviewed-on: https://gerrit.instructure.com/c/canvas-lms/+/246070
Tested-by: Service Cloud Jenkins <svc.cloudjenkins@instructure.com>
QA-Review: Caleb Guanzon <cguanzon@instructure.com>
Product-Review: Caleb Guanzon <cguanzon@instructure.com>
Reviewed-by: Rob Orton <rob@instructure.com>
This commit is contained in:
Davis Hyer 2020-08-26 07:59:54 -06:00
parent ae5ab6bfb6
commit 2f1587387a
5 changed files with 76 additions and 1 deletions

View File

@ -17,5 +17,6 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 3.5.0"
spec.add_dependency "swearjar", "~> 1.4"
end

View File

@ -28,6 +28,7 @@ class CanvasSlug
# Ensure we don't get naughties by looping until we get something
# "clean". Loop count is arbitrary, we use length as shorter strings
# are less likely to result in problematic strings.
uuid = ""
length.times do
uuid = Array.new(length) { CHARS[SecureRandom.random_number(CHARS.length)] }.join
return uuid unless SJ.profane?(uuid)
@ -35,7 +36,7 @@ class CanvasSlug
# TODO: raise exception to allow consumer to handle
# raise "CanvasSlug couldn't find valid uuid after #{length} attempts"
return uuid
uuid
end
def generate(purpose = nil, length = 4)

View File

@ -0,0 +1,43 @@
#
# Copyright (C) 2020 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#
require 'spec_helper'
describe CanvasSlug do
let(:subject) {CanvasSlug}
describe ".generate_securish_uuid" do
it "returns a securish uuid" do
expect(subject.generate_securish_uuid).to be_a(String)
end
it "works with length 0" do
expect(subject.generate_securish_uuid(0)).to eq ""
end
end
describe ".generate" do
it "returns a string" do
expect(subject.generate).to be_a(String)
end
it "prepends a provided purpose" do
expect(subject.generate("foobar")).to match /\Afoobar-\w{4}\z/
end
end
end

View File

@ -0,0 +1,25 @@
#
# Copyright (C) 2020 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
require 'canvas_slug'
RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.filter_run :focus
config.color = true
config.order = 'random'
end

5
gems/canvas_slug/test.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/bash
set -e
bundle check || bundle install
bundle exec rspec spec