2019-10-16 09:07:51 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-07 00:06:46 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2021-04-22 09:09:45 +00:00
|
|
|
RSpec.describe Environments::EnvironmentsFinder do
|
2021-07-07 00:07:23 +00:00
|
|
|
let_it_be(:project) { create(:project, :repository) }
|
|
|
|
let_it_be(:user) { project.creator }
|
|
|
|
let_it_be(:environment) { create(:environment, :available, project: project) }
|
|
|
|
let_it_be(:environment_stopped) { create(:environment, :stopped, name: 'test2', project: project) }
|
|
|
|
let_it_be(:environment_available) { create(:environment, :available, name: 'test3', project: project) }
|
2017-02-07 00:06:46 +00:00
|
|
|
|
2020-04-08 00:09:30 +00:00
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
2017-02-07 00:06:46 +00:00
|
|
|
|
2021-04-14 00:10:39 +00:00
|
|
|
describe '#execute' do
|
2020-04-08 00:09:30 +00:00
|
|
|
context 'with states parameter' do
|
2021-07-07 00:07:23 +00:00
|
|
|
let_it_be(:stopped_environment) { create(:environment, :stopped, project: project) }
|
2020-04-08 00:09:30 +00:00
|
|
|
|
|
|
|
it 'returns environments with the requested state' do
|
2021-04-14 00:10:39 +00:00
|
|
|
result = described_class.new(project, user, states: 'available').execute
|
2020-04-08 00:09:30 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
expect(result).to contain_exactly(environment, environment_available)
|
2020-04-08 00:09:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns environments with any of the requested states' do
|
2021-04-14 00:10:39 +00:00
|
|
|
result = described_class.new(project, user, states: %w(available stopped)).execute
|
2020-04-08 00:09:30 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
expect(result).to contain_exactly(environment, environment_stopped, environment_available, stopped_environment)
|
2020-04-08 00:09:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises exception when requested state is invalid' do
|
2021-04-14 00:10:39 +00:00
|
|
|
expect { described_class.new(project, user, states: %w(invalid stopped)).execute }.to(
|
2020-04-08 00:09:30 +00:00
|
|
|
raise_error(described_class::InvalidStatesError, 'Requested states are invalid')
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'works with symbols' do
|
|
|
|
it 'returns environments with the requested state' do
|
2021-04-14 00:10:39 +00:00
|
|
|
result = described_class.new(project, user, states: :available).execute
|
2020-04-08 00:09:30 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
expect(result).to contain_exactly(environment, environment_available)
|
2020-04-08 00:09:30 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns environments with any of the requested states' do
|
2021-04-14 00:10:39 +00:00
|
|
|
result = described_class.new(project, user, states: [:available, :stopped]).execute
|
2020-04-08 00:09:30 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
expect(result).to contain_exactly(environment, environment_stopped, environment_available, stopped_environment)
|
2020-04-08 00:09:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with search and states' do
|
|
|
|
it 'searches environments by name and state' do
|
2021-04-14 00:10:39 +00:00
|
|
|
result = described_class.new(project, user, search: 'test', states: :available).execute
|
2020-04-08 00:09:30 +00:00
|
|
|
|
2021-07-07 00:07:23 +00:00
|
|
|
expect(result).to contain_exactly(environment_available)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with id' do
|
|
|
|
it 'searches environments by name and state' do
|
|
|
|
result = described_class.new(project, user, search: 'test', environment_ids: [environment_available.id]).execute
|
|
|
|
|
|
|
|
expect(result).to contain_exactly(environment_available)
|
2020-04-08 00:09:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-02-07 00:06:46 +00:00
|
|
|
end
|