require 'db/mongo/models/image'

RSpec.describe Devops::Model::Image, type: :model do
  let(:image) { build(:image) }
  let(:string_with_dash) { 'asd-asd' }
  let(:string_with_slash) { 'asd/asd' }
  let(:string_with_parenthesis) { 'centos 6.5 x86_64 (development instance)' }
  let(:provider_instance) { double('provider instance', images: ['image']) }
  let(:available_images) { ['test_image', string_with_dash, string_with_slash, string_with_parenthesis] }

  before do
    allow(Devops::Model::Filter).to receive(:available_images) { available_images }
    allow(Provider).to receive(:get_connector) { provider_instance }
  end

  describe 'validation', stubbed_logger: true do
    it 'is valid with correct attrs' do
      expect(image).to be_valid
    end

    include_examples 'field type validation', :remote_user, :not_nil, :non_empty_string
    include_examples 'field type validation', :name, :not_nil, :non_empty_string

    it 'id should contain only letters, digits and dashes' do
      expect(build(:image, image_id: string_with_dash)).to be_valid
      expect(build(:image, image_id: string_with_slash)).not_to be_valid
      expect(build(:image, image_id: string_with_parenthesis)).not_to be_valid
    end

    it "id should be included in image filters" do
      expect(build(:image, image_id: 'wrong')).not_to be_valid
    end

    it 'name may contain everything' do
      expect(build(:image, name: string_with_dash)).to be_valid
      expect(build(:image, name: string_with_slash)).to be_valid
      expect(build(:image, name: string_with_parenthesis)).to be_valid
    end

    it 'remote_user should contain only a-zA-Z0-9_-' do
      expect(build(:image, remote_user: 'aA0-')).to be_valid
      expect(build(:image, remote_user: 'name/')).not_to be_valid
      expect(build(:image, remote_user: 'name!')).not_to be_valid
    end
  end
end