BigW Consortium Gitlab

namespaces_spec.rb 3.44 KB
Newer Older
1 2
require 'spec_helper'

3
describe API::Namespaces do
4
  let(:admin) { create(:admin) }
5
  let(:user) { create(:user) }
6
  let!(:group1) { create(:group) }
7
  let!(:group2) { create(:group, :nested) }
8 9 10

  describe "GET /namespaces" do
    context "when unauthenticated" do
11
      it "returns authentication error" do
12
        get api("/namespaces")
13
        expect(response).to have_gitlab_http_status(401)
14 15 16
      end
    end

17
    context "when authenticated as admin" do
18 19 20
      it "returns correct attributes" do
        get api("/namespaces", admin)

21 22 23
        group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' }
        user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' }

24
        expect(response).to have_gitlab_http_status(200)
25
        expect(response).to include_pagination_headers
26 27 28 29
        expect(group_kind_json_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
                                                                 'parent_id', 'members_count_with_descendants')

        expect(user_kind_json_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
30 31
      end

32
      it "admin: returns an array of all namespaces" do
33
        get api("/namespaces", admin)
34

35
        expect(response).to have_gitlab_http_status(200)
36
        expect(response).to include_pagination_headers
37 38
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(Namespace.count)
39
      end
40

41
      it "admin: returns an array of matched namespaces" do
42
        get api("/namespaces?search=#{group2.name}", admin)
43

44
        expect(response).to have_gitlab_http_status(200)
45
        expect(response).to include_pagination_headers
46 47
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
48 49
        expect(json_response.last['path']).to eq(group2.path)
        expect(json_response.last['full_path']).to eq(group2.full_path)
50 51 52 53
      end
    end

    context "when authenticated as a regular user" do
Oswaldo Ferreira committed
54
      it "returns correct attributes when user can admin group" do
55 56
        group1.add_owner(user)

57 58
        get api("/namespaces", user)

59 60 61 62 63 64
        owned_group_response = json_response.find { |resource| resource['id'] == group1.id }

        expect(owned_group_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
                                                             'parent_id', 'members_count_with_descendants')
      end

Oswaldo Ferreira committed
65
      it "returns correct attributes when user cannot admin group" do
66 67 68 69 70 71 72
        group1.add_guest(user)

        get api("/namespaces", user)

        guest_group_response = json_response.find { |resource| resource['id'] == group1.id }

        expect(guest_group_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
73 74
      end

75
      it "user: returns an array of namespaces" do
76
        get api("/namespaces", user)
77

78
        expect(response).to have_gitlab_http_status(200)
79
        expect(response).to include_pagination_headers
80 81 82 83
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
      end

84
      it "admin: returns an array of matched namespaces" do
85
        get api("/namespaces?search=#{user.username}", user)
86

87
        expect(response).to have_gitlab_http_status(200)
88
        expect(response).to include_pagination_headers
89 90 91
        expect(json_response).to be_an Array
        expect(json_response.length).to eq(1)
      end
92 93 94
    end
  end
end