BigW Consortium Gitlab

ability_spec.rb 8.13 KB
Newer Older
Yorick Peterse committed
1 2
require 'spec_helper'

3
describe Ability do
4
  context 'using a nil subject' do
5
    it 'has no permissions' do
6
      expect(described_class.policy_for(nil, nil)).to be_banned
7 8 9
    end
  end

10
  describe '.can_edit_note?' do
11
    let(:project) { create(:project) }
12
    let(:note) { create(:note_on_issue, project: project) }
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

    context 'using an anonymous user' do
      it 'returns false' do
        expect(described_class.can_edit_note?(nil, note)).to be_falsy
      end
    end

    context 'using a system note' do
      it 'returns false' do
        system_note = create(:note, system: true)
        user = create(:user)

        expect(described_class.can_edit_note?(user, system_note)).to be_falsy
      end
    end

    context 'using users with different access levels' do
      let(:user) { create(:user) }

      it 'returns true for the author' do
        expect(described_class.can_edit_note?(note.author, note)).to be_truthy
      end

      it 'returns false for a guest user' do
        project.team << [user, :guest]

        expect(described_class.can_edit_note?(user, note)).to be_falsy
      end

      it 'returns false for a developer' do
        project.team << [user, :developer]

        expect(described_class.can_edit_note?(user, note)).to be_falsy
      end

      it 'returns true for a master' do
        project.team << [user, :master]

        expect(described_class.can_edit_note?(user, note)).to be_truthy
      end

      it 'returns true for a group owner' do
        group = create(:group)
        project.project_group_links.create(
          group: group,
          group_access: Gitlab::Access::MASTER)
        group.add_owner(user)

        expect(described_class.can_edit_note?(user, note)).to be_truthy
      end
    end
  end

Yorick Peterse committed
66 67 68
  describe '.users_that_can_read_project' do
    context 'using a public project' do
      it 'returns all the users' do
69
        project = create(:project, :public)
Yorick Peterse committed
70 71
        user = build(:user)

72 73
        expect(described_class.users_that_can_read_project([user], project))
          .to eq([user])
Yorick Peterse committed
74 75 76 77
      end
    end

    context 'using an internal project' do
78
      let(:project) { create(:project, :internal) }
Yorick Peterse committed
79 80 81 82

      it 'returns users that are administrators' do
        user = build(:user, admin: true)

83 84
        expect(described_class.users_that_can_read_project([user], project))
          .to eq([user])
Yorick Peterse committed
85 86 87 88 89 90 91
      end

      it 'returns internal users while skipping external users' do
        user1 = build(:user)
        user2 = build(:user, external: true)
        users = [user1, user2]

92 93
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([user1])
Yorick Peterse committed
94 95 96 97 98 99 100
      end

      it 'returns external users if they are the project owner' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

101
        expect(project).to receive(:owner).at_least(:once).and_return(user1)
Yorick Peterse committed
102

103 104
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([user1])
Yorick Peterse committed
105 106 107 108 109 110 111
      end

      it 'returns external users if they are project members' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

112
        expect(project.team).to receive(:members).at_least(:once).and_return([user1])
Yorick Peterse committed
113

114 115
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([user1])
Yorick Peterse committed
116 117 118 119 120 121 122
      end

      it 'returns an empty Array if all users are external users without access' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

123 124
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([])
Yorick Peterse committed
125 126 127 128
      end
    end

    context 'using a private project' do
129
      let(:project) { create(:project, :private) }
Yorick Peterse committed
130 131 132 133

      it 'returns users that are administrators' do
        user = build(:user, admin: true)

134 135
        expect(described_class.users_that_can_read_project([user], project))
          .to eq([user])
Yorick Peterse committed
136 137 138 139 140 141 142
      end

      it 'returns external users if they are the project owner' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

143
        expect(project).to receive(:owner).at_least(:once).and_return(user1)
Yorick Peterse committed
144

145 146
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([user1])
Yorick Peterse committed
147 148 149 150 151 152 153
      end

      it 'returns external users if they are project members' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

