Skip to content

Commit

Permalink
Migrate/update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Sep 13, 2024
1 parent 8400a5b commit f53d8ce
Show file tree
Hide file tree
Showing 41 changed files with 1,001 additions and 919 deletions.
5 changes: 1 addition & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@
/.covered.db
/external

# rspec failure tracking
.rspec_status
.covered.db
.env
/.env
4 changes: 0 additions & 4 deletions .rspec

This file was deleted.

2 changes: 1 addition & 1 deletion cloudflare.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ Gem::Specification.new do |spec|

spec.required_ruby_version = ">= 3.1"

spec.add_dependency "async-rest", "~> 0.12"
spec.add_dependency "async-rest", "~> 0.18"
end
7 changes: 7 additions & 0 deletions config/sus.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "covered/sus"
include Covered::Sus
62 changes: 62 additions & 0 deletions fixtures/cloudflare/a_connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2024, by Samuel Williams.

require "cloudflare"
require "sus/fixtures/async/reactor_context"

module Cloudflare
AUTH_EMAIL = ENV["CLOUDFLARE_EMAIL"]
AUTH_KEY = ENV["CLOUDFLARE_KEY"]
PROXY_URL = ENV["CLOUDFLARE_PROXY"]

ACCOUNT_ID = ENV["CLOUDFLARE_ACCOUNT_ID"]

ZONE_NAMES = %w{alligator ant bear bee bird camel cat cheetah chicken chimpanzee cow crocodile deer dog dolphin duck eagle elephant fish fly fox frog giraffe goat goldfish hamster hippopotamus horse kangaroo kitten lion lobster monkey octopus owl panda pig puppy rabbit rat scorpion seal shark sheep snail snake spider squirrel tiger turtle wolf zebra}

JOB_ID = ENV.fetch("INVOCATION_ID", "testing").hash

ZONE_NAME = ENV["CLOUDFLARE_ZONE_NAME"] || "#{ZONE_NAMES[JOB_ID % ZONE_NAMES.size]}.com"

if AUTH_EMAIL.nil? || AUTH_EMAIL.empty? || AUTH_KEY.nil? || AUTH_KEY.empty?
$stderr.puts <<~EOF
Please make sure you have defined CLOUDFLARE_EMAIL and CLOUDFLARE_KEY in your environment. You can also specify CLOUDFLARE_ZONE_NAME to test with your own zone and CLOUDFLARE_ACCOUNT_ID to use a specific account
EOF
end

AConnection = Sus::Shared("a connection") do
include Sus::Fixtures::Async::ReactorContext

let(:connection) do
if proxy_url = PROXY_URL
proxy_endpoint = Async::HTTP::Endpoint.parse(proxy_url)
@client = Async::HTTP::Client.new(proxy_endpoint)
@connection = Cloudflare.connect(@client.proxied_endpoint(ENDPOINT), email: AUTH_EMAIL, key: AUTH_KEY)
else
@client = nil
@connection = Cloudflare.connect(email: AUTH_EMAIL, key: AUTH_KEY)
end
end

let(:account) do
if ACCOUNT_ID
connection.accounts.find_by_id(ACCOUNT_ID)
else
connection.accounts.first
end
end

let(:job_id) {JOB_ID}
let(:zone_names) {ZONE_NAMES}
let(:zone_name) {ZONE_NAME}

let(:zones) {connection.zones}
let(:zone) {zones.find_by_name(zone_name) || zones.create(zone_name, account)}

after do
@connection&.close
@client&.close
end
end
end
4 changes: 2 additions & 2 deletions gems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
end

group :test do
gem "rspec"
gem "sus"
gem "covered"
gem "decode"
gem "rubocop"

gem "async-rspec"
gem "sus-fixtures-async"

gem "sinatra"
gem "webmock"
Expand Down
24 changes: 11 additions & 13 deletions lib/cloudflare.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,18 @@

module Cloudflare
def self.connect(*arguments, **auth_info)
connection = Connection.open(*arguments)

if !auth_info.empty?
connection = connection.authenticated(**auth_info)
end

return connection unless block_given?

Sync do
connection = Connection.open(*arguments)

if !auth_info.empty?
connection = connection.authenticated(**auth_info)
end

return connection unless block_given?

