Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions lib/passbook/pkpass.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ class PKPass

TYPES = ['boarding-pass', 'coupon', 'event-ticket', 'store-card', 'generic']

# Require fields, meta programming for accessor
REQUIRED_FIELDS = %w(passTypeIdentifier teamIdentifier serialNumber organizationName formatVersion description)
REQUIRED_FIELDS.each do |accessor|
class_eval %Q{
def #{accessor}= value
json = JSON.parse(@pass)
json['#{accessor}'] = value
@pass = json.to_json
end
}
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this.. Can you add a unit test or two around these?

def initialize pass
@pass = pass
@manifest_files = []
Expand Down Expand Up @@ -44,8 +56,13 @@ def create
self.file.path
end

# Return a Tempfile containing our ZipStream
# Backward compatibility
def file(options = {})
to_file options
end

# Return a Tempfile containing our ZipStream
def to_file options = {}
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues here. The unit tests should catch these in the regressions.

options[:file_name] ||= 'pass.pkpass'

temp_file = Tempfile.new(options[:file_name])
Expand All @@ -55,8 +72,13 @@ def file(options = {})
temp_file
end

# Return a ZipOutputStream
# Backward compatibility
def stream
to_stream
end

# Return a ZipOutputStream
def to_stream
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should get caught in the regressions.

manifest, signature = build

outputZip manifest, signature
Expand All @@ -69,9 +91,9 @@ def get_p12_cert_and_key
key_hash[:cert] = OpenSSL::X509::Certificate.new File.read(Passbook.p12_certificate)
else
p12 = OpenSSL::PKCS12.new File.read(Passbook.p12_cert), Passbook.p12_password
key_hash[:key], key_hash[:cert] = p12.key, p12.certificate
key_hash[:key], key_hash[:cert] = p12.key, p12.certificate
end
key_hash
key_hash
end

def createSignature manifest
Expand All @@ -88,6 +110,16 @@ def createSignature manifest
return Base64.decode64(data)
end

def valid?
manifest = createManifest
begin
checkPass manifest
rescue
return false
end
return true
end

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have a unit test around it.

private

def checkPass manifest
Expand All @@ -96,13 +128,11 @@ def checkPass manifest
raise 'Icon@2x missing' unless manifest.include?('icon@2x.png')

# Check for developer field in JSON
raise 'Pass Type Identifier missing' unless @pass.include?('passTypeIdentifier')
raise 'Team Identifier missing' unless @pass.include?('teamIdentifier')
raise 'Serial Number missing' unless @pass.include?('serialNumber')
raise 'Organization Name Identifier missing' unless @pass.include?('organizationName')
raise 'Format Version' unless @pass.include?('formatVersion')
REQUIRED_FIELDS.each do |require_field|
raise "#{require_field} mising" unless @pass.include?(require_field)
end
# Specific test
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This didn't initially have test coverage, but we should add that in for this.

raise 'Format Version should be a numeric' unless JSON.parse(@pass)['formatVersion'].is_a?(Numeric)
raise 'Description' unless @pass.include?('description')
end

def createManifest
Expand Down
5 changes: 3 additions & 2 deletions spec/lib/passbook/pkpass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
File.should_receive(:read).with('my_p12_key').and_return 'my_p12_key_file'
File.should_receive(:read).with('my_p12_certificate').and_return 'my_p12_certificate_file'
OpenSSL::PKey::RSA.should_receive(:new).with('my_p12_key_file', 'password').and_return 'my_rsa_key'
OpenSSL::X509::Certificate.should_receive(:new).with('my_p12_certificate_file').and_return 'my_ssl_p12_cert'
OpenSSL::X509::Certificate.should_receive(:new).with('my_p12_certificate_file').and_return 'my_ssl_p12_cert'
end

subject {pass.get_p12_cert_and_key}
Expand Down Expand Up @@ -89,6 +89,7 @@
let (:entries) {["pass.json", "manifest.json", "signature", "icon.png", "icon@2x.png", "logo.png", "logo@2x.png"]}

before :each do
pass.passTypeIdentifier = 'test.pass.book'
pass.addFiles ["#{base_path}/icon.png","#{base_path}/icon@2x.png","#{base_path}/logo.png","#{base_path}/logo@2x.png"]
pass.should_receive(:createSignature).and_return('Signed by the Honey Badger')
@file_entries = []
Expand Down Expand Up @@ -122,7 +123,7 @@

after do
temp_file.delete
end
end
end
end

Expand Down