Module: Lowdown::Mock

Defined in:
lib/lowdown/mock.rb

Overview

Provides a collection of test helpers.

This file is not loaded by default.

Defined Under Namespace

Classes: Connection

Class Method Summary collapse

Class Method Details

.certificate(app_bundle_id) ⇒ Certificate

Generates a Certificate configured with a self-signed Universal Certificate.

Parameters:

  • app_bundle_id (String)

    the App ID / app Bundle ID to encode into the certificate.

Returns:

  • (Certificate)

    a Certificate configured with a self-signed certificate/key pair.



51
52
53
# File 'lib/lowdown/mock.rb', line 51

def self.certificate(app_bundle_id)
  Certificate.new(*ssl_certificate_and_key(app_bundle_id))
end

.client(uri: nil, app_bundle_id: "com.example.MockApp", keep_alive: false) ⇒ Client

Generates a Client with a mock Connection and a self-signed Universal Certificate.

Parameters:

  • uri (URI, String) (defaults to: nil)

    the details to connect to the APN service.

  • app_bundle_id (String) (defaults to: "com.example.MockApp")

    the App ID / app Bundle ID to encode into the certificate.

Returns:

  • (Client)

    a Client configured with the uri and a self-signed certificate that has the app_bundle_id encoded.



66
67
68
69
70
71
# File 'lib/lowdown/mock.rb', line 66

def self.client(uri: nil, app_bundle_id: "com.example.MockApp", keep_alive: false)
  certificate = certificate(app_bundle_id)
  connection = Connection.new(uri, certificate.ssl_context, keep_alive)
  connection.connect if keep_alive
  Client.client_with_connection(connection, certificate: certificate)
end

.ssl_certificate_and_key(app_bundle_id) ⇒ Array<OpenSSL::X509::Certificate, OpenSSL::PKey::RSA>

Generates a self-signed Universal Certificate.

Parameters:

  • app_bundle_id (String)

    the App ID / app Bundle ID to encode into the certificate.

Returns:

  • (Array<OpenSSL::X509::Certificate, OpenSSL::PKey::RSA>)

    the self-signed certificate and private key.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lowdown/mock.rb', line 21

def self.ssl_certificate_and_key(app_bundle_id)
  key = OpenSSL::PKey::RSA.new(1024)
  name = OpenSSL::X509::Name.parse("/UID=#{app_bundle_id}/CN=Stubbed APNS Certificate: #{app_bundle_id}")
  cert = OpenSSL::X509::Certificate.new
  cert.subject    = name
  cert.not_before = Time.now
  cert.not_after  = cert.not_before + 3600
  cert.public_key = key.public_key
  cert.sign(key, OpenSSL::Digest::SHA1.new)

  # Make it a Universal Certificate
  ext_name = Lowdown::Certificate::UNIVERSAL_CERTIFICATE_EXTENSION
  ext_value = OpenSSL::ASN1::Sequence.new(
    [
      OpenSSL::ASN1::UTF8String.new(app_bundle_id),
      OpenSSL::ASN1::Sequence.new([OpenSSL::ASN1::UTF8String.new("app")]),
    ]
  ).to_der
  cert.extensions = [OpenSSL::X509::Extension.new(ext_name, ext_value)]

  [cert, key]
end