summaryrefslogtreecommitdiff
path: root/spec/support/api/boards_shared_examples.rb
blob: b7aff32460d5bf3135102849d16b2950736f9a7b (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# frozen_string_literal: true

shared_examples_for 'group and project boards' do |route_definition, ee = false|
  let(:root_url) { route_definition.gsub(":id", board_parent.id.to_s) }

  before do
    board_parent.add_reporter(user)
    board_parent.add_guest(guest)
  end

  def expect_schema_match_for(response, schema_file, ee)
    if ee
      expect(response).to match_response_schema(schema_file, dir: "ee")
    else
      expect(response).to match_response_schema(schema_file)
    end
  end

  it 'avoids N+1 queries' do
    pat = create(:personal_access_token, user: user)
    control = ActiveRecord::QueryRecorder.new { get api(root_url, personal_access_token: pat) }

    create(:milestone, "#{board_parent.class.name.underscore}": board_parent)
    create(:board, "#{board_parent.class.name.underscore}": board_parent)

    expect { get api(root_url, personal_access_token: pat) }.not_to exceed_query_limit(control)
  end

  describe "GET #{route_definition}" do
    context "when unauthenticated" do
      it "returns authentication error" do
        get api(root_url)

        expect(response).to have_gitlab_http_status(401)
      end
    end

    context "when authenticated" do
      it "returns the issue boards" do
        get api(root_url, user)

        expect(response).to have_gitlab_http_status(200)
        expect(response).to include_pagination_headers

        expect_schema_match_for(response, 'public_api/v4/boards', ee)
      end

      describe "GET #{route_definition}/:board_id" do
        let(:url) { "#{root_url}/#{board.id}" }

        it 'get a single board by id' do
          get api(url, user)

          expect_schema_match_for(response, 'public_api/v4/board', ee)
        end
      end
    end
  end

  describe "GET #{route_definition}/:board_id/lists" do
    let(:url) { "#{root_url}/#{board.id}/lists" }

    it 'returns issue board lists' do
      get api(url, user)

      expect(response).to have_gitlab_http_status(200)
      expect(response).to include_pagination_headers
      expect(json_response).to be_an Array
      expect(json_response.length).to eq(2)
      expect(json_response.first['label']['name']).to eq(dev_label.title)
    end

    it 'returns 404 if board not found' do
      get api("#{root_url}/22343/lists", user)

      expect(response).to have_gitlab_http_status(404)
    end
  end

  describe "GET #{route_definition}/:board_id/lists/:list_id" do
    let(:url) { "#{root_url}/#{board.id}/lists" }

    it 'returns a list' do
      get api("#{url}/#{dev_list.id}", user)

      expect(response).to have_gitlab_http_status(200)
      expect(json_response['id']).to eq(dev_list.id)
      expect(json_response['label']['name']).to eq(dev_label.title)
      expect(json_response['position']).to eq(1)
    end

    it 'returns 404 if list not found' do
      get api("#{url}/5324", user)

      expect(response).to have_gitlab_http_status(404)
    end
  end

  describe "POST #{route_definition}/lists" do
    let(:url) { "#{root_url}/#{board.id}/lists" }

    it 'creates a new issue board list for labels' do
      post api(url, user), params: { label_id: ux_label.id }

      expect(response).to have_gitlab_http_status(201)
      expect(json_response['label']['name']).to eq(ux_label.title)
      expect(json_response['position']).to eq(3)
    end

    it 'returns 400 when creating a new list if label_id is invalid' do
      post api(url, user), params: { label_id: 23423 }

      expect(response).to have_gitlab_http_status(400)
    end

    it 'returns 403 for members with guest role' do
      put api("#{url}/#{test_list.id}", guest), params: { position: 1 }

      expect(response).to have_gitlab_http_status(403)
    end
  end

  describe "PUT #{route_definition}/:board_id/lists/:list_id to update only position" do
    let(:url) { "#{root_url}/#{board.id}/lists" }

    it "updates a list" do
      put api("#{url}/#{test_list.id}", user), params: { position: 1 }

      expect(response).to have_gitlab_http_status(200)
      expect(json_response['position']).to eq(1)
    end

    it "returns 404 error if list id not found" do
      put api("#{url}/44444", user), params: { position: 1 }

      expect(response).to have_gitlab_http_status(404)
    end

    it "returns 403 for members with guest role" do
      put api("#{url}/#{test_list.id}", guest), params: { position: 1 }

      expect(response).to have_gitlab_http_status(403)
    end
  end

  describe "DELETE #{route_definition}/lists/:list_id" do
    let(:url) { "#{root_url}/#{board.id}/lists" }

    it "rejects a non member from deleting a list" do
      delete api("#{url}/#{dev_list.id}", non_member)

      expect(response).to have_gitlab_http_status(403)
    end

    it "rejects a user with guest role from deleting a list" do
      delete api("#{url}/#{dev_list.id}", guest)

      expect(response).to have_gitlab_http_status(403)
    end

    it "returns 404 error if list id not found" do
      delete api("#{url}/44444", user)

      expect(response).to have_gitlab_http_status(404)
    end

    context "when the user is parent owner" do
      set(:owner) { create(:user) }

      before do
        if board_parent.try(:namespace)
          board_parent.update(namespace: owner.namespace)
        else
          board.parent.add_owner(owner)
        end
      end

      it "deletes the list if an admin requests it" do
        delete api("#{url}/#{dev_list.id}", owner)

        expect(response).to have_gitlab_http_status(204)
      end

      it_behaves_like '412 response' do
        let(:request) { api("#{url}/#{dev_list.id}", owner) }
      end
    end
  end
end