Commit bcd3a4c0 by Dima Bart

Merge pull request #185 from Shopify/feature/customer-logout

Add customer logout and update API.
parents f5c63d58 ea7bfafd
...@@ -130,6 +130,10 @@ ...@@ -130,6 +130,10 @@
@"https://_DOMAIN_/api/customers/customer_token.json" @"https://_DOMAIN_/api/customers/customer_token.json"
); );
XCTAssertEqualObjects( XCTAssertEqualObjects(
[self.client urlForCustomersTokenWithID:identifier].absoluteString,
@"https://_DOMAIN_/api/customers/_ID_/customer_token.json"
);
XCTAssertEqualObjects(
[self.client urlForCustomersTokenRenewalWithID:identifier].absoluteString, [self.client urlForCustomersTokenRenewalWithID:identifier].absoluteString,
@"https://_DOMAIN_/api/customers/_ID_/customer_token/renew.json" @"https://_DOMAIN_/api/customers/_ID_/customer_token/renew.json"
); );
......
...@@ -426,8 +426,8 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg"; ...@@ -426,8 +426,8 @@ NSString * const BUYFakeCustomerToken = @"dsfasdgafdg";
]; ];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:items]; BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:items];
NSString *customerID = @"12345"; NSString *customerID = @"12345";
NSString *customerToken = @"12345"; NSString *token = @"12345";
BUYRequestOperation *task = [self.client activateCustomerWithCredentials:credentials customerID:customerID customerToken:customerToken callback:^(BUYCustomer *customer, NSString *token, NSError *error) { BUYRequestOperation *task = [self.client activateCustomerWithCredentials:credentials customerID:customerID token:token callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
}]; }];
......
...@@ -40,28 +40,22 @@ ...@@ -40,28 +40,22 @@
@implementation BUYClientTest_Customer @implementation BUYClientTest_Customer
#pragma mark - Tear Down -
- (void)tearDown - (void)tearDown
{ {
[super tearDown]; [super tearDown];
[OHHTTPStubs removeAllStubs]; [OHHTTPStubs removeAllStubs];
} }
#pragma mark - Creation -
- (void)testCustomerDuplicateEmail - (void)testCustomerDuplicateEmail
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCustomerDuplicateEmail" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerDuplicateEmail"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCustomerWithCredentials:[self credentialsForCreation] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentialItem *passwordConfItem = [BUYAccountCredentialItem itemWithPasswordConfirmation:self.customerPassword];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem, passwordConfItem]];
[self.client createCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer); XCTAssertNil(customer);
XCTAssertNotNil(error); XCTAssertNotNil(error);
...@@ -85,20 +79,10 @@ ...@@ -85,20 +79,10 @@
- (void)testCustomerInvalidEmailPassword - (void)testCustomerInvalidEmailPassword
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCustomerInvalidEmailPassword" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerInvalidEmailPassword"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client createCustomerWithCredentials:[self credentialsForFailure] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:@"a"];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:@"b"];
BUYAccountCredentialItem *passwordConfItem = [BUYAccountCredentialItem itemWithPasswordConfirmation:@"c"];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem, passwordConfItem]];
[self.client createCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(customer); XCTAssertNil(customer);
XCTAssertNotNil(error); XCTAssertNotNil(error);
...@@ -125,21 +109,14 @@ ...@@ -125,21 +109,14 @@
}]; }];
} }
#pragma mark - Auth -
- (void)testCustomerLogin - (void)testCustomerLogin
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerLogin"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:self.customerEmail];
BUYAccountCredentialItem *passwordItem = [BUYAccountCredentialItem itemWithPassword:self.customerPassword];
BUYAccountCredentials *credentials = [BUYAccountCredentials credentialsWithItems:@[emailItem, passwordItem]];
[self.client loginCustomerWithCredentials:credentials callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
XCTAssertNil(error); XCTAssertNil(error);
XCTAssertNotNil(customer); XCTAssertNotNil(customer);
...@@ -153,4 +130,78 @@ ...@@ -153,4 +130,78 @@
}]; }];
} }
- (void)testCustomerLogout
{
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer *customer, NSString *token, NSError *error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogout" useMocks:[self shouldUseMocks]];
[self.client logoutCustomerID:customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertEqual(status, 204);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithTimeout:10 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 loginCustomerWithCredentials:[self credentialsForLogin] callback:^(BUYCustomer * _Nullable customer, NSString * _Nullable token, NSError * _Nullable error) {
[OHHTTPStubs stubUsingResponseWithKey:@"testCustomerLogin" useMocks:[self shouldUseMocks]];
[self.client updateCustomerWithCredentials:credentials customerID: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 - 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 @end
...@@ -65,11 +65,7 @@ ...@@ -65,11 +65,7 @@
- (void)testGetProductList - (void)testGetProductList
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetProducts_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProducts_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductsPage:0 completion:^(NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error) { [self.client getProductsPage:0 completion:^(NSArray *products, NSUInteger page, BOOL reachedEnd, NSError *error) {
...@@ -86,11 +82,7 @@ ...@@ -86,11 +82,7 @@
- (void)testGetShop - (void)testGetShop
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetShop_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetShop_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShop:^(BUYShop *shop, NSError *error) { [self.client getShop:^(BUYShop *shop, NSError *error) {
...@@ -107,11 +99,7 @@ ...@@ -107,11 +99,7 @@
- (void)testGetProductById - (void)testGetProductById
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetProduct_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProduct_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductById:self.productIds[0] completion:^(BUYProduct *product, NSError *error) { [self.client getProductById:self.productIds[0] completion:^(BUYProduct *product, NSError *error) {
...@@ -136,11 +124,7 @@ ...@@ -136,11 +124,7 @@
- (void)testGetMultipleProductByIds - (void)testGetMultipleProductByIds
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetProducts_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProducts_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -171,11 +155,7 @@ ...@@ -171,11 +155,7 @@
- (void)testProductRequestError - (void)testProductRequestError
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetNonexistentProduct_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetNonexistentProduct_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getProductById:@"asdfdsasdfdsasdfdsasdfjkllkj" completion:^(BUYProduct *product, NSError *error) { [self.client getProductById:@"asdfdsasdfdsasdfdsasdfjkllkj" completion:^(BUYProduct *product, NSError *error) {
...@@ -191,11 +171,7 @@ ...@@ -191,11 +171,7 @@
- (void)testCollections - (void)testCollections
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollections:^(NSArray *collections, NSError *error) { [self.client getCollections:^(NSArray *collections, NSError *error) {
...@@ -217,11 +193,7 @@ ...@@ -217,11 +193,7 @@
- (void)testCollectionsFromFirstPage - (void)testCollectionsFromFirstPage
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetCollection_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCollection_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) { [self.client getCollectionsPage:1 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
...@@ -244,11 +216,7 @@ ...@@ -244,11 +216,7 @@
- (void)testCollectionsFromEmptyPage - (void)testCollectionsFromEmptyPage
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetOutOfIndexCollectionPage_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetOutOfIndexCollectionPage_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getCollectionsPage:999 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) { [self.client getCollectionsPage:999 completion:^(NSArray<BUYCollection *> *collections, NSUInteger page, BOOL reachedEnd, NSError *error) {
...@@ -274,11 +242,7 @@ ...@@ -274,11 +242,7 @@
XCTAssertNotNil(self.collection); XCTAssertNotNil(self.collection);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetProductsInCollection_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProductsInCollection_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -298,11 +262,7 @@ ...@@ -298,11 +262,7 @@
- (void)testValidTags - (void)testValidTags
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetValidTag_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetValidTag_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......
...@@ -70,11 +70,7 @@ ...@@ -70,11 +70,7 @@
- (void)fetchProducts - (void)fetchProducts
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetProductPage_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetProductPage_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -103,11 +99,7 @@ ...@@ -103,11 +99,7 @@
- (void)createCheckout - (void)createCheckout
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_1"];
}];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
_checkout.shippingAddress = [self shippingAddress]; _checkout.shippingAddress = [self shippingAddress];
...@@ -129,11 +121,7 @@ ...@@ -129,11 +121,7 @@
- (void)testCheckoutAttributesAndNotes - (void)testCheckoutAttributesAndNotes
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_1"];
}];
[self createCart]; [self createCart];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
...@@ -163,11 +151,7 @@ ...@@ -163,11 +151,7 @@
- (void)fetchShippingRates - (void)fetchShippingRates
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_2"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client getShippingRatesForCheckout:_checkout completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) { [self.client getShippingRatesForCheckout:_checkout completion:^(NSArray *shippingRates, BUYStatus status, NSError *error) {
...@@ -186,11 +170,7 @@ ...@@ -186,11 +170,7 @@
- (void)updateCheckout - (void)updateCheckout
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_3"];
}];
_checkout.email = @"test@test.com"; _checkout.email = @"test@test.com";
_checkout.shippingRate = _shippingRates[0]; _checkout.shippingRate = _shippingRates[0];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -210,11 +190,7 @@ ...@@ -210,11 +190,7 @@
- (id<BUYPaymentToken>)addCreditCardToCheckout - (id<BUYPaymentToken>)addCreditCardToCheckout
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutFlowUsingCreditCard_4" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutFlowUsingCreditCard_4"];
}];
BUYCreditCard *creditCard = [self creditCard]; BUYCreditCard *creditCard = [self creditCard];
__block id<BUYPaymentToken> token = nil; __block id<BUYPaymentToken> token = nil;
...@@ -307,11 +283,7 @@ ...@@ -307,11 +283,7 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingGiftCardToCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingGiftCardToCheckout_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
} }
...@@ -356,11 +328,7 @@ ...@@ -356,11 +328,7 @@
XCTAssertGreaterThan([_checkout.paymentDue integerValue], 1); XCTAssertGreaterThan([_checkout.paymentDue integerValue], 1);
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingInvalidGiftCardToCheckout_2"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:@"000" toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardWithCode:@"000" toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
...@@ -384,11 +352,7 @@ ...@@ -384,11 +352,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingInvalidGiftCardToCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingInvalidGiftCardToCheckout_2"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client applyGiftCardWithCode:self.giftCardCodeExpired toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client applyGiftCardWithCode:self.giftCardCodeExpired toCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
...@@ -410,11 +374,7 @@ ...@@ -410,11 +374,7 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingGiftCardFromCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingGiftCardFromCheckout_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
...@@ -423,11 +383,7 @@ ...@@ -423,11 +383,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
NSDecimalNumber *giftCardValue = [giftCard.amountUsed copy]; NSDecimalNumber *giftCardValue = [giftCard.amountUsed copy];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingGiftCardFromCheckout_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingGiftCardFromCheckout_3"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client removeGiftCard:giftCard fromCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
...@@ -452,11 +408,7 @@ ...@@ -452,11 +408,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingInvalidGiftCardFromCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingInvalidGiftCardFromCheckout_2"];
}];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @(000) }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : @(000) }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -480,11 +432,7 @@ ...@@ -480,11 +432,7 @@
NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy]; NSDecimalNumber *originalPaymentDue = [_checkout.paymentDue copy];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingExpiredGiftCardFromCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingExpiredGiftCardFromCheckout_2"];
}];
BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : self.giftCardIdExpired }]; BUYGiftCard *giftCard = [[BUYGiftCard alloc] initWithModelManager:_modelManager JSONDictionary:@{ @"id" : self.giftCardIdExpired }];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -507,21 +455,13 @@ ...@@ -507,21 +455,13 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingTwoGiftCardsToCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingTwoGiftCardsToCheckout_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
XCTAssertEqual([_checkout.giftCards count], 1); XCTAssertEqual([_checkout.giftCards count], 1);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingTwoGiftCardsToCheckout_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingTwoGiftCardsToCheckout_3"];
}];
[self applyGiftCardTwoToCheckout]; [self applyGiftCardTwoToCheckout];
} }
...@@ -555,31 +495,19 @@ ...@@ -555,31 +495,19 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingThreeGiftCardsToCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingThreeGiftCardsToCheckout_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
XCTAssertEqual([_checkout.giftCards count], 1); XCTAssertEqual([_checkout.giftCards count], 1);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingThreeGiftCardsToCheckout_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingThreeGiftCardsToCheckout_3"];
}];
[self applyGiftCardTwoToCheckout]; [self applyGiftCardTwoToCheckout];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testApplyingThreeGiftCardsToCheckout_4" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testApplyingThreeGiftCardsToCheckout_4"];
}];
[self applyGiftCardThreeToCheckout]; [self applyGiftCardThreeToCheckout];
} }
...@@ -613,41 +541,25 @@ ...@@ -613,41 +541,25 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingSecondGiftCard_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingSecondGiftCard_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
XCTAssertEqual([_checkout.giftCards count], 1); XCTAssertEqual([_checkout.giftCards count], 1);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingSecondGiftCard_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingSecondGiftCard_3"];
}];
[self applyGiftCardTwoToCheckout]; [self applyGiftCardTwoToCheckout];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingSecondGiftCard_4" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingSecondGiftCard_4"];
}];
[self applyGiftCardThreeToCheckout]; [self applyGiftCardThreeToCheckout];
XCTAssertEqual([_checkout.giftCards count], 3); XCTAssertEqual([_checkout.giftCards count], 3);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingSecondGiftCard_5" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingSecondGiftCard_5"];
}];
[self removeSecondGiftCard]; [self removeSecondGiftCard];
} }
...@@ -681,51 +593,31 @@ ...@@ -681,51 +593,31 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingFirstGiftCard_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingFirstGiftCard_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
XCTAssertEqual([_checkout.giftCards count], 1); XCTAssertEqual([_checkout.giftCards count], 1);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingFirstGiftCard_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingFirstGiftCard_3"];
}];
[self applyGiftCardTwoToCheckout]; [self applyGiftCardTwoToCheckout];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingFirstGiftCard_4" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingFirstGiftCard_4"];
}];
[self applyGiftCardThreeToCheckout]; [self applyGiftCardThreeToCheckout];
XCTAssertEqual([_checkout.giftCards count], 3); XCTAssertEqual([_checkout.giftCards count], 3);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingFirstGiftCard_5" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingFirstGiftCard_5"];
}];
[self removeSecondGiftCard]; [self removeSecondGiftCard];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingFirstGiftCard_6" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingFirstGiftCard_6"];
}];
[self removeFirstGiftCard]; [self removeFirstGiftCard];
} }
...@@ -760,59 +652,35 @@ ...@@ -760,59 +652,35 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_2"];
}];
[self applyGiftCardToCheckout]; [self applyGiftCardToCheckout];
XCTAssertEqual([_checkout.giftCards count], 1); XCTAssertEqual([_checkout.giftCards count], 1);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_3" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_3"];
}];
[self applyGiftCardTwoToCheckout]; [self applyGiftCardTwoToCheckout];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_4" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_4"];
}];
[self applyGiftCardThreeToCheckout]; [self applyGiftCardThreeToCheckout];
XCTAssertEqual([_checkout.giftCards count], 3); XCTAssertEqual([_checkout.giftCards count], 3);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_5" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_5"];
}];
[self removeSecondGiftCard]; [self removeSecondGiftCard];
XCTAssertEqual([_checkout.giftCards count], 2); XCTAssertEqual([_checkout.giftCards count], 2);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_6" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_6"];
}];
[self removeFirstGiftCard]; [self removeFirstGiftCard];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testRemovingAllGiftCards_7" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testRemovingAllGiftCards_7"];
}];
[self removeAllGiftCards]; [self removeAllGiftCards];
} }
...@@ -844,16 +712,12 @@ ...@@ -844,16 +712,12 @@
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testInvalidIntegrationBadShopUrl_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testInvalidIntegrationBadShopUrl_0"];
}];
self.client = [[BUYClient alloc] initWithShopDomain:@"asdfdsasdfdsasdfdsadsfowinfaoinfw.myshopify.com" apiKey:self.apiKey appId:@"88234"]; self.client = [[BUYClient alloc] initWithShopDomain:@"asdfdsasdfdsasdfdsadsfowinfaoinfw.myshopify.com" apiKey:self.apiKey appId:@"88234"];
[self.client createCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) { [self.client createCheckout:_checkout completion:^(BUYCheckout *checkout, NSError *error) {
XCTAssertNotNil(error); XCTAssertNotNil(error);
XCTAssertEqualObjects(error.domain, @"shopify"); XCTAssertEqualObjects(error.domain, @"BUYShopifyErrorDomain");
XCTAssertEqual(error.code, 404); XCTAssertEqual(error.code, 404);
[expectation fulfill]; [expectation fulfill];
...@@ -866,11 +730,7 @@ ...@@ -866,11 +730,7 @@
- (void)testFetchingShippingRatesWithoutShippingAddressShouldReturnPreconditionFailed - (void)testFetchingShippingRatesWithoutShippingAddressShouldReturnPreconditionFailed
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testFetchingShippingRatesWithoutShippingAddress_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testFetchingShippingRatesWithoutShippingAddress_1"];
}];
[self createCart]; [self createCart];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
...@@ -888,11 +748,7 @@ ...@@ -888,11 +748,7 @@
XCTAssertNil(error); XCTAssertNil(error);
}]; }];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testFetchingShippingRatesWithoutShippingAddress_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testFetchingShippingRatesWithoutShippingAddress_2"];
}];
XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -908,11 +764,7 @@ ...@@ -908,11 +764,7 @@
- (void)testFetchingShippingRatesForInvalidCheckoutShouldReturnNotFound - (void)testFetchingShippingRatesForInvalidCheckoutShouldReturnNotFound
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testFetchingShippingRatesWithInvalidCheckout_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testFetchingShippingRatesWithInvalidCheckout_1"];
}];
BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:nil]; BUYCheckout *checkout = [[BUYCheckout alloc] initWithCart:nil];
checkout.token = @"bananaaaa"; checkout.token = @"bananaaaa";
...@@ -947,11 +799,7 @@ ...@@ -947,11 +799,7 @@
_checkout.partialAddressesValue = YES; _checkout.partialAddressesValue = YES;
} }
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCheckoutWithAPartialAddress" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCheckoutWithAPartialAddress"];
}];
//Create the checkout //Create the checkout
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -986,11 +834,7 @@ ...@@ -986,11 +834,7 @@
{ {
[self createCart]; [self createCart];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCreateCheckoutWithValidDiscount_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCreateCheckoutWithValidDiscount_1"];
}];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
_checkout.discount = [self applicableDiscount]; _checkout.discount = [self applicableDiscount];
...@@ -1019,11 +863,7 @@ ...@@ -1019,11 +863,7 @@
{ {
[self createCart]; [self createCart];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCreateCheckoutWithExpiredDiscount_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCreateCheckoutWithExpiredDiscount_1"];
}];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
_checkout.discount = [self inapplicableDiscount]; _checkout.discount = [self inapplicableDiscount];
...@@ -1045,11 +885,7 @@ ...@@ -1045,11 +885,7 @@
{ {
[self createCart]; [self createCart];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCreateCheckoutWithNonExistentDiscount_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCreateCheckoutWithNonExistentDiscount_1"];
}];
_checkout = [[BUYCheckout alloc] initWithCart:_cart]; _checkout = [[BUYCheckout alloc] initWithCart:_cart];
_checkout.discount = [self nonExistentDiscount]; _checkout.discount = [self nonExistentDiscount];
...@@ -1073,11 +909,7 @@ ...@@ -1073,11 +909,7 @@
[self createCart]; [self createCart];
[self createCheckout]; [self createCheckout];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testCreateCheckoutWithValidDiscount_1" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCreateCheckoutWithValidDiscount_1"];
}];
_checkout.discount = [self applicableDiscount]; _checkout.discount = [self applicableDiscount];
...@@ -1098,11 +930,7 @@ ...@@ -1098,11 +930,7 @@
- (void)testGetCheckoutWithInvalidToken - (void)testGetCheckoutWithInvalidToken
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetCheckoutWithInvalidToken_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetCheckoutWithInvalidToken_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -1193,11 +1021,7 @@ ...@@ -1193,11 +1021,7 @@
XCTAssertTrue([_checkout.reservationTime isKindOfClass:[NSNumber class]]); XCTAssertTrue([_checkout.reservationTime isKindOfClass:[NSNumber class]]);
XCTAssertEqual(300, _checkout.reservationTime.intValue); XCTAssertEqual(300, _checkout.reservationTime.intValue);
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testExpiringCheckout_2" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testExpiringCheckout_2"];
}];
// Expire the checkout // Expire the checkout
XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)];
...@@ -1218,11 +1042,7 @@ ...@@ -1218,11 +1042,7 @@
- (void)testCallbackQueue - (void)testCallbackQueue
{ {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) { [OHHTTPStubs stubUsingResponseWithKey:@"testGetShop_0" useMocks:[self shouldUseMocks]];
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testGetShop_0"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)]; XCTestExpectation *expectation2 = [self expectationWithDescription:NSStringFromSelector(_cmd)];
......
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
- (void)testInit - (void)testInit
{ {
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) { BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
// We don't start the session // We don't start the session
}]; }];
...@@ -357,7 +357,7 @@ ...@@ -357,7 +357,7 @@
- (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation completion:(dispatch_block_t)completion - (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation completion:(dispatch_block_t)completion
{ {
return [self operationFulfillingExpectation:expectation responseCompletion:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self operationFulfillingExpectation:expectation responseCompletion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (completion) { if (completion) {
completion(); completion();
} }
...@@ -366,7 +366,7 @@ ...@@ -366,7 +366,7 @@
- (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation responseCompletion:(void(^)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error))completion - (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation responseCompletion:(void(^)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error))completion
{ {
BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) { BUYRequestOperation *operation = [BUYRequestOperation operationWithSession:self.session request:self.request payload:nil completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self asyncMain:^{ [self asyncMain:^{
if (completion) { if (completion) {
completion(json, (id)response, error); completion(json, (id)response, error);
......
...@@ -26,8 +26,15 @@ ...@@ -26,8 +26,15 @@
#import <OHHTTPStubs/OHHTTPStubs.h> #import <OHHTTPStubs/OHHTTPStubs.h>
@interface OHHTTPStubsResponse (Helpers) @interface OHHTTPStubsResponse (Buy)
+ (instancetype)responseWithKey:(NSString *)key; + (instancetype)responseWithKey:(NSString *)key;
@end @end
@interface OHHTTPStubs (Buy)
+ (void)stubUsingResponseWithKey:(NSString *)key;
+ (void)stubUsingResponseWithKey:(NSString *)key useMocks:(BOOL)useMocks;
@end
...@@ -26,28 +26,49 @@ ...@@ -26,28 +26,49 @@
#import "OHHTTPStubsResponse+Helpers.h" #import "OHHTTPStubsResponse+Helpers.h"
static NSDictionary *JSONMock; @implementation OHHTTPStubsResponse (Buy)
@implementation OHHTTPStubsResponse (Helpers) + (NSDictionary *)mockResponses
+ (instancetype)responseWithKey:(NSString *)key
{ {
static NSDictionary *dictionary = nil;
static dispatch_once_t onceToken; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ dispatch_once(&onceToken, ^{
NSBundle *bundle = [NSBundle bundleForClass:[self class]]; NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *jsonPath = [bundle pathForResource:@"mocked_responses" ofType:@"json"]; NSString *jsonPath = [bundle pathForResource:@"mocked_responses" ofType:@"json"];
NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath]; NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
JSONMock = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil]; dictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
}); });
return dictionary;
}
+ (instancetype)responseWithKey:(NSString *)key
NSDictionary *json = JSONMock[key]; {
NSDictionary *mocks = [self mockResponses];
NSDictionary *json = mocks[key];
NSData *data = [json[@"body"] dataUsingEncoding:NSUTF8StringEncoding]; NSData *data = [json[@"body"] dataUsingEncoding:NSUTF8StringEncoding];
int code = [json[@"code"] intValue]; int code = [json[@"code"] intValue];
return [OHHTTPStubsResponse responseWithData:data statusCode:code headers:nil]; return [OHHTTPStubsResponse responseWithData:data statusCode:code headers:nil];
} }
@end
@implementation OHHTTPStubs (Buy)
+ (void)stubUsingResponseWithKey:(NSString *)key
{
[self stubUsingResponseWithKey:key useMocks:YES];
}
+ (void)stubUsingResponseWithKey:(NSString *)key useMocks:(BOOL)useMocks
{
if (useMocks) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return YES;
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:key];
}];
}
}
@end @end
...@@ -123,5 +123,6 @@ ...@@ -123,5 +123,6 @@
"testOutOfStockVariant":{"body":"{\"errors\":{\"checkout\":{\"line_items\":[null,{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]},{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]}],\"source_name\":[],\"reservation_time\":[]}}}"}, "testOutOfStockVariant":{"body":"{\"errors\":{\"checkout\":{\"line_items\":[null,{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]},{\"quantity\":[{\"code\":\"not_enough_in_stock\",\"message\":\"Not enough items available. Only 0 left.\",\"options\":{\"remaining\":0}}]}],\"source_name\":[],\"reservation_time\":[]}}}"},
"testCustomerDuplicateEmail":{"body":"{\"errors\":{\"customer\":{\"email\":[{\"code\":\"taken\",\"message\":\"has already been taken\",\"options\":{\"rescue_from_duplicate\":true,\"value\":\"asd@asd.com\"}}]}}}"}, "testCustomerDuplicateEmail":{"body":"{\"errors\":{\"customer\":{\"email\":[{\"code\":\"taken\",\"message\":\"has already been taken\",\"options\":{\"rescue_from_duplicate\":true,\"value\":\"asd@asd.com\"}}]}}}"},
"testCustomerInvalidEmailPassword":{"body":"{\"errors\":{\"customer\":{\"password\":[{\"code\":\"too_short\",\"message\":\"is too short (minimum is 5 characters)\",\"options\":{\"count\":5}}],\"password_confirmation\":[{\"code\":\"confirmation\",\"message\":\"doesn't match Password\",\"options\":{\"attribute\":\"Password\"}}],\"email\":[{\"code\":\"invalid\",\"message\":\"is invalid\",\"options\":{}}]}}}"}, "testCustomerInvalidEmailPassword":{"body":"{\"errors\":{\"customer\":{\"password\":[{\"code\":\"too_short\",\"message\":\"is too short (minimum is 5 characters)\",\"options\":{\"count\":5}}],\"password_confirmation\":[{\"code\":\"confirmation\",\"message\":\"doesn't match Password\",\"options\":{\"attribute\":\"Password\"}}],\"email\":[{\"code\":\"invalid\",\"message\":\"is invalid\",\"options\":{}}]}}}"},
"testCustomerLogout":{"body":"{}","code":204,"message":"OK"},
"testCustomerLogin":{"body":"{\"customer\":{\"id\":2529265094,\"email\":\"asd@asd.com\",\"default_address\":{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"country\":\"Bouvet Island\",\"zip\":\"24124124\",\"phone\":\"123124124\",\"name\":\"Fast Add\",\"province_code\":null,\"country_code\":\"BV\",\"country_name\":\"Bouvet Island\",\"default\":true},\"verified_email\":true,\"accepts_marketing\":false,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"orders_count\":9,\"total_spent\":\"375.00\",\"created_at\":\"2016-02-16T14:42:24-05:00\",\"updated_at\":\"2016-03-10T16:34:04-05:00\",\"state\":\"enabled\",\"last_order_id\":2566266118,\"last_order_name\":\"#1137\",\"addresses\":[{\"id\":2785988486,\"first_name\":\"AA\",\"last_name\":\"A\",\"phone\":\"\",\"company\":\"\",\"address1\":\"Assiniboine Road\",\"address2\":\"\",\"city\":\"Toronto\",\"province\":\"Ontario\",\"province_code\":\"ON\",\"country\":\"Canada\",\"country_code\":\"CA\",\"zip\":\"M3J1E1\"},{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"phone\":\"123124124\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"province_code\":null,\"country\":\"Bouvet Island\",\"country_code\":\"BV\",\"zip\":\"24124124\"}],\"multipass_identifier\":null,\"tax_exempt\":false}}","code":200,"message":"OK"} "testCustomerLogin":{"body":"{\"customer\":{\"id\":2529265094,\"email\":\"asd@asd.com\",\"default_address\":{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"country\":\"Bouvet Island\",\"zip\":\"24124124\",\"phone\":\"123124124\",\"name\":\"Fast Add\",\"province_code\":null,\"country_code\":\"BV\",\"country_name\":\"Bouvet Island\",\"default\":true},\"verified_email\":true,\"accepts_marketing\":false,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"orders_count\":9,\"total_spent\":\"375.00\",\"created_at\":\"2016-02-16T14:42:24-05:00\",\"updated_at\":\"2016-03-10T16:34:04-05:00\",\"state\":\"enabled\",\"last_order_id\":2566266118,\"last_order_name\":\"#1137\",\"addresses\":[{\"id\":2785988486,\"first_name\":\"AA\",\"last_name\":\"A\",\"phone\":\"\",\"company\":\"\",\"address1\":\"Assiniboine Road\",\"address2\":\"\",\"city\":\"Toronto\",\"province\":\"Ontario\",\"province_code\":\"ON\",\"country\":\"Canada\",\"country_code\":\"CA\",\"zip\":\"M3J1E1\"},{\"id\":2839567814,\"first_name\":\"Fast\",\"last_name\":\"Add\",\"phone\":\"123124124\",\"company\":\"Adidas\",\"address1\":\"Sass\",\"address2\":\"12\",\"city\":\"Qsdasd\",\"province\":null,\"province_code\":null,\"country\":\"Bouvet Island\",\"country_code\":\"BV\",\"zip\":\"24124124\"}],\"multipass_identifier\":null,\"tax_exempt\":false}}","code":200,"message":"OK"}
} }
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
- (void)handleCheckoutResponse:(NSDictionary *)json error:(NSError *)error block:(BUYDataCheckoutBlock)block - (void)handleCheckoutResponse:(NSDictionary *)json error:(NSError *)error block:(BUYDataCheckoutBlock)block
{ {
BUYCheckout *checkout = nil; BUYCheckout *checkout = nil;
if (!error) { if (json && !error) {
checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]]; checkout = [self.modelManager insertCheckoutWithJSONDictionary:json[@"checkout"]];
} }
block(checkout, error); block(checkout, error);
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
- (BUYRequestOperation *)postCheckout:(NSDictionary *)checkoutJSON completion:(BUYDataCheckoutBlock)block - (BUYRequestOperation *)postCheckout:(NSDictionary *)checkoutJSON completion:(BUYDataCheckoutBlock)block
{ {
return [self postRequestForURL:[self urlForCheckouts] object:checkoutJSON completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:[self urlForCheckouts] object:checkoutJSON completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
} }
...@@ -98,9 +98,9 @@ ...@@ -98,9 +98,9 @@
BUYAssert(giftCardCode.length > 0, @"Failed to apply gift card code. Invalid gift card code."); BUYAssert(giftCardCode.length > 0, @"Failed to apply gift card code. Invalid gift card code.");
BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode]; BUYGiftCard *giftCard = [self.modelManager giftCardWithCode:giftCardCode];
NSURL *route = [self urlForCheckoutsUsingGiftCardWithToken:checkout.token]; NSURL *url = [self urlForCheckoutsUsingGiftCardWithToken:checkout.token];
return [self postRequestForURL:route object:giftCard completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:url object:giftCard completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) { if (json && !error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:YES]; [self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:YES];
} }
...@@ -113,8 +113,8 @@ ...@@ -113,8 +113,8 @@
BUYAssertCheckout(checkout); BUYAssertCheckout(checkout);
BUYAssert(giftCard.identifier, @"Failed to remove gift card. Gift card must have a valid identifier."); BUYAssert(giftCard.identifier, @"Failed to remove gift card. Gift card must have a valid identifier.");
NSURL *route = [self urlForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token]; NSURL *url = [self urlForCheckoutsUsingGiftCard:giftCard.identifier token:checkout.token];
return [self deleteRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self deleteRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (!error) { if (!error) {
[self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO]; [self updateCheckout:checkout withGiftCardDictionary:json[@"gift_card"] addingGiftCard:NO];
} }
...@@ -148,8 +148,8 @@ ...@@ -148,8 +148,8 @@
{ {
BUYAssertCheckout(checkout); BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsWithToken:checkout.token]; NSURL *url = [self urlForCheckoutsWithToken:checkout.token];
return [self getRequestForURL:route start:start completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url start:start completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
} }
...@@ -158,8 +158,8 @@ ...@@ -158,8 +158,8 @@
{ {
BUYAssertCheckout(checkout); BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsWithToken:checkout.token]; NSURL *url = [self urlForCheckoutsWithToken:checkout.token];
return [self patchRequestForURL:route object:checkout completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self patchRequestForURL:url object:checkout completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
} }
...@@ -178,8 +178,8 @@ ...@@ -178,8 +178,8 @@
BUYAssert(paymentToken || isFree, @"Failed to complete checkout. Checkout must have a payment token or have a payment value equal to $0.00"); BUYAssert(paymentToken || isFree, @"Failed to complete checkout. Checkout must have a payment token or have a payment value equal to $0.00");
NSURL *route = [self urlForCheckoutsCompletionWithToken:checkout.token]; NSURL *url = [self urlForCheckoutsCompletionWithToken:checkout.token];
return [self postRequestForURL:route object:[paymentToken JSONDictionary] start:NO completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:url object:[paymentToken JSONDictionary] start:NO completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self handleCheckoutResponse:json error:error block:block]; [self handleCheckoutResponse:json error:error block:block];
}]; }];
} }
...@@ -210,10 +210,9 @@ ...@@ -210,10 +210,9 @@
- (BUYRequestOperation *)getCompletionStatusOfCheckoutToken:(NSString *)token start:(BOOL)start completion:(BUYDataStatusBlock)block - (BUYRequestOperation *)getCompletionStatusOfCheckoutToken:(NSString *)token start:(BOOL)start completion:(BUYDataStatusBlock)block
{ {
NSURL *route = [self urlForCheckoutsProcessingWithToken:token]; NSURL *url = [self urlForCheckoutsProcessingWithToken:token];
return [self getRequestForURL:route start:start completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url start:start completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; block([self statusForStatusCode:response.statusCode error:error], error);
block([self statusForStatusCode:statusCode error:error], error);
}]; }];
} }
...@@ -223,18 +222,17 @@ ...@@ -223,18 +222,17 @@
{ {
BUYAssertCheckout(checkout); BUYAssertCheckout(checkout);
NSURL *route = [self urlForCheckoutsShippingRatesWithToken:checkout.token parameters:@{ NSURL *url = [self urlForCheckoutsShippingRatesWithToken:checkout.token parameters:@{
@"checkout" : @"", @"checkout" : @"",
}]; }];
BUYRequestOperation *operation = [self getRequestForURL:route start:NO completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { BUYRequestOperation *operation = [self getRequestForURL:url start:NO completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *shippingRates = nil; NSArray *shippingRates = nil;
if (json && !error) { if (json && !error) {
shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]]; shippingRates = [self.modelManager insertShippingRatesWithJSONArray:json[@"shipping_rates"]];
} }
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode]; block(shippingRates, [self statusForStatusCode:response.statusCode error:error], error);
block(shippingRates, [self statusForStatusCode:statusCode error:error], error);
}]; }];
operation.pollingHandler = ^BOOL(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) { operation.pollingHandler = ^BOOL(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
...@@ -259,7 +257,7 @@ ...@@ -259,7 +257,7 @@
json[@"billing_address"] = [checkout.billingAddress jsonDictionaryForCheckout]; json[@"billing_address"] = [checkout.billingAddress jsonDictionaryForCheckout];
} }
return [self postRequestForURL:checkout.paymentURL object:@{ @"checkout" : json } completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:checkout.paymentURL object:@{ @"checkout" : json } completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
id<BUYPaymentToken> token = nil; id<BUYPaymentToken> token = nil;
if (!error) { if (!error) {
token = [[BUYCreditCardToken alloc] initWithPaymentSessionID:json[@"id"]]; token = [[BUYCreditCardToken alloc] initWithPaymentSessionID:json[@"id"]];
......
...@@ -67,6 +67,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -67,6 +67,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
@interface BUYClient (Customers) @interface BUYClient (Customers)
#pragma mark - Getting -
/** /**
* GET /api/customers/:customer_id * GET /api/customers/:customer_id
* Gets an existing customer * Gets an existing customer
...@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -78,6 +80,8 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/ */
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block; - (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
#pragma mark - Customer -
/** /**
* POST /api/customers * POST /api/customers
* Creates a new customer * Creates a new customer
...@@ -93,27 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -93,27 +97,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/** /**
* POST /api/customers/customer_token * PUT /api/customers/:customer_id/activate
* Logs in an existing customer * Activates an unactivated customer
* Expects email and password
* *
* @param credentials Credentials object containing items for required fields * @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer being activated
* @param token Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error) * @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
/** /**
* POST /api/customers/recover * PUT /api/customers/:customer_id
* Sends email for password recovery to an existing customer * Update customer credentials represented by BUYAccountCredentials object
* *
* @param email Email to send the password reset to * @param credentials Credentials containing a password and password confirmation
* @param block (BUYStatus status, NSError *error) * @param customerID ID of customer being activated
* @param block (BUYCustomer *customer, NSError *error)
* *
* @return the associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block; - (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block;
/**
* PUT /api/customers/:customer_id/reset
* Resets an existing customer's password
*
* @param credentials Credentials containing a password and password confirmation
* @param customerID ID of customer resetting password
* @param token Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
*
* @return The associated BUYRequestOperation
*/
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block;
#pragma mark - Token -
/** /**
* PUT /api/customers/:customer_id/customer_token/renew * PUT /api/customers/:customer_id/customer_token/renew
...@@ -126,31 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr ...@@ -126,31 +147,44 @@ typedef void (^BUYDataOrdersBlock)(NSArray <BUYOrder*> * _Nullable orders, NSErr
*/ */
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block; - (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block;
#pragma mark - Login -
/** /**
* PUT /api/customers/:customer_id/activate * POST /api/customers/customer_token
* Activates an unactivated customer * Logs in an existing customer
* Expects email and password
* *
* @param credentials Credentials containing a password and password confirmation * @param credentials Credentials object containing items for required fields
* @param customerID ID of customer being activated
* @param customerToken Token contained in activation URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error) * @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block;
/** /**
* PUT /api/customers/:customer_id/reset * DELETE /api/customers/:customer_id/customer_token
* Resets an existing customer's password * Logs out an existing customer
* Expects a customerID string
* *
* @param credentials Credentials containing a password and password confirmation * @param customer A customerID represented by a string
* @param customerID ID of customer resetting password * @param block (BUYStatus status, NSError *error)
* @param customerToken Token contained in reset URL
* @param block (BUYCustomer *customer, NSString *token, NSError *error)
* *
* @return The associated BUYRequestOperation * @return The associated BUYRequestOperation
*/ */
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block; - (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block;
/**
* POST /api/customers/recover
* Sends email for password recovery to an existing customer
*
* @param email Email to send the password reset to
* @param block (BUYStatus status, NSError *error)
*
* @return the associated BUYRequestOperation
*/
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block;
#pragma mark - Orders -
/** /**
* GET /api/customers/:customer_id/orders * GET /api/customers/:customer_id/orders
......
...@@ -36,12 +36,12 @@ ...@@ -36,12 +36,12 @@
@implementation BUYClient (Customers) @implementation BUYClient (Customers)
#pragma mark - Customer #pragma mark - Getting -
- (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block - (BUYRequestOperation *)getCustomerWithID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{ {
NSURL *route = [self urlForCustomersWithID:customerID]; NSURL *url = [self urlForCustomersWithID:customerID];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYCustomer *customer = nil; BUYCustomer *customer = nil;
if (json && !error) { if (json && !error) {
customer = [self.modelManager customerWithJSONDictionary:json]; customer = [self.modelManager customerWithJSONDictionary:json];
...@@ -50,10 +50,12 @@ ...@@ -50,10 +50,12 @@
}]; }];
} }
#pragma mark - Customer -
- (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)createCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomers]; NSURL *url = [self urlForCustomers];
return [self postRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self postRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) { if (json && !error) {
[self createTokenForCustomerWithCredentials:credentials customerJSON:json callback:block]; [self createTokenForCustomerWithCredentials:credentials customerJSON:json callback:block];
} }
...@@ -63,23 +65,15 @@ ...@@ -63,23 +65,15 @@
}]; }];
} }
- (BUYRequestOperation *)createTokenForCustomerWithCredentials:(BUYAccountCredentials *)credentials customerJSON:(NSDictionary *)customerJSON callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomersToken]; NSURL *url = [self urlForCustomersActivationWithID:customerID parameters:@{ @"token": token }];
return [self postRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json];
self.customerToken = authenticatedResponse.accessToken;
if (!customerJSON) { return [self putRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self getCustomerWithID:authenticatedResponse.customerID callback:^(BUYCustomer *customer, NSError *error) { NSString *email = json[@"customer"][@"email"];
block(customer, self.customerToken, error); if (email && !error) {
}]; BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
} [self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
else {
BUYCustomer *customer = [self.modelManager customerWithJSONDictionary:json[@"customer"]];
block(customer, self.customerToken, error);
}
} }
else { else {
block(nil, nil, error); block(nil, nil, error);
...@@ -87,40 +81,48 @@ ...@@ -87,40 +81,48 @@
}]; }];
} }
- (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)updateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID callback:(BUYDataCustomerBlock)block
{ {
return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block]; NSURL *url = [self urlForCustomersWithID:customerID];
return [self putRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYCustomer *customer = nil;
if (json && !error) {
customer = [self.modelManager customerWithJSONDictionary:json];
}
block(customer, error);
}];
} }
- (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block - (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID token:(NSString *)token callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomersPasswordRecovery]; NSURL *url = [self urlForCustomersPasswordResetWithID:customerID parameters:@{ @"token": token }];
return [self postRequestForURL:route object:@{@"email": email} completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) {
NSInteger statusCode = [(NSHTTPURLResponse *)response statusCode];
if (!error) {
error = [self errorFromJSON:json response:response];
}
block(statusCode, error); return [self putRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSString *email = json[@"customer"][@"email"];
if (email && !error) {
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email];
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block];
}
else {
block(nil, nil, error);
}
}]; }];
} }
#pragma mark - Token -
- (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block - (BUYRequestOperation *)renewCustomerTokenWithID:(NSString *)customerID callback:(BUYDataTokenBlock)block
{ {
if (self.customerToken) { if (self.customerToken) {
NSURL *route = [self urlForCustomersTokenRenewalWithID:customerID]; NSURL *url = [self urlForCustomersTokenRenewalWithID:customerID];
return [self putRequestForURL:route object:nil completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self putRequestForURL:url object:nil completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSString *accessToken = nil; NSString *accessToken = nil;
if (json && !error) { if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json]; BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json];
accessToken = authenticatedResponse.accessToken; accessToken = authenticatedResponse.accessToken;
} }
if (!error) {
error = [self errorFromJSON:json response:response];
}
block(accessToken, error); block(accessToken, error);
}]; }];
} }
...@@ -130,46 +132,35 @@ ...@@ -130,46 +132,35 @@
} }
} }
- (BUYRequestOperation *)activateCustomerWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block #pragma mark - Login -
{
NSURL *route = [self urlForCustomersActivationWithID:customerID parameters:@{
@"token": customerToken,
}];
return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { - (BUYRequestOperation *)logoutCustomerID:(NSString *)customerID callback:(BUYDataStatusBlock)block
NSString *email = json[@"customer"][@"email"]; {
if (email && !error) { NSURL *url = [self urlForCustomersTokenWithID:customerID];
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email]; return [self deleteRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block]; block(response.statusCode, error);
}
else {
block(nil, nil, error);
}
}]; }];
} }
- (BUYRequestOperation *)resetPasswordWithCredentials:(BUYAccountCredentials *)credentials customerID:(NSString *)customerID customerToken:(NSString *)customerToken callback:(BUYDataCustomerTokenBlock)block - (BUYRequestOperation *)loginCustomerWithCredentials:(BUYAccountCredentials *)credentials callback:(BUYDataCustomerTokenBlock)block
{ {
NSURL *route = [self urlForCustomersPasswordResetWithID:customerID parameters:@{ return [self createTokenForCustomerWithCredentials:credentials customerJSON:nil callback:block];
@"token": customerToken, }
}];
return [self putRequestForURL:route object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { - (BUYRequestOperation *)recoverPasswordForCustomer:(NSString *)email callback:(BUYDataStatusBlock)block
NSString *email = json[@"customer"][@"email"]; {
if (email && !error) { NSURL *url = [self urlForCustomersPasswordRecovery];
BUYAccountCredentialItem *emailItem = [BUYAccountCredentialItem itemWithEmail:email]; return [self postRequestForURL:url object:@{@"email": email} completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self loginCustomerWithCredentials:[credentials credentialsByAddingItems:@[emailItem]] callback:block]; block(response.statusCode, error);
}
else {
block(nil, nil, error);
}
}]; }];
} }
#pragma mark - Orders -
- (BUYRequestOperation *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block - (BUYRequestOperation *)getOrdersForCustomerWithCallback:(BUYDataOrdersBlock)block
{ {
NSURL *route = [self urlForCustomersOrders]; NSURL *url = [self urlForCustomersOrders];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) { if (json && !error) {
NSArray *orders = [self.modelManager ordersWithJSONDictionary:json]; NSArray *orders = [self.modelManager ordersWithJSONDictionary:json];
block(orders, error); block(orders, error);
...@@ -179,4 +170,30 @@ ...@@ -179,4 +170,30 @@
}]; }];
} }
#pragma mark - Helpers -
- (BUYRequestOperation *)createTokenForCustomerWithCredentials:(BUYAccountCredentials *)credentials customerJSON:(NSDictionary *)customerJSON callback:(BUYDataCustomerTokenBlock)block
{
NSURL *url = [self urlForCustomersToken];
return [self postRequestForURL:url object:credentials.JSONRepresentation completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (json && !error) {
BUYAuthenticatedResponse *authenticatedResponse = [BUYAuthenticatedResponse responseWithJSON:json];
self.customerToken = authenticatedResponse.accessToken;
if (!customerJSON) {
[self getCustomerWithID:authenticatedResponse.customerID callback:^(BUYCustomer *customer, NSError *error) {
block(customer, self.customerToken, error);
}];
}
else {
BUYCustomer *customer = [self.modelManager customerWithJSONDictionary:json[@"customer"]];
block(customer, self.customerToken, error);
}
}
else {
block(nil, nil, error);
}
}];
}
@end @end
...@@ -28,11 +28,11 @@ ...@@ -28,11 +28,11 @@
#import "BUYClient+Checkout.h" #import "BUYClient+Checkout.h"
#import "BUYSerializable.h" #import "BUYSerializable.h"
static NSString * const BUYShopifyErrorDomain = @"shopify"; static NSString * const BUYShopifyErrorDomain = @"BUYShopifyErrorDomain";
static NSString * const BUYClientVersionString = @"1.3"; static NSString * const BUYClientVersionString = @"1.3";
static NSString * const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token"; static NSString * const BUYClientCustomerAccessToken = @"X-Shopify-Customer-Access-Token";
typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSURLResponse *response, NSError *error); typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error);
@interface BUYClient (Internal) @interface BUYClient (Internal)
...@@ -51,7 +51,6 @@ typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSURLResponse ...@@ -51,7 +51,6 @@ typedef void (^BUYClientRequestJSONCompletion)(NSDictionary *json, NSURLResponse
- (BUYRequestOperation *)patchRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object start:(BOOL)start completionHandler:(BUYClientRequestJSONCompletion)completionHandler; - (BUYRequestOperation *)patchRequestForURL:(NSURL *)url object:(id <BUYSerializable>)object start:(BOOL)start completionHandler:(BUYClientRequestJSONCompletion)completionHandler;
- (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error; - (BUYStatus)statusForStatusCode:(NSUInteger)statusCode error:(NSError *)error;
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response;
- (void)startOperation:(BUYOperation *)operation; - (void)startOperation:(BUYOperation *)operation;
......
...@@ -50,6 +50,7 @@ ...@@ -50,6 +50,7 @@
- (NSURL *)urlForCustomersWithID:(NSString *)identifier; - (NSURL *)urlForCustomersWithID:(NSString *)identifier;
- (NSURL *)urlForCustomersActivationWithID:(NSString *)identifier parameters:(NSDictionary *)parameters; - (NSURL *)urlForCustomersActivationWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
- (NSURL *)urlForCustomersToken; - (NSURL *)urlForCustomersToken;
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID; - (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID;
- (NSURL *)urlForCustomersPasswordRecovery; - (NSURL *)urlForCustomersPasswordRecovery;
- (NSURL *)urlForCustomersPasswordResetWithID:(NSString *)identifier parameters:(NSDictionary *)parameters; - (NSURL *)urlForCustomersPasswordResetWithID:(NSString *)identifier parameters:(NSDictionary *)parameters;
......
...@@ -193,6 +193,11 @@ ...@@ -193,6 +193,11 @@
return [[[self urlForCustomers] appendPath:@"/customer_token"] appendExtension]; return [[[self urlForCustomers] appendPath:@"/customer_token"] appendExtension];
} }
- (NSURL *)urlForCustomersTokenWithID:(NSString *)customerID
{
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token"] appendExtension];
}
- (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID - (NSURL *)urlForCustomersTokenRenewalWithID:(NSString *)customerID
{ {
return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token/renew"] appendExtension]; return [[[self urlForCustomersWithID:customerID] appendPath:@"/customer_token/renew"] appendExtension];
......
...@@ -49,7 +49,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -49,7 +49,7 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getShop:(BUYDataShopBlock)block - (BUYRequestOperation *)getShop:(BUYDataShopBlock)block
{ {
return [self getRequestForURL:[self urlForShop] completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:[self urlForShop] completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
BUYShop *shop = nil; BUYShop *shop = nil;
if (json && !error) { if (json && !error) {
shop = [self.modelManager insertShopWithJSONDictionary:json]; shop = [self.modelManager insertShopWithJSONDictionary:json];
...@@ -60,12 +60,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -60,12 +60,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block - (BUYRequestOperation *)getProductsPage:(NSUInteger)page completion:(BUYDataProductListBlock)block
{ {
NSURL *route = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
}]; }];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && !error) { if (json && !error) {
...@@ -95,11 +95,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -95,11 +95,11 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
{ {
BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil."); BUYAssert(productIds, @"Failed to get product by IDs. Product IDs array must not be nil.");
NSURL *route = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"product_ids" : [productIds componentsJoinedByString:@","], @"product_ids" : [productIds componentsJoinedByString:@","],
}]; }];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && !error) { if (json && !error) {
...@@ -121,12 +121,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -121,12 +121,12 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
- (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block - (BUYRequestOperation *)getCollectionsPage:(NSUInteger)page completion:(BUYDataCollectionsListBlock)block
{ {
NSURL *route = [self urlForCollectionListingsWithParameters:@{ NSURL *url = [self urlForCollectionListingsWithParameters:@{
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
}]; }];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *collections = nil; NSArray *collections = nil;
if (json && !error) { if (json && !error) {
...@@ -145,14 +145,14 @@ static NSString * const BUYCollectionsKey = @"collection_listings"; ...@@ -145,14 +145,14 @@ static NSString * const BUYCollectionsKey = @"collection_listings";
{ {
BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID."); BUYAssert(collectionId, @"Failed to get products page. Invalid collectionID.");
NSURL *route = [self urlForProductListingsWithParameters:@{ NSURL *url = [self urlForProductListingsWithParameters:@{
@"collection_id" : collectionId, @"collection_id" : collectionId,
@"limit" : @(self.pageSize), @"limit" : @(self.pageSize),
@"page" : @(page), @"page" : @(page),
@"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder] @"sort_by" : [BUYCollection sortOrderParameterForCollectionSort:sortOrder]
}]; }];
return [self getRequestForURL:route completionHandler:^(NSDictionary *json, NSURLResponse *response, NSError *error) { return [self getRequestForURL:url completionHandler:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
NSArray *products = nil; NSArray *products = nil;
if (json && !error) { if (json && !error) {
......
...@@ -123,15 +123,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -123,15 +123,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
} }
} }
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSURLResponse *)response
{
NSInteger statusCode = [((NSHTTPURLResponse *) response) statusCode];
if ((int)(statusCode / 100.0) != 2) { // If not a 2xx response code
return [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:statusCode userInfo:json];
}
return nil;
}
#pragma mark - Auto Starting Convenience Requests #pragma mark - Auto Starting Convenience Requests
- (BUYRequestOperation *)getRequestForURL:(NSURL *)url completionHandler:(BUYClientRequestJSONCompletion)completionHandler - (BUYRequestOperation *)getRequestForURL:(NSURL *)url completionHandler:(BUYClientRequestJSONCompletion)completionHandler
...@@ -221,7 +212,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -221,7 +212,7 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
request.HTTPMethod = method; request.HTTPMethod = method;
BUYRequestOperation *operation = [[BUYRequestOperation alloc] initWithSession:self.session request:request payload:object completion:^(NSDictionary *json, NSURLResponse *response, NSError *error) { BUYRequestOperation *operation = [[BUYRequestOperation alloc] initWithSession:self.session request:request payload:object completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self.callbackQueue addOperationWithBlock:^{ [self.callbackQueue addOperationWithBlock:^{
completionHandler(json, response, error); completionHandler(json, response, error);
}]; }];
......
...@@ -26,8 +26,7 @@ ...@@ -26,8 +26,7 @@
#import "BUYRequestOperation.h" #import "BUYRequestOperation.h"
#import "BUYSerializable.h" #import "BUYSerializable.h"
#import "BUYClient+Internal.h"
NSString * const kShopifyError = @"shopify";
typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error); typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *response, NSError *error);
...@@ -169,9 +168,9 @@ typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse * ...@@ -169,9 +168,9 @@ typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *
json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
} }
NSHTTPURLResponse *httpResponse = (id)response; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (!httpResponse.successful && !error) { if (!error && !httpResponse.successful) {
error = [[NSError alloc] initWithDomain:kShopifyError code:httpResponse.statusCode userInfo:json]; error = [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:httpResponse.statusCode userInfo:json];
} }
completion(json, httpResponse, error); completion(json, httpResponse, error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment