require 'workers/create_server_worker'

RSpec.describe CreateServerWorker, type: :worker do
  let(:worker) { described_class.new }
  let(:server_attrs) { {'environment' => 'foo'} }
  let(:category) { build(:category) }
  let(:env) { build(:environment) }
  let(:project) { build(:project) }

  before do
    allow(worker).to receive(:set_task_data)
    allow(project).to receive(:environment) { env }
    allow(env).to receive(:get_category) { category }
    allow_any_instance_of(Devops::Executor::ServerExecutor).to receive(:create_server)
    allow(Devops::Model::Project).to receive_message_chain(:find_with_environment) { project }
    allow(Sidekiq.logger).to receive(:info)
  end

  it 'requires server attrs and owner' do
    expect{ worker.perform('owner' => 'user') }.to raise_error KeyError
    expect{ worker.perform('server_attrs' => {}) }.to raise_error KeyError
  end

  it 'updates task' do
    expect(worker).to receive(:set_task_data)
    worker.perform('owner' => 'user', 'server_attrs' => server_attrs)
  end

  it 'calls #create_server on executor' do
    expect_any_instance_of(Devops::Executor::ServerExecutor).to receive(:create_server)
    worker.perform('owner' => 'user', 'server_attrs' => server_attrs)
  end
end
