STPCustomerDeserializerTest -> STPCustomerTest

This commit is contained in:
Ben Guo 2017-05-18 14:15:46 -04:00
parent 72943d59c9
commit d035cc1262
3 changed files with 63 additions and 103 deletions

View File

@ -524,7 +524,7 @@
C1BD9B3A1E39416700CEE925 /* STPSourceOwner.h in Headers */ = {isa = PBXBuildFile; fileRef = C1BD9B381E39416700CEE925 /* STPSourceOwner.h */; settings = {ATTRIBUTES = (Public, ); }; };
C1C1012D1E57A26F00C7BFAE /* STPSource+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = C1C1012C1E57A26F00C7BFAE /* STPSource+Private.h */; };
C1C1012E1E57A26F00C7BFAE /* STPSource+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = C1C1012C1E57A26F00C7BFAE /* STPSource+Private.h */; };
C1D23FAD1D37F81F002FD83C /* STPCustomerDeserializerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D23FAC1D37F81F002FD83C /* STPCustomerDeserializerTest.m */; };
C1D23FAD1D37F81F002FD83C /* STPCustomerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D23FAC1D37F81F002FD83C /* STPCustomerTest.m */; };
C1D7B51A1E36B8B9002181F5 /* STPSourceParams.h in Headers */ = {isa = PBXBuildFile; fileRef = C1D7B5181E36B8B9002181F5 /* STPSourceParams.h */; settings = {ATTRIBUTES = (Public, ); }; };
C1D7B51B1E36B8B9002181F5 /* STPSourceParams.h in Headers */ = {isa = PBXBuildFile; fileRef = C1D7B5181E36B8B9002181F5 /* STPSourceParams.h */; settings = {ATTRIBUTES = (Public, ); }; };
C1D7B51C1E36B8B9002181F5 /* STPSourceParams.m in Sources */ = {isa = PBXBuildFile; fileRef = C1D7B5191E36B8B9002181F5 /* STPSourceParams.m */; };
@ -1058,7 +1058,7 @@
C1BD9B331E3940C400CEE925 /* STPSourceVerification.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPSourceVerification.m; sourceTree = "<group>"; };
C1BD9B381E39416700CEE925 /* STPSourceOwner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = STPSourceOwner.h; path = PublicHeaders/STPSourceOwner.h; sourceTree = "<group>"; };
C1C1012C1E57A26F00C7BFAE /* STPSource+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "STPSource+Private.h"; sourceTree = "<group>"; };
C1D23FAC1D37F81F002FD83C /* STPCustomerDeserializerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPCustomerDeserializerTest.m; sourceTree = "<group>"; };
C1D23FAC1D37F81F002FD83C /* STPCustomerTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPCustomerTest.m; sourceTree = "<group>"; };
C1D23FAF1D37FC90002FD83C /* STPTestUtils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STPTestUtils.h; sourceTree = "<group>"; };
C1D23FB01D37FC90002FD83C /* STPTestUtils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STPTestUtils.m; sourceTree = "<group>"; };
C1D23FB31D37FE0B002FD83C /* Card.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = Card.json; sourceTree = "<group>"; };
@ -1459,7 +1459,7 @@
04CDB5251A5F3A9300B854EE /* STPCardTest.m */,
0438EF4A1B741B0100D506CC /* STPCardValidatorTest.m */,
04CDB5261A5F3A9300B854EE /* STPCertTest.m */,
C1D23FAC1D37F81F002FD83C /* STPCustomerDeserializerTest.m */,
C1D23FAC1D37F81F002FD83C /* STPCustomerTest.m */,
C1EEDCC51CA2126000A54582 /* STPDelegateProxyTest.m */,
04A488351CA34DC600506E53 /* STPEmailAddressValidatorTest.m */,
04CDB51F1A5F3A9300B854EE /* STPFormEncoderTest.m */,
@ -2548,7 +2548,7 @@
C124A1851CCAB750007D42EE /* STPAnalyticsClientTest.m in Sources */,
C18867DC1E8B0C4100A77634 /* STPFixtures.m in Sources */,
C16F66AB1CA21BAC006A21B5 /* STPFormTextFieldTest.m in Sources */,
C1D23FAD1D37F81F002FD83C /* STPCustomerDeserializerTest.m in Sources */,
C1D23FAD1D37F81F002FD83C /* STPCustomerTest.m in Sources */,
04827D181D257A6C002DB3E8 /* STPImageLibraryTest.m in Sources */,
0438EF4C1B741B0100D506CC /* STPCardValidatorTest.m in Sources */,
04A4C3921C4F263300B3B290 /* NSArray+StripeBoundSafeTests.m in Sources */,

View File

