autocomplete_controller_spec.rb 3.83 KB
Newer Older
1 2 3 4 5 6
require 'spec_helper'

describe AutocompleteController do
  let!(:project) { create(:project) }
  let!(:user)    { create(:user) }
  let!(:user2)   { create(:user) }
7
  let!(:non_member)   { create(:user) }
8 9 10 11 12 13 14

  context 'project members' do
    before do
      sign_in(user)
      project.team << [user, :master]
    end

15 16 17 18 19
    describe 'GET #users with project ID' do
      before do
        get(:users, project_id: project.id)
      end

20 21
      let(:body) { JSON.parse(response.body) }

22 23
      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq 1 }
24
      it { expect(body.map { |u| u["username"] }).to include(user.username) }
25 26 27 28 29 30 31
    end

    describe 'GET #users with unknown project' do
      before do
        get(:users, project_id: 'unknown')
      end

32
      it { expect(response).to have_http_status(404) }
33
    end
34 35 36 37 38 39 40 41 42 43 44 45
  end

  context 'group members' do
    let(:group) { create(:group) }

    before do
      sign_in(user)
      group.add_owner(user)
    end

    let(:body) { JSON.parse(response.body) }

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    describe 'GET #users with group ID' do
      before do
        get(:users, group_id: group.id)
      end

      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq 1 }
      it { expect(body.first["username"]).to eq user.username }
    end

    describe 'GET #users with unknown group ID' do
      before do
        get(:users, group_id: 'unknown')
      end

61
      it { expect(response).to have_http_status(404) }
62
    end
63 64
  end

65 66 67 68 69 70 71 72 73 74 75 76
  context 'non-member login for public project' do
    let!(:project) { create(:project, :public) }

    before do
      sign_in(non_member)
      project.team << [user, :master]
    end

    let(:body) { JSON.parse(response.body) }

    describe 'GET #users with project ID' do
      before do
77
        get(:users, project_id: project.id, current_user: true)
78 79 80 81 82 83 84 85
      end

      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq 2 }
      it { expect(body.map { |u| u['username'] }).to match_array([user.username, non_member.username]) }
    end
  end

86 87 88 89 90 91 92 93
  context 'all users' do
    before do
      sign_in(user)
      get(:users)
    end

    let(:body) { JSON.parse(response.body) }

94
    it { expect(body).to be_kind_of(Array) }
95
    it { expect(body.size).to eq User.count }
96
  end
97 98

  context 'unauthenticated user' do
99
    let(:public_project) { create(:project, :public) }
100 101 102 103
    let(:body) { JSON.parse(response.body) }

    describe 'GET #users with public project' do
      before do
104 105
        public_project.team << [user, :guest]
        get(:users, project_id: public_project.id)
106 107 108 109 110 111
      end

      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq 1 }
    end

112 113 114 115 116
    describe 'GET #users with project' do
      before do
        get(:users, project_id: project.id)
      end

117
      it { expect(response).to have_http_status(404) }
118 119 120 121 122 123 124
    end

    describe 'GET #users with unknown project' do
      before do
        get(:users, project_id: 'unknown')
      end

125
      it { expect(response).to have_http_status(404) }
126 127 128 129 130 131 132 133
    end

    describe 'GET #users with inaccessible group' do
      before do
        project.team << [user, :guest]
        get(:users, group_id: user.namespace.id)
      end

134
      it { expect(response).to have_http_status(404) }
135 136
    end

137 138 139 140 141
    describe 'GET #users with no project' do
      before do
        get(:users)
      end

142 143
      it { expect(body).to be_kind_of(Array) }
      it { expect(body.size).to eq 0 }
144 145
    end
  end
146 147 148 149 150 151 152 153

  context 'author of issuable included' do
    before do
      sign_in(user)
    end

    let(:body) { JSON.parse(response.body) }

154 155 156
    it 'includes the author' do
      get(:users, author_id: non_member.id)

157 158
      expect(body.first["username"]).to eq non_member.username
    end
159 160 161 162 163 164

    it 'rejects non existent user ids' do
      get(:users, author_id: 99999)

      expect(body.collect { |u| u['id'] }).not_to include(99999)
    end
165
  end
166
end