Skip to content

Commit 0590b98

Browse files
committed
Teller::Config module and tests
1 parent 633a0e5 commit 0590b98

9 files changed

+138
-9
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
Gemfile.lock
12
/.bundle/
23
/.yardoc
34
/_yardoc/

lib/teller.rb

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "teller/version"
2+
require "teller/config"
23

34
module Teller
45
class Error < StandardError; end

lib/teller/config.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'openssl'
2+
3+
module Teller::Config
4+
class Error < StandardError; end
5+
6+
attr_accessor :access_token, :certificate, :private_key
7+
8+
def setup(opts = {})
9+
if block_given?
10+
yield(self)
11+
else
12+
opts.each { |k, v| send(:"#{k}=", v) }
13+
end
14+
end
15+
16+
def certificate=(path)
17+
begin
18+
File.open(path, 'r') do |file|
19+
@certificate = OpenSSL::X509::Certificate.new(file)
20+
end
21+
rescue Errno::ENOENT
22+
raise Error, "Certificate file not found: #{path}"
23+
rescue OpenSSL::X509::CertificateError
24+
raise Error, "Invalid certificate data in file: #{path}"
25+
end
26+
end
27+
28+
def private_key=(path)
29+
begin
30+
File.open(path, 'r') do |file|
31+
@private_key = OpenSSL::PKey.read(file, nil)
32+
end
33+
rescue Errno::ENOENT
34+
raise Error, "Private key file not found: #{path}"
35+
rescue OpenSSL::PKey::PKeyError
36+
raise Error, "Invalid private key data in file: #{path}"
37+
end
38+
end
39+
40+
extend self
41+
end

teller.gemspec

+4-5
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,16 @@ Gem::Specification.new do |spec|
66
spec.authors = ["Stevie Graham"]
77
spec.email = ["[email protected]"]
88

9-
spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
10-
spec.description = %q{TODO: Write a longer description or delete this line.}
11-
spec.homepage = "TODO: Put your gem's website or public repo URL here."
9+
spec.summary = "The official Teller Ruby language bindings/"
10+
spec.homepage = "https://github.com/tellerhq/teller-ruby"
1211
spec.license = "MIT"
1312
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
1413

1514
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
1615

1716
spec.metadata["homepage_uri"] = spec.homepage
18-
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
19-
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
17+
spec.metadata["source_code_uri"] = spec.homepage
18+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
2019

2120
# Specify which files should be added to the gem when it is released.
2221
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.

test/config_test.rb

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require "minitest/autorun"
2+
require "teller"
3+
4+
describe Teller::Config do
5+
let(:config) { Teller::Config }
6+
7+
it "allows setting access_token" do
8+
config.access_token = "your_access_token"
9+
_(config.access_token).must_equal "your_access_token"
10+
end
11+
12+
it "allows setting certificate using certificate=" do
13+
config.certificate = "test/fixtures/certificate.pem"
14+
_(config.certificate).must_be_kind_of OpenSSL::X509::Certificate
15+
end
16+
17+
it "allows setting private_key using private_key =" do
18+
private_key_path = "test/fixtures/private_key.pem"
19+
File.stub(:read, "your_private_key_content") do
20+
private_key = config.private_key = private_key_path
21+
_(config.private_key).must_be_kind_of OpenSSL::PKey::EC
22+
end
23+
end
24+
25+
it "allows setting multiple attributes using setup block" do
26+
config.setup do |conf|
27+
conf.access_token = "your_access_token"
28+
conf.certificate = "test/fixtures/certificate.pem"
29+
conf.private_key = "test/fixtures/private_key.pem"
30+
end
31+
32+
_(config.access_token).must_equal "your_access_token"
33+
_(config.certificate).must_be_kind_of OpenSSL::X509::Certificate
34+
_(config.private_key).must_be_kind_of OpenSSL::PKey::EC
35+
end
36+
37+
it "allows setting multiple attributes using setup hash" do
38+
config.setup(access_token: "your_access_token",
39+
certificate: "test/fixtures/certificate.pem",
40+
private_key: "test/fixtures/private_key.pem")
41+
42+
_(config.access_token).must_equal "your_access_token"
43+
_(config.certificate).must_be_kind_of OpenSSL::X509::Certificate
44+
_(config.private_key).must_be_kind_of OpenSSL::PKey::EC
45+
end
46+
47+
it "raises Teller::Config::Error for invalid certificate file path" do
48+
assert_raises(Teller::Config::Error) do
49+
config.certificate = "non_existent_cert.pem"
50+
end
51+
end
52+
53+
it "raises Teller::Config::Error for invalid private key file path" do
54+
assert_raises(Teller::Config::Error) do
55+
config.private_key = "non_existent_key.pem"
56+
end
57+
end
58+
59+
it "raises Teller::Config::Error for invalid certificate data" do
60+
assert_raises(Teller::Config::Error) do
61+
config.certificate = "test/fixtures/invalid.pem"
62+
end
63+
end
64+
65+
it "raises Teller::Config::Error for invalid private key data" do
66+
assert_raises(Teller::Config::Error) do
67+
config.private_key = "test/fixtures/invalid.pem"
68+
end
69+
end
70+
71+
end

test/fixtures/certificate.pem

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-----BEGIN CERTIFICATE-----
2+
MIIBhTCCASsCFBdHfhuDeSxgG8mGPj95G3fVSNZ8MAoGCCqGSM49BAMCMEUxCzAJ
3+
BgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5l
4+
dCBXaWRnaXRzIFB0eSBMdGQwHhcNMjMwNzIyMTQzNzI5WhcNNDMwNzE3MTQzNzI5
5+
WjBFMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwY
6+
SW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcD
7+
QgAEp6vPheUZZ2xmlRTJvJyaHtiPMx28nW/Dygeq0x6VaIhpW1dbFBujnlvBh9xD
8+
fER1YdhtjcdSbwrZvI7b0dcgQzAKBggqhkjOPQQDAgNIADBFAiBb/RuqMGU58pPt
9+
qX3WoVtk3s4q54zqpWcwtV7/zHYb3AIhAOCC1+9ETevi20+a5I5cBUl8aF6Hq1/o
10+
N6owQ4/Nl4la
11+
-----END CERTIFICATE-----

test/fixtures/invalid.pem

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
LOL

test/fixtures/private_key.pem

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-----BEGIN EC PARAMETERS-----
2+
BggqhkjOPQMBBw==
3+
-----END EC PARAMETERS-----
4+
-----BEGIN EC PRIVATE KEY-----
5+
MHcCAQEEIJhX5NCMeg8EymIls/nMKxCii2TLIMqVmraFDwFLK5WloAoGCCqGSM49
6+
AwEHoUQDQgAEp6vPheUZZ2xmlRTJvJyaHtiPMx28nW/Dygeq0x6VaIhpW1dbFBuj
7+
nlvBh9xDfER1YdhtjcdSbwrZvI7b0dcgQw==
8+
-----END EC PRIVATE KEY-----

test/teller_test.rb

-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,4 @@ class TellerTest < Minitest::Test
44
def test_that_it_has_a_version_number
55
refute_nil ::Teller::VERSION
66
end
7-
8-
def test_it_does_something_useful
9-
assert false
10-
end
117
end

0 commit comments

Comments
 (0)