BigW Consortium Gitlab

notes.rb 3.8 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
    note { generate(:title) }
9
    author
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 committed
21

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

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

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

32 33
    factory :discussion_note_on_personal_snippet, traits: [:on_personal_snippet], class: DiscussionNote

Douwe Maan committed
34
    factory :legacy_diff_note_on_commit, traits: [:on_commit, :legacy_diff_note], class: LegacyDiffNote
35 36 37 38

    factory :legacy_diff_note_on_merge_request, traits: [:on_merge_request, :legacy_diff_note], class: LegacyDiffNote do
      association :project, :repository
    end
Douwe Maan committed
39 40

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

      transient do
        line_number 14
Douwe Maan committed
45
        diff_refs { noteable.try(:diff_refs) }
46 47
      end

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

      trait :resolved do
        resolved_at { Time.now }
        resolved_by { create(:user) }
      end
Douwe Maan committed
62 63 64
    end

    factory :diff_note_on_commit, traits: [:on_commit], class: DiffNote do
65
      association :project, :repository
Douwe Maan committed
66 67 68 69 70 71 72 73 74 75 76
      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

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

Douwe Maan committed
85
    trait :legacy_diff_note do
86 87 88
      line_code "0_184_184"
    end

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

    trait :on_merge_request do
Grzegorz Bizon committed
94
      noteable { create(:merge_request, source_project: project) }
95 96
    end

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

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

106 107 108 109
    trait :system do
      system true
    end

110 111 112 113 114 115 116 117
    trait :downvote do
      note "thumbsdown"
    end

    trait :upvote do
      note "thumbsup"
    end

118
    trait :with_attachment do
119 120 121 122 123
      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") }
124
    end
Douwe Maan committed
125 126 127 128 129 130 131 132 133 134

    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 committed
135 136

      note.assign_attributes(discussion.reply_attributes.merge(project: discussion.project))
Douwe Maan committed
137
    end
138 139
  end
end