From a9f5f2238986e1722f796a2d4a34ac62466bdb8a Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Wed, 13 Oct 2021 01:08:20 +0200 Subject: [PATCH] Added tests for closePassSymbol method --- spec/PKPass.ts | 162 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) diff --git a/spec/PKPass.ts b/spec/PKPass.ts index 21252e5..875b484 100644 --- a/spec/PKPass.ts +++ b/spec/PKPass.ts @@ -8,6 +8,7 @@ import { propsSymbol, passTypeSymbol, importMetadataSymbol, + closePassSymbol, } from "../lib/PKPass"; describe("PKPass", () => { @@ -815,4 +816,165 @@ describe("PKPass", () => { expect(pass.backFields[0]).toEqual({ ...baseField, key: "bf0" }); }); }); + + describe("[closePassSymbol]", () => { + it("should add props to pass.json", () => { + pass.addBuffer( + "pass.json", + Buffer.from( + JSON.stringify({ + boardingPass: { + headerFields: [], + primaryFields: [], + auxiliaryFields: [], + secondaryFields: [], + backFields: [], + }, + serialNumber: "h12kj5b12k3331", + } as PassProps), + ), + ); + + pass.setBarcodes({ + format: "PKBarcodeFormatQR", + message: "meh a test barcode", + }); + + pass.addBuffer("icon@2x.png", Buffer.alloc(0)); + + pass[closePassSymbol](true); + + expect( + JSON.parse(pass[filesSymbol]["pass.json"].toString("utf-8")), + ).toEqual({ + boardingPass: { + headerFields: [], + primaryFields: [], + auxiliaryFields: [], + secondaryFields: [], + backFields: [], + }, + serialNumber: "h12kj5b12k3331", + barcodes: [ + { + format: "PKBarcodeFormatQR", + message: "meh a test barcode", + messageEncoding: "iso-8859-1", + }, + ], + }); + }); + + it("Should warn user if no icons have been added to bundle", () => { + console.warn = jasmine.createSpy("log"); + + pass.addBuffer( + "pass.json", + Buffer.from( + JSON.stringify({ + boardingPass: { + headerFields: [], + primaryFields: [], + auxiliaryFields: [], + secondaryFields: [], + backFields: [], + }, + serialNumber: "h12kj5b12k3331", + } as PassProps), + ), + ); + + pass[closePassSymbol](true); + + expect(console.warn).toHaveBeenCalledWith( + "At least one icon file is missing in your bundle. Your pass won't be openable by any Apple Device.", + ); + }); + + it("should create back again pass.strings files", () => { + pass.addBuffer( + "pass.json", + Buffer.from( + JSON.stringify({ + boardingPass: { + headerFields: [], + primaryFields: [], + auxiliaryFields: [], + secondaryFields: [], + backFields: [], + }, + serialNumber: "h12kj5b12k3331", + } as PassProps), + ), + ); + + pass.localize("it", { + home: "casa", + ciao: "hello", + cosa: "thing", + }); + + pass[closePassSymbol](true); + + expect(pass[filesSymbol]["it.lproj/pass.strings"].length).not.toBe( + 0, + ); + }); + + it("should remove all personalisation if requirements are not met", () => { + pass.addBuffer( + "personalization.json", + Buffer.from( + JSON.stringify({ + description: + "A test description for a test personalization", + requiredPersonalizationFields: [ + "PKPassPersonalizationFieldName", + "PKPassPersonalizationFieldPostalCode", + "PKPassPersonalizationFieldEmailAddress", + ], + }), + ), + ); + + pass.addBuffer("personalizationLogo@2x.png", Buffer.alloc(0)); + + pass[closePassSymbol](true); + + /** Pass is missing nfc data. */ + expect(pass[filesSymbol]["personalization.json"]).toBeUndefined(); + expect( + pass[filesSymbol]["personalizationLogo@2x.png"], + ).toBeUndefined(); + + /** Next: pass will miss personalizationLogo */ + + pass.addBuffer( + "personalization.json", + Buffer.from( + JSON.stringify({ + description: + "A test description for a test personalization", + requiredPersonalizationFields: [ + "PKPassPersonalizationFieldName", + "PKPassPersonalizationFieldPostalCode", + "PKPassPersonalizationFieldEmailAddress", + ], + }), + ), + ); + + pass.setNFC({ + message: "nfc-encrypted-message", + encryptionPublicKey: "none", + }); + + pass[closePassSymbol](true); + + expect(pass[filesSymbol]["personalization.json"]).toBeUndefined(); + expect( + pass[filesSymbol]["personalizationLogo@2x.png"], + ).toBeUndefined(); + }); + }); });