# frozen_string_literal: true

require 'rails_helper'

RSpec.describe LetsEncrypt::VerificationsController, type: :controller do
  routes { LetsEncrypt::Engine.routes }
  class OtherModel < LetsEncrypt::Certificate
  end
  before(:each) do
    LetsEncrypt.config.certificate_model = 'OtherModel'
  end

  it 'returns 404 status when no valid verification path found' do
    open(:get, :show, verification_path: :invalid_path)
    expect(response.status).to eq(404)
  end

  context 'has certificate' do
    let!(:certificate) do
      LetsEncrypt.certificate_model.create(
        domain: 'example.com',
        verification_path: '.well-known/acme-challenge/valid_path',
        verification_string: 'verification'
      )
    end

    it 'returns verification string when found verification path' do
      open(:get, :show, verification_path: 'valid_path')
      expect(response.status).to eq(200)
      expect(response.body).to eq(certificate.verification_string)
    end
  end
end

# frozen_string_literal: true

require 'rails_helper'

RSpec.describe LetsEncrypt::VerificationsController, type: :controller do
  routes { LetsEncrypt::Engine.routes }
  class OtherModel < LetsEncrypt::Certificate
  end

  it 'returns 404 status when no valid verification path found' do
    open(:get, :show, verification_path: :invalid_path)
    expect(response.status).to eq(404)
  end


  describe 'has certificate' do
   let!(:certificate) do
      LetsEncrypt.certificate_model.create(
        domain: 'example.com',
        verification_path: '.well-known/acme-challenge/valid_path',
        verification_string: 'verification'
      )
    end
    context 'with default model' do
      it 'returns verification string when found verification path' do
        open(:get, :show, verification_path: 'valid_path')
        expect(response.status).to eq(200)
        expect(response.body).to eq(certificate.verification_string)
      end
    end

    context 'with customize model' do
      LetsEncrypt.config.certificate_model = 'OtherModel'
      it 'set the certificate_model to customize model' do
        expect(LetsEncrypt.certificate_model).to eq('OtherModel'.constantize)
      end

      it 'returns verification string when found verification path' do
        open(:get, :show, verification_path: 'valid_path')
        expect(response.status).to eq(200)
        expect(response.body).to eq(certificate.verification_string)
      end
    end
  end
end

Add test to check controller will work properly with default model and customize model
Add test to check controller will work properly with default model and customize model
