This commit is contained in:
0
test/models/.keep
Normal file
0
test/models/.keep
Normal file
36
test/models/book/accessable_test.rb
Normal file
36
test/models/book/accessable_test.rb
Normal file
@@ -0,0 +1,36 @@
|
||||
require "test_helper"
|
||||
|
||||
class Book::AccessableTest < ActiveSupport::TestCase
|
||||
test "update_access always grants read access to everyone when everyone_access is set" do
|
||||
book = Book.create!(title: "My new book")
|
||||
book.update_access(editors: [], readers: [])
|
||||
|
||||
assert book.everyone_access?
|
||||
|
||||
User.all.each do |user|
|
||||
assert book.accessable?(user: user)
|
||||
assert_not book.editable?(user: user) unless user.administrator?
|
||||
end
|
||||
end
|
||||
|
||||
test "update_access updates existing access" do
|
||||
book = Book.create!(title: "My new book", everyone_access: false)
|
||||
|
||||
book.update_access(editors: [ users(:kevin).id ], readers: [])
|
||||
assert book.editable?(user: users(:kevin))
|
||||
|
||||
book.update_access(editors: [], readers: [ users(:kevin).id ])
|
||||
assert book.accessable?(user: users(:kevin))
|
||||
assert_not book.editable?(user: users(:kevin))
|
||||
end
|
||||
|
||||
test "update_access removes stale accesses" do
|
||||
book = Book.create!(title: "My new book", everyone_access: false)
|
||||
|
||||
book.update_access(editors: [ users(:kevin).id ], readers: [ users(:jz).id ])
|
||||
assert_equal 2, book.accesses.size
|
||||
|
||||
book.update_access(editors: [ users(:kevin).id ], readers: [])
|
||||
assert_equal 1, book.accesses.size
|
||||
end
|
||||
end
|
||||
16
test/models/book_test.rb
Normal file
16
test/models/book_test.rb
Normal file
@@ -0,0 +1,16 @@
|
||||
require "test_helper"
|
||||
|
||||
class BookTest < ActiveSupport::TestCase
|
||||
test "slug is generated from title" do
|
||||
book = Book.create!(title: "Hello, World!")
|
||||
assert_equal "hello-world", book.slug
|
||||
end
|
||||
|
||||
test "press a leafable" do
|
||||
leaf = books(:manual).press Page.new(body: "Important words"), title: "Introduction"
|
||||
|
||||
assert leaf.page?
|
||||
assert_equal "Important words", leaf.page.body.content.to_s
|
||||
assert_equal "Introduction", leaf.title
|
||||
end
|
||||
end
|
||||
37
test/models/first_run_test.rb
Normal file
37
test/models/first_run_test.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
require "test_helper"
|
||||
|
||||
class FirstRunTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
Book.destroy_all
|
||||
User.destroy_all
|
||||
Account.destroy_all
|
||||
end
|
||||
|
||||
test "creating makes first user an administrator" do
|
||||
user = create_first_run_user
|
||||
assert user.administrator?
|
||||
end
|
||||
|
||||
test "creates an account" do
|
||||
assert_changes -> { Account.count }, +1 do
|
||||
create_first_run_user
|
||||
end
|
||||
end
|
||||
|
||||
test "creates a demo book" do
|
||||
assert_changes -> { Book.count }, to: 1 do
|
||||
create_first_run_user
|
||||
end
|
||||
|
||||
book = Book.first
|
||||
|
||||
assert book.editable?(user: User.first)
|
||||
assert book.cover.attached?
|
||||
assert book.leaves.any?
|
||||
end
|
||||
|
||||
private
|
||||
def create_first_run_user
|
||||
FirstRun.create!({ name: "User", email_address: "user@example.com", password: "secret123456" })
|
||||
end
|
||||
end
|
||||
66
test/models/leaf/editable_test.rb
Normal file
66
test/models/leaf/editable_test.rb
Normal file
@@ -0,0 +1,66 @@
|
||||
require "test_helper"
|
||||
|
||||
class Leaf::EditableTest < ActiveSupport::TestCase
|
||||
test "editing a leafable records the edit" do
|
||||
leaves(:welcome_page).edit leafable_params: { body: "New body" }
|
||||
|
||||
assert_equal "New body", leaves(:welcome_page).page.body.content
|
||||
|
||||
assert leaves(:welcome_page).edits.last.revision?
|
||||
assert_equal "This is _such_ a great handbook.", leaves(:welcome_page).edits.last.page.body.content
|
||||
end
|
||||
|
||||
test "edits that are close together don't create new revisions" do
|
||||
assert_difference -> { leaves(:welcome_page).edits.count }, +1 do
|
||||
leaves(:welcome_page).edit leafable_params: { body: "First change" }
|
||||
end
|
||||
|
||||
freeze_time
|
||||
travel 1.minute
|
||||
|
||||
assert_no_difference -> { leaves(:welcome_page).edits.count } do
|
||||
leaves(:welcome_page).edit leafable_params: { body: "Second change" }
|
||||
end
|
||||
|
||||
assert_equal "Second change", leaves(:welcome_page).page.body.content
|
||||
assert_equal Time.now, leaves(:welcome_page).edits.last.updated_at
|
||||
|
||||
travel 1.hour
|
||||
|
||||
assert_difference -> { leaves(:welcome_page).edits.count }, +1 do
|
||||
leaves(:welcome_page).edit leafable_params: { body: "Third change" }
|
||||
end
|
||||
end
|
||||
|
||||
test "changing a leaf title doesn't create a revision" do
|
||||
assert_no_difference -> { Edit.count } do
|
||||
leaves(:welcome_page).edit leaf_params: { title: "New title" }
|
||||
end
|
||||
|
||||
assert_equal "New title", leaves(:welcome_page).title
|
||||
end
|
||||
|
||||
test "changes that don't affect the leafable don't create a revision" do
|
||||
assert_no_difference -> { Edit.count } do
|
||||
leaves(:welcome_page).edit leafable_params: {}
|
||||
end
|
||||
end
|
||||
|
||||
test "editing a leafable with an attachment includes the attachments in the new version" do
|
||||
assert leaves(:reading_picture).picture.image.attached?
|
||||
|
||||
leaves(:reading_picture).edit leaf_params: { title: "New title" }
|
||||
|
||||
assert_equal "New title", leaves(:reading_picture).title
|
||||
assert leaves(:reading_picture).picture.image.attached?
|
||||
end
|
||||
|
||||
test "trashing a leaf records the edit" do
|
||||
leaves(:welcome_page).trashed!
|
||||
|
||||
assert leaves(:welcome_page).trashed?
|
||||
|
||||
assert leaves(:welcome_page).edits.last.trash?
|
||||
assert_equal "This is _such_ a great handbook.", leaves(:welcome_page).edits.last.page.body.content
|
||||
end
|
||||
end
|
||||
96
test/models/leaf/positionable_test.rb
Normal file
96
test/models/leaf/positionable_test.rb
Normal file
@@ -0,0 +1,96 @@
|
||||
require "test_helper"
|
||||
|
||||
class Leaf::PositionableTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@leaves = books(:handbook).leaves.positioned
|
||||
end
|
||||
|
||||
test "items are sorted in positioned order" do
|
||||
assert_equal [ leaves(:welcome_section), leaves(:welcome_page), leaves(:summary_page), leaves(:reading_picture) ], @leaves
|
||||
end
|
||||
|
||||
test "items can be moved earlier" do
|
||||
leaves(:welcome_page).move_to_position(0)
|
||||
|
||||
assert_equal [ leaves(:welcome_page), leaves(:welcome_section), leaves(:summary_page), leaves(:reading_picture) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "items can be moved beyond the start, which puts them at the start" do
|
||||
leaves(:welcome_page).move_to_position(-99)
|
||||
|
||||
assert_equal [ leaves(:welcome_page), leaves(:welcome_section), leaves(:summary_page), leaves(:reading_picture) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "items can be moved later" do
|
||||
leaves(:welcome_section).move_to_position(2)
|
||||
|
||||
assert_equal [ leaves(:welcome_page), leaves(:summary_page), leaves(:welcome_section), leaves(:reading_picture) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "items can be moved beyond the end, which puts them at the end" do
|
||||
leaves(:welcome_section).move_to_position(99)
|
||||
|
||||
assert_equal [ leaves(:welcome_page), leaves(:summary_page), leaves(:reading_picture), leaves(:welcome_section) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "items can be moved to their existing position" do
|
||||
leaves(:welcome_page).move_to_position(1)
|
||||
|
||||
assert_equal [ leaves(:welcome_section), leaves(:welcome_page), leaves(:summary_page), leaves(:reading_picture) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "items can be moved in blocks" do
|
||||
leaves(:welcome_section).move_to_position(1, followed_by: [ leaves(:welcome_page), leaves(:summary_page) ])
|
||||
|
||||
assert_equal [ leaves(:reading_picture), leaves(:welcome_section), leaves(:welcome_page), leaves(:summary_page) ], @leaves.reload
|
||||
end
|
||||
|
||||
test "new items are inserted at the end" do
|
||||
new_page = books(:handbook).press Page.new(body: "New Page"), title: "New Page"
|
||||
|
||||
assert_equal new_page, books(:handbook).leaves.positioned.last
|
||||
end
|
||||
|
||||
test "the first item in the collection has the expected score" do
|
||||
books(:handbook).leaves.destroy_all
|
||||
new_page = books(:handbook).press Page.new(body: "New Page"), title: "New Page"
|
||||
|
||||
assert_equal 1, new_page.position_score
|
||||
end
|
||||
|
||||
test "positioning is rebalanced when necessary" do
|
||||
leaves(:welcome_section).update!(position_score: 1e-11)
|
||||
leaves(:welcome_page).update!(position_score: 2e-11)
|
||||
|
||||
leaves(:summary_page).move_to_position(1)
|
||||
|
||||
assert_equal leaves(:summary_page), @leaves.reload.second
|
||||
assert_equal [ 1, 2, 3, 4 ], @leaves.pluck(:position_score)
|
||||
end
|
||||
|
||||
test "items know their neighbours" do
|
||||
assert_equal leaves(:welcome_section), leaves(:welcome_page).previous
|
||||
assert_equal leaves(:summary_page), leaves(:welcome_page).next
|
||||
|
||||
assert_nil leaves(:welcome_section).previous
|
||||
assert_nil leaves(:reading_picture).next
|
||||
end
|
||||
|
||||
test "only active items are included as neighbours" do
|
||||
assert_equal leaves(:summary_page), leaves(:welcome_page).next
|
||||
|
||||
leaves(:summary_page).trashed!
|
||||
|
||||
assert_equal leaves(:reading_picture), leaves(:welcome_page).next
|
||||
end
|
||||
|
||||
test "only active items are counted when determining position" do
|
||||
leaves(:welcome_page).trashed!
|
||||
|
||||
leaves(:welcome_section).move_to_position(1)
|
||||
assert_equal [ leaves(:summary_page), leaves(:welcome_section), leaves(:reading_picture) ], @leaves.reload.active
|
||||
|
||||
leaves(:welcome_section).move_to_position(0)
|
||||
assert_equal [ leaves(:welcome_section), leaves(:summary_page), leaves(:reading_picture) ], @leaves.reload.active
|
||||
end
|
||||
end
|
||||
13
test/models/leaf_test.rb
Normal file
13
test/models/leaf_test.rb
Normal file
@@ -0,0 +1,13 @@
|
||||
require "test_helper"
|
||||
|
||||
class LeafTest < ActiveSupport::TestCase
|
||||
test "slug is generated from title" do
|
||||
leaf = Leaf.new(title: "Hello, World!")
|
||||
assert_equal "hello-world", leaf.slug
|
||||
end
|
||||
|
||||
test "slug is never completely blank" do
|
||||
leaf = Leaf.new(title: "")
|
||||
assert_equal "-", leaf.slug
|
||||
end
|
||||
end
|
||||
10
test/models/page_test.rb
Normal file
10
test/models/page_test.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require "test_helper"
|
||||
|
||||
class PageTest < ActiveSupport::TestCase
|
||||
test "html preview" do
|
||||
page = Page.new(body: "# Hello\n\nWorld!")
|
||||
|
||||
assert_match /<h1>Hello<\/h1>/, page.html_preview
|
||||
assert_match /<p>World!<\/p>/, page.html_preview
|
||||
end
|
||||
end
|
||||
11
test/models/qr_code_link_test.rb
Normal file
11
test/models/qr_code_link_test.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
require "test_helper"
|
||||
|
||||
class QrCodeLinkTest < ActiveSupport::TestCase
|
||||
test "links can be signed and verified" do
|
||||
link = QrCodeLink.new "https://example.com"
|
||||
signed_link = link.signed
|
||||
|
||||
verified = QrCodeLink.from_signed(signed_link)
|
||||
assert_equal link.url, verified.url
|
||||
end
|
||||
end
|
||||
14
test/models/user/role_test.rb
Normal file
14
test/models/user/role_test.rb
Normal file
@@ -0,0 +1,14 @@
|
||||
require "test_helper"
|
||||
|
||||
class User::RoleTest < ActiveSupport::TestCase
|
||||
test "creating users makes them members by default" do
|
||||
assert User.create!(name: "User", email_address: "user@example.com", password: "secret123456").member?
|
||||
end
|
||||
|
||||
test "can_administer?" do
|
||||
assert User.new(role: :administrator).can_administer?
|
||||
|
||||
assert_not User.new(role: :member).can_administer?
|
||||
assert_not User.new.can_administer?
|
||||
end
|
||||
end
|
||||
20
test/models/user_test.rb
Normal file
20
test/models/user_test.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
require "test_helper"
|
||||
|
||||
class UserTest < ActiveSupport::TestCase
|
||||
test "user does not prevent very long passwords" do
|
||||
users(:david).update(password: "secret" * 50)
|
||||
assert users(:david).valid?
|
||||
end
|
||||
|
||||
test "new users get access to everyone books" do
|
||||
everyone_book = Book.create!(title: "My new book", everyone_access: true)
|
||||
other_book = Book.create!(title: "My secret book", everyone_access: false)
|
||||
|
||||
bob = User.create!(email_address: "bob@example.com", name: "Bob", password: "secret123456")
|
||||
|
||||
assert everyone_book.accessable?(user: bob)
|
||||
assert_not everyone_book.editable?(user: bob)
|
||||
|
||||
assert_not other_book.accessable?(user: bob)
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user