Best Python code snippet using green
complete_disputed.py
Source:complete_disputed.py
1import requests2import json3import time4from collections import OrderedDict5from test_framework.test_framework import OpenBazaarTestFramework, TestFailure6class CompleteDisputedTest(OpenBazaarTestFramework):7 def __init__(self):8 super().__init__()9 self.num_nodes = 310 def run_test(self):11 alice = self.nodes[0]12 bob = self.nodes[1]13 charlie = self.nodes[2]14 # generate some coins and send them to bob15 generated_coins = 1016 time.sleep(4)17 api_url = bob["gateway_url"] + "wallet/address"18 r = requests.get(api_url)19 if r.status_code == 200:20 resp = json.loads(r.text)21 address = resp["address"]22 elif r.status_code == 404:23 raise TestFailure("CompleteDisputedTest - FAIL: Address endpoint not found")24 else:25 raise TestFailure("CompleteDisputedTest - FAIL: Unknown response")26 self.send_bitcoin_cmd("sendtoaddress", address, generated_coins)27 time.sleep(20)28 # create a profile for charlie29 pro = {"name": "Charlie"}30 api_url = charlie["gateway_url"] + "ob/profile"31 r = requests.post(api_url, data=json.dumps(pro, indent=4))32 if r.status_code == 404:33 raise TestFailure("CompleteDisputedTest - FAIL: Profile post endpoint not found")34 elif r.status_code != 200:35 resp = json.loads(r.text)36 raise TestFailure("CompleteDisputedTest - FAIL: Profile POST failed. Reason: %s", resp["reason"])37 time.sleep(4)38 # make charlie a moderator39 with open('testdata/moderation.json') as listing_file:40 moderation_json = json.load(listing_file, object_pairs_hook=OrderedDict)41 api_url = charlie["gateway_url"] + "ob/moderator"42 r = requests.put(api_url, data=json.dumps(moderation_json, indent=4))43 if r.status_code == 404:44 raise TestFailure("CompleteDisputedTest - FAIL: Moderator post endpoint not found")45 elif r.status_code != 200:46 resp = json.loads(r.text)47 raise TestFailure("CompleteDisputedTest - FAIL: Moderator POST failed. Reason: %s", resp["reason"])48 moderatorId = charlie["peerId"]49 time.sleep(4)50 # post profile for alice51 with open('testdata/profile.json') as profile_file:52 profile_json = json.load(profile_file, object_pairs_hook=OrderedDict)53 api_url = alice["gateway_url"] + "ob/profile"54 requests.post(api_url, data=json.dumps(profile_json, indent=4))55 # post listing to alice56 with open('testdata/listing.json') as listing_file:57 listing_json = json.load(listing_file, object_pairs_hook=OrderedDict)58 listing_json["moderators"] = [moderatorId]59 api_url = alice["gateway_url"] + "ob/listing"60 r = requests.post(api_url, data=json.dumps(listing_json, indent=4))61 if r.status_code == 404:62 raise TestFailure("CompleteDisputedTest - FAIL: Listing post endpoint not found")63 elif r.status_code != 200:64 resp = json.loads(r.text)65 raise TestFailure("CompleteDisputedTest - FAIL: Listing POST failed. Reason: %s", resp["reason"])66 resp = json.loads(r.text)67 slug = resp["slug"]68 time.sleep(4)69 # get listing hash70 api_url = alice["gateway_url"] + "ipns/" + alice["peerId"] + "/listings.json"71 r = requests.get(api_url)72 if r.status_code != 200:73 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't get listing index")74 resp = json.loads(r.text)75 listingId = resp[0]["hash"]76 # bob send order77 with open('testdata/order_direct.json') as order_file:78 order_json = json.load(order_file, object_pairs_hook=OrderedDict)79 order_json["items"][0]["listingHash"] = listingId80 order_json["moderator"] = moderatorId81 api_url = bob["gateway_url"] + "ob/purchase"82 r = requests.post(api_url, data=json.dumps(order_json, indent=4))83 if r.status_code == 404:84 raise TestFailure("CompleteDisputedTest - FAIL: Purchase post endpoint not found")85 elif r.status_code != 200:86 resp = json.loads(r.text)87 self.print_logs(alice, "ob.log")88 raise TestFailure("CompleteDisputedTest - FAIL: Purchase POST failed. Reason: %s", resp["reason"])89 resp = json.loads(r.text)90 orderId = resp["orderId"]91 payment_address = resp["paymentAddress"]92 payment_amount = resp["amount"]93 # check the purchase saved correctly94 api_url = bob["gateway_url"] + "ob/order/" + orderId95 r = requests.get(api_url)96 if r.status_code != 200:97 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Bob")98 resp = json.loads(r.text)99 if resp["state"] != "AWAITING_PAYMENT":100 raise TestFailure("CompleteDisputedTest - FAIL: Bob purchase saved in incorrect state")101 if resp["funded"] == True:102 raise TestFailure("CompleteDisputedTest - FAIL: Bob incorrectly saved as funded")103 # check the sale saved correctly104 api_url = alice["gateway_url"] + "ob/order/" + orderId105 r = requests.get(api_url)106 if r.status_code != 200:107 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Alice")108 resp = json.loads(r.text)109 if resp["state"] != "AWAITING_PAYMENT":110 raise TestFailure("CompleteDisputedTest - FAIL: Alice purchase saved in incorrect state")111 if resp["funded"] == True:112 raise TestFailure("CompleteDisputedTest - FAIL: Alice incorrectly saved as funded")113 # fund order114 spend = {115 "address": payment_address,116 "amount": payment_amount,117 "feeLevel": "NORMAL"118 }119 api_url = bob["gateway_url"] + "wallet/spend"120 r = requests.post(api_url, data=json.dumps(spend, indent=4))121 if r.status_code == 404:122 raise TestFailure("CompleteDisputedTest - FAIL: Spend post endpoint not found")123 elif r.status_code != 200:124 resp = json.loads(r.text)125 raise TestFailure("CompleteDisputedTest - FAIL: Spend POST failed. Reason: %s", resp["reason"])126 time.sleep(20)127 # check bob detected payment128 api_url = bob["gateway_url"] + "ob/order/" + orderId129 r = requests.get(api_url)130 if r.status_code != 200:131 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Bob")132 resp = json.loads(r.text)133 if resp["state"] != "AWAITING_FULFILLMENT":134 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to detect his payment")135 if resp["funded"] == False:136 raise TestFailure("CompleteDisputedTest - FAIL: Bob incorrectly saved as unfunded")137 # check alice detected payment138 api_url = alice["gateway_url"] + "ob/order/" + orderId139 r = requests.get(api_url)140 if r.status_code != 200:141 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Alice")142 resp = json.loads(r.text)143 if resp["state"] != "AWAITING_FULFILLMENT":144 raise TestFailure("CompleteDisputedTest - FAIL: Alice failed to detect payment")145 if resp["funded"] == False:146 raise TestFailure("CompleteDisputedTest - FAIL: Alice incorrectly saved as unfunded")147 148 # Bob open dispute149 dispute = {150 "orderId": orderId,151 "claim": "Bastard ripped me off"152 }153 api_url = bob["gateway_url"] + "ob/opendispute/"154 r = requests.post(api_url, data=json.dumps(dispute, indent=4))155 if r.status_code == 404:156 raise TestFailure("CompleteDisputedTest - FAIL: OpenDispute post endpoint not found")157 elif r.status_code != 200:158 resp = json.loads(r.text)159 raise TestFailure("CompleteDisputedTest - FAIL: OpenDispute POST failed. Reason: %s", resp["reason"])160 time.sleep(4)161 # Bob check dispute opened correctly162 api_url = bob["gateway_url"] + "ob/order/" + orderId163 r = requests.get(api_url)164 if r.status_code != 200:165 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Bob")166 resp = json.loads(r.text)167 if resp["state"] != "DISPUTED":168 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to detect his dispute")169 # Alice check dispute opened correctly170 api_url = alice["gateway_url"] + "ob/order/" + orderId171 r = requests.get(api_url)172 if r.status_code != 200:173 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Alice")174 resp = json.loads(r.text)175 if resp["state"] != "DISPUTED":176 raise TestFailure("CompleteDisputedTest - FAIL: Alice failed to detect the dispute")177 # Charlie check dispute opened correctly178 api_url = charlie["gateway_url"] + "ob/case/" + orderId179 r = requests.get(api_url)180 if r.status_code != 200:181 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load case from Clarlie")182 resp = json.loads(r.text, object_pairs_hook=OrderedDict)183 if resp["state"] != "DISPUTED":184 raise TestFailure("CompleteDisputedTest - FAIL: Charlie failed to detect the dispute")185 # Charlie close dispute186 dispute_resolution = {187 "OrderID": orderId,188 "Resolution": "I'm siding with Bob",189 "BuyerPercentage": 100,190 "VendorPercentage": 0191 }192 api_url = charlie["gateway_url"] + "ob/closedispute/"193 r = requests.post(api_url, data=json.dumps(dispute_resolution, indent=4))194 if r.status_code == 404:195 raise TestFailure("CompleteDisputedTest - FAIL: CloseDispute post endpoint not found")196 elif r.status_code != 200:197 resp = json.loads(r.text)198 raise TestFailure("CompleteDisputedTest - FAIL: CloseDispute POST failed. Reason: %s", resp["reason"])199 time.sleep(4)200 # Alice check dispute closed correctly201 api_url = alice["gateway_url"] + "ob/order/" + orderId202 r = requests.get(api_url)203 if r.status_code != 200:204 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Alice")205 resp = json.loads(r.text)206 if resp["state"] != "DECIDED":207 self.print_logs(alice, "ob.log")208 raise TestFailure("CompleteDisputedTest - FAIL: Alice failed to detect the dispute resolution")209 # Bob check dispute closed correctly210 api_url = bob["gateway_url"] + "ob/order/" + orderId211 r = requests.get(api_url)212 if r.status_code != 200:213 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Bob")214 resp = json.loads(r.text, object_pairs_hook=OrderedDict)215 if resp["state"] != "DECIDED":216 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to detect the dispute resolution")217 # Charlie check dispute closed correctly218 api_url = charlie["gateway_url"] + "ob/case/" + orderId219 r = requests.get(api_url)220 if r.status_code != 200:221 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load case from Charlie")222 resp = json.loads(r.text, object_pairs_hook=OrderedDict)223 if resp["state"] != "RESOLVED":224 raise TestFailure("CompleteDisputedTest - FAIL: Charlie failed to detect the dispute resolution")225 # Bob relase funds226 release = {227 "OrderID": orderId,228 }229 api_url = bob["gateway_url"] + "ob/releasefunds/"230 r = requests.post(api_url, data=json.dumps(release, indent=4))231 if r.status_code == 404:232 raise TestFailure("CompleteDisputedTest - FAIL: ReleaseFunds post endpoint not found")233 elif r.status_code != 200:234 resp = json.loads(r.text)235 raise TestFailure("CompleteDisputedTest - FAIL: ReleaseFunds POST failed. Reason: %s", resp["reason"])236 time.sleep(20)237 self.send_bitcoin_cmd("generate", 1)238 time.sleep(2)239 # Check bob received payout240 api_url = bob["gateway_url"] + "wallet/balance"241 r = requests.get(api_url)242 if r.status_code == 200:243 resp = json.loads(r.text)244 confirmed = int(resp["confirmed"])245 #unconfirmed = int(resp["unconfirmed"])246 if confirmed <= (generated_coins*100000000) - payment_amount:247 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to detect dispute payout")248 elif r.status_code == 404:249 raise TestFailure("CompleteDisputedTest - FAIL: Receive coins endpoint not found")250 else:251 raise TestFailure("CompleteDisputedTest - FAIL: Unknown response")252 # Bob check payout transaction recorded253 api_url = bob["gateway_url"] + "ob/order/" + orderId254 r = requests.get(api_url)255 if r.status_code != 200:256 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Bob")257 resp = json.loads(r.text, object_pairs_hook=OrderedDict)258 if len(resp["paymentAddressTransactions"]) != 2:259 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to record payout transaction")260 if resp["state"] != "RESOLVED":261 raise TestFailure("CompleteDisputedTest - FAIL: Bob failed to set state to RESOLVED")262 # Alice check payout transaction recorded263 api_url = alice["gateway_url"] + "ob/order/" + orderId264 r = requests.get(api_url)265 if r.status_code != 200:266 raise TestFailure("CompleteDisputedTest - FAIL: Couldn't load order from Alice")267 resp = json.loads(r.text, object_pairs_hook=OrderedDict)268 if len(resp["paymentAddressTransactions"]) != 2:269 raise TestFailure("CompleteDisputedTest - FAIL: Alice failed to record payout transaction")270 if resp["state"] != "RESOLVED":271 raise TestFailure("CompleteDisputedTest - FAIL: Alice failed to set state to RESOLVED")272 # bob send order completion273 oc = {274 "orderId": orderId,275 "ratings": [276 {277 "slug": slug,278 "overall": 4,279 "quality": 5,280 "description": 5,281 "customerService": 4,282 "deliverySpeed": 3,283 "Review": "I love it!"284 }285 ]286 }287 api_url = bob["gateway_url"] + "ob/ordercompletion"288 r = requests.post(api_url, data=json.dumps(oc, indent=4))289 if r.status_code == 404:290 raise TestFailure("CompleteDirectOnlineTest - FAIL: Completion post endpoint not found")291 elif r.status_code != 200:292 resp = json.loads(r.text)293 raise TestFailure("CompleteDirectOnlineTest - FAIL: Completion POST failed. Reason: %s", resp["reason"])294 time.sleep(4)295 # check alice received completion296 api_url = alice["gateway_url"] + "ob/order/" + orderId297 r = requests.get(api_url)298 if r.status_code != 200:299 raise TestFailure("CompleteDirectOnlineTest - FAIL: Couldn't load order from Alice")300 resp = json.loads(r.text)301 if resp["state"] != "COMPLETED":302 raise TestFailure("CompleteDirectOnlineTest - FAIL: Alice failed to detect order completion")303 # check bob set completion correctly304 api_url = bob["gateway_url"] + "ob/order/" + orderId305 r = requests.get(api_url)306 if r.status_code != 200:307 raise TestFailure("CompleteDirectOnlineTest - FAIL: Couldn't load order from Bob")308 resp = json.loads(r.text)309 if resp["state"] != "COMPLETED":310 raise TestFailure("CompleteDirectOnlineTest - FAIL: Bob failed to order completion")311 print("CompleteDisputedTest - PASS")312if __name__ == '__main__':313 print("Running CompleteDisputedTest")...
dispute_close_buyer.py
Source:dispute_close_buyer.py
1import requests2import json3import time4from collections import OrderedDict5from test_framework.test_framework import OpenBazaarTestFramework, TestFailure6class DisputeCloseBuyerTest(OpenBazaarTestFramework):7 def __init__(self):8 super().__init__()9 self.num_nodes = 310 def run_test(self):11 alice = self.nodes[0]12 bob = self.nodes[1]13 charlie = self.nodes[2]14 # generate some coins and send them to bob15 generated_coins = 1016 time.sleep(4)17 api_url = bob["gateway_url"] + "wallet/address"18 r = requests.get(api_url)19 if r.status_code == 200:20 resp = json.loads(r.text)21 address = resp["address"]22 elif r.status_code == 404:23 raise TestFailure("DisputeCloseBuyerTest - FAIL: Address endpoint not found")24 else:25 raise TestFailure("DisputeCloseBuyerTest - FAIL: Unknown response")26 self.send_bitcoin_cmd("sendtoaddress", address, generated_coins)27 time.sleep(20)28 # create a profile for charlie29 pro = {"name": "Charlie"}30 api_url = charlie["gateway_url"] + "ob/profile"31 r = requests.post(api_url, data=json.dumps(pro, indent=4))32 if r.status_code == 404:33 raise TestFailure("DisputeCloseBuyerTest - FAIL: Profile post endpoint not found")34 elif r.status_code != 200:35 resp = json.loads(r.text)36 raise TestFailure("DisputeCloseBuyerTest - FAIL: Profile POST failed. Reason: %s", resp["reason"])37 time.sleep(4)38 # make charlie a moderator39 with open('testdata/moderation.json') as listing_file:40 moderation_json = json.load(listing_file, object_pairs_hook=OrderedDict)41 api_url = charlie["gateway_url"] + "ob/moderator"42 r = requests.put(api_url, data=json.dumps(moderation_json, indent=4))43 if r.status_code == 404:44 raise TestFailure("DisputeCloseBuyerTest - FAIL: Moderator post endpoint not found")45 elif r.status_code != 200:46 resp = json.loads(r.text)47 raise TestFailure("DisputeCloseBuyerTest - FAIL: Moderator POST failed. Reason: %s", resp["reason"])48 moderatorId = charlie["peerId"]49 time.sleep(4)50 # post profile for alice51 with open('testdata/profile.json') as profile_file:52 profile_json = json.load(profile_file, object_pairs_hook=OrderedDict)53 api_url = alice["gateway_url"] + "ob/profile"54 requests.post(api_url, data=json.dumps(profile_json, indent=4))55 # post listing to alice56 with open('testdata/listing.json') as listing_file:57 listing_json = json.load(listing_file, object_pairs_hook=OrderedDict)58 listing_json["moderators"] = [moderatorId]59 api_url = alice["gateway_url"] + "ob/listing"60 r = requests.post(api_url, data=json.dumps(listing_json, indent=4))61 if r.status_code == 404:62 raise TestFailure("DisputeCloseBuyerTest - FAIL: Listing post endpoint not found")63 elif r.status_code != 200:64 resp = json.loads(r.text)65 raise TestFailure("DisputeCloseBuyerTest - FAIL: Listing POST failed. Reason: %s", resp["reason"])66 time.sleep(4)67 # get listing hash68 api_url = alice["gateway_url"] + "ipns/" + alice["peerId"] + "/listings.json"69 r = requests.get(api_url)70 if r.status_code != 200:71 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't get listing index")72 resp = json.loads(r.text)73 listingId = resp[0]["hash"]74 # bob send order75 with open('testdata/order_direct.json') as order_file:76 order_json = json.load(order_file, object_pairs_hook=OrderedDict)77 order_json["items"][0]["listingHash"] = listingId78 order_json["moderator"] = moderatorId79 api_url = bob["gateway_url"] + "ob/purchase"80 r = requests.post(api_url, data=json.dumps(order_json, indent=4))81 if r.status_code == 404:82 raise TestFailure("DisputeCloseBuyerTest - FAIL: Purchase post endpoint not found")83 elif r.status_code != 200:84 resp = json.loads(r.text)85 self.print_logs(alice, "ob.log")86 raise TestFailure("DisputeCloseBuyerTest - FAIL: Purchase POST failed. Reason: %s", resp["reason"])87 resp = json.loads(r.text)88 orderId = resp["orderId"]89 payment_address = resp["paymentAddress"]90 payment_amount = resp["amount"]91 # check the purchase saved correctly92 api_url = bob["gateway_url"] + "ob/order/" + orderId93 r = requests.get(api_url)94 if r.status_code != 200:95 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Bob")96 resp = json.loads(r.text)97 if resp["state"] != "AWAITING_PAYMENT":98 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob purchase saved in incorrect state")99 if resp["funded"] == True:100 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob incorrectly saved as funded")101 # check the sale saved correctly102 api_url = alice["gateway_url"] + "ob/order/" + orderId103 r = requests.get(api_url)104 if r.status_code != 200:105 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Alice")106 resp = json.loads(r.text)107 if resp["state"] != "AWAITING_PAYMENT":108 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice purchase saved in incorrect state")109 if resp["funded"] == True:110 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice incorrectly saved as funded")111 # fund order112 spend = {113 "address": payment_address,114 "amount": payment_amount,115 "feeLevel": "NORMAL"116 }117 api_url = bob["gateway_url"] + "wallet/spend"118 r = requests.post(api_url, data=json.dumps(spend, indent=4))119 if r.status_code == 404:120 raise TestFailure("DisputeCloseBuyerTest - FAIL: Spend post endpoint not found")121 elif r.status_code != 200:122 resp = json.loads(r.text)123 raise TestFailure("DisputeCloseBuyerTest - FAIL: Spend POST failed. Reason: %s", resp["reason"])124 time.sleep(20)125 # check bob detected payment126 api_url = bob["gateway_url"] + "ob/order/" + orderId127 r = requests.get(api_url)128 if r.status_code != 200:129 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Bob")130 resp = json.loads(r.text)131 if resp["state"] != "AWAITING_FULFILLMENT":132 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to detect his payment")133 if resp["funded"] == False:134 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob incorrectly saved as unfunded")135 # check alice detected payment136 api_url = alice["gateway_url"] + "ob/order/" + orderId137 r = requests.get(api_url)138 if r.status_code != 200:139 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Alice")140 resp = json.loads(r.text)141 if resp["state"] != "AWAITING_FULFILLMENT":142 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice failed to detect payment")143 if resp["funded"] == False:144 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice incorrectly saved as unfunded")145 146 # Bob open dispute147 dispute = {148 "orderId": orderId,149 "claim": "Bastard ripped me off"150 }151 api_url = bob["gateway_url"] + "ob/opendispute/"152 r = requests.post(api_url, data=json.dumps(dispute, indent=4))153 if r.status_code == 404:154 raise TestFailure("DisputeCloseBuyerTest - FAIL: OpenDispute post endpoint not found")155 elif r.status_code != 200:156 resp = json.loads(r.text)157 raise TestFailure("DisputeCloseBuyerTest - FAIL: OpenDispute POST failed. Reason: %s", resp["reason"])158 time.sleep(4)159 # Bob check dispute opened correctly160 api_url = bob["gateway_url"] + "ob/order/" + orderId161 r = requests.get(api_url)162 if r.status_code != 200:163 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Bob")164 resp = json.loads(r.text)165 if resp["state"] != "DISPUTED":166 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to detect his dispute")167 # Alice check dispute opened correctly168 api_url = alice["gateway_url"] + "ob/order/" + orderId169 r = requests.get(api_url)170 if r.status_code != 200:171 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Alice")172 resp = json.loads(r.text)173 if resp["state"] != "DISPUTED":174 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice failed to detect the dispute")175 # Charlie check dispute opened correctly176 api_url = charlie["gateway_url"] + "ob/case/" + orderId177 r = requests.get(api_url)178 if r.status_code != 200:179 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load case from Clarlie")180 resp = json.loads(r.text, object_pairs_hook=OrderedDict)181 if resp["state"] != "DISPUTED":182 raise TestFailure("DisputeCloseBuyerTest - FAIL: Charlie failed to detect the dispute")183 # Charlie close dispute184 dispute_resolution = {185 "OrderID": orderId,186 "Resolution": "I'm siding with Bob",187 "BuyerPercentage": 100,188 "VendorPercentage": 0189 }190 api_url = charlie["gateway_url"] + "ob/closedispute/"191 r = requests.post(api_url, data=json.dumps(dispute_resolution, indent=4))192 if r.status_code == 404:193 raise TestFailure("DisputeCloseBuyerTest - FAIL: CloseDispute post endpoint not found")194 elif r.status_code != 200:195 resp = json.loads(r.text)196 raise TestFailure("DisputeCloseBuyerTest - FAIL: CloseDispute POST failed. Reason: %s", resp["reason"])197 time.sleep(4)198 # Alice check dispute closed correctly199 api_url = alice["gateway_url"] + "ob/order/" + orderId200 r = requests.get(api_url)201 if r.status_code != 200:202 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Alice")203 resp = json.loads(r.text)204 if resp["state"] != "DECIDED":205 self.print_logs(alice, "ob.log")206 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice failed to detect the dispute resolution")207 # Bob check dispute closed correctly208 api_url = bob["gateway_url"] + "ob/order/" + orderId209 r = requests.get(api_url)210 if r.status_code != 200:211 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Bob")212 resp = json.loads(r.text, object_pairs_hook=OrderedDict)213 if resp["state"] != "DECIDED":214 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to detect the dispute resolution")215 # Charlie check dispute closed correctly216 api_url = charlie["gateway_url"] + "ob/case/" + orderId217 r = requests.get(api_url)218 if r.status_code != 200:219 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load case from Charlie")220 resp = json.loads(r.text, object_pairs_hook=OrderedDict)221 if resp["state"] != "RESOLVED":222 raise TestFailure("DisputeCloseBuyerTest - FAIL: Charlie failed to detect the dispute resolution")223 # Bob relase funds224 release = {225 "OrderID": orderId,226 }227 api_url = bob["gateway_url"] + "ob/releasefunds/"228 r = requests.post(api_url, data=json.dumps(release, indent=4))229 if r.status_code == 404:230 raise TestFailure("DisputeCloseBuyerTest - FAIL: ReleaseFunds post endpoint not found")231 elif r.status_code != 200:232 resp = json.loads(r.text)233 raise TestFailure("DisputeCloseBuyerTest - FAIL: ReleaseFunds POST failed. Reason: %s", resp["reason"])234 time.sleep(20)235 self.send_bitcoin_cmd("generate", 1)236 time.sleep(2)237 # Check bob received payout238 api_url = bob["gateway_url"] + "wallet/balance"239 r = requests.get(api_url)240 if r.status_code == 200:241 resp = json.loads(r.text)242 confirmed = int(resp["confirmed"])243 #unconfirmed = int(resp["unconfirmed"])244 if confirmed <= (generated_coins*100000000) - payment_amount:245 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to detect dispute payout")246 elif r.status_code == 404:247 raise TestFailure("DisputeCloseBuyerTest - FAIL: Receive coins endpoint not found")248 else:249 raise TestFailure("DisputeCloseBuyerTest - FAIL: Unknown response")250 # Bob check payout transaction recorded251 api_url = bob["gateway_url"] + "ob/order/" + orderId252 r = requests.get(api_url)253 if r.status_code != 200:254 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Bob")255 resp = json.loads(r.text, object_pairs_hook=OrderedDict)256 if len(resp["paymentAddressTransactions"]) != 2:257 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to record payout transaction")258 if resp["state"] != "RESOLVED":259 raise TestFailure("DisputeCloseBuyerTest - FAIL: Bob failed to set state to RESOLVED")260 # Alice check payout transaction recorded261 api_url = alice["gateway_url"] + "ob/order/" + orderId262 r = requests.get(api_url)263 if r.status_code != 200:264 raise TestFailure("DisputeCloseBuyerTest - FAIL: Couldn't load order from Alice")265 resp = json.loads(r.text, object_pairs_hook=OrderedDict)266 if len(resp["paymentAddressTransactions"]) != 2:267 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice failed to record payout transaction")268 if resp["state"] != "RESOLVED":269 raise TestFailure("DisputeCloseBuyerTest - FAIL: Alice failed to set state to RESOLVED")270 print("DisputeCloseBuyerTest - PASS")271if __name__ == '__main__':272 print("Running DisputeCloseBuyerTest")...
test_failures.py
Source:test_failures.py
1# Copyright (C) 2010 Google Inc. All rights reserved.2#3# Redistribution and use in source and binary forms, with or without4# modification, are permitted provided that the following conditions are5# met:6#7# * Redistributions of source code must retain the above copyright8# notice, this list of conditions and the following disclaimer.9# * Redistributions in binary form must reproduce the above10# copyright notice, this list of conditions and the following disclaimer11# in the documentation and/or other materials provided with the12# distribution.13# * Neither the name of Google Inc. nor the names of its14# contributors may be used to endorse or promote products derived from15# this software without specific prior written permission.16#17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.28import cPickle29from webkitpy.layout_tests.models import test_expectations30def is_reftest_failure(failure_list):31 input_failure_types = {type(f) for f in failure_list}32 reftest_failure_types = {33 FailureReftestMismatch,34 FailureReftestMismatchDidNotOccur,35 FailureReftestNoImageGenerated,36 FailureReftestNoReferenceImageGenerated37 }38 return bool(input_failure_types & reftest_failure_types)39# FIXME: This is backwards. Each TestFailure subclass should know what40# test_expectation type it corresponds too. Then this method just41# collects them all from the failure list and returns the worst one.42def determine_result_type(failure_list):43 """Takes a set of test_failures and returns which result type best fits44 the list of failures. "Best fits" means we use the worst type of failure.45 Returns:46 one of the test_expectations result types - PASS, FAIL, CRASH, etc.47 """48 if not failure_list or len(failure_list) == 0:49 return test_expectations.PASS50 failure_types = [type(f) for f in failure_list]51 if FailureCrash in failure_types:52 return test_expectations.CRASH53 elif FailureLeak in failure_types:54 return test_expectations.LEAK55 elif FailureTimeout in failure_types:56 return test_expectations.TIMEOUT57 elif FailureEarlyExit in failure_types:58 return test_expectations.SKIP59 elif (FailureMissingResult in failure_types or60 FailureMissingImage in failure_types or61 FailureMissingImageHash in failure_types or62 FailureMissingAudio in failure_types):63 return test_expectations.MISSING64 else:65 is_text_failure = (FailureTextMismatch in failure_types or66 FailureTestHarnessAssertion in failure_types)67 is_image_failure = (FailureImageHashIncorrect in failure_types or68 FailureImageHashMismatch in failure_types or69 is_reftest_failure(failure_list))70 is_audio_failure = (FailureAudioMismatch in failure_types)71 if is_text_failure and is_image_failure:72 return test_expectations.IMAGE_PLUS_TEXT73 elif is_text_failure:74 return test_expectations.TEXT75 elif is_image_failure:76 return test_expectations.IMAGE77 elif is_audio_failure:78 return test_expectations.AUDIO79 else:80 raise ValueError('unclassifiable set of failures: '81 + str(failure_types))82class TestFailure(object):83 """Abstract base class that defines the failure interface."""84 @staticmethod85 def loads(s):86 """Creates a TestFailure object from the specified string."""87 return cPickle.loads(s)88 def message(self):89 """Returns a string describing the failure in more detail."""90 raise NotImplementedError91 def __eq__(self, other):92 return self.__class__.__name__ == other.__class__.__name__93 def __ne__(self, other):94 return self.__class__.__name__ != other.__class__.__name__95 def __hash__(self):96 return hash(self.__class__.__name__)97 def dumps(self):98 """Returns the string/JSON representation of a TestFailure."""99 return cPickle.dumps(self)100 def driver_needs_restart(self):101 """Returns True if we should kill the driver before the next test."""102 return False103class FailureTimeout(TestFailure):104 def __init__(self, is_reftest=False):105 super(FailureTimeout, self).__init__()106 self.is_reftest = is_reftest107 def message(self):108 return 'test timed out'109 def driver_needs_restart(self):110 return True111class FailureCrash(TestFailure):112 def __init__(self, is_reftest=False, process_name='content_shell', pid=None, has_log=False):113 super(FailureCrash, self).__init__()114 self.process_name = process_name115 self.pid = pid116 self.is_reftest = is_reftest117 self.has_log = has_log118 def message(self):119 if self.pid:120 return '%s crashed [pid=%d]' % (self.process_name, self.pid)121 return self.process_name + ' crashed'122 def driver_needs_restart(self):123 return True124class FailureLeak(TestFailure):125 def __init__(self, is_reftest=False, log=''):126 super(FailureLeak, self).__init__()127 self.is_reftest = is_reftest128 self.log = log129 def message(self):130 return 'leak detected: %s' % (self.log)131class FailureMissingResult(TestFailure):132 def message(self):133 return '-expected.txt was missing'134class FailureTestHarnessAssertion(TestFailure):135 def message(self):136 return 'asserts failed'137class FailureTextMismatch(TestFailure):138 def message(self):139 return 'text diff'140class FailureMissingImageHash(TestFailure):141 def message(self):142 return '-expected.png was missing an embedded checksum'143class FailureMissingImage(TestFailure):144 def message(self):145 return '-expected.png was missing'146class FailureImageHashMismatch(TestFailure):147 def message(self):148 return 'image diff'149class FailureImageHashIncorrect(TestFailure):150 def message(self):151 return '-expected.png embedded checksum is incorrect'152class FailureReftestMismatch(TestFailure):153 def __init__(self, reference_filename=None):154 super(FailureReftestMismatch, self).__init__()155 self.reference_filename = reference_filename156 def message(self):157 return 'reference mismatch'158class FailureReftestMismatchDidNotOccur(TestFailure):159 def __init__(self, reference_filename=None):160 super(FailureReftestMismatchDidNotOccur, self).__init__()161 self.reference_filename = reference_filename162 def message(self):163 return "reference mismatch didn't happen"164class FailureReftestNoImageGenerated(TestFailure):165 def __init__(self, reference_filename=None):166 super(FailureReftestNoImageGenerated, self).__init__()167 self.reference_filename = reference_filename168 def message(self):169 return "reference test didn't generate pixel results"170class FailureReftestNoReferenceImageGenerated(TestFailure):171 def __init__(self, reference_filename=None):172 super(FailureReftestNoReferenceImageGenerated, self).__init__()173 self.reference_filename = reference_filename174 def message(self):175 return "-expected.html didn't generate pixel results"176class FailureMissingAudio(TestFailure):177 def message(self):178 return 'expected audio result was missing'179class FailureAudioMismatch(TestFailure):180 def message(self):181 return 'audio mismatch'182class FailureEarlyExit(TestFailure):183 def message(self):184 return 'skipped due to early exit'185# Convenient collection of all failure classes for anything that might186# need to enumerate over them all.187ALL_FAILURE_CLASSES = (FailureTimeout, FailureCrash, FailureMissingResult,188 FailureTestHarnessAssertion,189 FailureTextMismatch, FailureMissingImageHash,190 FailureMissingImage, FailureImageHashMismatch,191 FailureImageHashIncorrect, FailureReftestMismatch,192 FailureReftestMismatchDidNotOccur,193 FailureReftestNoImageGenerated,194 FailureReftestNoReferenceImageGenerated,195 FailureMissingAudio, FailureAudioMismatch,...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!