154
        expect(project.team).to receive(:members).at_least(:once).and_return([user1])
Yorick Peterse committed
155

156 157
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([user1])
Yorick Peterse committed
158 159 160 161 162 163 164
      end

      it 'returns an empty Array if all users are internal users without access' do
        user1 = build(:user)
        user2 = build(:user)
        users = [user1, user2]

165 166
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([])
Yorick Peterse committed
167 168 169 170 171 172 173
      end

      it 'returns an empty Array if all users are external users without access' do
        user1 = build(:user, external: true)
        user2 = build(:user, external: true)
        users = [user1, user2]

174 175
        expect(described_class.users_that_can_read_project(users, project))
          .to eq([])
Yorick Peterse committed
176 177 178
      end
    end
  end
179

180
  describe '.users_that_can_read_personal_snippet' do
Jarka Kadlecova committed
181 182 183 184
    def users_for_snippet(snippet)
      described_class.users_that_can_read_personal_snippet(users, snippet)
    end

185 186 187
    let(:users)  { create_list(:user, 3) }
    let(:author) { users[0] }

Jarka Kadlecova committed
188 189
    it 'private snippet is readable only by its author' do
      snippet = create(:personal_snippet, :private, author: author)
190

Jarka Kadlecova committed
191
      expect(users_for_snippet(snippet)).to match_array([author])
192 193
    end

Jarka Kadlecova committed
194 195
    it 'internal snippet is readable by all registered users' do
      snippet = create(:personal_snippet, :public, author: author)
196

Jarka Kadlecova committed
197
      expect(users_for_snippet(snippet)).to match_array(users)
198 199
    end

Jarka Kadlecova committed
200 201
    it 'public snippet is readable by all users' do
      snippet = create(:personal_snippet, :public, author: author)
202

Jarka Kadlecova committed
203
      expect(users_for_snippet(snippet)).to match_array(users)
204 205 206
    end
  end

207 208 209 210 211 212
  describe '.issues_readable_by_user' do
    context 'with an admin user' do
      it 'returns all given issues' do
        user = build(:user, admin: true)
        issue = build(:issue)

213 214
        expect(described_class.issues_readable_by_user([issue], user))
          .to eq([issue])
215 216 217 218 219 220 221 222 223 224
      end
    end

    context 'with a regular user' do
      it 'returns the issues readable by the user' do
        user = build(:user)
        issue = build(:issue)

        expect(issue).to receive(:readable_by?).with(user).and_return(true)

225 226
        expect(described_class.issues_readable_by_user([issue], user))
          .to eq([issue])
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      end

      it 'returns an empty Array when no issues are readable' do
        user = build(:user)
        issue = build(:issue)

        expect(issue).to receive(:readable_by?).with(user).and_return(false)

        expect(described_class.issues_readable_by_user([issue], user)).to eq([])
      end
    end

    context 'without a regular user' do
      it 'returns issues that are publicly visible' do
        hidden_issue = build(:issue)
        visible_issue = build(:issue)

        expect(hidden_issue).to receive(:publicly_visible?).and_return(false)
        expect(visible_issue).to receive(:publicly_visible?).and_return(true)

247 248
        issues = described_class
          .issues_readable_by_user([hidden_issue, visible_issue])
249 250 251 252 253

        expect(issues).to eq([visible_issue])
      end
    end
  end
254 255

  describe '.project_disabled_features_rules' do
256
    let(:project) { create(:project, :wiki_disabled) }
257

258
    subject { described_class.policy_for(project.owner, project) }
259 260 261

    context 'wiki named abilities' do
      it 'disables wiki abilities if the project has no wiki' do
262
        expect(project).to receive(:has_external_wiki?).and_return(false)
263 264 265 266
        expect(subject).not_to be_allowed(:read_wiki)
        expect(subject).not_to be_allowed(:create_wiki)
        expect(subject).not_to be_allowed(:update_wiki)
        expect(subject).not_to be_allowed(:admin_wiki)
267 268 269
      end
    end
  end
Yorick Peterse committed
270
end