Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Implement search for orders with customers email address in dashboard

Jakub Draganek128c36b10196
diff --git a/saleor/search/documents.py b/saleor/search/documents.py
index 9075ff299c..45f642916d 100644
--- a/saleor/search/documents.py
+++ b/saleor/search/documents.py
@@ -49,7 +49,10 @@ class OrderDocument(DocType):
user = fields.StringField(analyzer=email_analyzer)
def prepare_user(self, instance):
- return instance.user_email
+ if instance.user:
+ return instance.user.email
+ else:
+ return instance.user_email
class Meta:
model = Order
diff --git a/tests/cassettes/test_dashboard_search_orders_by_user.yaml b/tests/cassettes/test_dashboard_search_orders_by_user.yaml
new file mode 100644
index 0000000000..52164c38a1
--- /dev/null
+++ b/tests/cassettes/test_dashboard_search_orders_by_user.yaml
@@ -0,0 +1,49 @@
+interactions:
+- request:
+ body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["name",
+ "description"], "query": "[email protected]"}}}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/storefront/product_document/_search
+ response:
+ body: {string: '{"took":0,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}'}
+ headers:
+ Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
+ expected [excludes] instead" "Wed, 15 Nov 2017 14:45:10 GMT"']
+ content-length: ['122']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+- request:
+ body: '{"_source": {"exclude": ["*"]}, "query": {"multi_match": {"fields": ["user",
+ "status", "discount_name"], "query": "[email protected]"}}}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/orders/order_document/_search
+ response:
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":2.6390574,"hits":[{"_index":"orders","_type":"order_document","_id":"20","_score":2.6390574,"_source":{}}]}}'}
+ headers:
+ Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
+ expected [excludes] instead" "Wed, 15 Nov 2017 14:45:10 GMT"']
+ content-length: ['214']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+- request:
+ body: '{"_source": {"exclude": ["*"]}, "query": {"match": {"user": "[email protected]"}}}'
+ headers:
+ connection: [keep-alive]
+ content-type: [application/json]
+ method: GET
+ uri: http://search:9200/users/user_document/_search
+ response:
+ body: {string: '{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"failed":0},"hits":{"total":1,"max_score":2.6855774,"hits":[{"_index":"users","_type":"user_document","_id":"12","_score":2.6855774,"_source":{}}]}}'}
+ headers:
+ Warning: ['299 Elasticsearch-5.4.3-eed30a8 "Deprecated field [exclude] used,
+ expected [excludes] instead" "Wed, 15 Nov 2017 14:45:11 GMT"']
+ content-length: ['212']
+ content-type: [application/json; charset=UTF-8]
+ status: {code: 200, message: OK}
+version: 1
diff --git a/tests/test_search.py b/tests/test_search.py
index 0ef96dfd67..e3099a404e 100644
--- a/tests/test_search.py
+++ b/tests/test_search.py
@@ -1,5 +1,6 @@
from saleor.product.models import Product
from saleor.userprofile.models import User
+from saleor.order.models import Order
from django.core.urlresolvers import reverse
from elasticsearch_dsl.connections import connections
from decimal import Decimal
@@ -118,10 +119,14 @@ def test_dashboard_search_with_product_result(db, indexed_products,
assert DASHBOARD_PRODUCTS == products
+# data below must be aligned with recorded communication every time
EXISTING_EMAIL = '[email protected]'
NON_EXISTING_EMAIL = '[email protected]'
+USER_WITH_ORDER = '[email protected]'
USERS = {EXISTING_EMAIL: 9,
- NON_EXISTING_EMAIL: 100}
+ NON_EXISTING_EMAIL: 100,
+ USER_WITH_ORDER: 666}
+ORDERS = {USER_WITH_ORDER: 20}
@pytest.fixture
@@ -136,3 +141,22 @@ def test_dashboard_search_user_by_email(db, admin_client, customers):
''' user can be found in dashboard search by email address '''
_, users, _ = execute_dashboard_search(admin_client, EXISTING_EMAIL)
assert {USERS[EXISTING_EMAIL]} == users
+
+
+def orders(db, billing_address):
+ pks = set()
+ for email, pk in ORDERS.items():
+ Order.objects.create(
+ billing_address=billing_address, user_email=email, pk=pk)
+ pks.add(pk)
+ return pks
+
+
[email protected](record_mode='once', match_on=MATCH_SEARCH_REQUEST)
+def test_dashboard_search_orders_by_user(db, admin_client, customers, orders):
+ ''' order can be found in dashboard search by customers email '''
+ _, _, orders = execute_dashboard_search(admin_client, USER_WITH_ORDER)
+ assert 1 == len(orders)
+ assert ORDERS[USER_WITH_ORDER] in orders