1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
|
# frozen_string_literal: true
require "spec_helper"
# rubocop:disable Rails/SaveBang -- None of the offenses are ActiveRecord calls
RSpec.shared_examples 'wiki_page' do |container_type|
let(:user) { create(:user) }
let(:owner) { create(:user) }
let(:container) { create(container_type) }
let(:wiki) { container.wiki }
def create_file_in_repository(path:)
wiki.create_wiki_repository
wiki.repository.create_file(
user, path, 'test content',
branch_name: wiki.default_branch,
message: 'test commit'
)
title = Pathname(path).sub_ext('').to_s
wiki.find_page(title)
end
def create_wiki_page(container, attrs = {})
page = build_wiki_page(container, attrs)
page.create(message: attrs[:message] || 'test commit')
container.wiki.find_page(page.slug)
end
def build_wiki_page(container, attrs = {})
wiki_page_attrs = { container: container, content: 'test content' }.merge(attrs)
build(:wiki_page, wiki_page_attrs)
end
def force_wiki_change_branch
old_default_branch = wiki.default_branch
wiki.repository.add_branch(user, 'another_branch', old_default_branch)
wiki.repository.rm_branch(user, old_default_branch)
wiki.repository.expire_status_cache
wiki.container.clear_memoization(:wiki)
end
before do
container.add_owner(owner)
end
# Use for groups of tests that do not modify their `subject`.
#
# include_context 'when subject is a persisted page', title: 'my title'
shared_context 'when subject is a persisted page' do |attrs = {}|
let(:persisted_page) { create_wiki_page(container, attrs) }
subject { persisted_page }
end
describe '#front_matter' do
let(:wiki_page) { create(:wiki_page, container: container, content: content) }
shared_examples 'a page without front-matter' do
it { expect(wiki_page).to have_attributes(front_matter: {}, content: content) }
end
shared_examples 'a page with front-matter' do
let(:front_matter) { { title: 'Foo', slugs: %w[slug_a slug_b] } }
it { expect(wiki_page.front_matter).to eq(front_matter) }
it { expect(wiki_page.front_matter_title).to eq(front_matter[:title]) }
end
context 'when the wiki page has front matter' do
let(:content) do
<<~MD
---
title: Foo
slugs:
- slug_a
- slug_b
---
My actual content
MD
end
it_behaves_like 'a page with front-matter'
it 'strips the front matter from the content' do
expect(wiki_page.content.strip).to eq('My actual content')
end
end
context 'when the wiki page does not have front matter' do
let(:content) { 'My actual content' }
it_behaves_like 'a page without front-matter'
end
context 'when the wiki page has fenced blocks, but nothing in them' do
let(:content) do
<<~MD
---
---
My actual content
MD
end
it_behaves_like 'a page without front-matter'
end
context 'when the wiki page has invalid YAML type in fenced blocks' do
let(:content) do
<<~MD
---
this isn't YAML
---
My actual content
MD
end
it_behaves_like 'a page without front-matter'
end
context 'when the wiki page has a disallowed class in fenced block' do
let(:content) do
<<~MD
---
date: 2010-02-11 11:02:57
---
My actual content
MD
end
it_behaves_like 'a page without front-matter'
end
context 'when the wiki page has invalid YAML in fenced block' do
let(:content) do
<<~MD
---
invalid-use-of-reserved-indicator: @text
---
My actual content
MD
end
it_behaves_like 'a page without front-matter'
end
end
describe '.unhyphenize' do
it 'removes hyphens from a name' do
name = 'a-name--with-hyphens'
expect(described_class.unhyphenize(name)).to eq('a name with hyphens')
end
end
describe "#initialize" do
context "when initialized with an existing page" do
include_context 'when subject is a persisted page', title: 'test initialization'
it "sets the slug attribute" do
expect(subject.slug).to eq("test-initialization")
end
it "sets the title attribute" do
expect(subject.title).to eq("test initialization")
end
it "sets the formatted content attribute" do
expect(subject.content).to eq("test content")
end
it "sets the format attribute" do
expect(subject.format).to eq(:markdown)
end
it "sets the message attribute" do
expect(subject.message).to eq("test commit")
end
it "sets the version attribute" do
expect(subject.version).to be_a Gitlab::Git::WikiPageVersion
end
end
end
describe "validations" do
subject { build_wiki_page(container) }
it "validates presence of title" do
subject.attributes.delete(:title)
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(title: ["can't be blank"])
end
it "does not validate presence of content" do
subject.attributes.delete(:content)
expect(subject).to be_valid
end
describe '#validate_content_size_limit' do
context 'with a new page' do
before do
stub_application_setting(wiki_page_max_content_bytes: 10)
end
it 'accepts content below the limit' do
subject.attributes[:content] = 'a' * 10
expect(subject).to be_valid
end
it 'rejects content exceeding the limit' do
subject.attributes[:content] = 'a' * 11
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(
content: ['is too long (11 B). The maximum size is 10 B.']
)
end
it 'counts content size in bytes rather than characters' do
subject.attributes[:content] = '💩💩💩'
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(
content: ['is too long (12 B). The maximum size is 10 B.']
)
end
end
context 'with an existing page exceeding the limit' do
include_context 'when subject is a persisted page'
before do
subject
stub_application_setting(wiki_page_max_content_bytes: 11)
end
it 'accepts content when it has not changed' do
expect(subject).to be_valid
end
it 'rejects content when it has changed' do
subject.attributes[:content] = 'a' * 12
expect(subject).not_to be_valid
expect(subject.errors.messages).to eq(
content: ['is too long (12 B). The maximum size is 11 B.']
)
end
end
end
describe '#validate_path_limits' do
let(:max_title) { Gitlab::WikiPages::MAX_TITLE_BYTES }
let(:max_directory) { Gitlab::WikiPages::MAX_DIRECTORY_BYTES }
where(:character) do
['a', 'ä', '🙈']
end
with_them do
let(:size) { character.bytesize.to_f }
let(:valid_title) { character * (max_title / size).floor }
let(:valid_directory) { character * (max_directory / size).floor }
let(:invalid_title) { character * ((max_title + 1) / size).ceil }
let(:invalid_directory) { character * ((max_directory + 1) / size).ceil }
it 'accepts page titles below the limit' do
subject.title = valid_title
expect(subject).to be_valid
end
it 'accepts directories below the limit' do
subject.title = "#{valid_directory}/foo"
expect(subject).to be_valid
end
it 'accepts a path with page title and directory below the limit' do
subject.title = "#{valid_directory}/#{valid_title}"
expect(subject).to be_valid
end
it 'rejects page titles exceeding the limit' do
subject.title = invalid_title
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_title} bytes"
)
end
it 'rejects directories exceeding the limit' do
subject.title = "#{invalid_directory}/#{invalid_directory}2/foo"
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\"",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}2\""
)
end
it 'rejects a page with both title and directory exceeding the limit' do
subject.title = "#{invalid_directory}/#{invalid_title}"
expect(subject).not_to be_valid
expect(subject.errors[:title]).to contain_exactly(
"exceeds the limit of #{max_title} bytes",
"exceeds the limit of #{max_directory} bytes for directory name \"#{invalid_directory}\""
)
end
end
context 'with an existing page title exceeding the limit' do
subject do
title = 'a' * (max_title + 1)
wiki.create_page(title, 'content')
wiki.find_page(title)
end
it 'accepts the exceeding title length when unchanged' do
expect(subject).to be_valid
end
it 'rejects the exceeding title length when changed' do
subject.title = 'b' * (max_title + 1)
expect(subject).not_to be_valid
expect(subject.errors).to include(:title)
end
end
end
end
describe "#create" do
let(:attributes) do
{
title: SecureRandom.hex,
content: "Home Page",
format: "markdown",
message: 'Custom Commit Message'
}
end
let(:title) { attributes[:title] }
subject { build_wiki_page(container) }
context "with valid attributes" do
it "saves the wiki page" do
subject.create(attributes)
expect(wiki.find_page(title)).not_to be_nil
end
it "returns true" do
expect(subject.create(attributes)).to be(true)
end
it 'saves the wiki page with message' do
subject.create(attributes)
expect(wiki.find_page(title).message).to eq 'Custom Commit Message'
end
it 'if the title is preceded by a / it is removed' do
subject.create(attributes.merge(title: '/New Page'))
expect(wiki.find_page('New Page')).not_to be_nil
end
end
context "with invalid attributes" do
it 'does not create the page' do
expect { subject.create(title: '') }.not_to change { wiki.list_pages.length }
end
end
context "with front matter context" do
let(:attributes) do
{
title: SecureRandom.hex,
content: "---\nxxx: abc\n---\nHome Page",
format: "markdown",
message: 'Custom Commit Message'
}
end
it 'create the page with front matter' do
subject.create(attributes)
expect(wiki.find_page(title).front_matter).to eq({ xxx: "abc" })
end
end
context "with existing page" do
let(:title) { 'Existing Page' }
it 'do not create the page with the same title' do
page = create_wiki_page(container, title: title, content: 'content')
subject.create(attributes.merge(title: title))
expect(subject.create(attributes.merge(title: title))).to be_falsy
expect(wiki.find_page(title).content).to eq(page.content)
end
it 'do not create the page with the same title, even if the orginal path contains spaces' do
page = create_file_in_repository(path: "#{title}.md")
subject.create(attributes.merge(title: title))
expect(subject.create(attributes.merge(title: title))).to be_falsy
expect(wiki.find_page(title).content).to eq(page.content)
end
end
context 'when the repository fails' do
it 'do not create the page if the repository raise an error' do
page = build_wiki_page(container)
allow(Gitlab::GitalyClient).to receive(:call) do
raise GRPC::Unavailable, 'Gitaly broken in this spec'
end
saved = page.create(attributes)
# unstub
allow(Gitlab::GitalyClient).to receive(:call).and_call_original
expect(saved).to be(false)
expect(page.errors.messages[:base]).to include(/Gitaly broken in this spec/)
expect(wiki.find_page(title)).to be_nil
end
end
end
describe "dot in the title" do
let(:title) { 'Index v1.2.3' }
describe "#create" do
subject { build_wiki_page(container) }
it "saves the wiki page and returns true", :aggregate_failures do
attributes = { title: title, content: "Home Page", format: "markdown" }
expect(subject.create(attributes)).to be(true)
expect(wiki.find_page(title)).not_to be_nil
end
end
describe '#update' do
subject { create_wiki_page(container, title: title) }
it 'updates the content of the page and returns true', :aggregate_failures do
expect(subject.update(content: 'new content')).to be_truthy
page = wiki.find_page(title)
expect([subject.content, page.content]).to all(eq('new content'))
end
end
end
describe "#update" do
let!(:original_title) { subject.title }
subject { create_wiki_page(container) }
context "with valid attributes" do
it "updates the content of the page" do
new_content = "new content"
subject.update(content: new_content)
page = wiki.find_page(original_title)
expect([subject.content, page.content]).to all(eq("new content"))
end
it "updates the title of the page" do
new_title = "Index v.1.2.4"
subject.update(title: new_title)
page = wiki.find_page(new_title)
expect([subject.title, page.title]).to all(eq(new_title))
end
describe 'updating front_matter' do
shared_examples 'able to update front-matter' do
it 'updates the wiki-page front-matter' do
content = subject.content
subject.update(front_matter: { slugs: ['x'] })
page = wiki.find_page(original_title)
expect([subject, page]).to all(
have_attributes(
front_matter: include(slugs: include('x')),
content: content
))
end
end
it_behaves_like 'able to update front-matter'
context 'when the front matter is too long' do
let(:new_front_matter) do
{
title: generate(:wiki_page_title),
slugs: Array.new(51).map { FFaker::Lorem.characters(512) }
}
end
it 'raises an error' do
expect do
subject.update(front_matter: new_front_matter)
end.to raise_error(described_class::FrontMatterTooLong)
end
end
it 'updates the wiki-page front-matter and content together' do
content = 'totally new content'
subject.update(content: content, front_matter: { slugs: ['x'] })
page = wiki.find_page(original_title)
expect([subject, page]).to all(
have_attributes(
front_matter: include(slugs: include('x')),
content: content
))
end
end
it "returns true" do
expect(subject.update(content: "more content")).to be_truthy
end
end
context 'with same last commit sha' do
it 'returns true' do
expect(subject.update(content: 'more content', last_commit_sha: subject.last_commit_sha)).to be_truthy
end
end
context 'with different last commit sha' do
it 'raises exception' do
expect do
subject.update(content: 'more content', last_commit_sha: 'xxx')
end.to raise_error(WikiPage::PageChangedError)
end
end
describe 'in subdir' do
it 'keeps the page in the same dir when the content is updated' do
title = 'foo/Existing Page'
page = create_wiki_page(container, title: title)
expect(page.slug).to eq 'foo/Existing-Page'
expect(page.update(title: title, content: 'new_content')).to be_truthy
page = wiki.find_page(title)
expect(page.slug).to eq 'foo/Existing-Page'
expect(page.content).to eq 'new_content'
end
end
context 'when renaming a page' do
it 'raises an error if the page already exists' do
existing_page = create_wiki_page(container)
expect do
subject.update(title: existing_page.title, content: 'new_content')
end.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq original_title
expect(subject.content).to eq 'new_content' # We don't revert the content
end
it 'updates the content and rename the file' do
new_title = 'Renamed Page'
new_content = 'updated content'
expect(subject.update(title: new_title, content: new_content)).to be_truthy
page = wiki.find_page(new_title)
expect(page).not_to be_nil
expect(page.content).to eq new_content
end
end
context 'when moving a page' do
it 'raises an error if the page already exists' do
wiki.create_page('foo/Existing Page', 'content')
expect do
subject.update(title: 'foo/Existing Page', content: 'new_content')
end.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq original_title
expect(subject.content).to eq 'new_content'
end
it 'raises an error if the page already exists even if it contains spaces in the orginal path' do
create_file_in_repository(path: 'foo/Existing Page.md')
expect do
subject.update(title: 'foo/Existing Page', content: 'new_content')
end.to raise_error(WikiPage::PageRenameError)
expect(subject.title).to eq original_title
expect(subject.content).to eq 'new_content'
end
it 'updates the content and moves the file' do
new_title = 'foo/Other Page'
new_content = 'new_content'
expect(subject.update(title: new_title, content: new_content)).to be_truthy
page = wiki.find_page(new_title)
expect(page).not_to be_nil
expect(page.content).to eq new_content
end
context 'when page combine with directory' do
it 'moving the file and directory' do
wiki.create_page('testpage/testtitle', 'content')
wiki.create_page('testpage', 'content')
page = wiki.find_page('testpage')
page.update(title: 'testfolder/testpage')
page = wiki.find_page('testfolder/testpage/testtitle')
expect(page.slug).to eq 'testfolder/testpage/testtitle'
end
end
describe 'in subdir' do
it 'moves the page to the root folder if the title is preceded by /' do
page = create_wiki_page(container, title: 'foo/Existing Page')
expect(page.slug).to eq 'foo/Existing-Page'
expect(page.update(title: '/Existing Page', content: 'new_content')).to be_truthy
expect(page.slug).to eq 'Existing-Page'
end
it 'does nothing if it has the same title' do
page = create_wiki_page(container, title: 'foo/Another Existing Page')
original_path = page.slug
expect(page.update(title: 'Another Existing Page', content: 'new_content')).to be_truthy
expect(page.slug).to eq original_path
end
it 'moves the page to the another folder if the original path has spaces' do
page = create_file_in_repository(path: 'Existing Folder/Existing Page.md')
original_path = page.slug
expect(page.update(title: 'Existing Page', content: 'new_content')).to be_truthy
expect(page.slug).not_to eq original_path
expect(page.slug).to eq "Existing-Folder/Existing-Page"
end
end
context 'in root dir' do
it 'does nothing if the title is preceded by /' do
original_path = subject.slug
expect(subject.update(title: "/#{subject.title}", content: 'new_content')).to be_truthy
expect(subject.slug).to eq original_path
end
end
end
context "with invalid attributes" do
it 'aborts update if title blank' do
expect(subject.update(title: '', content: 'new_content')).to be_falsey
expect(subject.content).to eq 'new_content'
page = wiki.find_page(original_title)
expect(page.content).to eq 'test content'
end
end
context 'when the repository fails' do
it 'do not update the page if the repository raise an error' do
page = create_wiki_page(container)
allow(Gitlab::GitalyClient).to receive(:call) do
raise GRPC::Unavailable, 'Gitaly broken in this spec'
end
saved = page.update(content: "new content")
# unstub
allow(Gitlab::GitalyClient).to receive(:call).and_call_original
expect(saved).to be(false)
expect(page.errors.messages[:base]).to include(/Gitaly broken in this spec/)
page_found = wiki.find_page(original_title)
expect(page_found.content).to eq 'test content'
end
end
end
describe "#delete" do
it "deletes the page and returns true", :aggregate_failures do
page = create_wiki_page(container)
expect do
expect(page.delete).to be(true)
end.to change { wiki.list_pages.length }.by(-1)
end
context 'when the repository fails' do
it 'do not delete the page if the repository raise an error' do
page = create_wiki_page(container)
allow(Gitlab::GitalyClient).to receive(:call) do
raise GRPC::Unavailable, 'Gitaly broken in this spec'
end
deleted = page.delete
# unstub
allow(Gitlab::GitalyClient).to receive(:call).and_call_original
expect(deleted).to be(false)
expect(wiki.error_message).to match(/Gitaly broken in this spec/)
expect(wiki.list_pages.length).to be(1)
end
end
end
describe "#versions" do
subject { create_wiki_page(container) }
before do
3.times { |i| subject.update(content: "content #{i}") }
end
context 'when number of versions is less than the default paginiated per page' do
it "returns an array of all commits for the page" do
expect(subject.versions).to be_a(::CommitCollection)
expect(subject.versions.length).to eq(4)
expect(subject.versions.first.id).to eql(subject.last_version.id)
end
end
context 'when number of versions is more than the default paginiated per page' do
before do
allow(Kaminari.config).to receive(:default_per_page).and_return(3)
end
it "returns an arrary containing the first page of commits for the page" do
expect(subject.versions).to be_a(::CommitCollection)
expect(subject.versions.length).to eq(3)
expect(subject.versions.first.id).to eql(subject.last_version.id)
end
it "returns an arrary containing the second page of commits for the page with options[:page] = 2" do
versions = subject.versions(page: 2)
expect(versions).to be_a(::CommitCollection)
expect(versions.length).to eq(1)
end
end
context "when wiki repository's default is updated" do
before do
force_wiki_change_branch
end
it "returns the correct versions in the default branch" do
page = container.wiki.find_page(subject.title)
expect(page.versions).to be_a(::CommitCollection)
expect(page.versions.length).to eq(4)
expect(page.versions.first.id).to eql(page.last_version.id)
page.update(content: "final content")
expect(page.versions.length).to eq(5)
end
end
end
describe "#count_versions" do
subject { create_wiki_page(container) }
it "returns the total numbers of commits" do
expect do
3.times { |i| subject.update(content: "content #{i}") }
end.to change { subject.count_versions }.from(1).to(4)
end
context "when wiki repository's default is updated" do
before do
subject
force_wiki_change_branch
end
it "returns the correct number of versions in the default branch" do
page = container.wiki.find_page(subject.title)
expect(page.count_versions).to eq(1)
page.update(content: "final content")
expect(page.count_versions).to eq(2)
end
end
end
describe '#title_changed?' do
using RSpec::Parameterized::TableSyntax
let(:unsaved_page) { build_wiki_page(container, title: 'test page') }
let(:existing_page) { create_wiki_page(container, title: 'test page') }
let(:directory_page) { create_wiki_page(container, title: 'parent directory/child page') }
let(:page_with_special_characters) { create_wiki_page(container, title: 'test+page') }
let(:untitled_page) { described_class.new(wiki) }
where(:page, :title, :changed) do
:untitled_page | nil | false
:untitled_page | 'new title' | true
:unsaved_page | nil | true
:unsaved_page | 'test page' | true
:unsaved_page | 'test-page' | true
:unsaved_page | 'test+page' | true
:unsaved_page | 'new title' | true
:existing_page | nil | false
:existing_page | 'test page' | false
:existing_page | 'test-page' | false
:existing_page | '/test page' | false
:existing_page | '/test-page' | false
:existing_page | 'test+page' | true
:existing_page | ' test page ' | true
:existing_page | 'new title' | true
:existing_page | 'new-title' | true
:directory_page | nil | false
:directory_page | 'parent directory/child page' | false
:directory_page | 'parent-directory/child page' | false
:directory_page | 'parent-directory/child-page' | false
:directory_page | 'child page' | false
:directory_page | 'child-page' | false
:directory_page | '/child page' | true
:directory_page | 'parent directory/other' | true
:directory_page | 'parent-directory/other' | true
:directory_page | 'parent-directory / child-page' | true
:directory_page | 'other directory/child page' | true
:directory_page | 'other-directory/child page' | true
:page_with_special_characters | nil | false
:page_with_special_characters | 'test+page' | false
:page_with_special_characters | 'test-page' | true
:page_with_special_characters | 'test page' | true
end
with_them do
it 'returns the expected value' do
subject = public_send(page)
subject.title = title if title
expect(subject.title_changed?).to be(changed)
end
end
end
describe '#content_changed?' do
context 'with a new page' do
subject { build_wiki_page(container) }
it 'returns true if content is set' do
subject.attributes[:content] = 'new'
expect(subject.content_changed?).to be(true)
end
it 'returns false if content is blank' do
subject.attributes[:content] = ' '
expect(subject.content_changed?).to be(false)
end
end
context 'with an existing page' do
include_context 'when subject is a persisted page'
it 'returns false' do
expect(subject.content_changed?).to be(false)
end
it 'returns false if content is set to the same value' do
subject.attributes[:content] = 'test content'
expect(subject.content_changed?).to be(false)
end
it 'returns true if content is changed' do
subject.attributes[:content] = 'new'
expect(subject.content_changed?).to be(true)
end
it 'returns true if content is changed to a blank string' do
subject.attributes[:content] = ' '
expect(subject.content_changed?).to be(true)
end
it 'returns false if only the newline format has changed from LF to CRLF' do
expect(subject.page).to receive(:text_data).and_return("foo\nbar")
subject.attributes[:content] = "foo\r\nbar"
expect(subject.content_changed?).to be(false)
end
it 'returns false if only the newline format has changed from CRLF to LF' do
expect(subject.page).to receive(:text_data).and_return("foo\r\nbar")
subject.attributes[:content] = "foo\nbar"
expect(subject.content_changed?).to be(false)
end
end
end
describe '#path' do
it 'returns the path when persisted' do
existing_page = create_wiki_page(container, title: 'path test')
expect(existing_page.path).to eq('path-test.md')
end
it 'returns nil when not persisted' do
unsaved_page = build_wiki_page(container, title: 'path test')
expect(unsaved_page.path).to be_nil
end
end
describe '#directory' do
context 'when the page is at the root directory' do
include_context 'when subject is a persisted page', title: 'directory test'
it 'returns an empty string' do
expect(subject.directory).to eq('')
end
end
context 'when the page is inside an actual directory' do
include_context 'when subject is a persisted page', title: 'dir_1/dir_1_1/directory test'
it 'returns the full directory hierarchy' do
expect(subject.directory).to eq('dir_1/dir_1_1')
end
end
end
describe '#historical?' do
let!(:container) { create(container_type) }
let(:wiki) { subject.wiki }
let(:old_version) { subject.versions.last.id }
let(:old_page) { wiki.find_page(subject.title, old_version) }
let(:latest_version) { subject.versions.first.id }
let(:latest_page) { wiki.find_page(subject.title, latest_version) }
subject { create_wiki_page(container) }
before do
3.times { |i| subject.update(content: "content #{i}") }
end
it 'returns true when requesting an old version' do
expect(old_page.historical?).to be_truthy
end
it 'returns false when requesting latest version' do
expect(latest_page.historical?).to be_falsy
end
it 'returns false when version is nil' do
expect(latest_page).to receive(:version).and_return(nil)
expect(latest_page.historical?).to be_falsy
end
it 'returns false when the last version is nil' do
expect(old_page).to receive(:last_version).and_return(nil)
expect(old_page.historical?).to be_falsy
end
it 'returns false when the version is nil' do
expect(old_page).to receive(:version).and_return(nil)
expect(old_page.historical?).to be_falsy
end
end
describe '#persisted?' do
it 'returns true for a persisted page' do
expect(create_wiki_page(container)).to be_persisted
end
it 'returns false for an unpersisted page' do
expect(build_wiki_page(container)).not_to be_persisted
end
end
describe '#to_partial_path' do
it 'returns the relative path to the partial to be used' do
expect(build_wiki_page(container).to_partial_path).to eq('shared/wikis/wiki_page')
end
end
describe '#==' do
include_context 'when subject is a persisted page'
it 'returns true for identical wiki page' do
expect(subject == subject).to be(true)
end
it 'returns true for updated wiki page' do
subject.update(content: "Updated content")
updated_page = wiki.find_page(subject.slug)
expect(updated_page).not_to be_nil
expect(updated_page).to eq(subject)
end
it 'returns false for a completely different wiki page' do
other_page = create(:wiki_page)
expect(subject.slug).not_to eq(other_page.slug)
expect(subject.container).not_to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
it 'returns false for page with different slug on same container' do
other_page = create_wiki_page(container)
expect(subject.slug).not_to eq(other_page.slug)
expect(subject.container).to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
it 'returns false for page with the same slug on a different container' do
other_page = create(:wiki_page, title: subject.slug)
expect(subject.slug).to eq(other_page.slug)
expect(subject.container).not_to eq(other_page.container)
expect(subject).not_to eq(other_page)
end
end
describe '#last_commit_sha' do
include_context 'when subject is a persisted page'
it 'returns commit sha' do
expect(subject.last_commit_sha).to eq subject.last_version.sha
end
it 'is changed after page updated' do
last_commit_sha_before_update = subject.last_commit_sha
subject.update(content: "new content")
page = wiki.find_page(subject.title)
expect(page.last_commit_sha).not_to eq last_commit_sha_before_update
end
end
describe '#hook_attrs' do
subject { build_wiki_page(container) }
it 'includes specific attributes' do
keys = subject.hook_attrs.keys
expect(keys).not_to include(:content)
expect(keys).to include(:version_id)
end
end
describe '#version_commit_timestamp' do
context 'for a new page' do
it 'returns nil' do
expect(build_wiki_page(container).version_commit_timestamp).to be_nil
end
end
context 'for page that exists' do
it 'returns the timestamp of the commit' do
existing_page = create_wiki_page(container)
expect(existing_page.version_commit_timestamp).to eq(existing_page.version.commit.committed_date)
end
end
end
describe '#diffs' do
include_context 'when subject is a persisted page'
it 'returns a diff instance' do
diffs = subject.diffs(foo: 'bar')
expect(diffs).to be_a(Gitlab::Diff::FileCollection::WikiPage)
expect(diffs.diffable).to be_a(Commit)
expect(diffs.diffable.id).to eq(subject.version.id)
expect(diffs.project).to be(subject.wiki)
expect(diffs.diff_options).to include(
expanded: true,
paths: [subject.path],
foo: 'bar'
)
end
end
describe "#human_title" do
context "with front matter title" do
let(:front_matter_title) { "abc" }
let(:content_with_front_matter_title) { "---\ntitle: #{front_matter_title}\n---\nHome Page" }
let(:wiki_page) { create(:wiki_page, container: container, content: content_with_front_matter_title) }
it 'returns the front matter title' do
expect(wiki_page.human_title).to eq front_matter_title
end
end
end
end
# rubocop:enable Rails/SaveBang
|