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.



44
45
46
# File 'lib/lowdown/mock.rb', line 44

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") ⇒ 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.



59
60
61
62
63
# File 'lib/lowdown/mock.rb', line 59

def self.client(uri: nil, app_bundle_id: "com.example.MockApp")
  certificate = certificate(app_bundle_id)
  connection = Connection.new(uri: uri, ssl_context: certificate.ssl_context)
  Client.client_with_connection(connection, 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.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/lowdown/mock.rb', line 20

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
  cert.extensions = [OpenSSL::X509::Extension.new(ext_name, "0d..#{app_bundle_id}0...app")]

  [cert, key]
end