forked from assaf/node-passbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpass_test.js
More file actions
250 lines (215 loc) · 7.38 KB
/
Copy pathpass_test.js
File metadata and controls
250 lines (215 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
var assert = require("assert");
var createTemplate = require("../");
var Crypto = require("crypto");
var execFile = require("child_process").execFile;
var File = require("fs");
describe("Pass", function() {
before(function() {
this.template = createTemplate("coupon", {
passTypeIdentifier: "pass.com.example.passbook",
teamIdentifier: "MXL"
});
this.template.keys(__dirname + "/../keys", "secret");
this.fields = {
serialNumber: "123456",
organizationName: "Acme flowers",
description: "20% of black roses"
};
});
describe("from template", function() {
before(function() {
this.pass = this.template.createPass();
});
it("should copy template fields", function() {
assert.equal(this.pass.fields.passTypeIdentifier, "pass.com.example.passbook");
});
it("should start with no images", function() {
assert.deepEqual(this.pass.images, {});
});
it("should create a structure based on style", function() {
assert(this.pass.fields.coupon);
assert(!this.pass.fields.eventTicket);
});
});
describe("without serial number", function() {
it("should not be valid", function() {
var pass = this.template.createPass(cloneExcept(this.fields, "serialNumber"));
try {
pass.validate();
assert(false, "Pass validated without serialNumber");
} catch(ex) {
assert.equal(ex.message, "Missing field serialNumber");
}
});
});
describe("without organization name", function() {
it("should not be valid", function() {
var pass = this.template.createPass(cloneExcept(this.fields, "organizationName"));
try {
pass.validate();
assert(false, "Pass validated without organizationName");
} catch(ex) {
assert.equal(ex.message, "Missing field organizationName");
}
});
});
describe("without description", function() {
it("should not be valid", function() {
var pass = this.template.createPass(cloneExcept(this.fields, "description"));
try {
pass.validate();
assert(false, "Pass validated without description");
} catch(ex) {
assert.equal(ex.message, "Missing field description");
}
});
});
describe("without icon.png", function() {
it("should not be valid", function() {
var pass = this.template.createPass(this.fields);
try {
pass.validate();
assert(false, "Pass validated without icon.png");
} catch(ex) {
assert.equal(ex.message, "Missing image icon.png");
}
});
});
describe("without logo.png", function() {
var validationError;
before(function(done) {
var pass = this.template.createPass(this.fields);
pass.icon("icon.png");
var file = File.createWriteStream("/tmp/pass.pkpass");
pass.pipe(file);
pass.on("done", done);
pass.on("error", function(error) {
validationError = error;
done();
});
});
it("should not be valid", function() {
assert(validationError, "Pass validated without logo.png");
assert.equal(validationError.message, "Missing image logo.png");
});
});
describe("generated", function() {
before(function() {
this.pass = this.template.createPass(this.fields);
this.pass.loadImagesFrom(__dirname + "/resources");
this.pass.headerFields.add("date", "Date", "Nov 1");
this.pass.primaryFields.add([
{ key: "location", label: "Place", value: "High ground" }
]);
});
before(function(done) {
if (File.existsSync("/tmp/pass.pkpass"))
File.unlinkSync("/tmp/pass.pkpass");
var file = File.createWriteStream("/tmp/pass.pkpass");
this.pass.pipe(file);
this.pass.on("end", done);
});
it("should be a valid ZIP", function(done) {
execFile("unzip", ["-t", "/tmp/pass.pkpass"], function(error, stdout) {
if (error)
error = new Error(stdout);
done(error);
});
});
it("should contain pass.json", function(done) {
unzip("/tmp/pass.pkpass", "pass.json", function(error, buffer) {
assert.deepEqual(JSON.parse(buffer), {
passTypeIdentifier: 'pass.com.example.passbook',
teamIdentifier: 'MXL',
serialNumber: '123456',
organizationName: 'Acme flowers',
description: '20% of black roses',
coupon: {
headerFields: [
{ key: "date",
label: "Date",
value: "Nov 1"
}
],
primaryFields: [
{ key: "location",
label: "Place",
value: "High ground"
}
]
},
formatVersion: 1
});
done();
});
});
it("should contain a manifest", function(done) {
unzip("/tmp/pass.pkpass", "manifest.json", function(error, buffer) {
assert.deepEqual(JSON.parse(buffer), {
"pass.json": "87c2bd96d4bcaf55f0d4d7846a5ae1fea85ea628",
"icon.png": "e0f0bcd503f6117bce6a1a3ff8a68e36d26ae47f",
"icon@2x.png": "10e4a72dbb02cc526cef967420553b459ccf2b9e",
"logo.png": "abc97e3b2bc3b0e412ca4a853ba5fd90fe063551",
"logo@2x.png": "87ca39ddc347646b5625062a349de4d3f06714ac",
"strip.png": "68fc532d6c76e7c6c0dbb9b45165e62fbb8e9e32",
"strip@2x.png": "17e4f5598362d21f92aa75bc66e2011a2310f48e",
"thumbnail.png": "e199fc0e2839ad5698b206d5f4b7d8cb2418927c",
"thumbnail@2x.png": "ac640c623741c0081fb1592d6353ebb03122244f"
});
done();
});
});
it("should contain a signature", function(done) {
execFile("signpass", ["-v", "/tmp/pass.pkpass"], function(error, stdout) {
assert(/\*\*\* SUCCEEDED \*\*\*/.test(stdout), stdout);
done();
});
});
it("should contain the icon", function(done) {
unzip("/tmp/pass.pkpass", "icon.png", function(error, buffer) {
assert.equal(Crypto.createHash("sha1").update(buffer).digest("hex"),
"e0f0bcd503f6117bce6a1a3ff8a68e36d26ae47f");
done();
});
});
it("should contain the logo", function(done) {
unzip("/tmp/pass.pkpass", "logo.png", function(error, buffer) {
assert.equal(Crypto.createHash("sha1").update(buffer).digest("hex"),
"abc97e3b2bc3b0e412ca4a853ba5fd90fe063551");
done();
});
});
describe("unzip", function() {
before(function(done) {
var unzip = require("../lib/unzip");
var input = File.createReadStream("/tmp/pass.pkpass");
var files = new unzip(input);
console.dir(files);
});
it("should do something", function() {
});
});
});
});
// Clone all the fields in object, except the named field, and return a new
// object.
//
// object - Object to clone
// field - Except this field
function cloneExcept(object, field) {
var clone = {};
for (var key in object) {
if (key !== field)
clone[key] = object[key];
}
return clone;
}
function unzip(zipFile, filename, callback) {
execFile("unzip", ["-p", zipFile, filename], { encoding: "binary" }, function(error, stdout) {
if (error) {
callback(new Error(stdout));
} else {
callback(null, new Buffer(stdout, "binary"));
}
});
}