|
1 | 1 | import datetime |
2 | | -from decimal import Decimal |
3 | | -import warnings |
4 | 2 | import json |
5 | | -import mock |
6 | | -import sift |
7 | | -import unittest |
8 | 3 | import sys |
| 4 | +import unittest |
| 5 | +import warnings |
| 6 | +from decimal import Decimal |
| 7 | + |
| 8 | +import mock |
9 | 9 | import requests.exceptions |
| 10 | + |
| 11 | +import sift |
| 12 | + |
10 | 13 | if sys.version_info[0] < 3: |
11 | 14 | import six.moves.urllib as urllib |
12 | 15 | else: |
@@ -74,6 +77,23 @@ def score_response_json(): |
74 | 77 | }""" |
75 | 78 |
|
76 | 79 |
|
| 80 | +def workflow_statuses_json(): |
| 81 | + return """{ |
| 82 | + "route" : { |
| 83 | + "name" : "my route" |
| 84 | + }, |
| 85 | + "history": [ |
| 86 | + { |
| 87 | + "app": "decision", |
| 88 | + "name": "Order Looks OK", |
| 89 | + "state": "running", |
| 90 | + "config": { |
| 91 | + "decision_id": "order_looks_ok_payment_abuse" |
| 92 | + } |
| 93 | + } |
| 94 | + ] |
| 95 | + }""" |
| 96 | + |
77 | 97 | # A sample response from the /{version}/users/{userId}/score API. |
78 | 98 | USER_SCORE_RESPONSE_JSON = """{ |
79 | 99 | "status": 0, |
@@ -436,6 +456,35 @@ def test_sync_score_ok(self): |
436 | 456 | assert(response.body['score_response']['scores']['content_abuse']['score'] == 0.14) |
437 | 457 | assert(response.body['score_response']['scores']['payment_abuse']['score'] == 0.97) |
438 | 458 |
|
| 459 | + def test_sync_workflow_ok(self): |
| 460 | + event = '$transaction' |
| 461 | + mock_response = mock.Mock() |
| 462 | + mock_response.content = ('{"status": 0, "error_message": "OK", "workflow_statuses": %s}' |
| 463 | + % workflow_statuses_json()) |
| 464 | + mock_response.json.return_value = json.loads(mock_response.content) |
| 465 | + mock_response.status_code = 200 |
| 466 | + mock_response.headers = response_with_data_header() |
| 467 | + with mock.patch.object(self.sift_client.session, 'post') as mock_post: |
| 468 | + mock_post.return_value = mock_response |
| 469 | + response = self.sift_client.track( |
| 470 | + event, |
| 471 | + valid_transaction_properties(), |
| 472 | + return_workflow_status=True, |
| 473 | + return_route_info=True, |
| 474 | + abuse_types=['payment_abuse', 'content_abuse', 'legacy']) |
| 475 | + mock_post.assert_called_with( |
| 476 | + 'https://api.siftscience.com/v205/events', |
| 477 | + data=mock.ANY, |
| 478 | + headers=mock.ANY, |
| 479 | + timeout=mock.ANY, |
| 480 | + params={'return_workflow_status': 'true', 'return_route_info': 'true', |
| 481 | + 'abuse_types': 'payment_abuse,content_abuse,legacy'}) |
| 482 | + self.assertIsInstance(response, sift.client.Response) |
| 483 | + assert(response.is_ok()) |
| 484 | + assert(response.api_status == 0) |
| 485 | + assert(response.api_error_message == "OK") |
| 486 | + assert(response.body['workflow_statuses']['route']['name'] == 'my route') |
| 487 | + |
439 | 488 | def test_get_decisions_fails(self): |
440 | 489 | with self.assertRaises(ValueError): |
441 | 490 | self.sift_client.get_decisions('usr') |
|
0 commit comments