Commit 639aa798 by Dima Bart

Add customer logout test.

- add mocked response for test
- move error generation into BUYRequestOperation
- renamed shopify error domain constant value (ensure uniqueness)
parent 5f313014
...@@ -153,4 +153,38 @@ ...@@ -153,4 +153,38 @@
}]; }];
} }
- (void)testCustomerLogout
{
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerLogin"];
}];
XCTestExpectation *expectation = [self expectationWithDescription:NSStringFromSelector(_cmd)];
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) {
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest * _Nonnull request) {
return [self shouldUseMocks];
} withStubResponse:^OHHTTPStubsResponse * _Nonnull(NSURLRequest * _Nonnull request) {
return [OHHTTPStubsResponse responseWithKey:@"testCustomerLogout"];
}];
[self.client logoutCustomerID:customer.identifier.stringValue callback:^(BUYStatus status, NSError * _Nullable error) {
XCTAssertNil(error);
XCTAssertEqual(status, BUYStatusComplete);
[expectation fulfill];
}];
}];
[self waitForExpectationsWithTimeout:10000 handler:nil];
}
@end @end
...@@ -853,7 +853,7 @@ ...@@ -853,7 +853,7 @@
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];
......
...@@ -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":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"} "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);
......
...@@ -28,7 +28,7 @@ ...@@ -28,7 +28,7 @@
#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";
......
...@@ -108,14 +108,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -108,14 +108,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json";
return status; return status;
} }
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSHTTPURLResponse *)response
{
if ((int)(response.statusCode / 100.0) != 2) { // If not a 2xx response code
return [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:response.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
...@@ -206,11 +198,6 @@ static NSString * const BUYClientJSONMimeType = @"application/json"; ...@@ -206,11 +198,6 @@ 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, NSHTTPURLResponse *response, NSError *error) { BUYRequestOperation *operation = [[BUYRequestOperation alloc] initWithSession:self.session request:request payload:object completion:^(NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
if (!error) {
error = [self errorFromJSON:json response:response];
}
[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) {
error = [[NSError alloc] initWithDomain:kShopifyError code:httpResponse.statusCode userInfo:json]; error = [self errorFromJSON:json response:httpResponse];
} }
completion(json, httpResponse, error); completion(json, httpResponse, error);
...@@ -179,4 +178,14 @@ typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse * ...@@ -179,4 +178,14 @@ typedef void (^BUYRequestJSONCompletion)(NSDictionary *json, NSHTTPURLResponse *
return task; return task;
} }
#pragma mark - Error -
- (NSError *)errorFromJSON:(NSDictionary *)json response:(NSHTTPURLResponse *)response
{
if ((int)(response.statusCode / 100.0) != 2) { // If not a 2xx response code
return [[NSError alloc] initWithDomain:BUYShopifyErrorDomain code:response.statusCode userInfo:json];
}
return nil;
}
@end @end
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