BUYClientTestBase.m 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
//
//  BUYClientTestBase.m
//  Mobile Buy SDK
//
//  Created by Shopify.
//  Copyright (c) 2015 Shopify Inc. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

#import "BUYClientTestBase.h"
#import "BUYTestConstants.h"

NSString * const BUYShopDomain_Placeholder = @"test_shop";
31 32
NSString * const BUYAPIKey_Placeholder = @"api_key";
NSString * const BUYAppId_Placeholder = @"app_id";
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52

@implementation BUYClientTestBase

- (void)setUp
{
	[super setUp];
	
	[self setupClient];
}

- (void)setupClient
{
	NSBundle *bundle = [NSBundle bundleForClass:[self class]];
	NSString *jsonPath = [bundle pathForResource:@"test_shop_data" ofType:@"json"];
	NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
	NSDictionary *jsonConfig = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:nil];
	
	NSDictionary *environment = [[NSProcessInfo processInfo] environment];
	self.shopDomain = environment[kBUYTestDomain] ?: jsonConfig[kBUYTestDomain];
	self.apiKey = environment[kBUYTestAPIKey] ?: jsonConfig[kBUYTestAPIKey];
53
	self.appId = environment[kBUYTestAppId] ?: jsonConfig[kBUYTestAppId];
54 55
	self.merchantId = environment[kBUYTestMerchantId] ?: jsonConfig[kBUYTestMerchantId];
	
56 57 58
	self.customerEmail = environment[kBUYTestEmail] ?: jsonConfig[kBUYTestEmail];
	self.customerPassword = environment[kBUYTestPassword] ?: jsonConfig[kBUYTestPassword];
	
59 60 61 62 63 64 65 66 67 68 69 70 71 72
	NSDictionary *giftCards = jsonConfig[@"gift_cards"];
	
	self.giftCardCode = environment[kBUYTestGiftCardCode11] ?: giftCards[@"valid11"][@"code"];
	self.giftCardCode2 = environment[kBUYTestGiftCardCode25] ?: giftCards[@"valid25"][@"code"];
	self.giftCardCode3 = environment[kBUYTestGiftCardCode50] ?: giftCards[@"valid50"][@"code"];
	self.giftCardCodeInvalid = environment[kBUYTestInvalidGiftCardCode] ?: giftCards[@"invalid"][@"code"];
	self.giftCardCodeExpired = environment[kBUYTestExpiredGiftCardCode] ?: giftCards[@"expired"][@"code"];
	self.giftCardIdExpired = environment[kBUYTestExpiredGiftCardID] ?: giftCards[@"expired"][@"id"];
	self.discountCodeValid = environment[kBUYTestDiscountCodeValid] ?: jsonConfig[@"discounts"][@"valid"][@"code"];
	self.discountCodeExpired = environment[kBUYTestDiscountCodeExpired] ?: jsonConfig[@"discounts"][@"expired"][@"code"];
	if (environment[kBUYTestProductIdsCommaSeparated]) {
		NSString *productIdsString = [environment[kBUYTestProductIdsCommaSeparated] stringByReplacingOccurrencesOfString:@" " withString:@""];
		self.productIds = [productIdsString componentsSeparatedByString:@","];
	} else {
73 74 75 76
		self.variantUntrackedId = jsonConfig[@"variants"][@"variant_untracked_id"];
		self.variantInventory1Id = jsonConfig[@"variants"][@"variant_inventory1_id"];
		self.variantSoldOutId = jsonConfig[@"variants"][@"variant_soldout_id"];
		
77 78 79 80 81 82 83 84
		self.productIds = jsonConfig[@"product_ids"];
	}
	
	if ([self shouldUseMocks] == YES) {
		NSLog(@"***** Using Mock Tests *****");
		
		self.shopDomain = BUYShopDomain_Placeholder;
		self.apiKey = BUYAPIKey_Placeholder;
85
		self.appId = BUYAppId_Placeholder;
86 87 88 89 90 91 92 93
		
		self.giftCardCode = @"rd11";
		self.giftCardCode2 = @"rd25";
		self.giftCardCode3 = @"rd50";
		self.giftCardCodeExpired = @"1234";
		self.giftCardCodeExpired = @"gibberish";
	}
	
94
	self.client = [[BUYClient alloc] initWithShopDomain:self.shopDomain apiKey:self.apiKey appId:self.appId];
95 96 97 98
}

- (BOOL)shouldUseMocks
{	
99
	if (!self.shopDomain.length && !self.apiKey.length && !self.appId.length) {
100 101 102 103 104 105 106
		_shouldUseMocks = YES;
	}
	
	return _shouldUseMocks;
}

@end