Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 1 | import os |
| 2 | import time |
| 3 | import unittest |
| 4 | |
| 5 | import frappe |
| 6 | import requests |
| 7 | |
rohitwaghchaure | fb997d6 | 2018-06-14 09:13:39 +0530 | [diff] [blame] | 8 | from erpnext.erpnext_integrations.connectors.woocommerce_connection import order |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 9 | |
Chillar Anand | 915b343 | 2021-09-02 16:44:59 +0530 | [diff] [blame] | 10 | |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 11 | class TestWoocommerce(unittest.TestCase): |
Nabin Hait | 41b6307 | 2018-09-01 18:37:07 +0530 | [diff] [blame] | 12 | def setUp(self): |
Rushabh Mehta | a2288ff | 2018-08-31 16:12:49 +0530 | [diff] [blame] | 13 | |
| 14 | woo_settings = frappe.get_doc("Woocommerce Settings") |
| 15 | if not woo_settings.secret: |
| 16 | woo_settings.secret = "ec434676aa1de0e502389f515c38f89f653119ab35e9117c7a79e576" |
| 17 | woo_settings.woocommerce_server_url = "https://woocommerce.mntechnique.com/" |
| 18 | woo_settings.api_consumer_key = "ck_fd43ff5756a6abafd95fadb6677100ce95a758a1" |
| 19 | woo_settings.api_consumer_secret = "cs_94360a1ad7bef7fa420a40cf284f7b3e0788454e" |
| 20 | woo_settings.enable_sync = 1 |
Ankush Menat | afe09d4 | 2021-10-29 13:45:07 +0530 | [diff] [blame] | 21 | woo_settings.company = "_Test Company" |
| 22 | woo_settings.tax_account = "Sales Expenses - _TC" |
| 23 | woo_settings.f_n_f_account = "Expenses - _TC" |
karthikeyan5 | 605a0a0 | 2019-04-12 16:44:51 +0530 | [diff] [blame] | 24 | woo_settings.creation_user = "Administrator" |
Rushabh Mehta | a2288ff | 2018-08-31 16:12:49 +0530 | [diff] [blame] | 25 | woo_settings.save(ignore_permissions=True) |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 26 | |
Shivam Mishra | a788ab2 | 2020-05-08 15:13:36 +0530 | [diff] [blame] | 27 | def test_sales_order_for_woocommerce(self): |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 28 | frappe.flags.woocomm_test_order_data = { |
| 29 | "id": 75, |
| 30 | "parent_id": 0, |
| 31 | "number": "74", |
| 32 | "order_key": "wc_order_5aa1281c2dacb", |
| 33 | "created_via": "checkout", |
| 34 | "version": "3.3.3", |
| 35 | "status": "processing", |
| 36 | "currency": "INR", |
| 37 | "date_created": "2018-03-08T12:10:04", |
| 38 | "date_created_gmt": "2018-03-08T12:10:04", |
| 39 | "date_modified": "2018-03-08T12:10:04", |
| 40 | "date_modified_gmt": "2018-03-08T12:10:04", |
| 41 | "discount_total": "0.00", |
| 42 | "discount_tax": "0.00", |
| 43 | "shipping_total": "150.00", |
| 44 | "shipping_tax": "0.00", |
| 45 | "cart_tax": "0.00", |
| 46 | "total": "649.00", |
| 47 | "total_tax": "0.00", |
| 48 | "prices_include_tax": False, |
| 49 | "customer_id": 12, |
| 50 | "customer_ip_address": "103.54.99.5", |
| 51 | "customer_user_agent": "mozilla\\/5.0 (x11; linux x86_64) applewebkit\\/537.36 (khtml, like gecko) chrome\\/64.0.3282.186 safari\\/537.36", |
| 52 | "customer_note": "", |
| 53 | "billing": { |
| 54 | "first_name": "Tony", |
| 55 | "last_name": "Stark", |
| 56 | "company": "_Test Company", |
| 57 | "address_1": "Mumbai", |
| 58 | "address_2": "", |
| 59 | "city": "Dadar", |
| 60 | "state": "MH", |
| 61 | "postcode": "123", |
| 62 | "country": "IN", |
| 63 | "email": "tony@gmail.com", |
| 64 | "phone": "123457890", |
| 65 | }, |
| 66 | "shipping": { |
| 67 | "first_name": "Tony", |
| 68 | "last_name": "Stark", |
| 69 | "company": "", |
| 70 | "address_1": "Mumbai", |
| 71 | "address_2": "", |
| 72 | "city": "Dadar", |
| 73 | "state": "MH", |
| 74 | "postcode": "123", |
| 75 | "country": "IN", |
| 76 | }, |
| 77 | "payment_method": "cod", |
| 78 | "payment_method_title": "Cash on delivery", |
| 79 | "transaction_id": "", |
| 80 | "date_paid": "", |
| 81 | "date_paid_gmt": "", |
| 82 | "date_completed": "", |
| 83 | "date_completed_gmt": "", |
| 84 | "cart_hash": "8e76b020d5790066496f244860c4703f", |
| 85 | "meta_data": [], |
| 86 | "line_items": [ |
| 87 | { |
| 88 | "id": 80, |
| 89 | "name": "Marvel", |
| 90 | "product_id": 56, |
| 91 | "variation_id": 0, |
| 92 | "quantity": 1, |
| 93 | "tax_class": "", |
| 94 | "subtotal": "499.00", |
| 95 | "subtotal_tax": "0.00", |
| 96 | "total": "499.00", |
| 97 | "total_tax": "0.00", |
| 98 | "taxes": [], |
| 99 | "meta_data": [], |
| 100 | "sku": "", |
| 101 | "price": 499, |
| 102 | } |
| 103 | ], |
| 104 | "tax_lines": [], |
| 105 | "shipping_lines": [ |
| 106 | { |
| 107 | "id": 81, |
| 108 | "method_title": "Flat rate", |
| 109 | "method_id": "flat_rate:1", |
| 110 | "total": "150.00", |
| 111 | "total_tax": "0.00", |
| 112 | "taxes": [], |
| 113 | "meta_data": [{"id": 623, "key": "Items", "value": "Marvel × 1"}], |
| 114 | } |
| 115 | ], |
| 116 | "fee_lines": [], |
| 117 | "coupon_lines": [], |
| 118 | "refunds": [], |
| 119 | } |
karthikeyan5 | 2b0a8de | 2019-04-08 11:57:15 +0530 | [diff] [blame] | 120 | order() |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 121 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 122 | self.assertTrue(frappe.get_value("Customer", {"woocommerce_email": "tony@gmail.com"})) |
| 123 | self.assertTrue(frappe.get_value("Item", {"woocommerce_id": 56})) |
| 124 | self.assertTrue(frappe.get_value("Sales Order", {"woocommerce_id": 75})) |
karthikeyan5 | 2b0a8de | 2019-04-08 11:57:15 +0530 | [diff] [blame] | 125 | frappe.flags.woocomm_test_order_data = {} |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 126 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 127 | |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 128 | def emulate_request(): |
| 129 | # Emulate Woocommerce Request |
| 130 | headers = { |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 131 | "X-Wc-Webhook-Event": "created", |
| 132 | "X-Wc-Webhook-Signature": "h1SjzQMPwd68MF5bficeFq20/RkQeRLsb9AVCUz/rLs=", |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 133 | } |
| 134 | # Emulate Request Data |
rohitwaghchaure | fb997d6 | 2018-06-14 09:13:39 +0530 | [diff] [blame] | 135 | data = """{"id":74,"parent_id":0,"number":"74","order_key":"wc_order_5aa1281c2dacb","created_via":"checkout","version":"3.3.3","status":"processing","currency":"INR","date_created":"2018-03-08T12:10:04","date_created_gmt":"2018-03-08T12:10:04","date_modified":"2018-03-08T12:10:04","date_modified_gmt":"2018-03-08T12:10:04","discount_total":"0.00","discount_tax":"0.00","shipping_total":"150.00","shipping_tax":"0.00","cart_tax":"0.00","total":"649.00","total_tax":"0.00","prices_include_tax":false,"customer_id":12,"customer_ip_address":"103.54.99.5","customer_user_agent":"mozilla\\/5.0 (x11; linux x86_64) applewebkit\\/537.36 (khtml, like gecko) chrome\\/64.0.3282.186 safari\\/537.36","customer_note":"","billing":{"first_name":"Tony","last_name":"Stark","company":"Woocommerce","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN","email":"tony@gmail.com","phone":"123457890"},"shipping":{"first_name":"Tony","last_name":"Stark","company":"","address_1":"Mumbai","address_2":"","city":"Dadar","state":"MH","postcode":"123","country":"IN"},"payment_method":"cod","payment_method_title":"Cash on delivery","transaction_id":"","date_paid":null,"date_paid_gmt":null,"date_completed":null,"date_completed_gmt":null,"cart_hash":"8e76b020d5790066496f244860c4703f","meta_data":[],"line_items":[{"id":80,"name":"Marvel","product_id":56,"variation_id":0,"quantity":1,"tax_class":"","subtotal":"499.00","subtotal_tax":"0.00","total":"499.00","total_tax":"0.00","taxes":[],"meta_data":[],"sku":"","price":499}],"tax_lines":[],"shipping_lines":[{"id":81,"method_title":"Flat rate","method_id":"flat_rate:1","total":"150.00","total_tax":"0.00","taxes":[],"meta_data":[{"id":623,"key":"Items","value":"Marvel × 1"}]}],"fee_lines":[],"coupon_lines":[],"refunds":[]}""" |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 136 | |
| 137 | # Build URL |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 138 | port = frappe.get_site_config().webserver_port or "8000" |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 139 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 140 | if os.environ.get("CI"): |
| 141 | host = "localhost" |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 142 | else: |
| 143 | host = frappe.local.site |
| 144 | |
Ankush Menat | 494bd9e | 2022-03-28 18:52:46 +0530 | [diff] [blame] | 145 | url = "http://{site}:{port}/api/method/erpnext.erpnext_integrations.connectors.woocommerce_connection.order".format( |
| 146 | site=host, port=port |
| 147 | ) |
Vinayak Jethe | df83148 | 2018-04-03 11:17:03 +0530 | [diff] [blame] | 148 | |
| 149 | r = requests.post(url=url, headers=headers, data=data) |
| 150 | |
rohitwaghchaure | fb997d6 | 2018-06-14 09:13:39 +0530 | [diff] [blame] | 151 | time.sleep(5) |
karthikeyan5 | 2b0a8de | 2019-04-08 11:57:15 +0530 | [diff] [blame] | 152 | return r |