From e1867c38fc5a4b931b4b2256d4909182e94f1051 Mon Sep 17 00:00:00 2001 From: GitLab Bot Date: Fri, 6 Dec 2019 18:07:44 +0000 Subject: Add latest changes from gitlab-org/gitlab@master --- spec/requests/api/deployments_spec.rb | 10 ++++ spec/requests/api/pipelines_spec.rb | 14 +++++ spec/requests/api/projects_spec.rb | 110 ++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) (limited to 'spec/requests') diff --git a/spec/requests/api/deployments_spec.rb b/spec/requests/api/deployments_spec.rb index 986a44cc640..6e78b747a8c 100644 --- a/spec/requests/api/deployments_spec.rb +++ b/spec/requests/api/deployments_spec.rb @@ -30,6 +30,16 @@ describe API::Deployments do expect(json_response.last['iid']).to eq(deployment_3.iid) end + context 'with updated_at filters specified' do + it 'returns projects deployments with last update in specified datetime range' do + get api("/projects/#{project.id}/deployments", user), params: { updated_before: 30.minutes.ago, updated_after: 90.minutes.ago } + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to include_pagination_headers + expect(json_response.first['id']).to eq(deployment_3.id) + end + end + describe 'ordering' do let(:order_by) { 'iid' } let(:sort) { 'desc' } diff --git a/spec/requests/api/pipelines_spec.rb b/spec/requests/api/pipelines_spec.rb index 2b34b64812d..a9d570b5696 100644 --- a/spec/requests/api/pipelines_spec.rb +++ b/spec/requests/api/pipelines_spec.rb @@ -237,6 +237,20 @@ describe API::Pipelines do end end + context 'when updated_at filters are specified' do + let!(:pipeline1) { create(:ci_pipeline, project: project, updated_at: 2.days.ago) } + let!(:pipeline2) { create(:ci_pipeline, project: project, updated_at: 4.days.ago) } + let!(:pipeline3) { create(:ci_pipeline, project: project, updated_at: 1.hour.ago) } + + it 'returns pipelines with last update date in specified datetime range' do + get api("/projects/#{project.id}/pipelines", user), params: { updated_before: 1.day.ago, updated_after: 3.days.ago } + + expect(response).to have_gitlab_http_status(:ok) + expect(response).to include_pagination_headers + expect(json_response.first['id']).to eq(pipeline1.id) + end + end + context 'when order_by and sort are specified' do context 'when order_by user_id' do before do diff --git a/spec/requests/api/projects_spec.rb b/spec/requests/api/projects_spec.rb index cda2dd7d2f4..5161de6de5e 100644 --- a/spec/requests/api/projects_spec.rb +++ b/spec/requests/api/projects_spec.rb @@ -155,6 +155,35 @@ describe API::Projects do project4 end + # This is a regression spec for https://gitlab.com/gitlab-org/gitlab/issues/37919 + context 'batch counting forks and open issues and refreshing count caches' do + # We expect to count these projects (only the ones on the first page, not all matching ones) + let(:projects) { Project.public_to_user(nil).order(id: :desc).first(per_page) } + let(:per_page) { 2 } + let(:count_service) { double } + + before do + # Create more projects, so we have more than one page + create_list(:project, 5, :public) + end + + it 'batch counts project forks' do + expect(::Projects::BatchForksCountService).to receive(:new).with(projects).and_return(count_service) + expect(count_service).to receive(:refresh_cache) + + get api("/projects?per_page=#{per_page}") + expect(response.status).to eq 200 + end + + it 'batch counts open issues' do + expect(::Projects::BatchOpenIssuesCountService).to receive(:new).with(projects).and_return(count_service) + expect(count_service).to receive(:refresh_cache) + + get api("/projects?per_page=#{per_page}") + expect(response.status).to eq 200 + end + end + context 'when unauthenticated' do it_behaves_like 'projects response' do let(:filter) { { search: project.name } } @@ -570,6 +599,87 @@ describe API::Projects do let(:projects) { Project.all } end end + + context 'with keyset pagination' do + let(:current_user) { user } + let(:projects) { [public_project, project, project2, project3] } + + context 'headers and records' do + let(:params) { { pagination: 'keyset', order_by: :id, sort: :asc, per_page: 1 } } + + it 'includes a pagination header with link to the next page' do + get api('/projects', current_user), params: params + + expect(response.header).to include('Links') + expect(response.header['Links']).to include('pagination=keyset') + expect(response.header['Links']).to include("id_after=#{public_project.id}") + end + + it 'contains only the first project with per_page = 1' do + get api('/projects', current_user), params: params + + expect(response).to have_gitlab_http_status(200) + expect(json_response).to be_an Array + expect(json_response.map { |p| p['id'] }).to contain_exactly(public_project.id) + end + + it 'does not include a link if the end has reached and there is no more data' do + get api('/projects', current_user), params: params.merge(id_after: project2.id) + + expect(response.header).not_to include('Links') + end + + it 'responds with 501 if order_by is different from id' do + get api('/projects', current_user), params: params.merge(order_by: :created_at) + + expect(response).to have_gitlab_http_status(405) + end + end + + context 'with descending sorting' do + let(:params) { { pagination: 'keyset', order_by: :id, sort: :desc, per_page: 1 } } + + it 'includes a pagination header with link to the next page' do + get api('/projects', current_user), params: params + + expect(response.header).to include('Links') + expect(response.header['Links']).to include('pagination=keyset') + expect(response.header['Links']).to include("id_before=#{project3.id}") + end + + it 'contains only the last project with per_page = 1' do + get api('/projects', current_user), params: params + + expect(response).to have_gitlab_http_status(200) + expect(json_response).to be_an Array + expect(json_response.map { |p| p['id'] }).to contain_exactly(project3.id) + end + end + + context 'retrieving the full relation' do + let(:params) { { pagination: 'keyset', order_by: :id, sort: :desc, per_page: 2 } } + + it 'returns all projects' do + url = '/projects' + requests = 0 + ids = [] + + while url && requests <= 5 # circuit breaker + requests += 1 + get api(url, current_user), params: params + + links = response.header['Links'] + url = links&.match(/<[^>]+(\/projects\?[^>]+)>; rel="next"/) do |match| + match[1] + end + + ids += JSON.parse(response.body).map { |p| p['id'] } + end + + expect(ids).to contain_exactly(*projects.map(&:id)) + end + end + end end describe 'POST /projects' do -- cgit v1.2.3