BigW Consortium Gitlab

issue_policy_spec.rb 9.67 KB
Newer Older
1 2 3
require 'spec_helper'

describe IssuePolicy, models: true do
4 5 6 7 8 9 10 11
  let(:guest) { create(:user) }
  let(:author) { create(:user) }
  let(:assignee) { create(:user) }
  let(:reporter) { create(:user) }
  let(:group) { create(:group, :public) }
  let(:reporter_from_group_link) { create(:user) }

  def permissions(user, issue)
12
    described_class.new(user, issue)
13 14 15 16 17
  end

  context 'a private project' do
    let(:non_member) { create(:user) }
    let(:project) { create(:empty_project, :private) }
Clement Ho committed
18
    let(:issue) { create(:issue, project: project, assignees: [assignee], author: author) }
19 20 21 22 23 24 25 26 27 28 29 30 31 32
    let(:issue_no_assignee) { create(:issue, project: project) }

    before do
      project.team << [guest, :guest]
      project.team << [author, :guest]
      project.team << [assignee, :guest]
      project.team << [reporter, :reporter]

      group.add_reporter(reporter_from_group_link)

      create(:project_group_link, group: group, project: project)
    end

    it 'does not allow non-members to read issues' do
33 34
      expect(permissions(non_member, issue)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
      expect(permissions(non_member, issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
35 36 37
    end

    it 'allows guests to read issues' do
38 39
      expect(permissions(guest, issue)).to be_allowed(:read_issue)
      expect(permissions(guest, issue)).to be_disallowed(:update_issue, :admin_issue)
40

41 42
      expect(permissions(guest, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(guest, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
43 44 45
    end

    it 'allows reporters to read, update, and admin issues' do
46 47
      expect(permissions(reporter, issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
      expect(permissions(reporter, issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
48 49 50
    end

    it 'allows reporters from group links to read, update, and admin issues' do
51 52
      expect(permissions(reporter_from_group_link, issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
      expect(permissions(reporter_from_group_link, issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
53 54 55
    end

    it 'allows issue authors to read and update their issues' do
56 57
      expect(permissions(author, issue)).to be_allowed(:read_issue, :update_issue)
      expect(permissions(author, issue)).to be_disallowed(:admin_issue)
58

59 60
      expect(permissions(author, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(author, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
61 62 63
    end

    it 'allows issue assignees to read and update their issues' do
64 65
      expect(permissions(assignee, issue)).to be_allowed(:read_issue, :update_issue)
      expect(permissions(assignee, issue)).to be_disallowed(:admin_issue)
66

67 68
      expect(permissions(assignee, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(assignee, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
69
    end
70

71
    context 'with confidential issues' do
Clement Ho committed
72
      let(:confidential_issue) { create(:issue, :confidential, project: project, assignees: [assignee], author: author) }
73
      let(:confidential_issue_no_assignee) { create(:issue, :confidential, project: project) }
74

75
      it 'does not allow non-members to read confidential issues' do
76 77
        expect(permissions(non_member, confidential_issue)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(non_member, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
78 79 80
      end

      it 'does not allow guests to read confidential issues' do
81 82
        expect(permissions(guest, confidential_issue)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(guest, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
83
      end
84

85
      it 'allows reporters to read, update, and admin confidential issues' do
86 87
        expect(permissions(reporter, confidential_issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(reporter, confidential_issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
88
      end
89

90
      it 'allows reporters from group links to read, update, and admin confidential issues' do
91 92
        expect(permissions(reporter_from_group_link, confidential_issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(reporter_from_group_link, confidential_issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
93 94
      end

95
      it 'allows issue authors to read and update their confidential issues' do
96 97
        expect(permissions(author, confidential_issue)).to be_allowed(:read_issue, :update_issue)
        expect(permissions(author, confidential_issue)).to be_disallowed(:admin_issue)
98

99
        expect(permissions(author, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
100
      end
101

102
      it 'allows issue assignees to read and update their confidential issues' do
103 104
        expect(permissions(assignee, confidential_issue)).to be_allowed(:read_issue, :update_issue)
        expect(permissions(assignee, confidential_issue)).to be_disallowed(:admin_issue)
105

106
        expect(permissions(assignee, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
107 108
      end
    end
109
  end
110

111 112
  context 'a public project' do
    let(:project) { create(:empty_project, :public) }
Clement Ho committed
113
    let(:issue) { create(:issue, project: project, assignees: [assignee], author: author) }
114
    let(:issue_no_assignee) { create(:issue, project: project) }
115

116 117 118
    before do
      project.team << [guest, :guest]
      project.team << [reporter, :reporter]
119

120
      group.add_reporter(reporter_from_group_link)
121

122 123
      create(:project_group_link, group: group, project: project)
    end
124

125
    it 'allows guests to read issues' do
126 127
      expect(permissions(guest, issue)).to be_allowed(:read_issue)
      expect(permissions(guest, issue)).to be_disallowed(:update_issue, :admin_issue)
128

129 130
      expect(permissions(guest, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(guest, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
131 132 133
    end

    it 'allows reporters to read, update, and admin issues' do
134 135
      expect(permissions(reporter, issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
      expect(permissions(reporter, issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
136 137 138
    end

    it 'allows reporters from group links to read, update, and admin issues' do
139 140
      expect(permissions(reporter_from_group_link, issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
      expect(permissions(reporter_from_group_link, issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
141
    end
142

143
    it 'allows issue authors to read and update their issues' do
144 145
      expect(permissions(author, issue)).to be_allowed(:read_issue, :update_issue)
      expect(permissions(author, issue)).to be_disallowed(:admin_issue)
146

147 148
      expect(permissions(author, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(author, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
149 150 151
    end

    it 'allows issue assignees to read and update their issues' do
152 153
      expect(permissions(assignee, issue)).to be_allowed(:read_issue, :update_issue)
      expect(permissions(assignee, issue)).to be_disallowed(:admin_issue)
154

155 156
      expect(permissions(assignee, issue_no_assignee)).to be_allowed(:read_issue)
      expect(permissions(assignee, issue_no_assignee)).to be_disallowed(:update_issue, :admin_issue)
157
    end
158

159
    context 'with confidential issues' do
Clement Ho committed
160
      let(:confidential_issue) { create(:issue, :confidential, project: project, assignees: [assignee], author: author) }
161
      let(:confidential_issue_no_assignee) { create(:issue, :confidential, project: project) }
162

163
      it 'does not allow guests to read confidential issues' do
164 165
        expect(permissions(guest, confidential_issue)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(guest, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
166 167
      end

168
      it 'allows reporters to read, update, and admin confidential issues' do
169 170
        expect(permissions(reporter, confidential_issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(reporter, confidential_issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
171 172 173
      end

      it 'allows reporter from group links to read, update, and admin confidential issues' do
174 175
        expect(permissions(reporter_from_group_link, confidential_issue)).to be_allowed(:read_issue, :update_issue, :admin_issue)
        expect(permissions(reporter_from_group_link, confidential_issue_no_assignee)).to be_allowed(:read_issue, :update_issue, :admin_issue)
176
      end
177

178
      it 'allows issue authors to read and update their confidential issues' do
179 180
        expect(permissions(author, confidential_issue)).to be_allowed(:read_issue, :update_issue)
        expect(permissions(author, confidential_issue)).to be_disallowed(:admin_issue)
181

182
        expect(permissions(author, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
183
      end
184

185
      it 'allows issue assignees to read and update their confidential issues' do
186 187
        expect(permissions(assignee, confidential_issue)).to be_allowed(:read_issue, :update_issue)
        expect(permissions(assignee, confidential_issue)).to be_disallowed(:admin_issue)
188

189
        expect(permissions(assignee, confidential_issue_no_assignee)).to be_disallowed(:read_issue, :update_issue, :admin_issue)
190 191 192 193
      end
    end
  end
end