summaryrefslogtreecommitdiff
path: root/spec/controllers/autocomplete_controller_spec.rb
blob: 1230017c27099c059004824f6927f6c4a3182a4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
require 'spec_helper'

describe AutocompleteController do
  let!(:project) { create(:project) }
  let!(:user)    { create(:user) }
  let!(:user2)   { create(:user) }

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

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

    describe 'GET #users with project ID' do
      before do
        get(:users, project_id: project.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 project' do
      before do
        get(:users, project_id: 'unknown')
      end

      it { expect(response.status).to eq(404) }
    end
  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) }

    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

      it { expect(response.status).to eq(404) }
    end
  end

  context 'all users' do
    before do
      sign_in(user)
      get(:users)
    end

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

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

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

    describe 'GET #users with public project' do
      before do
        public_project.team << [user, :guest]
        get(:users, project_id: public_project.id)
      end

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

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

      it { expect(response.status).to eq(302) }
    end

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

      it { expect(response.status).to eq(302) }
    end

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

      it { expect(response.status).to eq(302) }
    end

    describe 'GET #users with no project' do
      before do
        get(:users)
      end

      it { expect(response.status).to eq(302) }
    end
  end
end