@ -1,99 +0,0 @@
//
// STPCustomerDeserializerTest.m
// Stripe
//
// Created by Ben Guo on 7/14/16.
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "STPCustomer.h"
#import "StripeError.h"
#import "STPTestUtils.h"
#import "STPSourceProtocol.h"
@interface STPCustomerDeserializerTest : XCTestCase
@end
@implementation STPCustomerDeserializerTest
- (void)testInitWithData_error {
NSError *error = [NSError stp_genericFailedToParseResponseError];
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithData:nil
urlResponse:nil
error:error];
XCTAssertNil(sut.customer);
XCTAssertEqualObjects(sut.error, error);
}
- (void)testInitWithData_nil {
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithData:nil
urlResponse:nil
error:nil];
XCTAssertNil(sut.customer);
XCTAssertNotNil(sut.error);
}
- (void)testInitWithData_invalidData {
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithData:[NSData new]
urlResponse:nil
error:nil];
XCTAssertNil(sut.customer);
XCTAssertNotNil(sut.error);
}
- (void)testInitWithData_validData {
NSDictionary *customer = [STPTestUtils jsonNamed:@"Customer"];
NSData *data = [NSJSONSerialization dataWithJSONObject:customer options:(NSJSONWritingOptions)kNilOptions error:nil];
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithData:data
urlResponse:nil
error:nil];
XCTAssertNotNil(sut.customer);
XCTAssertNil(sut.error);
XCTAssertEqualObjects(sut.customer.stripeID, customer[@"id"]);
}
- (void)testInitWithJSONResponse_invalidJSON {
id json = [NSObject new];
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithJSONResponse:json];
XCTAssertNil(sut.customer);
XCTAssertEqualObjects(sut.error, [NSError stp_genericFailedToParseResponseError]);
}
- (void)testInitWithJSONResponse_validJSON {
NSMutableDictionary *card1 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
card1[@"id"] = @"card_123";
NSMutableDictionary *card2 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
card2[@"id"] = @"card_456";
NSMutableDictionary *applePayCard1 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
applePayCard1[@"id"] = @"card_apple_pay1";
applePayCard1[@"tokenization_method"] = @"apple_pay";
NSMutableDictionary *applePayCard2 = [applePayCard1 mutableCopy];
applePayCard2[@"id"] = @"card_apple_pay2";
NSDictionary *cardSource = [STPTestUtils jsonNamed:@"CardSource"];
NSDictionary *threeDSSource = [STPTestUtils jsonNamed:@"3DSSource"];
NSMutableDictionary *customer = [[STPTestUtils jsonNamed:@"Customer"] mutableCopy];
NSMutableDictionary *sources = [customer[@"sources"] mutableCopy];
sources[@"data"] = @[applePayCard1, card1, applePayCard2, card2, cardSource, threeDSSource];
customer[@"default_source"] = card1[@"id"];
customer[@"sources"] = sources;
STPCustomerDeserializer *sut = [[STPCustomerDeserializer alloc] initWithJSONResponse:customer];
XCTAssertNotNil(sut.customer);
XCTAssertNil(sut.error);
XCTAssertEqualObjects(sut.customer.stripeID, customer[@"id"]);
XCTAssertTrue(sut.customer.sources.count == 4);
XCTAssertEqualObjects(sut.customer.sources[0].stripeID, card1[@"id"]);
XCTAssertEqualObjects(sut.customer.sources[1].stripeID, card2[@"id"]);
XCTAssertEqualObjects(sut.customer.defaultSource.stripeID, card1[@"id"]);
XCTAssertEqualObjects(sut.customer.sources[2].stripeID, cardSource[@"id"]);
XCTAssertEqualObjects(sut.customer.sources[3].stripeID, threeDSSource[@"id"]);
}
@end

View File

@ -0,0 +1,59 @@
//
// STPCustomerTest.m
// Stripe
//
// Created by Ben Guo on 7/14/16.
// Copyright © 2016 Stripe, Inc. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "STPCustomer.h"
#import "StripeError.h"
#import "STPTestUtils.h"
#import "STPSourceProtocol.h"
@interface STPCustomerTest : XCTestCase
@end
@implementation STPCustomerTest
- (void)testDecoding_invalidJSON {
STPCustomer *sut = [STPCustomer decodedObjectFromAPIResponse:@{}];
XCTAssertNil(sut);
}
- (void)testDecoding_validJSON {
NSMutableDictionary *card1 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
card1[@"id"] = @"card_123";
NSMutableDictionary *card2 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
card2[@"id"] = @"card_456";
NSMutableDictionary *applePayCard1 = [[STPTestUtils jsonNamed:@"Card"] mutableCopy];
applePayCard1[@"id"] = @"card_apple_pay1";
applePayCard1[@"tokenization_method"] = @"apple_pay";
NSMutableDictionary *applePayCard2 = [applePayCard1 mutableCopy];
applePayCard2[@"id"] = @"card_apple_pay2";
NSDictionary *cardSource = [STPTestUtils jsonNamed:@"CardSource"];
NSDictionary *threeDSSource = [STPTestUtils jsonNamed:@"3DSSource"];
NSMutableDictionary *customer = [[STPTestUtils jsonNamed:@"Customer"] mutableCopy];
NSMutableDictionary *sources = [customer[@"sources"] mutableCopy];
sources[@"data"] = @[applePayCard1, card1, applePayCard2, card2, cardSource, threeDSSource];
customer[@"default_source"] = card1[@"id"];
customer[@"sources"] = sources;
STPCustomer *sut = [STPCustomer decodedObjectFromAPIResponse:customer];
XCTAssertNotNil(sut);
XCTAssertEqualObjects(sut.stripeID, customer[@"id"]);
XCTAssertTrue(sut.sources.count == 4);
XCTAssertEqualObjects(sut.sources[0].stripeID, card1[@"id"]);
XCTAssertEqualObjects(sut.sources[1].stripeID, card2[@"id"]);
XCTAssertEqualObjects(sut.defaultSource.stripeID, card1[@"id"]);
XCTAssertEqualObjects(sut.sources[2].stripeID, cardSource[@"id"]);
XCTAssertEqualObjects(sut.sources[3].stripeID, threeDSSource[@"id"]);
}
@end