begin
yield connection
ensure
connection.close
end
yield connection
ensure
connection.close
end
end
end
2 changes: 1 addition & 1 deletion lib/cloudflare/accounts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
module Cloudflare
class Account < Representation
def id
value[:id]
result[:id]
end

def kv_namespaces
Expand Down
8 changes: 4 additions & 4 deletions lib/cloudflare/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ def authenticated(token: nil, key: nil, email: nil)
headers = {}

if token
headers["Authorization"] = "Bearer #{token}"
headers["authorization"] = "bearer #{token}"
elsif key
if email
headers["X-Auth-Key"] = key
headers["X-Auth-Email"] = email
headers["x-auth-key"] = key
headers["x-auth-email"] = email
else
headers["X-Auth-User-Service-Key"] = key
headers["x-auth-user-service-key"] = key
end
end

Expand Down
3 changes: 2 additions & 1 deletion lib/cloudflare/custom_hostname/ssl_attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "./ssl_attribute/settings"
require_relative "ssl_attribute/settings"
require_relative "../representation"

module Cloudflare
class CustomHostname < Representation
Expand Down
4 changes: 3 additions & 1 deletion lib/cloudflare/custom_hostname/ssl_attribute/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# Copyright, 2019, by Rob Widmer.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "../../representation"

module Cloudflare
class CustomHostname < Representation
class SSLAttribute
class Settings
def initialize(settings)
def initialize(settings = {})
@settings = settings
end

Expand Down
94 changes: 58 additions & 36 deletions lib/cloudflare/custom_hostnames.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,92 @@

module Cloudflare
class CustomHostname < Representation
include Async::REST::Representation::Mutable

# Only available if enabled for your zone
def custom_origin
value[:custom_origin_server]
result[:custom_origin_server]
end

# Only available if enabled for your zone
def custom_metadata
value[:custom_metadata]
result[:custom_metadata]
end

def hostname
value[:hostname]
result[:hostname]
end

def id
value[:id]
result[:id]
end

def ssl
@ssl ||= SSLAttribute.new(value[:ssl])
@ssl ||= SSLAttribute.new(result[:ssl])
end

# Check if the cert has been validated
# passing true will send a request to Cloudflare to try to validate the cert
def ssl_active?(force_update = false)
send_patch(ssl: { method: ssl.method, type: ssl.type }) if force_update && ssl.pending_validation?
ssl.active?
if force_update && ssl.pending_validation?
self.patch(ssl: {method: ssl.method, type: ssl.type})
end

return ssl.active?
end

def update_settings(metadata: nil, origin: nil, ssl: nil)
attrs = {}
attrs[:custom_metadata] = metadata if metadata
attrs[:custom_origin_server] = origin if origin
attrs[:ssl] = ssl if ssl

send_patch(attrs)
payload = {}

payload[:custom_metadata] = metadata if metadata
payload[:custom_origin_server] = origin if origin
payload[:ssl] = ssl if ssl

self.patch(payload)
end

alias :to_s :hostname

private

def send_patch(data)
response = patch(data)

@ssl = nil # Kill off our cached version of the ssl object so it will be regenerated from the response
@value = response.result

def patch(payload)
self.class.patch(@resource, payload) do |resource, response|
value = response.read

if value[:sucess]
@ssl = nil
@value = value
else
raise RequestError.new(@resource, value)
end
end
end
end

class CustomHostnames < Representation
include Paginate

def representation
CustomHostname
end

# initializes a custom hostname object and yields it for customization before saving
def create(hostname, metadata: nil, origin: nil, ssl: {}, &block)
attrs = { hostname: hostname, ssl: { method: "http", type: "dv" }.merge(ssl) }
attrs[:custom_metadata] = metadata if metadata
attrs[:custom_origin_server] = origin if origin

represent_message(self.post(attrs))

def create(hostname, metadata: nil, origin: nil, ssl: {}, **options)
payload = {hostname: hostname, ssl: {method: "http", type: "dv"}.merge(ssl), **options}

payload[:custom_metadata] = metadata if metadata
payload[:custom_origin_server] = origin if origin

CustomHostname.post(@resource, payload) do |resource, response|
value = response.read
result = value[:result]
metadata = response.headers

if id = result[:id]
resource = resource.with(path: id)
end

CustomHostname.new(resource, value: value, metadata: metadata)
end
end

def find_by_hostname(hostname)
Expand Down
Loading

0 comments on commit f53d8ce

Please sign in to comment.