gitlab-org--gitlab-foss/spec/factories/notes.rb

147 lines
3.9 KiB
Ruby
Raw Normal View History

require_relative '../support/repo_helpers'
include ActionDispatch::TestProcess
FactoryBot.define do
factory :note do
project
2017-04-27 06:41:26 -04:00
note { generate(:title) }
author { project&.creator || create(:user) }
2016-05-18 13:38:52 -04:00
on_issue
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]
2017-01-05 08:36:06 -05:00
factory :note_on_personal_snippet, traits: [:on_personal_snippet]
factory :system_note, traits: [:system]
factory :discussion_note_on_merge_request, traits: [:on_merge_request], class: DiscussionNote do
association :project, :repository
2017-03-13 18:13:12 -04:00
trait :resolved do
resolved_at { Time.now }
resolved_by { create(:user) }
end
end
2017-04-04 18:27:23 -04:00
factory :discussion_note_on_issue, traits: [:on_issue], class: DiscussionNote
2017-03-17 15:25:52 -04:00
2017-04-04 18:27:23 -04:00
factory :discussion_note_on_commit, traits: [:on_commit], class: DiscussionNote
2017-03-17 15:25:52 -04:00
2017-05-03 04:48:01 -04:00
factory :discussion_note_on_personal_snippet, traits: [:on_personal_snippet], class: DiscussionNote
2017-04-04 18:27:23 -04:00
factory :legacy_diff_note_on_commit, traits: [:on_commit, :legacy_diff_note], class: LegacyDiffNote
factory :legacy_diff_note_on_merge_request, traits: [:on_merge_request, :legacy_diff_note], class: LegacyDiffNote do
association :project, :repository
end
2016-06-20 13:20:39 -04:00
factory :diff_note_on_merge_request, traits: [:on_merge_request], class: DiffNote do
association :project, :repository
transient do
line_number 14
2017-04-10 16:17:47 -04:00
diff_refs { noteable.try(:diff_refs) }
end
2016-06-20 13:20:39 -04:00
position do
Gitlab::Diff::Position.new(
old_path: "files/ruby/popen.rb",
new_path: "files/ruby/popen.rb",
old_line: nil,
new_line: line_number,
2017-04-10 16:17:47 -04:00
diff_refs: diff_refs
2016-06-20 13:20:39 -04:00
)
end
trait :resolved do
resolved_at { Time.now }
resolved_by { create(:user) }
end
2016-06-20 13:20:39 -04:00
end
factory :diff_note_on_commit, traits: [:on_commit], class: DiffNote do
association :project, :repository
2017-12-01 14:08:30 -05:00
transient do
line_number 14
diff_refs { project.commit(commit_id).try(:diff_refs) }
end
2016-06-20 13:20:39 -04:00
position do
Gitlab::Diff::Position.new(
old_path: "files/ruby/popen.rb",
new_path: "files/ruby/popen.rb",
old_line: nil,
2017-12-01 14:08:30 -05:00
new_line: line_number,
diff_refs: diff_refs
2016-06-20 13:20:39 -04:00
)
end
end
trait :on_commit do
association :project, :repository
noteable nil
2016-05-18 13:38:52 -04:00
noteable_type 'Commit'
2016-06-20 13:20:39 -04:00
noteable_id nil
commit_id RepoHelpers.sample_commit.id
end
2016-06-20 13:20:39 -04:00
trait :legacy_diff_note do
line_code "0_184_184"
end
2016-05-04 05:17:16 -04:00
trait :on_issue do
noteable { create(:issue, project: project) }
end
trait :on_merge_request do
2016-05-18 12:21:14 -04:00
noteable { create(:merge_request, source_project: project) }
end
trait :on_project_snippet do
Merge branch 'jej-note-search-uses-finder' into 'security' Fix missing Note access checks in by moving Note#search to updated NoteFinder Split from !2024 to partially solve https://gitlab.com/gitlab-org/gitlab-ce/issues/23867 ## Which fixes are in this MR? :warning: - Potentially untested :bomb: - No test coverage :traffic_light: - Test coverage of some sort exists (a test failed when error raised) :vertical_traffic_light: - Test coverage of return value (a test failed when nil used) :white_check_mark: - Permissions check tested ### Note lookup without access check - [x] :white_check_mark: app/finders/notes_finder.rb:13 :download_code check - [x] :white_check_mark: app/finders/notes_finder.rb:19 `SnippetsFinder` - [x] :white_check_mark: app/models/note.rb:121 [`Issue#visible_to_user`] - [x] :white_check_mark: lib/gitlab/project_search_results.rb:113 - This is the only use of `app/models/note.rb:121` above, but importantly has no access checks at all. This means it leaks MR comments and snippets when those features are `team-only` in addition to the issue comments which would be fixed by `app/models/note.rb:121`. - It is only called from SearchController where `can?(current_user, :download_code, @project)` is checked, so commit comments are not leaked. ### Previous discussions - [x] https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/2024/diffs#b915c5267a63628b0bafd23d37792ae73ceae272_13_13 `: download_code` check on commit - [x] https://dev.gitlab.org/gitlab/gitlabhq/merge_requests/2024/diffs#b915c5267a63628b0bafd23d37792ae73ceae272_19_19 `SnippetsFinder` should be used - `SnippetsFinder` should check if the snippets feature is enabled -> https://gitlab.com/gitlab-org/gitlab-ce/issues/25223 ### Acceptance criteria met? - [x] Tests added for new code - [x] TODO comments removed - [x] Squashed and removed skipped tests - [x] Changelog entry - [ ] State Gitlab versions affected and issue severity in description - [ ] Create technical debt issue for NotesFinder. - Either split into `NotesFinder::ForTarget` and `NotesFinder::Search` or consider object per notable type such as `NotesFinder::OnIssue`. For the first option could create `NotesFinder::Base` which is either inherited from or which can be included in the other two. - Avoid case statement anti-pattern in this finder with use of `NotesFinder::OnCommit` etc. Consider something on the finder for this? `Model.finder(user, project)` - Move `inc_author` to the controller, and implement `related_notes` to replace `non_diff_notes`/`mr_and_commit_notes` See merge request !2035
2016-12-08 20:56:31 -05:00
noteable { create(:project_snippet, project: project) }
end
2017-01-05 08:36:06 -05:00
trait :on_personal_snippet do
noteable { create(:personal_snippet) }
project nil
end
trait :system do
system true
end
trait :downvote do
note "thumbsdown"
end
trait :upvote do
note "thumbsup"
end
trait :with_attachment do
2018-01-29 12:57:34 -05:00
attachment { fixture_file_upload(Rails.root.join( "spec/fixtures/dk.png"), "image/png") }
end
trait :with_svg_attachment do
2018-01-29 12:57:34 -05:00
attachment { fixture_file_upload(Rails.root.join("spec/fixtures/unsanitized.svg"), "image/svg+xml") }
end
2017-03-13 18:13:12 -04:00
transient do
in_reply_to nil
end
before(:create) do |note, evaluator|
discussion = evaluator.in_reply_to
next unless discussion
2017-03-13 18:13:12 -04:00
discussion = discussion.to_discussion if discussion.is_a?(Note)
next unless discussion
2017-03-17 15:25:52 -04:00
note.assign_attributes(discussion.reply_attributes.merge(project: discussion.project))
2017-03-13 18:13:12 -04:00
end
end
end