notes.rb 3.68 KB
Newer Older
1 2
require_relative '../support/repo_helpers'

3 4
include ActionDispatch::TestProcess

5 6
FactoryGirl.define do
  factory :note do
7
    project factory: :empty_project
8 9
    note "Note"
    author
Grzegorz Bizon's avatar
Grzegorz Bizon committed
10
    on_issue
11

12 13 14 15
    factory :note_on_commit,             traits: [:on_commit]
    factory :note_on_issue,              traits: [:on_issue], aliases: [:votable_note]
    factory :note_on_merge_request,      traits: [:on_merge_request]
    factory :note_on_project_snippet,    traits: [:on_project_snippet]
16
    factory :note_on_personal_snippet,   traits: [:on_personal_snippet]
17
    factory :system_note,                traits: [:system]
18

19 20
    factory :discussion_note_on_merge_request, traits: [:on_merge_request], class: DiscussionNote do
      association :project, :repository
Douwe Maan's avatar
Douwe Maan committed
21

22 23 24 25 26 27
      trait :resolved do
        resolved_at { Time.now }
        resolved_by { create(:user) }
      end
    end

Douwe Maan's avatar
Douwe Maan committed
28
    factory :discussion_note_on_issue, traits: [:on_issue], class: DiscussionNote
Douwe Maan's avatar
Douwe Maan committed
29

Douwe Maan's avatar
Douwe Maan committed
30
    factory :discussion_note_on_commit, traits: [:on_commit], class: DiscussionNote
Douwe Maan's avatar
Douwe Maan committed
31

Douwe Maan's avatar
Douwe Maan committed
32
    factory :legacy_diff_note_on_commit, traits: [:on_commit, :legacy_diff_note], class: LegacyDiffNote
33 34 35 36

    factory :legacy_diff_note_on_merge_request, traits: [:on_merge_request, :legacy_diff_note], class: LegacyDiffNote do
      association :project, :repository
    end
Douwe Maan's avatar
Douwe Maan committed
37 38

    factory :diff_note_on_merge_request, traits: [:on_merge_request], class: DiffNote do
39
      association :project, :repository
40 41 42

      transient do
        line_number 14
Douwe Maan's avatar
Douwe Maan committed
43
        diff_refs { noteable.try(:diff_refs) }
44 45
      end

Douwe Maan's avatar
Douwe Maan committed
46 47 48 49 50
      position do
        Gitlab::Diff::Position.new(
          old_path: "files/ruby/popen.rb",
          new_path: "files/ruby/popen.rb",
          old_line: nil,
51
          new_line: line_number,
Douwe Maan's avatar
Douwe Maan committed
52
          diff_refs: diff_refs
Douwe Maan's avatar
Douwe Maan committed
53 54
        )
      end
55 56 57 58 59

      trait :resolved do
        resolved_at { Time.now }
        resolved_by { create(:user) }
      end
Douwe Maan's avatar
Douwe Maan committed
60 61 62
    end

    factory :diff_note_on_commit, traits: [:on_commit], class: DiffNote do
63
      association :project, :repository
Douwe Maan's avatar
Douwe Maan committed
64 65 66 67 68 69 70 71 72 73 74
      position do
        Gitlab::Diff::Position.new(
          old_path: "files/ruby/popen.rb",
          new_path: "files/ruby/popen.rb",
          old_line: nil,
          new_line: 14,
          diff_refs: project.commit(commit_id).try(:diff_refs)
        )
      end
    end

75
    trait :on_commit do
76
      association :project, :repository
77
      noteable nil
Grzegorz Bizon's avatar
Grzegorz Bizon committed
78
      noteable_type 'Commit'
Douwe Maan's avatar
Douwe Maan committed
79
      noteable_id nil
80 81 82
      commit_id RepoHelpers.sample_commit.id
    end

Douwe Maan's avatar
Douwe Maan committed
83
    trait :legacy_diff_note do
84 85 86
      line_code "0_184_184"
    end

87 88
    trait :on_issue do
      noteable { create(:issue, project: project) }
89 90 91
    end

    trait :on_merge_request do
Grzegorz Bizon's avatar
Grzegorz Bizon committed
92
      noteable { create(:merge_request, source_project: project) }
93 94
    end

95
    trait :on_project_snippet do
96
      noteable { create(:project_snippet, project: project) }
97 98
    end

99 100 101 102 103
    trait :on_personal_snippet do
      noteable { create(:personal_snippet) }
      project nil
    end

104 105 106 107
    trait :system do
      system true
    end

108 109 110 111 112 113 114 115
    trait :downvote do
      note "thumbsdown"
    end

    trait :upvote do
      note "thumbsup"
    end

116
    trait :with_attachment do
117 118 119 120 121
      attachment { fixture_file_upload(Rails.root + "spec/fixtures/dk.png", "image/png") }
    end

    trait :with_svg_attachment do
      attachment { fixture_file_upload(Rails.root + "spec/fixtures/unsanitized.svg", "image/svg+xml") }
122
    end
Douwe Maan's avatar
Douwe Maan committed
123 124 125 126 127 128 129 130 131 132

    transient do
      in_reply_to nil
    end

    before(:create) do |note, evaluator|
      discussion = evaluator.in_reply_to
      next unless discussion
      discussion = discussion.to_discussion if discussion.is_a?(Note)
      next unless discussion
Douwe Maan's avatar
Douwe Maan committed
133 134

      note.assign_attributes(discussion.reply_attributes.merge(project: discussion.project))
Douwe Maan's avatar
Douwe Maan committed
135
    end
136 137
  end
end