summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/controllers/content_controller_test.rb3
-rw-r--r--test/controllers/node_actions_controller_test.rb51
-rw-r--r--test/controllers/nodes_controller_test.rb14
-rw-r--r--test/fixtures/nodes.yml10
-rw-r--r--test/models/concerns/rrule_humanizer_test.rb25
-rw-r--r--test/models/helpers/node_actions_helper_test.rb107
-rw-r--r--test/models/node_action_test.rb16
-rw-r--r--test/models/node_test.rb100
-rw-r--r--test/models/page_test.rb32
-rw-r--r--test/models/user_test.rb72
10 files changed, 416 insertions, 14 deletions
diff --git a/test/controllers/content_controller_test.rb b/test/controllers/content_controller_test.rb
index e8bc55f..2449a0c 100644
--- a/test/controllers/content_controller_test.rb
+++ b/test/controllers/content_controller_test.rb
@@ -73,8 +73,7 @@ class ContentControllerTest < ActionController::TestCase
73 def test_nonexistant_custom_template_defaults_to_standard_template 73 def test_nonexistant_custom_template_defaults_to_standard_template
74 new_node = create_node_under_root "fnord" 74 new_node = create_node_under_root "fnord"
75 draft = find_or_create_draft(new_node, @user1) 75 draft = find_or_create_draft(new_node, @user1)
76 draft.template_name = "huchibu" 76 draft.update_column(:template_name, "huchibu")
77 draft.save
78 new_node.publish_draft! 77 new_node.publish_draft!
79 78
80 get :render_page, params: { :locale => 'de', :page_path => ["fnord"] } 79 get :render_page, params: { :locale => 'de', :page_path => ["fnord"] }
diff --git a/test/controllers/node_actions_controller_test.rb b/test/controllers/node_actions_controller_test.rb
new file mode 100644
index 0000000..8bc68ec
--- /dev/null
+++ b/test/controllers/node_actions_controller_test.rb
@@ -0,0 +1,51 @@
1require 'test_helper'
2
3class NodeActionsControllerTest < ActionController::TestCase
4
5 def setup
6 login_as :quentin
7 end
8
9 def logged_node slug, title
10 node = Node.root.children.create!(:slug => slug)
11 Globalize.with_locale(I18n.default_locale) { node.draft.update!(:title => title) }
12 node
13 end
14
15 test "index renders" do
16 get :index
17 assert_response :success
18 end
19
20 test "index filters by node" do
21 node_a = logged_node("log_filter_a", "Alpha Subject")
22 node_b = logged_node("log_filter_b", "Beta Subject")
23 NodeAction.record!(:node => node_a, :action => "create", :user => users(:quentin))
24 NodeAction.record!(:node => node_b, :action => "create", :user => users(:quentin))
25
26 get :index, params: { :node_id => node_a.id }
27
28 assert_response :success
29 assert_includes @response.body, "Alpha Subject"
30 assert_not_includes @response.body, "Beta Subject"
31 end
32
33 test "index filters by user" do
34 node = logged_node("log_filter_user", "Gamma Subject")
35 NodeAction.record!(:node => node, :action => "create", :user => users(:quentin))
36
37 get :index, params: { :user_id => users(:quentin).id }
38 assert_includes @response.body, "Gamma Subject"
39
40 other = User.where.not(:id => users(:quentin).id).first
41 get :index, params: { :user_id => other.id }
42 assert_not_includes @response.body, "Gamma Subject"
43 end
44
45 test "index requires login" do
46 session[:user_id] = nil
47 @controller.instance_variable_set(:@current_user, nil)
48 get :index
49 assert_response :redirect
50 end
51end
diff --git a/test/controllers/nodes_controller_test.rb b/test/controllers/nodes_controller_test.rb
index 5b66bd3..b0d7416 100644
--- a/test/controllers/nodes_controller_test.rb
+++ b/test/controllers/nodes_controller_test.rb
@@ -140,7 +140,7 @@ class NodesControllerTest < ActionController::TestCase
140 :page => { 140 :page => {
141 :title => "Hello", 141 :title => "Hello",
142 :body => "There", 142 :body => "There",
143 :template_name => "Foobar" 143 :template_name => "title_only"
144 } 144 }
145 } 145 }
146 146
@@ -148,7 +148,17 @@ class NodesControllerTest < ActionController::TestCase
148 test_node.reload 148 test_node.reload
149 assert_equal "Hello", test_node.head.title 149 assert_equal "Hello", test_node.head.title
150 assert_equal "There", test_node.head.body 150 assert_equal "There", test_node.head.body
151 assert_equal "Foobar", test_node.head.template_name 151 assert_equal "title_only", test_node.head.template_name
152 end
153
154 def test_update_rejects_a_template_name_not_on_disk
155 test_node = Node.root.children.create! :slug => "test_node"
156 login_as :quentin
157 put :update, params: { :id => test_node.id,
158 :page => { :title => "Hello", :template_name => "Foobar" } }
159
160 test_node.reload
161 assert_not_equal "Foobar", test_node.draft&.template_name
152 end 162 end
153 163
154 test "publish draft with staged_slug unqueal slug" do 164 test "publish draft with staged_slug unqueal slug" do
diff --git a/test/fixtures/nodes.yml b/test/fixtures/nodes.yml
index 9ca325e..e94f10f 100644
--- a/test/fixtures/nodes.yml
+++ b/test/fixtures/nodes.yml
@@ -2,8 +2,6 @@
2 2
3root: 3root:
4 id: 1 4 id: 1
5 lft: 1
6 rgt: 10
7 parent_id: 5 parent_id:
8 slug: 6 slug:
9 unique_name: 7 unique_name:
@@ -11,8 +9,6 @@ root:
11 9
12first_child: 10first_child:
13 id: 2 11 id: 2
14 lft: 2
15 rgt: 3
16 parent_id: 1 12 parent_id: 1
17 draft_id: 100 13 draft_id: 100
18 slug: first_child 14 slug: first_child
@@ -20,8 +16,6 @@ first_child:
20 16
21second_child: 17second_child:
22 id: 3 18 id: 3
23 lft: 4
24 rgt: 5
25 parent_id: 1 19 parent_id: 1
26 draft_id: 101 20 draft_id: 101
27 slug: second_child 21 slug: second_child
@@ -29,8 +23,6 @@ second_child:
29 23
30third_child: 24third_child:
31 id: 4 25 id: 4
32 lft: 6
33 rgt: 7
34 parent_id: 1 26 parent_id: 1
35 draft_id: 102 27 draft_id: 102
36 slug: third_child 28 slug: third_child
@@ -38,8 +30,6 @@ third_child:
38 30
39fourth_child: 31fourth_child:
40 id: 5 32 id: 5
41 lft: 8
42 rgt: 9
43 parent_id: 1 33 parent_id: 1
44 slug: fourth_child 34 slug: fourth_child
45 unique_name: fourth_child 35 unique_name: fourth_child
diff --git a/test/models/concerns/rrule_humanizer_test.rb b/test/models/concerns/rrule_humanizer_test.rb
index 279ff73..d747171 100644
--- a/test/models/concerns/rrule_humanizer_test.rb
+++ b/test/models/concerns/rrule_humanizer_test.rb
@@ -47,6 +47,24 @@ class RruleHumanizerTest < ActiveSupport::TestCase
47 assert_equal "Every second-to-last Thursday of the month", humanize("FREQ=MONTHLY;BYDAY=-2TH", :en) 47 assert_equal "Every second-to-last Thursday of the month", humanize("FREQ=MONTHLY;BYDAY=-2TH", :en)
48 end 48 end
49 49
50 test "monthly selected weeks" do
51 assert_equal "Jeden ersten, zweiten und fünften Dienstag im Monat",
52 humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU")
53 assert_equal "Every first, second and fifth Tuesday of the month",
54 humanize("FREQ=MONTHLY;BYDAY=1TU,2TU,5TU", :en)
55 end
56
57 test "monthly fifth weekday" do
58 assert_equal "Jeden fünften Dienstag im Monat", humanize("FREQ=MONTHLY;BYDAY=5TU")
59 assert_equal "Every fifth Tuesday of the month", humanize("FREQ=MONTHLY;BYDAY=5TU", :en)
60 end
61
62 test "monthly weekday excluding one ordinal" do
63 rrule = "FREQ=MONTHLY;BYDAY=1TU,3TU,4TU,5TU"
64 assert_equal "Jeden Dienstag im Monat, außer dem zweiten Dienstag", humanize(rrule)
65 assert_equal "Every Tuesday of the month, except the second Tuesday", humanize(rrule, :en)
66 end
67
50 test "monthly no byday" do 68 test "monthly no byday" do
51 assert_equal "Monatlich", humanize("FREQ=MONTHLY") 69 assert_equal "Monatlich", humanize("FREQ=MONTHLY")
52 assert_equal "Monthly", humanize("FREQ=MONTHLY", :en) 70 assert_equal "Monthly", humanize("FREQ=MONTHLY", :en)
@@ -96,4 +114,11 @@ class RruleHumanizerTest < ActiveSupport::TestCase
96 test "wday_abbr falls back to :de for an unrecognized locale" do 114 test "wday_abbr falls back to :de for an unrecognized locale" do
97 assert_equal "Mo", RruleHumanizer.wday_abbr(Time.parse("2026-07-06"), :fr) 115 assert_equal "Mo", RruleHumanizer.wday_abbr(Time.parse("2026-07-06"), :fr)
98 end 116 end
117
118 test "monthly two selected weeks" do
119 assert_equal "Jeden ersten und dritten Dienstag im Monat",
120 humanize("FREQ=MONTHLY;BYDAY=1TU,3TU")
121 assert_equal "Every first and third Tuesday of the month",
122 humanize("FREQ=MONTHLY;BYDAY=1TU,3TU", :en)
123 end
99end 124end
diff --git a/test/models/helpers/node_actions_helper_test.rb b/test/models/helpers/node_actions_helper_test.rb
new file mode 100644
index 0000000..5244c19
--- /dev/null
+++ b/test/models/helpers/node_actions_helper_test.rb
@@ -0,0 +1,107 @@
1require 'test_helper'
2
3class NodeActionsHelperTest < ActionView::TestCase
4 def setup
5 @original_locale = I18n.locale
6 I18n.locale = :en
7 end
8
9 def default_url_options
10 { :locale => nil }
11 end
12
13 def teardown
14 I18n.locale = @original_locale
15 end
16
17 def entry action, metadata = {}, node: nil, user: nil, page: nil
18 NodeAction.create!(:node => node, :user => user, :page => page,
19 :action => action, :occurred_at => Time.now,
20 :metadata => { "username" => "quentin",
21 "human_readable_node_name" => "Subject" }.merge(metadata))
22 end
23
24 test "publish renders the revision sentence, not the titles" do
25 out = action_summary(entry("publish",
26 { "via" => "draft", "title" => { "from" => "Old", "to" => "New" } }))
27
28 assert_includes out, "quentin"
29 assert_includes out, "new revision"
30 assert_not_includes out, "Old"
31 end
32
33 test "first publish uses its own sentence" do
34 out = action_summary(entry("publish",
35 { "via" => "draft", "title" => { "from" => nil, "to" => "New" } }))
36
37 assert_includes out, "for the first time"
38 end
39
40 test "rollback publishes get the rollback sentence" do
41 out = action_summary(entry("publish",
42 { "via" => "revision", "title" => { "from" => "Now", "to" => "Then" } }))
43
44 assert_includes out, "rolled"
45 end
46
47 test "move renders the path pair" do
48 out = action_summary(entry("move",
49 { "path" => { "from" => "a/b", "to" => "a/c" } }))
50
51 assert_includes out, "a/b"
52 assert_includes out, "a/c"
53 end
54
55 test "unknown verbs degrade to a generic sentence, never an error" do
56 out = action_summary(entry("frobnicate"))
57
58 assert_includes out, "frobnicate"
59 assert_includes out, "quentin"
60 end
61
62 test "dead references render as plain names from metadata, no links" do
63 out = action_summary(entry("publish",
64 { "title" => { "from" => "Old", "to" => "New" } }))
65
66 assert_not_includes out, "<a "
67 assert_includes out, "Subject"
68 end
69
70 test "metadata values are escaped" do
71 out = action_summary(entry("publish",
72 { "human_readable_node_name" => "<b>bold</b>" }))
73
74 assert_not_includes out, "<b>"
75 end
76
77 test "live associations upgrade names to links" do
78 out = action_summary(entry("publish",
79 { "title" => { "from" => "Old", "to" => "New" } },
80 :user => users(:quentin)))
81
82 assert_includes out, "<a "
83 end
84
85 test "details are guarded off when nothing but an unchanged title is present" do
86 unchanged = entry("publish", { "title" => { "from" => "Same", "to" => "Same" } })
87 changed = entry("publish", { "title" => { "from" => "Old", "to" => "New" } })
88
89 assert_not action_details?(unchanged)
90 assert action_details?(changed)
91 end
92
93 test "first publish with a byline names the author" do
94 out = action_summary(entry("publish",
95 { "title" => { "from" => nil, "to" => "New" },
96 "author" => { "from" => nil, "to" => "quentin" } }))
97
98 assert_includes out, "author"
99 end
100
101 test "create entries with their flat title render and are guarded off details" do
102 e = entry("create", { "title" => "Initial", "path" => "a/b" })
103
104 assert_includes action_summary(e), "a/b"
105 assert_not action_details?(e)
106 end
107end
diff --git a/test/models/node_action_test.rb b/test/models/node_action_test.rb
index 24d8245..b177cca 100644
--- a/test/models/node_action_test.rb
+++ b/test/models/node_action_test.rb
@@ -18,6 +18,12 @@ class NodeActionTest < ActiveSupport::TestCase
18 assert_equal({ :title => { "from" => nil, "to" => "Erstausgabe" } }, diff) 18 assert_equal({ :title => { "from" => nil, "to" => "Erstausgabe" } }, diff)
19 end 19 end
20 20
21 test "first publish carries the byline when the page has one" do
22 diff = NodeAction.head_diff(nil, build_page(:user => users(:quentin)))
23
24 assert_equal({ "from" => nil, "to" => users(:quentin).login }, diff[:author])
25 end
26
21 test "title pair is always present, even when unchanged" do 27 test "title pair is always present, even when unchanged" do
22 diff = NodeAction.head_diff(build_page, build_page) 28 diff = NodeAction.head_diff(build_page, build_page)
23 29
@@ -109,4 +115,14 @@ class NodeActionTest < ActiveSupport::TestCase
109 115
110 assert_nil NodeAction.head_diff(old_page, new_page)[:translation_diff] 116 assert_nil NodeAction.head_diff(old_page, new_page)[:translation_diff]
111 end 117 end
118
119 test "record! passes provenance and historical timestamps through" do
120 long_ago = 2.years.ago
121 action = NodeAction.record!(:node => nil, :action => "publish",
122 :occurred_at => long_ago,
123 :inferred_from => "from_page_revision")
124
125 assert_equal "from_page_revision", action.inferred_from
126 assert_in_delta long_ago.to_f, action.occurred_at.to_f, 1.0
127 end
112end 128end
diff --git a/test/models/node_test.rb b/test/models/node_test.rb
index c8e49e5..3fb2d23 100644
--- a/test/models/node_test.rb
+++ b/test/models/node_test.rb
@@ -761,4 +761,104 @@ class NodeTest < ActiveSupport::TestCase
761 assert_equal "Second", action.metadata.dig("title", "from") 761 assert_equal "Second", action.metadata.dig("title", "from")
762 assert_equal "First", action.metadata.dig("title", "to") 762 assert_equal "First", action.metadata.dig("title", "to")
763 end 763 end
764
765 test "default_template_name rejects values not present in the template directory" do
766 node = Node.root.children.build(:slug => "template_guard_test",
767 :default_template_name => "../../layouts/admin")
768 assert_not node.valid?
769
770 node.default_template_name = "standard_template"
771 assert node.valid?
772 end
773
774 test "destroying a node with children is refused" do
775 parent = Node.root.children.create!(:slug => "destroy_guard_parent")
776 parent.children.create!(:slug => "destroy_guard_child")
777
778 assert_no_difference "Node.count" do
779 assert_not parent.destroy
780 end
781 assert parent.errors[:base].any?
782 end
783
784 test "destroying a childless node leaves no orphaned pages or asset links" do
785 node = create_node_with_published_page
786 page_ids = node.pages.pluck(:id)
787 asset = Asset.create!(:name => "destroy cascade probe",
788 :upload_file_name => "test_image.png",
789 :upload_content_type => "image/png",
790 :upload_file_size => 49854,
791 :upload_updated_at => Time.current)
792 node.head.related_assets.create!(:asset_id => asset.id, :position => 1)
793
794 node.destroy
795
796 assert_equal 0, Page.where(:id => page_ids).count
797 assert_equal 0, RelatedAsset.where(:page_id => page_ids).count
798 end
799
800 test "destroying a node also removes its autosave page" do
801 node = create_node_with_published_page
802 node.lock_for_editing!(@user1)
803 node.autosave!({:title => "in flight"}, @user1)
804 autosave_id = node.autosave_id
805
806 node.destroy
807
808 assert_equal 0, Page.where(:id => autosave_id).count
809 end
810
811 test "Node.trash lazily creates the container exactly once" do
812 assert_difference "Node.count", 1 do
813 Node.trash
814 end
815 assert_no_difference "Node.count" do
816 assert_equal Node.trash, Node.trash
817 end
818 assert Node.trash.trash_node?
819 assert_not Node.trash.in_trash?
820 end
821
822 test "in_trash? walks the whole parent chain" do
823 child = Node.trash.children.create!(:slug => "trashed_thing")
824 grandchild = child.children.create!(:slug => "trashed_deeper")
825
826 assert child.in_trash?
827 assert grandchild.in_trash?
828 assert_not Node.root.children.create!(:slug => "living_thing").in_trash?
829 end
830
831 test "the reserved slug is refused for other root children, live and staged" do
832 Node.trash
833
834 assert_not Node.root.children.build(:slug => CccConventions::TRASH_SLUG).valid?
835 assert_not Node.root.children.build(:slug => "fine", :staged_slug => CccConventions::TRASH_SLUG).valid?
836 assert Node.trash.children.create!(:slug => "sub").children.build(:slug => CccConventions::TRASH_SLUG).valid?
837 end
838
839 test "the Trash node refuses rename, move, and destroy" do
840 trash = Node.trash
841 other = Node.root.children.create!(:slug => "not_the_trash")
842
843 trash.slug = "recycling"
844 assert_not trash.valid?
845
846 trash.reload.parent_id = other.id
847 assert_not trash.valid?
848
849 assert_no_difference "Node.count" do
850 assert_not trash.reload.destroy
851 end
852 end
853
854 test "a head cannot exist inside the Trash, and publishing there is refused" do
855 node = Node.trash.children.create!(:slug => "no_publish_here")
856 page = node.pages.create!
857
858 node.head = page
859 assert_not node.valid?
860
861 node.reload
862 assert_raises(ActiveRecord::RecordInvalid) { node.publish_draft! }
863 end
764end 864end
diff --git a/test/models/page_test.rb b/test/models/page_test.rb
index edb7c37..58794d5 100644
--- a/test/models/page_test.rb
+++ b/test/models/page_test.rb
@@ -370,4 +370,36 @@ class PageTest < ActiveSupport::TestCase
370 assert en_entry[:changed] 370 assert en_entry[:changed]
371 refute en_entry[:exists_there] 371 refute en_entry[:exists_there]
372 end 372 end
373
374 test "aggregate ignores order_by values outside the allowlist" do
375 sql = Page.aggregate(:order_by => "pages.id; DROP TABLE pages--").to_sql
376
377 assert_not_includes sql, "DROP"
378 assert_includes sql, "pages.id ASC"
379 end
380
381 test "aggregate accepts allowlisted order columns, bare or prefixed" do
382 assert_includes Page.aggregate(:order_by => "published_at").to_sql,
383 "pages.published_at ASC"
384 assert_includes Page.aggregate(:order_by => "pages.published_at").to_sql,
385 "pages.published_at ASC"
386 end
387
388 test "template_name rejects values not present in the template directory" do
389 page = Page.create!(:title => "Template guard")
390
391 page.template_name = "../../partials/_article"
392 assert_not page.valid?
393
394 page.template_name = "standard_template"
395 assert page.valid?
396 end
397
398 test "a stale legacy template_name does not block unrelated saves" do
399 page = Page.create!(:title => "Stale template")
400 page.update_column(:template_name, "long_deleted_template")
401
402 page.reload
403 assert page.update(:abstract => "still saveable")
404 end
373end 405end
diff --git a/test/models/user_test.rb b/test/models/user_test.rb
index 6e4d2d7..feccce2 100644
--- a/test/models/user_test.rb
+++ b/test/models/user_test.rb
@@ -54,6 +54,78 @@ class UserTest < ActiveSupport::TestCase
54 def test_should_authenticate_user 54 def test_should_authenticate_user
55 assert_equal users(:quentin), User.authenticate('quentin', 'monkey') 55 assert_equal users(:quentin), User.authenticate('quentin', 'monkey')
56 end 56 end
57
58 def test_should_not_authenticate_wrong_password
59 assert_nil User.authenticate("quentin", "wrong password")
60 end
61
62 def test_should_not_authenticate_unknown_user
63 assert_nil User.authenticate("nosuchuser", "monkey")
64 end
65
66 def test_user_with_crypted_password_is_migrated_on_login
67 user = users(:quentin)
68
69 assert_nil user.password_digest
70
71 assert User.authenticate("quentin", "monkey")
72
73 user.reload
74
75 assert_not_nil user.password_digest
76 assert_nil user.crypted_password
77 assert_nil user.salt
78 end
79
80 def test_new_user_uses_password_digest
81 user = create_user
82
83 assert_not_nil user.password_digest
84 assert_nil user.crypted_password
85 assert_nil user.salt
86
87 assert_equal user, User.authenticate("quire", "quire69")
88 end
89
90 def test_legacy_user_is_migrated_on_login
91 user = users(:quentin)
92
93 assert_nil user.password_digest
94 assert_not_nil user.crypted_password
95 assert_not_nil user.salt
96
97 assert_equal user, User.authenticate("quentin", "monkey")
98
99 user.reload
100
101 assert_not_nil user.password_digest
102 assert_nil user.crypted_password
103 assert_nil user.salt
104 end
105
106 def test_migrated_user_authenticates_using_password_digest
107 user = users(:quentin)
108
109 # Trigger automatic migration.
110 assert_equal user, User.authenticate("quentin", "monkey")
111
112 user.reload
113
114 assert_not_nil user.password_digest
115 assert_nil user.crypted_password
116 assert_nil user.salt
117
118 # Second login should now use password_digest.
119 assert_equal user, User.authenticate("quentin", "monkey")
120 end
121
122 def test_migrated_user_can_be_updated_without_password
123 user = users(:quentin)
124 assert_equal user, User.authenticate("quentin", "monkey")
125 user.reload
126
127 assert user.update(:email => "quentin@example.org")
128 end
57 129
58protected 130protected
59 def create_user(options = {}) 131 def create_user(options = {})