summaryrefslogtreecommitdiff
path: root/spec/requests/api/namespaces_spec.rb
blob: c1edf384d5cccd3622b31bb985f7ec1f1f12cd35 (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
require 'spec_helper'

describe API::Namespaces, api: true  do
  include ApiHelpers
  let(:admin) { create(:admin) }
  let(:user) { create(:user) }
  let!(:group1) { create(:group) }
  let!(:group2) { create(:group) }

  describe "GET /namespaces" do
    context "when unauthenticated" do
      it "returns authentication error" do
        get api("/namespaces")
        expect(response).to have_http_status(401)
      end
    end

    context "when authenticated as admin" do
      it "admin: returns an array of all namespaces" do
        get api("/namespaces", admin)
        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array

        expect(json_response.length).to eq(Namespace.count)
      end

      it "admin: returns an array of matched namespaces" do
        get api("/namespaces?search=#{group1.name}", admin)
        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array

        expect(json_response.length).to eq(1)
      end
    end

    context "when authenticated as a regular user" do
      it "user: returns an array of namespaces" do
        get api("/namespaces", user)
        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array

        expect(json_response.length).to eq(1)
      end

      it "admin: returns an array of matched namespaces" do
        get api("/namespaces?search=#{user.username}", user)
        expect(response).to have_http_status(200)
        expect(json_response).to be_an Array

        expect(json_response.length).to eq(1)
      end
    end
  end
end