note_policy_spec.rb 4.43 KB
Newer Older
1 2
require 'spec_helper'

3
describe NotePolicy do
4 5 6 7
  describe '#rules' do
    let(:user) { create(:user) }
    let(:project) { create(:project, :public) }
    let(:issue) { create(:issue, project: project) }
8 9 10
    let(:noteable) { issue }
    let(:policy) { described_class.new(user, note) }
    let(:note) { create(:note, noteable: noteable, author: user, project: project) }
11

12 13 14 15 16 17
    shared_examples_for 'a discussion with a private noteable' do
      context 'when the note author can no longer see the noteable' do
        it 'can not edit nor read the note' do
          expect(policy).to be_disallowed(:admin_note)
          expect(policy).to be_disallowed(:resolve_note)
          expect(policy).to be_disallowed(:read_note)
18
          expect(policy).to be_disallowed(:award_emoji)
19 20 21 22 23 24 25 26 27 28 29 30
        end
      end

      context 'when the note author can still see the noteable' do
        before do
          project.add_developer(user)
        end

        it 'can edit the note' do
          expect(policy).to be_allowed(:admin_note)
          expect(policy).to be_allowed(:resolve_note)
          expect(policy).to be_allowed(:read_note)
31
          expect(policy).to be_allowed(:award_emoji)
32 33 34 35
        end
      end
    end

36 37 38 39 40 41 42 43 44
    context 'when the noteable is a commit' do
      let(:commit) { project.repository.head_commit }
      let(:note) { create(:note_on_commit, commit_id: commit.id, author: user, project: project) }

      context 'when the project is private' do
        let(:project) { create(:project, :private, :repository) }

        it_behaves_like 'a discussion with a private noteable'
      end
45

46 47 48 49 50
      context 'when the project is public' do
        context 'when repository access level is private' do
          let(:project) { create(:project, :public, :repository, :repository_private) }

          it_behaves_like 'a discussion with a private noteable'
51 52 53 54
        end
      end
    end

55 56 57
    context 'when the project is public' do
      context 'when the note author is not a project member' do
        it 'can edit a note' do
58 59 60
          expect(policy).to be_allowed(:admin_note)
          expect(policy).to be_allowed(:resolve_note)
          expect(policy).to be_allowed(:read_note)
61 62 63
        end
      end

64
      context 'when the noteable is a project snippet' do
65
        let(:noteable) { create(:project_snippet, :public, project: project) }
66

67 68 69 70
        it 'can edit note' do
          expect(policy).to be_allowed(:admin_note)
          expect(policy).to be_allowed(:resolve_note)
          expect(policy).to be_allowed(:read_note)
71 72 73
        end

        context 'when it is private' do
74 75 76
          let(:noteable) { create(:project_snippet, :private, project: project) }

          it_behaves_like 'a discussion with a private noteable'
77 78 79 80
        end
      end

      context 'when the noteable is a personal snippet' do
81
        let(:noteable) { create(:personal_snippet, :public) }
82

83 84 85 86
        it 'can edit note' do
          expect(policy).to be_allowed(:admin_note)
          expect(policy).to be_allowed(:resolve_note)
          expect(policy).to be_allowed(:read_note)
87
        end
88 89

        context 'when it is private' do
90
          let(:noteable) { create(:personal_snippet, :private) }
91

92 93 94 95
          it 'can not edit nor read the note' do
            expect(policy).to be_disallowed(:admin_note)
            expect(policy).to be_disallowed(:resolve_note)
            expect(policy).to be_disallowed(:read_note)
96 97 98 99 100 101 102 103 104 105
          end
        end
      end

      context 'when a discussion is confidential' do
        before do
          issue.update_attribute(:confidential, true)
        end

        it_behaves_like 'a discussion with a private noteable'
106 107
      end

108 109 110 111 112 113 114 115 116 117
      context 'when a discussion is locked' do
        before do
          issue.update_attribute(:discussion_locked, true)
        end

        context 'when the note author is a project member' do
          before do
            project.add_developer(user)
          end

118
          it 'can edit a note' do
119 120 121
            expect(policy).to be_allowed(:admin_note)
            expect(policy).to be_allowed(:resolve_note)
            expect(policy).to be_allowed(:read_note)
122 123 124 125 126
          end
        end

        context 'when the note author is not a project member' do
          it 'can not edit a note' do
127 128
            expect(policy).to be_disallowed(:admin_note)
            expect(policy).to be_disallowed(:resolve_note)
129 130 131
          end

          it 'can read a note' do
132
            expect(policy).to be_allowed(:read_note)
133 134 135 136 137 138
          end
        end
      end
    end
  end
end