1
|
|
|
from authorizenet.constants import constants |
2
|
|
|
from decimal import * |
3
|
|
|
import logging |
4
|
|
|
import datetime |
5
|
|
|
import unittest |
6
|
|
|
from authorizenet import utility |
7
|
|
|
import xml.dom.minidom |
8
|
|
|
from authorizenet import apicontractsv1 |
9
|
|
|
|
10
|
|
|
class test_CreateTransactionUnitTest(unittest.TestCase): |
11
|
|
|
def testPyxbDeserializationElementAtMid(self): |
12
|
|
|
self.__PyxbDeserialization(False) |
13
|
|
|
|
14
|
|
|
def testPyxbDeserializationElementAtLast(self): |
15
|
|
|
self.__PyxbDeserialization(True) |
16
|
|
|
|
17
|
|
|
def testPyxbDeserializationGoodXML(self): |
18
|
|
|
self.__PyxbDeserialization() |
19
|
|
|
|
20
|
|
|
def __PyxbDeserialization(self, lastElement = None): |
21
|
|
|
loggingfilename = utility.helper.getproperty(constants.propertiesloggingfilename) |
22
|
|
|
logginglevel = utility.helper.getproperty(constants.propertiesexecutionlogginglevel) |
23
|
|
|
|
24
|
|
|
deserializedObject = None |
25
|
|
|
deserializedBadObject = None |
26
|
|
|
|
27
|
|
|
if (None == loggingfilename): |
28
|
|
|
loggingfilename = constants.defaultLogFileName |
29
|
|
|
if (None == logginglevel): |
30
|
|
|
logginglevel = constants.defaultLoggingLevel |
31
|
|
|
|
32
|
|
|
logging.basicConfig(filename=loggingfilename, level=logginglevel, format=constants.defaultlogformat) |
33
|
|
|
|
34
|
|
|
merchantAuth = apicontractsv1.merchantAuthenticationType() |
35
|
|
|
merchantAuth.name = "unknown" |
36
|
|
|
merchantAuth.transactionKey = "anon" |
37
|
|
|
|
38
|
|
|
creditCard = apicontractsv1.creditCardType() |
39
|
|
|
creditCard.cardNumber = "4111111111111111" |
40
|
|
|
creditCard.expirationDate = "2020-12" |
41
|
|
|
|
42
|
|
|
payment = apicontractsv1.paymentType() |
43
|
|
|
payment.creditCard = creditCard |
44
|
|
|
|
45
|
|
|
transactionrequest = apicontractsv1.transactionRequestType() |
46
|
|
|
transactionrequest.transactionType = "authCaptureTransaction" |
47
|
|
|
transactionrequest.amount = Decimal( 6.99) |
48
|
|
|
transactionrequest.payment = payment |
49
|
|
|
|
50
|
|
|
createtransactionrequest = apicontractsv1.createTransactionRequest() |
51
|
|
|
createtransactionrequest.merchantAuthentication = merchantAuth |
52
|
|
|
createtransactionrequest.transactionRequest = transactionrequest |
53
|
|
|
createtransactionrequest.refId = "MerchantID-0001" |
54
|
|
|
|
55
|
|
|
logging.debug( "Request: %s " % datetime.datetime.now()) |
56
|
|
|
logging.debug( " : %s " % createtransactionrequest ) |
57
|
|
|
|
58
|
|
|
try: |
59
|
|
|
xmlRequest = createtransactionrequest.toxml(encoding=constants.xml_encoding, element_name='createTransactionRequest') |
60
|
|
|
xmlRequest = xmlRequest.replace(constants.nsNamespace1, '') |
61
|
|
|
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '') |
62
|
|
|
##print ("xmlRequest %s " %xmlRequest) |
63
|
|
|
logging.debug( "Xml Request: %s" % xmlRequest) |
64
|
|
|
except Exception as ex: |
65
|
|
|
logging.debug( "Xml Exception: %s" % ex) |
66
|
|
|
|
67
|
|
|
badXmlElement = None |
68
|
|
|
|
69
|
|
|
if (lastElement == None): |
70
|
|
|
try: |
71
|
|
|
deserializedObject = apicontractsv1.CreateFromDocument(xmlRequest) |
72
|
|
|
self.assertIsNotNone(deserializedObject, "Null deserializedObject ") |
73
|
|
|
|
74
|
|
|
if type(createtransactionrequest) == type(deserializedObject): |
75
|
|
|
##print (" for good xml objects are equal") |
76
|
|
|
logging.debug( "createtransactionrequest object is equal to deserializedObject") |
77
|
|
|
else: |
78
|
|
|
##print ("for good xml some error: objects are NOT equal" ) |
79
|
|
|
logging.debug( "createtransactionrequest object is NOT equal to deserializedObject") |
80
|
|
|
|
81
|
|
|
deseriaziedObjectXmlRequest = deserializedObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject') |
82
|
|
|
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace1, '') |
83
|
|
|
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace2, '') |
84
|
|
|
logging.debug( "Good Dom Request: %s " % deseriaziedObjectXmlRequest ) |
85
|
|
|
##print ( "Good De-serialized XML: %s \n" % deseriaziedObjectXmlRequest ) |
86
|
|
|
except Exception as ex: |
87
|
|
|
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
88
|
|
|
else: |
89
|
|
|
if (lastElement == False): |
90
|
|
|
try: |
91
|
|
|
splitString = "<amount>" |
92
|
|
|
lines = xmlRequest.split( splitString) |
93
|
|
|
badXmlElement = "<badElement>BadElement</badElement>" |
94
|
|
|
badXmlRequest = lines[0] + badXmlElement + splitString + lines[1] |
95
|
|
|
logging.debug( "Bad XmlRequest: %s" % badXmlRequest) |
96
|
|
|
##print ("ElementInMidXML Request: %s \n" %badXmlRequest) |
97
|
|
|
except Exception as ex: |
98
|
|
|
##print ("ElementInMidXML can not be inserted: %s, %s",type(ex), ex.args) |
99
|
|
|
logging.debug( "ElementInMidXML can not be inserted: %s, %s" ,type(ex), ex.args) |
100
|
|
|
if (lastElement == True): |
101
|
|
|
try: |
102
|
|
|
splitStringAtLast = "</payment>" |
103
|
|
|
lines = xmlRequest.split( splitStringAtLast) |
104
|
|
|
badXmlElementAtLast = "<badElementAtLast>BadElementAtLast</badElementAtLast>" |
105
|
|
|
badXmlRequest = lines[0] + badXmlElementAtLast + splitStringAtLast + lines[1] |
106
|
|
|
logging.debug( "Bad XmlRequest at Last: %s" % badXmlRequest) |
107
|
|
|
##print ("ElementAtLastXML Request: %s \n" %badXmlRequest) |
108
|
|
|
except Exception as ex: |
109
|
|
|
##print ("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args) |
110
|
|
|
logging.debug("ElementAtLastXML can not be inserted: %s, %s",type(ex), ex.args) |
111
|
|
|
try: |
112
|
|
|
deserializedBadObject = apicontractsv1.CreateFromDocument(badXmlRequest) |
113
|
|
|
self.assertIsNotNone(deserializedBadObject, "Null deserializedObject ") |
114
|
|
|
badDomXml = deserializedBadObject.toxml(encoding=constants.xml_encoding, element_name='deserializedBadObject') |
115
|
|
|
badDomXml = badDomXml.replace(constants.nsNamespace1, '') |
116
|
|
|
badDomXml = badDomXml.replace(constants.nsNamespace2, '') |
117
|
|
|
logging.debug( "Bad Dom Request: %s " % badDomXml ) |
118
|
|
|
##print ("Bad Dom De-serialized: %s \n" %badDomXml) |
119
|
|
|
except Exception as ex: |
120
|
|
|
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
121
|
|
|
##print ("Exception while de-serializing bad dom: %s, %s",type(ex), ex.args) |
122
|
|
|
|
123
|
|
|
class test_CustomerProfile(unittest.TestCase): |
124
|
|
|
def testGetCustomerProfile(self): |
125
|
|
|
loggingfilename = utility.helper.getproperty(constants.propertiesloggingfilename) |
126
|
|
|
logginglevel = utility.helper.getproperty(constants.propertiesexecutionlogginglevel) |
127
|
|
|
logging.basicConfig(filename=loggingfilename, level=logginglevel, format=constants.defaultlogformat) |
128
|
|
|
|
129
|
|
|
merchantAuth = apicontractsv1.merchantAuthenticationType() |
130
|
|
|
merchantAuth.name = "unknown" |
131
|
|
|
merchantAuth.transactionKey = "anon" |
132
|
|
|
|
133
|
|
|
getCustomerProfileRequest = apicontractsv1.getCustomerProfileRequest() |
134
|
|
|
getCustomerProfileRequest.merchantAuthentication = merchantAuth |
135
|
|
|
getCustomerProfileRequest.customerProfileId = '36152115' |
136
|
|
|
getCustomerProfileRequest.abc = 'aaaaaaaa' #extra property not in getCustomerProfileRequest object |
137
|
|
|
|
138
|
|
|
logging.debug( "Request: %s " % datetime.datetime.now()) |
139
|
|
|
logging.debug( " : %s " % getCustomerProfileRequest ) |
140
|
|
|
|
141
|
|
|
try: |
142
|
|
|
'''serialzing object to XML ''' |
143
|
|
|
xmlRequest = getCustomerProfileRequest.toxml(encoding=constants.xml_encoding, element_name='getCustomerProfileRequest') |
144
|
|
|
xmlRequest = xmlRequest.replace(constants.nsNamespace1, '') |
145
|
|
|
xmlRequest = xmlRequest.replace(constants.nsNamespace2, '') |
146
|
|
|
logging.debug( "Xml Request: %s" % xmlRequest) |
147
|
|
|
#print( "Xml Request: %s" % xmlRequest) |
148
|
|
|
except Exception as ex: |
149
|
|
|
logging.debug( "Xml Exception: %s" % ex) |
150
|
|
|
|
151
|
|
|
try: |
152
|
|
|
'''deserialize XML to object ''' |
153
|
|
|
deserializedObject = None |
154
|
|
|
deserializedObject = apicontractsv1.CreateFromDocument(xmlRequest) |
155
|
|
|
self.assertIsNotNone(deserializedObject, "Null deserializedObject ") |
156
|
|
|
|
157
|
|
|
if type(getCustomerProfileRequest) == type(deserializedObject): |
158
|
|
|
#print ("objects are equal") |
159
|
|
|
logging.debug( "createtransactionrequest object is equal to deserializedObject") |
160
|
|
|
else: |
161
|
|
|
#print ("some error: objects are NOT equal" ) |
162
|
|
|
logging.debug( "createtransactionrequest object is NOT equal to deserializedObject") |
163
|
|
|
|
164
|
|
|
deseriaziedObjectXmlRequest = deserializedObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject') |
165
|
|
|
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace1, '') |
166
|
|
|
deseriaziedObjectXmlRequest = deseriaziedObjectXmlRequest.replace(constants.nsNamespace2, '') |
167
|
|
|
logging.debug( "Good Dom Request: %s " % deseriaziedObjectXmlRequest ) |
168
|
|
|
#print( "Good Dom Request: %s " % deseriaziedObjectXmlRequest ) |
169
|
|
|
#print("de-serialized successfully. GOOD CASE COMPLETE \n ") |
170
|
|
|
except Exception as ex: |
171
|
|
|
|
172
|
|
|
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
173
|
|
|
|
174
|
|
|
self.assertEquals(type(getCustomerProfileRequest), type(deserializedObject), "deseriaziedObject does not match original object") |
175
|
|
|
|
176
|
|
|
try: |
177
|
|
|
#print("starting with element in mid") |
178
|
|
|
newxml = '<?xml version="1.0" encoding="utf-8"?><getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"><merchantAuthentication><name>unknown</name><transactionKey>anon</transactionKey></merchantAuthentication><kriti>11Jan</kriti><customerProfileId>36152115</customerProfileId></getCustomerProfileRequest>' |
179
|
|
|
|
180
|
|
|
#print ("newxml: %s" %newxml) |
181
|
|
|
DEserializedNEWObject = apicontractsv1.CreateFromDocument(newxml) |
182
|
|
|
self.assertIsNotNone(DEserializedNEWObject, "Null deserializedObject ") |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
DEseriaziedNEWObjectXmlRequest = DEserializedNEWObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject') |
186
|
|
|
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace1, '') |
187
|
|
|
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace2, '') |
188
|
|
|
logging.debug( "Good Dom Request: %s " % DEseriaziedNEWObjectXmlRequest ) |
189
|
|
|
#print( " DEseriaziedNEWObjectXmlRequest Request: %s " % DEseriaziedNEWObjectXmlRequest ) |
190
|
|
|
#print("de-serialized successfully") |
191
|
|
|
#print("FINISHED element in mid \n ") |
192
|
|
|
except Exception as ex: |
193
|
|
|
#print("DEseriaziedNEWObjectXmlRequest is NOT DESerialized") |
194
|
|
|
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
try: |
198
|
|
|
#print("starting with element at last") |
199
|
|
|
newxmlATLAst = '<?xml version="1.0" encoding="utf-8"?><getCustomerProfileRequest xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"><merchantAuthentication><name>unknown</name><transactionKey>anon</transactionKey></merchantAuthentication><customerProfileId>36152115</customerProfileId><gupta>11Jan</gupta></getCustomerProfileRequest>' |
200
|
|
|
#print ("newxmlATLAst: %s" %newxmlATLAst) |
201
|
|
|
DEserializedNEWObject = apicontractsv1.CreateFromDocument(newxmlATLAst) |
202
|
|
|
self.assertIsNotNone(DEserializedNEWObject, "Null deserializedObject ") |
203
|
|
|
DEseriaziedNEWObjectXmlRequest = DEserializedNEWObject.toxml(encoding=constants.xml_encoding, element_name='deserializedObject') |
204
|
|
|
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace1, '') |
205
|
|
|
DEseriaziedNEWObjectXmlRequest = DEseriaziedNEWObjectXmlRequest.replace(constants.nsNamespace2, '') |
206
|
|
|
logging.debug( "Good Dom Request: %s " % DEseriaziedNEWObjectXmlRequest ) |
207
|
|
|
#print( " DEseriaziedNEWATLASTObjectXmlRequest Request: %s " % DEseriaziedNEWObjectXmlRequest ) |
208
|
|
|
#print("de-serialized successfully") |
209
|
|
|
#print("Finished element at last \n " ) |
210
|
|
|
except Exception as ex: |
211
|
|
|
#print("DEseriaziedNEWATLASTObjectXmlRequest is NOT DESerialized") |
212
|
|
|
logging.error( 'Create Document Exception: %s, %s', type(ex), ex.args ) |
213
|
|
|
|
214
|
|
|
if __name__ =='__main__': |
215
|
|
|
unittest.main() |
216
|
|
|
|