forked from parse-community/Parse-SDK-iOS-OSX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PurchaseControllerTests.m
375 lines (278 loc) · 15.3 KB
/
PurchaseControllerTests.m
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/**
* Copyright (c) 2015-present, Parse, LLC.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
@import StoreKit;
#import <OCMock/OCMock.h>
@import Bolts.BFExecutor;
@import Bolts.BFTask;
#import "PFCommandResult.h"
#import "PFCommandRunning.h"
#import "PFEncoder.h"
#import "PFFileManager.h"
#import "PFFileObject_Private.h"
#import "PFMacros.h"
#import "PFPaymentTransactionObserver.h"
#import "PFProductsRequestHandler.h"
#import "PFPurchaseController.h"
#import "PFRESTCommand.h"
#import "PFTestSKPaymentTransaction.h"
#import "PFTestSKProduct.h"
#import "PFTestSKProductsRequest.h"
#import "PFUnitTestCase.h"
#import "Parse_Private.h"
#import "BFTask+Private.h"
@protocol PurchaseControllerDataSource <PFCommandRunnerProvider, PFFileManagerProvider>
@end
@interface PurchaseControllerTests : PFUnitTestCase
@end
@implementation PurchaseControllerTests
///--------------------------------------
#pragma mark - XCTestCase
///--------------------------------------
- (void)setUp {
[super setUp];
PFTestSKProductsRequest.validProducts = PF_SET([self sampleProduct]);
}
- (void)tearDown {
PFTestSKProductsRequest.validProducts = nil;
[[NSFileManager defaultManager] removeItemAtPath:[self sampleReceiptFilePath] error:nil];
[super tearDown];
}
///--------------------------------------
#pragma mark - Helpers
///--------------------------------------
- (id<PFCommandRunnerProvider, PFFileManagerProvider>)mockedDataSource {
id dataSource = PFStrictProtocolMock(@protocol(PurchaseControllerDataSource));
id commandRunner = PFStrictProtocolMock(@protocol(PFCommandRunning));
OCMStub([dataSource commandRunner]).andReturn(commandRunner);
id fileManager = PFStrictClassMock([PFFileManager class]);
OCMStub([dataSource fileManager]).andReturn(fileManager);
return dataSource;
}
- (SKProduct *)sampleProduct {
return [PFTestSKProduct productWithProductIdentifier:@"product"
price:[NSDecimalNumber decimalNumberWithString:@"13.37"]
title:@"Fizz"
description:@"FizzBuzz"];
}
- (NSData *)sampleData {
uint8_t sampleData[16] = {
0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00,
0xFF, 0x00, 0xFF, 0x00,
};
return [NSData dataWithBytes:sampleData length:sizeof(sampleData)];
}
- (NSString *)sampleReceiptFilePath {
return [NSTemporaryDirectory() stringByAppendingPathComponent:@"receipt.data"];
}
///--------------------------------------
#pragma mark - Tests
///--------------------------------------
- (void)testConstructor {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *controller = [[PFPurchaseController alloc] initWithDataSource:dataSource bundle:bundle];
XCTAssertNotNil(controller);
XCTAssertEqual((id)controller.dataSource, dataSource);
XCTAssertEqual(controller.bundle, bundle);
// This makes the test less sad.
controller.paymentQueue = PFClassMock([SKPaymentQueue class]);
XCTAssertNotNil(controller.paymentQueue);
XCTAssertNotNil(controller.transactionObserver);
}
- (void)testFindProductsAsync {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[purchaseController findProductsAsyncWithIdentifiers:PF_SET(@"product")] continueWithSuccessBlock:^id(BFTask *task) {
NSSet *products = [(PFProductsRequestResult *)task.result validProducts];
XCTAssertEqual(products.count, 1);
id product = [products anyObject];
XCTAssertEqualObjects([product productIdentifier], @"product");
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testBuyProductsAsync {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
purchaseController.paymentQueue = PFStrictClassMock([SKPaymentQueue class]);
__block SKPaymentTransaction *transaction = nil;
SKPaymentQueue *paymentQueue = purchaseController.paymentQueue;
// Use a block for this so that we don't accidentally force lazy loading of the transaction observer too early.
// Otherwise it will call addTransactionObserver right away, which will cause us to crash of course.
OCMStub([paymentQueue addTransactionObserver:[OCMArg checkWithBlock:^BOOL(id obj) {
return [obj isEqual:purchaseController.transactionObserver];
}]]);
OCMStub([paymentQueue finishTransaction:[OCMArg checkWithBlock:^BOOL(id obj) {
return obj == transaction;
}]]);
OCMStub([paymentQueue addPayment:OCMOCK_ANY]).andDo(^(NSInvocation *invocation) {
// Do stuff
__unsafe_unretained SKPayment *payment = nil;
[invocation getArgument:&payment atIndex:2];
if ([payment.productIdentifier isEqualToString:@"product"]) {
transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStatePurchased];
} else {
transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStateFailed];
}
[purchaseController.transactionObserver paymentQueue:paymentQueue updatedTransactions: @[ transaction ]];
});
XCTestExpectation *successExpectation = [self expectationWithDescription:@"Success"];
[[purchaseController buyProductAsyncWithIdentifier:@"product"] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
[successExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
id partial = [OCMockObject partialMockForObject:purchaseController];
OCMStub([partial canPurchase]).andReturn(YES);
XCTestExpectation *failInvalidProductExpectation = [self expectationWithDescription:@"Failed Invalid Product"];
[[purchaseController buyProductAsyncWithIdentifier:@"nonexistent"] continueWithBlock:^id(BFTask *task) {
XCTAssertTrue(task.faulted);
XCTAssertNotNil(task.error);
[failInvalidProductExpectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testDownloadAssetAsync {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
purchaseController.paymentQueue = PFStrictClassMock([SKPaymentQueue class]);
SKPayment *payment = [SKPayment paymentWithProduct:[self sampleProduct]];
PFTestSKPaymentTransaction *transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStatePurchased];
NSString *receiptFile = [self sampleReceiptFilePath];
OCMStub([bundle appStoreReceiptURL]).andReturn([NSURL fileURLWithPath:receiptFile]);
[[self sampleData] writeToFile:receiptFile atomically:YES];
PFFileObject *mockedFile = PFPartialMock([PFFileObject fileObjectWithName:@"testData" data:[self sampleData]]);
// lol. Probably should just stick this in the PFFile_Private header.
NSString *stagedPath = [mockedFile valueForKey:@"stagedFilePath"];
OCMStub([mockedFile _cachedFilePath]).andReturn(stagedPath);
PFCommandResult *mockedCommandResult = [PFCommandResult commandResultWithResult:(NSDictionary *)mockedFile
resultString:nil
httpResponse:nil];
BFTask *mockedTask = [BFTask taskWithResult:mockedCommandResult];
NSString *tempDirectory = [NSTemporaryDirectory() stringByAppendingPathComponent:[[NSUUID UUID] UUIDString]];
id commandRunner = [dataSource commandRunner];
OCMStub([[commandRunner ignoringNonObjectArgs] runCommandAsync:[OCMArg isNotNil] withOptions:0]).andReturn(mockedTask);
id fileManager = [dataSource fileManager];
OCMStub([fileManager parseDataItemPathForPathComponent:@"product"]).andReturn(tempDirectory);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
XCTestExpectation *progressExpectation = [self expectationWithDescription:@"progress"];
__block int lastProgress = -1;
[[purchaseController downloadAssetAsyncForTransaction:transaction
withProgressBlock:^(int percentDone) {
XCTAssertGreaterThan(percentDone, lastProgress);
lastProgress = percentDone;
if (lastProgress == 100) {
[progressExpectation fulfill];
}
}
sessionToken:@"token"] continueWithBlock:^id(BFTask *task) {
XCTAssertFalse(task.faulted);
NSData *contentsOfFile = [NSData dataWithContentsOfFile:task.result];
XCTAssertEqualObjects(contentsOfFile, [self sampleData]);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testDownloadInvalidReceipt {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
purchaseController.paymentQueue = PFStrictClassMock([SKPaymentQueue class]);
SKPayment *payment = [SKPayment paymentWithProduct:[self sampleProduct]];
PFTestSKPaymentTransaction *transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStatePurchased];
OCMStub([bundle appStoreReceiptURL]).andReturn(nil);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[purchaseController downloadAssetAsyncForTransaction:transaction
withProgressBlock:nil
sessionToken:@"token"] continueWithBlock:^id(BFTask *task) {
XCTAssertTrue(task.faulted);
XCTAssertNotNil(task.error);
XCTAssertEqual(task.error.code, kPFErrorReceiptMissing);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testDownloadMissingReceiptData {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
purchaseController.paymentQueue = PFStrictClassMock([SKPaymentQueue class]);
SKPayment *payment = [SKPayment paymentWithProduct:[self sampleProduct]];
PFTestSKPaymentTransaction *transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStatePurchased];
OCMStub([bundle appStoreReceiptURL]).andReturn([NSURL fileURLWithPath:[self sampleReceiptFilePath]]);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[purchaseController downloadAssetAsyncForTransaction:transaction
withProgressBlock:nil
sessionToken:@"token"] continueWithBlock:^id(BFTask *task) {
XCTAssertTrue(task.faulted);
XCTAssertNotNil(task.error);
XCTAssertEqual(task.error.code, kPFErrorReceiptMissing);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
- (void)testDownloadInvalidFile {
id dataSource = [self mockedDataSource];
id bundle = PFStrictClassMock([NSBundle class]);
PFPurchaseController *purchaseController = [PFPurchaseController controllerWithDataSource:dataSource bundle:bundle];
purchaseController.productsRequestClass = [PFTestSKProductsRequest class];
purchaseController.paymentQueue = PFStrictClassMock([SKPaymentQueue class]);
SKPayment *payment = [SKPayment paymentWithProduct:[self sampleProduct]];
PFTestSKPaymentTransaction *transaction = [PFTestSKPaymentTransaction transactionForPayment:payment
withError:nil
inState:SKPaymentTransactionStatePurchased];
NSString *temporaryFile = [self sampleReceiptFilePath];
OCMStub([bundle appStoreReceiptURL]).andReturn([NSURL fileURLWithPath:temporaryFile]);
[[self sampleData] writeToFile:temporaryFile atomically:YES];
PFCommandResult *mockedResult = [PFCommandResult commandResultWithResult:@{ @"a" : @"Hello" }
resultString:nil
httpResponse:nil];
BFTask *mockedTask = [BFTask taskWithResult:mockedResult];
id commandRunner = [dataSource commandRunner];
OCMStub([[commandRunner ignoringNonObjectArgs] runCommandAsync:OCMOCK_ANY withOptions:0]).andReturn(mockedTask);
XCTestExpectation *expectation = [self currentSelectorTestExpectation];
[[purchaseController downloadAssetAsyncForTransaction:transaction
withProgressBlock:nil
sessionToken:@"token"] continueWithBlock:^id(BFTask *task) {
XCTAssertTrue(task.faulted);
XCTAssertNotNil(task.error);
XCTAssertEqual(task.error.code, kPFErrorInvalidPurchaseReceipt);
[expectation fulfill];
return nil;
}];
[self waitForTestExpectations];
}
@end