Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more tests for Node (for note and wiki page titles) #6366

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions app/models/node.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
class UniqueUrlValidator < ActiveModel::Validator
def validate(record)
if record.title == '' || record.title.nil?
# record.errors[:base] << "You must provide a title."
if record.title.blank?
record.errors[:base] << "You must provide a title."
# otherwise the below title uniqueness check fails, as title presence validation doesn't run until after
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oddly, tests are passing whether or not the title is checked for blankness!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really needed? Isn't it covered by:
validates title, presence: true, .... (on line 97)

elsif record.type == 'page'
array = %w(create edit update delete new)
array.each do |x|
if record.title == x
record.errors[:base] << "You may not use the title '" + x + "'"
end
if array.include? record.title.downcase
record.errors[:base] << "You may not use the title '#{record.title}'"
end
else
if !Node.where(path: record.generate_path).first.nil? && record.type == 'note'
Expand Down Expand Up @@ -96,7 +94,7 @@ def updated_month

belongs_to :user, foreign_key: 'uid'

validates :title, presence: true
validates :title, presence: true, length: { minimum: 3 }
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyone thinks the minimum length of the title should be longer/shorter?

validates_with UniqueUrlValidator, on: :create

scope :published, -> { where(status: 1) }
Expand Down
2 changes: 1 addition & 1 deletion test/functional/notes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ def teardown
assert_response :success
assert_not_nil @response.body
json = JSON.parse(@response.body)
assert_equal ["can't be blank"], json['title']
assert_equal ["can't be blank", "is too short (minimum is 3 characters)"], json['title']
assert !json['title'].empty?
end

Expand Down
35 changes: 35 additions & 0 deletions test/unit/node_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,41 @@ def setup
assert_equal 'Wiki page content/body', node.body
end

test 'wikipage with wrong title should not be created' do
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please compare this with the test on line 190

node = Node.new(uid: users(:bob).id,
type: 'page')
words = %w(create update delete new edit)
words.each do |word|
node.title = word.capitalize
assert_not node.save
end
end

test 'research note with empty/blank title should not be created' do
node = Node.new(uid: users(:bob).id,
type: 'note')
titles = [ '', ' ' * 5 ]
titles.each do |t|
node.title = t
assert_not node.valid?
end
end

test 'research note with duplicate title should not be created' do
node = Node.new(uid: users(:bob).id,
type: 'note',
title: 'My research note')
dup_node = node.dup
node.save
assert_not dup_node.save
end

test 'title should not be too short' do
node = Node.new(uid: users(:bob).id,
type: 'note',
title: 'ok')
assert_not node.valid?
end
test 'create a node_revision' do
# in testing, uid and id should be matched, although this is not yet true in production db
revision_count = nodes(:one).revisions.length
Expand Down