require 'db/mongo/models/project'

RSpec.describe Devops::Model::Project, type: :model do
  let(:project) { build(:project) }

  describe 'validation rules:', stubbed_logger: true do
    include_examples 'field type validation', :description, :maybe_nil, :maybe_empty_string
    include_examples 'field type validation', :named_tasks, :not_nil, :maybe_empty_array

    describe 'named_tasks validation' do
      it 'is valid with array of hashes like {"name" => "foo", "run_list" => ["role[bar]"]}' do
        expect(build(:project, named_tasks: [{"name" => "foo", "run_list" => ["role[bar]"]}] )).to be_valid
      end

      it "isn't valid with array of hashes with invalid run_list elements" do
        expect(build(:project, named_tasks: [{"name" => "foo", "run_list" => ["bar"]}] )).not_to be_valid
      end

      it "isn't valid with array of hashes without name or run_list" do
        expect(build(:project, named_tasks: [{run_list: ["role[bar]"]}] )).not_to be_valid
        expect(build(:project, named_tasks: [{"name" => "foo"}] )).not_to be_valid
      end
    end
  end

  describe '.fields' do
    it "should match array ['deploy_envs', 'type', 'description', 'named_tasks']" do
      expect(described_class.list_fields).to eq %w(deploy_envs type description named_tasks)
    end
  end

  describe '#initialize' do
    it "doesn't set @archived to false by default" do
      expect(build(:project, archived: nil).archived).to eq nil
    end
  end

  describe '#environment' do
    let(:project) { build(:project, with_env_identifiers: %w(foo bar)) }

    it 'returns found env' do
      expect(project.environment('bar')).to be_an(Devops::Model::Environment)
    end

    it 'raises RecordNotFound if there is no such env' do
      expect {
        project.environment('missing')
      }.to raise_error Devops::Exception::RecordNotFound
    end
  end

  describe '#add_environment', stubbed_logger: true do
    let(:env) {build(:environment)}
    subject { project.add_environment(env) }

    it 'inserts deploy env' do
      expect(project).to receive(:push).with(hash_including(environments: env.to_hash))
      subject
    end
  end

  describe '#add_authorized_user' do
    let(:env1) { build(:environment, id: 'foo') }
    let(:env2) { build(:environment, id: 'bar') }
    let(:project) { build(:project, set_environments: [env1, env2]) }

    it 'returns nil if user is nil' do
      expect(project.add_authorized_user(nil)).to be_nil
    end

    context "when env isn't given" do
      context 'when user is a String' do
        it 'adds given user to all envs' do
          pending 'bug: Devops::Db.connector in Project#add_authorized_user'
          project.add_authorized_user('John')
          expect(env1.users).to match_array(%w(root John))
          expect(env2.users).to match_array(%w(root John))
        end
      end

      context 'when user is an Array of strings' do
        it 'adds given users to all envs' do
          pending 'bug: Devops::Db.connector in Project#add_authorized_user'
          project.add_authorized_user(['John', 'Matz'])
          expect(env1.users).to match_array(%w(root John Matz))
          expect(env2.users).to match_array(%w(root John Matz))
        end
      end
    end

    context 'when env is given' do
      context 'when user is a String' do
        it 'adds given user to given env' do
          pending 'bug: Devops::Db.connector in Project#add_authorized_user'
          project.add_authorized_user('John', 'bar')
          expect(env1.users).to match_array(%w(root))
          expect(env2.users).to match_array(%w(root John))
        end
      end

      context 'when user is an Array of strings' do
        it 'adds given users to all envs' do
          pending 'bug: Devops::Db.connector in Project#add_authorized_user'
          project.add_authorized_user(['John', 'Matz'], 'bar')
          expect(env1.users).to match_array(%w(root))
          expect(env2.users).to match_array(%w(root John Matz))
        end
      end
    end
  end

  describe '#remove_authorized_user' do
    let(:env1) { build(:environment, id: 'foo', users: %w(root John Matz)) }
    let(:env2) { build(:environment, id: 'bar', users: %w(root John Matz)) }
    let(:project) { build(:project, environments: [env1, env2]) }

    it 'returns nil if user is nil' do
      expect(project.remove_authorized_user(nil)).to be_nil
    end

    context "when env isn't given" do
      context 'when user is a String' do
        it 'adds given user to all envs' do
          pending 'bug: Devops::Db.connector in Project#remove_authorized_user'
          project.remove_authorized_user('John')
          expect(env1.users).to match_array(%w(root Matz))
          expect(env2.users).to match_array(%w(root Matz))
        end
      end

      context 'when user is an Array of strings' do
        it 'adds given users to all envs' do
          pending 'bug: Devops::Db.connector in Project#remove_authorized_user'
          project.remove_authorized_user(['John', 'Matz'])
          expect(env1.users).to match_array(%w(root))
          expect(env2.users).to match_array(%w(root))
        end
      end
    end

    context 'when env is given' do
      context 'when user is a String' do
        it 'adds given user to given env' do
          pending 'bug: Devops::Db.connector in Project#remove_authorized_user'
          project.remove_authorized_user('John', 'bar')
          expect(env1.users).to match_array(%w(root John Matz))
          expect(env2.users).to match_array(%w(root Matz))
        end
      end

      context 'when user is an Array of strings' do
        it 'adds given users to all envs' do
          pending 'bug: Devops::Db.connector in Project#remove_authorized_user'
          project.remove_authorized_user(['John', 'Matz'], 'bar')
          expect(env1.users).to match_array(%w(root John Matz))
          expect(env2.users).to match_array(%w(root))
        end
      end
    end
  end

  describe '#check_authorization' do
    let(:env) { build(:environment, id: 'foo', users: %w(root Matz)) }
    let(:project) { build(:project, environments: [env]) }

    it 'returns true for root user' do
      expect(project.check_authorization('root', 'foo')).to be true
    end

    it "returns false if env's users don't include given user" do
      expect(project.check_authorization('John', 'foo')).to be false
    end

    it "returns true if env's users include given user" do
      expect(project.check_authorization('Matz', 'foo')).to be true
    end

    it 'returns false if there is no such env' do
      expect(project.check_authorization('Matz', 'wrong')).to be false
    end
  end

  describe '#delete_environment', clean_db_after_example: true do
    it 'removes env' do
      project.save
      project.delete_environment('foo')
      expect(project.reload.environments).to match_array []
    end
  end

  describe '#to_hash' do
    subject { project.to_hash }
    it 'returns hash' do
     expect(subject).to be_a(Hash)
    end

    it 'contains deploy_envs converted to hashes' do
      expect(subject['environments']).to be_an_array_of(Hash)
    end

    it 'also contains descriptions, run_list and named_tasks' do
      expect(subject).to include('description', 'run_list', 'named_tasks')
    end

    it 'contains archived key if project is archived' do
      project.archived = true
      expect(subject).to include('archived')
    end

    it "doesn't contain archived if project isn't archived" do
      expect(subject).not_to include('archived')
    end
  end

  describe '#to_hash_list' do
    it 'returns hash' do
      expect(project.to_hash_list).to be_a(Hash)
    end
  end

  describe '#deploy_info' do
    subject { project.deploy_info(project.environment('foo')) }
    it 'returns hash' do
      expect(subject).to be_a(Hash)
    end

    it 'includes use_json_file, project and project_info' do
      expect(subject).to include('use_json_file', 'project', 'project_info')
      expect(subject['project_info']).to be_a(Hash)
    end
  end

end