Commit 2aeb45bd authored by Grzegorz Bizon's avatar Grzegorz Bizon Committed by Filipa Lacerda

Add support for environment scopes in controller

parent b3309bb2
...@@ -9,17 +9,22 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -9,17 +9,22 @@ class Projects::EnvironmentsController < Projects::ApplicationController
before_action :verify_api_request!, only: :terminal_websocket_authorize before_action :verify_api_request!, only: :terminal_websocket_authorize
def index def index
@scope = params[:scope] @environments = project.environments
@environments = project.environments.includes(:last_deployment) .includes(:last_deployment)
.with_state(params[:scope] || :available)
respond_to do |format| respond_to do |format|
format.html format.html
format.json do format.json do
render json: EnvironmentSerializer render json: {
.new(project: @project, user: @current_user) environments: EnvironmentSerializer
.with_pagination(request, response) .new(project: @project, user: @current_user)
.within_folders .with_pagination(request, response)
.represent(@environments) .within_folders
.represent(@environments),
available_count: project.environments.available.count,
stopped_count: project.environments.stopped.count
}
end end
end end
end end
......
...@@ -7,8 +7,7 @@ describe Projects::EnvironmentsController do ...@@ -7,8 +7,7 @@ describe Projects::EnvironmentsController do
let(:project) { create(:empty_project) } let(:project) { create(:empty_project) }
let(:environment) do let(:environment) do
create(:environment, name: 'production', create(:environment, name: 'production', project: project)
project: project)
end end
before do before do
...@@ -28,18 +27,56 @@ describe Projects::EnvironmentsController do ...@@ -28,18 +27,56 @@ describe Projects::EnvironmentsController do
context 'when requesting JSON response for folders' do context 'when requesting JSON response for folders' do
before do before do
create(:environment, project: project, name: 'staging/review-1') create(:environment, project: project,
create(:environment, project: project, name: 'staging/review-2') name: 'staging/review-1',
state: :available)
create(:environment, project: project,
name: 'staging/review-2',
state: :available)
create(:environment, project: project,
name: 'staging/review-3',
state: :stopped)
end
let(:environments) { json_response['environments'] }
context 'when requesting available environments scope' do
before do
get :index, environment_params(format: :json, scope: :available)
end
it 'responds with a payload describing available environments' do
expect(environments.count).to eq 2
expect(environments.first['name']).to eq 'production'
expect(environments.second['name']).to eq 'staging'
expect(environments.second['size']).to eq 2
expect(environments.second['latest']['name']).to eq 'staging/review-2'
end
it 'contains values describing environment scopes sizes' do
expect(json_response['available_count']).to eq 3
expect(json_response['stopped_count']).to eq 1
end
end end
it 'responds with correct JSON' do context 'when requesting stopped environments scope' do
get :index, environment_params(format: :json) before do
get :index, environment_params(format: :json, scope: :stopped)
end
expect(json_response.count).to eq 2 it 'responds with a payload describing stopped environments' do
expect(json_response.first['name']).to eq 'production' expect(environments.count).to eq 1
expect(json_response.second['name']).to eq 'staging' expect(environments.first['name']).to eq 'staging'
expect(json_response.second['size']).to eq 2 expect(environments.first['size']).to eq 1
expect(json_response.second['latest']['name']).to eq 'staging/review-2' expect(environments.first['latest']['name']).to eq 'staging/review-3'
end
it 'contains values describing environment scopes sizes' do
expect(json_response['available_count']).to eq 3
expect(json_response['stopped_count']).to eq 1
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment