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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// BUYClientTest_Customer.h
// Mobile Buy SDK
//
// Created by Shopify.
// Copyright (c) 2015 Shopify Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
@import XCTest;
#import "BUYClientTestBase.h"
#import "BUYClient+Customers.h"
#import "BUYAccountCredentials.h"
#import "BUYError+BUYAdditions.h"
#import <OHHTTPStubs/OHHTTPStubs.h>
#import "OHHTTPStubsResponse+Helpers.h"
@interface BUYClientTest_Customer : BUYClientTestBase
@property (strong, nonatomic) BUYCustomer *customer;
@property (strong, nonatomic) BUYAddress *createdAddress;
@end
@implementation BUYClientTest_Customer
#pragma mark - Lifecycle -
- (void)setUp
{
[super setUp];
[self loginDefaultCustomer];
}
- (void)tearDown
{
[super tearDown];
[OHHTTPStubs removeAllStubs];
}
- (void)loginDefaultCustomer
{
if (self.customer) {
return;
}
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(customer);
XCTAssertEqualObjects(customer.email, self.customerEmail);
self.customer = customer;
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) {
XCTAssertNil(error);
}];
}
#pragma mark - Creation -
- (void)testCustomerDuplicateEmail
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerDuplicateEmail" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCustomerWithCredentials:[self credentialsForCreation] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer);
XCTAssertNotNil(error);
NSArray *errors = [BUYError errorsFromSignUpJSON:error.userInfo];
XCTAssertEqual(errors.count, 1);
BUYError *customerError = errors[0];
XCTAssertEqualObjects(customerError.code, @"taken");
XCTAssertEqualObjects(customerError.options[@"rescue_from_duplicate"], @YES);
XCTAssertEqualObjects(customerError.options[@"value"], self.customerEmail);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) {
XCTAssertNil(error);
}];
}
- (void)testCustomerInvalidEmailPassword
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerInvalidEmailPassword" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCustomerWithCredentials:[self credentialsForFailure] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer);
XCTAssertNotNil(error);
NSArray<BUYError *> *errors = [BUYError errorsFromSignUpJSON:error.userInfo];
XCTAssertEqual(errors.count, 3);
BUYError *emailError = errors[0];
XCTAssertEqualObjects(emailError.code, @"invalid");
BUYError *passwordConfError = errors[1];
XCTAssertEqualObjects(passwordConfError.code, @"confirmation");
XCTAssertEqualObjects(passwordConfError.options[@"attribute"], @"Password");
BUYError *passwordError = errors[2];
XCTAssertEqualObjects(passwordError.code, @"too_short");
XCTAssertEqualObjects(passwordError.options[@"count"], @5);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:^(NSError * _Nullable error) {
XCTAssertNil(error);
}];
}
#pragma mark - Auth -
- (void)testCustomerLogin
{
[self loginDefaultCustomer];
}
- (void)testCustomerLogout
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogout" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client logoutCustomerID:self.customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertEqual(status, 204);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
#pragma mark - Orders -
- (void)testCustomerOrders
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerGetOrders" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getOrdersForCustomerWithID:self.customer.identifier.stringValue callback:^(NSArray<BUYOrder *> * _Nullable orders, NSError * _Nullable error) {
XCTAssertNotNil(orders);
XCTAssertNil(error);
XCTAssertTrue(orders.count > 0);
XCTAssertTrue([orders.firstObject isKindOfClass:[BUYOrder class]]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
- (void)testCustomerOrderWithID
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerGetOrder" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getOrderWithID:self.customerOrderIDs.firstObject customerID:self.customer.identifier.stringValue callback:^(BUYOrder * _Nullable order, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertTrue([order isKindOfClass:[BUYOrder class]]);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10.0 handler:nil];
}
#pragma mark - Update -
- (void)testCustomerUpdate
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[email]];
[self.client updateCustomerWithCredentials:credentials customerID:self.customer.identifier.stringValue callback:^(BUYCustomer *customer, NSError *error) {
XCTAssertNil(error);
XCTAssertNotNil(customer);
XCTAssertEqualObjects(customer.email, self.customerEmail);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
#pragma mark - Address -
- (void)testGetAddresses
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerAddresses"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getAddressesForCustomerID:self.customer.identifier.stringValue callback:^(NSArray<BUYAddress *> * _Nullable addresses, NSError * _Nullable error) {
XCTAssertNotNil(addresses);
XCTAssertTrue(addresses.count > 0);
XCTAssertTrue([addresses.firstObject isKindOfClass:[BUYAddress class]]);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)testAddressCRUD
{
[self createAddress];
[self getAddress];
[self updateAddress];
[self deleteAddress];
}
- (void)createAddress
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerAddress1"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAddress *address = [self address];
[self.client createAddress:address customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTAssertNotNil(returnedAddress);
XCTAssertNil(error);
XCTAssertNotNil(returnedAddress.identifier);
XCTAssertEqualObjects(address.address1, returnedAddress.address1);
XCTAssertEqualObjects(address.city, returnedAddress.city);
XCTAssertEqualObjects(address.province, returnedAddress.province);
XCTAssertEqualObjects(address.provinceCode, returnedAddress.provinceCode);
XCTAssertEqualObjects(address.country, returnedAddress.country);
XCTAssertEqualObjects(address.countryCode, returnedAddress.countryCode);
XCTAssertEqualObjects(address.zip, returnedAddress.zip);
self.createdAddress = returnedAddress;
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)getAddress
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerAddress1"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getAddressWithID:self.createdAddress.identifier customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable address, NSError * _Nullable error) {
XCTAssertNotNil(address);
XCTAssertNil(error);
XCTAssertEqualObjects(address.identifier, self.createdAddress.identifier);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)updateAddress
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerAddress2"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
BUYAddress *modifiedAddress = [self addressByModyfyingAddress:self.createdAddress];
[self.client updateAddress:modifiedAddress customerID:self.customer.identifier.stringValue callback:^(BUYAddress * _Nullable returnedAddress, NSError * _Nullable error) {
XCTAssertNotNil(returnedAddress);
XCTAssertNil(error);
XCTAssertEqualObjects(modifiedAddress.address1, returnedAddress.address1);
XCTAssertEqualObjects(modifiedAddress.city, returnedAddress.city);
XCTAssertEqualObjects(modifiedAddress.province, returnedAddress.province);
XCTAssertEqualObjects(modifiedAddress.provinceCode, returnedAddress.provinceCode);
XCTAssertEqualObjects(modifiedAddress.country, returnedAddress.country);
XCTAssertEqualObjects(modifiedAddress.countryCode, returnedAddress.countryCode);
XCTAssertEqualObjects(modifiedAddress.zip, returnedAddress.zip);
self.createdAddress = returnedAddress;
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
- (void)deleteAddress
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerAddressDelete"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client deleteAddressWithID:self.createdAddress.identifier customerID:self.customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertEqual(status, 204);
XCTAssertNil(error);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:10 handler:nil];
}
#pragma mark - Address -
- (BUYAddress *)address
{
BUYAddress *address = [[BUYAddress alloc] initWithModelManager:self.client.modelManager JSONDictionary:nil];
address.address1 = @"3892 Streewell Rd.";
address.city = @"Toronto";
address.province = @"Ontario";
address.provinceCode = @"ON";
address.country = @"Canada";
address.countryCode = @"CA";
address.zip = @"L8S 2W2";
return address;
}
- (BUYAddress *)addressByModyfyingAddress:(BUYAddress *)oldAddress;
{
BUYAddress *address = [[BUYAddress alloc] initWithModelManager:self.client.modelManager JSONDictionary:nil];
address.identifier = oldAddress.identifier;
address.address1 = @"8493 Southwest St.";
address.city = @"Vancouver";
address.province = @"British Columbia";
address.provinceCode = @"BC";
address.country = @"Canada";
address.countryCode = @"CA";
address.zip = @"T3G 4D9";
return address;
}
#pragma mark - Credentials -
- (BUYAccountCredentials *)credentialsForLogin
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
return [BUYAccountCredentials credentialsWithItems:@[email, password]];
}
- (BUYAccountCredentials *)credentialsForCreation
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentialItem *password2 = [BUYAccountCredentialItem itemWithPasswordConfirmation:self.customerPassword];
return [BUYAccountCredentials credentialsWithItems:@[email, password, password2]];
}
- (BUYAccountCredentials *)credentialsForFailure
{
BUYAccountCredentialItem *email = [BUYAccountCredentialItem itemWithEmail:@"a"];
BUYAccountCredentialItem *password = [BUYAccountCredentialItem itemWithPassword:@"b"];
BUYAccountCredentialItem *password2 = [BUYAccountCredentialItem itemWithPasswordConfirmation:@"c"];
return [BUYAccountCredentials credentialsWithItems:@[email, password, password2]];
}
@end