forked from robotmedia/RMStore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRMStoreKeychainPersistenceTests.m
More file actions
196 lines (150 loc) · 5.56 KB
/
Copy pathRMStoreKeychainPersistenceTests.m
File metadata and controls
196 lines (150 loc) · 5.56 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
//
// RMStoreKeychainPersistenceTests.m
// RMStore
//
// Created by Hermes on 10/19/13.
// Copyright (c) 2013 Robot Media. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "RMStoreKeychainPersistence.h"
#import <OCMock/OCMock.h>
#import <objc/runtime.h>
@interface NSBundle(bundleIdentifier)
@end
extern void RMKeychainSetValue(NSData *value, NSString *key);
extern NSString* const RMStoreTransactionsKeychainKey;
/**
[NSBundle bundleIdentifier] returns nil during unit tests. Since RMStoreKeychainPersistence uses it as the keychain service value we have to swizzle it to return a value.
*/
@implementation NSBundle(bundleIdentifier)
- (NSString*)swizzled_bundleIdentifier
{
return @"test";
}
+(void)load
{
Method original = class_getInstanceMethod(self, @selector(bundleIdentifier));
Method swizzle = class_getInstanceMethod(self, @selector(swizzled_bundleIdentifier));
method_exchangeImplementations(original, swizzle);
}
@end
@interface RMStoreKeychainPersistenceTests : XCTestCase
@end
@implementation RMStoreKeychainPersistenceTests {
RMStoreKeychainPersistence *_persistor;
}
- (void)setUp
{
[super setUp];
_persistor = [[RMStoreKeychainPersistence alloc] init];
}
- (void)tearDown
{
[_persistor removeTransactions];
[super tearDown];
}
- (void)testInitialState
{
XCTAssertTrue([_persistor purchasedProductIdentifiers].count == 0, @"");
}
- (void)testPersistTransaction
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[_persistor persistTransaction:transaction];
XCTAssertTrue([_persistor isPurchasedProductOfIdentifier:@"test"], @"");
}
- (void)testRemoveTransactions
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
[_persistor removeTransactions];
XCTAssertFalse([_persistor isPurchasedProductOfIdentifier:@"test"], @"");
}
- (void)testConsumeProductOfIdentifier_YES
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
BOOL result = [_persistor consumeProductOfIdentifier:@"test"];
XCTAssertTrue(result, @"");
}
- (void)testConsumeProductOfIdentifier_NO_inexistingProduct
{
BOOL result = [_persistor consumeProductOfIdentifier:@"test"];
XCTAssertFalse(result, @"");
}
- (void)testConsumeProductOfIdentifier_NO_alreadyConsumedProduct
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
[_persistor consumeProductOfIdentifier:@"test"];
BOOL result = [_persistor consumeProductOfIdentifier:@"test"];
XCTAssertFalse(result, @"");
}
- (void)testcountProductOfdentifier_zero
{
XCTAssertTrue([_persistor countProductOfdentifier:@"test"] == 0, @"");
}
- (void)testcountProductOfdentifier_one
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
XCTAssertTrue([_persistor countProductOfdentifier:@"test"] == 1, @"");
}
- (void)testcountProductOfdentifier_many
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[_persistor persistTransaction:transaction];
[_persistor persistTransaction:transaction];
[_persistor persistTransaction:transaction];
XCTAssertTrue([_persistor countProductOfdentifier:@"test"] == 3, @"");
}
- (void)testIsPurchasedProductOfIdentifier_YES
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
BOOL result = [_persistor isPurchasedProductOfIdentifier:@"test"];
XCTAssertTrue(result, @"");
}
- (void)testIsPurchasedProductOfIdentifier_NO
{
BOOL result = [_persistor isPurchasedProductOfIdentifier:@"test"];
XCTAssertFalse(result, @"");
}
- (void)testPurchasedProductIdentifiers_empty
{
NSSet *result = [_persistor purchasedProductIdentifiers];
XCTAssertTrue(result.count == 0, @"");
}
- (void)testPurchasedProductIdentifiers_one
{
SKPaymentTransaction *transaction = [self mockTransactionOfProductIdentifer:@"test"];
[self keychainPersistTransaction:transaction];
NSSet *result = [_persistor purchasedProductIdentifiers];
XCTAssertTrue(result.count == 1, @"");
XCTAssertEqualObjects([result anyObject], @"test");
}
- (void)testPurchasedProductIdentifiers_many
{
SKPaymentTransaction *transaction1 = [self mockTransactionOfProductIdentifer:@"test1"];
[_persistor persistTransaction:transaction1];
SKPaymentTransaction *transaction2 = [self mockTransactionOfProductIdentifer:@"test2"];
[_persistor persistTransaction:transaction2];
NSSet *result = [_persistor purchasedProductIdentifiers];
XCTAssertTrue(result.count == 2, @"");
}
#pragma mark - Private
- (void)keychainPersistTransaction:(SKPaymentTransaction*)transaction
{
NSDictionary *dictionary = @{transaction.payment.productIdentifier : @1};
NSData *data = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
RMKeychainSetValue(data, RMStoreTransactionsKeychainKey);
}
- (SKPaymentTransaction*)mockTransactionOfProductIdentifer:(NSString*)productIdentifier
{
id transaction = [OCMockObject mockForClass:[SKPaymentTransaction class]];
id payment = [OCMockObject mockForClass:[SKPayment class]];
[[[payment stub] andReturn:productIdentifier] productIdentifier];
[[[transaction stub] andReturn:payment] payment];
return transaction;
}
@end