Skip to content

Commit

Permalink
Merge a507211 into 1f079f6
Browse files Browse the repository at this point in the history
  • Loading branch information
telsaiori committed Oct 7, 2019
2 parents 1f079f6 + a507211 commit b4c7fbc
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 14 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ LetsEncrypt.config do |config|
# The redis server url
# Default is nil
config.redis_url = 'redis://localhost:6379/1'

# Enable it if you want to customize the model
# Default is LetsEncrypt::Certificate
#config.certificate_model = 'MyCertificate'
end
```

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/lets_encrypt/verifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def render_verification_string
end

def certificate
LetsEncrypt::Certificate.find_by(verification_path: filename)
LetsEncrypt.certificate_model.find_by(verification_path: filename)
end

def filename
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/lets_encrypt/renew_certificates_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class RenewCertificatesJob < ApplicationJob
queue_as :default

def perform
LetsEncrypt::Certificate.renewable.each do |certificate|
LetsEncrypt.certificate_model.renewable.each do |certificate|
next if certificate.renew
certificate.update(renew_after: 1.day.from_now)
end
Expand Down
4 changes: 4 additions & 0 deletions lib/letsencrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ def config(&block)
def table_name_prefix
'letsencrypt_'
end

def certificate_model
@certificate_model ||= config.certificate_model.constantize
end
end
end
4 changes: 4 additions & 0 deletions lib/letsencrypt/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class Configuration
config_accessor :save_to_redis
config_accessor :redis_url

config_accessor :certificate_model do
'LetsEncrypt::Certificate'
end

# Returns true if enabled `save_to_redis` feature
def use_redis?
save_to_redis == true
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/letsencrypt_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace :letsencrypt do
task renew: :environment do
count = 0
failed = 0
LetsEncrypt::Certificate.renewable do |certificate|
LetsEncrypt.certificate_model.renewable do |certificate|
count += 1
next if certificate.renew
failed += 1
Expand Down
43 changes: 32 additions & 11 deletions spec/controllers/lets_encrypt/verifications_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,46 @@

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

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

describe 'has certificate' do
context 'with default model' 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

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)
context 'with customize model' do
before { LetsEncrypt.config.certificate_model = 'OtherModel' }
let!(:certificate) do
LetsEncrypt.certificate_model.create(
domain: 'example.com',
verification_path: '.well-known/acme-challenge/valid_path',
verification_string: 'verification'
)
end
after { LetsEncrypt.config.certificate_model = 'LetsEncrypt::Certificate' }
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
25 changes: 25 additions & 0 deletions spec/letsencrypt/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,29 @@
expect(subject.use_staging?).to eq(subject.use_staging)
end
end

describe 'customize certificate model' do
class OtherModel < LetsEncrypt::Certificate
after_update :success

def success
'success'
end
end

before(:each) do
LetsEncrypt.config.certificate_model = 'OtherModel'
LetsEncrypt.stub(:certificate_model) { 'OtherModel'.constantize }
LetsEncrypt.certificate_model.create(
domain: 'example.com',
verification_path: '.well-known/acme-challenge/valid_path',
verification_string: 'verification'
)
end
after { LetsEncrypt.config.certificate_model = 'LetsEncrypt::Certificate' }
it 'update data' do
expect_any_instance_of(OtherModel).to receive(:success)
LetsEncrypt.certificate_model.first.update(renew_after: 3.days.ago)
end
end
end
13 changes: 13 additions & 0 deletions spec/letsencrypt_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@
LetsEncrypt.register('example@example.com')
end
end

describe 'certificate_model' do
class OtherModel < LetsEncrypt::Certificate
end
before do
LetsEncrypt.config.certificate_model = 'OtherModel'
LetsEncrypt.stub(:certificate_model) { 'OtherModel'.constantize }
end
after { LetsEncrypt.config.certificate_model = 'LetsEncrypt::Certificate' }
it 'set the certificate_model to customize model' do
expect(LetsEncrypt.certificate_model).to eq('OtherModel'.constantize)
end
end
end

0 comments on commit b4c7fbc

Please sign in to comment.