blob: 881f286baebb398d3d3c890f4577d373ebc750cc [file] [log] [blame]
Aditya Hasef3c22f32019-01-22 18:22:20 +05301from __future__ import unicode_literals
Chillar Anand915b3432021-09-02 16:44:59 +05302
3import os
4import time
5import unittest
6
7import frappe
8import requests
9
rohitwaghchaurefb997d62018-06-14 09:13:39 +053010from erpnext.erpnext_integrations.connectors.woocommerce_connection import order
Vinayak Jethedf831482018-04-03 11:17:03 +053011
Chillar Anand915b3432021-09-02 16:44:59 +053012
Vinayak Jethedf831482018-04-03 11:17:03 +053013class TestWoocommerce(unittest.TestCase):
Nabin Hait41b63072018-09-01 18:37:07 +053014 def setUp(self):
Rushabh Mehtaa2288ff2018-08-31 16:12:49 +053015 if not frappe.db.exists('Company', 'Woocommerce'):
16 company = frappe.new_doc("Company")
17 company.company_name = "Woocommerce"
18 company.abbr = "W"
19 company.default_currency = "INR"
20 company.save()
21
22 woo_settings = frappe.get_doc("Woocommerce Settings")
23 if not woo_settings.secret:
24 woo_settings.secret = "ec434676aa1de0e502389f515c38f89f653119ab35e9117c7a79e576"
25 woo_settings.woocommerce_server_url = "https://woocommerce.mntechnique.com/"
26 woo_settings.api_consumer_key = "ck_fd43ff5756a6abafd95fadb6677100ce95a758a1"
27 woo_settings.api_consumer_secret = "cs_94360a1ad7bef7fa420a40cf284f7b3e0788454e"
28 woo_settings.enable_sync = 1
Rucha Mahabalb4553182019-11-07 18:20:32 +053029 woo_settings.company = "Woocommerce"
Rushabh Mehtaa2288ff2018-08-31 16:12:49 +053030 woo_settings.tax_account = "Sales Expenses - W"
31 woo_settings.f_n_f_account = "Expenses - W"
karthikeyan5605a0a02019-04-12 16:44:51 +053032 woo_settings.creation_user = "Administrator"
Rushabh Mehtaa2288ff2018-08-31 16:12:49 +053033 woo_settings.save(ignore_permissions=True)
Vinayak Jethedf831482018-04-03 11:17:03 +053034
Shivam Mishraa788ab22020-05-08 15:13:36 +053035 def test_sales_order_for_woocommerce(self):
karthikeyan52b0a8de2019-04-08 11:57:15 +053036 frappe.flags.woocomm_test_order_data = {"id":75,"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":"","date_paid_gmt":"","date_completed":"","date_completed_gmt":"","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":[]}
37 order()
Vinayak Jethedf831482018-04-03 11:17:03 +053038
Vinayak Jethedf831482018-04-03 11:17:03 +053039 self.assertTrue(frappe.get_value("Customer",{"woocommerce_email":"tony@gmail.com"}))
40 self.assertTrue(frappe.get_value("Item",{"woocommerce_id": 56}))
rohitwaghchaurefb997d62018-06-14 09:13:39 +053041 self.assertTrue(frappe.get_value("Sales Order",{"woocommerce_id":75}))
karthikeyan52b0a8de2019-04-08 11:57:15 +053042 frappe.flags.woocomm_test_order_data = {}
Vinayak Jethedf831482018-04-03 11:17:03 +053043
44def emulate_request():
45 # Emulate Woocommerce Request
46 headers = {
47 "X-Wc-Webhook-Event":"created",
rohitwaghchaurefb997d62018-06-14 09:13:39 +053048 "X-Wc-Webhook-Signature":"h1SjzQMPwd68MF5bficeFq20/RkQeRLsb9AVCUz/rLs="
Vinayak Jethedf831482018-04-03 11:17:03 +053049 }
50 # Emulate Request Data
rohitwaghchaurefb997d62018-06-14 09:13:39 +053051 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 Jethedf831482018-04-03 11:17:03 +053052
53 # Build URL
54 port = frappe.get_site_config().webserver_port or '8000'
55
56 if os.environ.get('CI'):
57 host = 'localhost'
58 else:
59 host = frappe.local.site
60
61 url = "http://{site}:{port}/api/method/erpnext.erpnext_integrations.connectors.woocommerce_connection.order".format(site=host, port=port)
62
63 r = requests.post(url=url, headers=headers, data=data)
64
rohitwaghchaurefb997d62018-06-14 09:13:39 +053065 time.sleep(5)
karthikeyan52b0a8de2019-04-08 11:57:15 +053066 return r