def test_chat_messages_endpoint_exists(self, a_client, a_client_user): chat_response = self.create_chat(a_client, a_client_user, str(a_client_user.id), "b", "c") chat = json.loads(chat_response.data) response = self.get_chat_messages(a_client, a_client_user, str(chat["id"]), 0, 50) assert_200(response)
def test_get_rule_details_return_all_details(self, a_client, a_rule, a_client_user): response = self.get_rule(a_client, a_client_user, a_rule) assert_200(response) rule = json.loads(response.data) assert rule == { 'id': str(a_rule.id), 'name': a_rule.name, 'conditions': [{ 'variable': a_rule.conditions[0].variable, 'operator': a_rule.conditions[0].operator, 'condition_value': a_rule.conditions[0].condition_value }], 'consequence': { 'consequence_type': a_rule.consequence.consequence_type, 'value': a_rule.consequence.value, 'variable': None }, 'active': a_rule.active, 'redeemable': a_rule.redeemable, 'cost': a_rule.cost }
def test_rule_history_returns_all_rule_versions(self, a_client, a_client_user, a_rule): self.login(a_client, a_client_user.email, a_client_user.password) self.patch(a_client, 'api/v1/rules/{}'.format(str(a_rule.id)), {'name': 'new name'}) response = self.get(a_client, 'api/v1/rules/{}/history'.format(str(a_rule.id))) assert_200(response) rule_history = json.loads(response.data) edited_rule = Rule.objects.get(id=a_rule.id) assert len(rule_history) == 2 assert rule_history['versions'][0] == { 'name': a_rule.name, 'conditions': [{ 'variable': a_rule.conditions[0].variable, 'operator': a_rule.conditions[0].operator, 'condition_value': a_rule.conditions[0].condition_value }], 'consequence': { 'consequence_type': a_rule.consequence.consequence_type, 'value': a_rule.consequence.value, 'variable': None }, 'active': a_rule.active } assert rule_history['rule'] == { 'id': str(edited_rule.id), 'name': edited_rule.name, 'conditions': [{ 'variable': edited_rule.conditions[0].variable, 'operator': edited_rule.conditions[0].operator, 'condition_value': edited_rule.conditions[0].condition_value }], 'consequence': { 'consequence_type': edited_rule.consequence.consequence_type, 'value': edited_rule.consequence.value, 'variable': None, }, 'active': edited_rule.active, 'redeemable': a_rule.redeemable, 'cost': a_rule.cost }
def test_update_user_location(self, a_client, a_client_user, a_location): self.login(a_client, a_client_user.email, a_client_user.password) response = self.patch(a_client, '/api/v1/users/{}'.format(a_client_user.id), {'location': a_location}) assert_200(response) assert User.objects.get(id=a_client_user.id).location == a_location
def test_update_user_balance(self, a_client, a_client_user): self.login(a_client, a_client_user.email, a_client_user.password) response = self.patch(a_client, '/api/v1/users/{}'.format(a_client_user.id), {'balance': 10}) assert_200(response) assert User.objects.get(id=a_client_user.id).balance == 10
def test_get_variables_should_list_all(self, a_client, a_client_user): response = self.get_data('variables', a_client, a_client_user) assert_200(response) variables = json.loads(response.data) assert variables == list(RuleCondition.VARIABLES)
def test_get_operators_should_list_all(self, a_client, a_client_user): response = self.get_data('operators', a_client, a_client_user) assert_200(response) variables = json.loads(response.data) assert variables == list(RuleCondition.OPERATORS)
def test_consequence_types_should_list_all(self, a_client, a_client_user): response = self.get_data('consequence_types', a_client, a_client_user) assert_200(response) variables = json.loads(response.data) assert variables == list(RuleConsequence.CONSEQUENCE_TYPES)
def test_list_redeemable_benefits(self, a_client, a_client_user, a_redeemable_rule): # pylint: disable=unused-argument self.login(a_client, a_client_user.email, a_client_user.password) response = self.get(a_client, 'api/v1/rules/redeemable') assert_200(response) assert json.loads(response.data)
def test_get_me_admin(self, a_client, an_admin_user): self.login(a_client, an_admin_user.email, an_admin_user.password) response = self.get(a_client, 'api/v1/users/me') assert_200(response) user_data = json.loads(response.data) assert user_data["id"] == str(an_admin_user.id) assert user_data["name"] == an_admin_user.name assert user_data["email"] == an_admin_user.email assert user_data["type"] == an_admin_user.type
def test_get_public_user_data_when_customer_user(self, a_client, a_client_user, a_customer_user): self.login(a_client, a_client_user.email, a_client_user.password) response = self.get(a_client, 'api/v1/users/{}'.format(str(a_customer_user.id))) assert_200(response) customer_data = json.loads(response.data) assert a_customer_user.name == customer_data["name"]
def test_update_should_change_order_status(self, a_client, an_order, a_delivery_user, a_client_user): response = self.patch_order(a_client, an_order, a_client_user, { 'status': Order.TAKEN_STATUS, 'delivery': str(a_delivery_user.id) }) assert_200(response) assert order_repository.get_order( an_order.id).status == Order.TAKEN_STATUS
def test_list_cancelled_orders(self, a_client, a_client_user, a_cancelled_order): # pylint: disable=unused-argument self.login(a_client, a_client_user.email, a_client_user.password) response = self.get(a_client, 'api/v1/statistics/cancelled_orders') assert_200(response) orders = json.loads(response.data) assert orders[0]['count'] == 1
def test_redeem_rule(self, a_client, a_client_user, a_redeemable_rule): self.login(a_client, a_client_user.email, a_client_user.password) response = self.post( a_client, 'api/v1/rules/{}/redeem'.format(str(a_redeemable_rule.id))) assert_200(response) assert Rule.objects.get(id=a_redeemable_rule.id, redeemed_by=[a_client_user.id])
def test_get_complete_user_data_when_admin_user(self, a_client, an_admin_user, a_customer_user): self.login(a_client, an_admin_user.email, an_admin_user.password) response = self.get(a_client, 'api/v1/users/{}'.format(str(a_customer_user.id))) assert_200(response) customer_data = json.loads(response.data) assert a_customer_user assert "phone" in customer_data
def test_get_orders_filtered_by_favors(self, a_client, a_favor_order, an_order_factory, a_client_user): an_order_factory() response = self.get_favor_orders(a_client, a_client_user) assert_200(response) orders = json.loads(response.data) assert len(orders) == 1 assert orders[0]['id'] == str(a_favor_order.id)
def test_mark_order_as_completed(self, a_client, a_client_user, an_order, a_delivery_user): self.login(a_client, a_client_user.email, a_client_user.password) self.patch(a_client, 'api/v1/orders/{}'.format(str(an_order.id)), {'delivery': a_delivery_user.id}) response = self.patch(a_client, 'api/v1/orders/{}'.format(str(an_order.id)), {'status': Order.DELIVERED_STATUS}) assert_200(response) assert Order.objects.get( id=an_order.id).status == Order.DELIVERED_STATUS
def test_get_directions(self, mocked_get, a_client, a_client_user, an_order, a_directions_response): # pylint: disable=too-many-arguments mocked_get.return_value = a_directions_response self.login(a_client, a_client_user.email, a_client_user.password) response = self.get( a_client, 'api/v1/orders/{}/directions'.format(str(an_order.id))) assert_200(response) assert json.loads(response.data)
def test_list_rules_returns_all(self, a_client, a_rule, a_client_user): response = self.get_rules(a_client, a_client_user) assert_200(response) rule = json.loads(response.data)[0] assert rule == { 'id': str(a_rule.id), 'name': a_rule.name, 'active': a_rule.active }
def test_get_chat(self, a_client, a_client_user): chat_response = self.create_chat(a_client, a_client_user, str(a_client_user.id), "b", "c") chat = json.loads(chat_response.data) response = self.get_chat(a_client, str(chat["id"]), a_client_user) assert_200(response) assert chat == { 'id': str(chat["id"]), 'uid_1': str(a_client_user.id), 'uid_2': "b", 'id_order': "c", }
def test_patch_field_should_update_it(self, a_client, a_client_user, a_rule): response = self.update_rule( a_client, a_client_user, a_rule, {'conditions': [{ 'variable': RuleCondition.ORDER_DISTANCE }]}) assert_200(response) assert Rule.objects.get( id=a_rule.id ).conditions[0].variable == RuleCondition.ORDER_DISTANCE
def test_orders_placed_returns_placed_orders_for_user( self, a_client, a_client_user, an_order): an_order.owner = a_client_user an_order.save() self.login(a_client, a_client_user.email, a_client_user.password) response = self.get(a_client, 'api/v1/orders/placed') assert_200(response) orders = json.loads(response.data) assert len(orders) == 1 assert orders[0]['id'] == str(an_order.id)
def test_registrations_returns_registrations_by_date( self, a_client, user_factory, a_client_user): user_factory() another_user = user_factory() another_user.created = datetime.now() + timedelta(days=1) another_user.save() self.login(a_client, a_client_user.email, a_client_user.password) response = self.get(a_client, 'api/v1/statistics/registrations') assert_200(response) data = json.loads(response.data) assert len(data) == 2
def test_create_favor_order_cycle(self, a_client, a_client_user_factory, another_customer_user, an_ordered_product): order_gp = 5 a_client_user = a_client_user_factory(order_gp) self.login(a_client, a_client_user.email, a_client_user.password) response = self.post( a_client, 'api/v1/orders/', { 'name': 'new order', 'order_type': Order.FAVOR_TYPE, 'ordered_products': [{ 'quantity': an_ordered_product.quantity, 'product': str(an_ordered_product.product.id) }], 'payment_method': 'GPPM', 'gratitude_points': order_gp }) assert_201(response) order = json.loads(response.data) response = self.patch(a_client, 'api/v1/orders/{}'.format(str(order['id'])), {'delivery': another_customer_user.id}) assert_200(response) assert User.objects.get( id=a_client_user.id).gratitude_points == order_gp response = self.patch(a_client, 'api/v1/orders/{}'.format(str(order['id'])), {'status': Order.DELIVERED_STATUS}) assert_200(response) assert User.objects.get(id=a_client_user.id).gratitude_points == 0 assert User.objects.get( id=another_customer_user.id).gratitude_points == 10 + order_gp
def test_quote_order_returns_order_quotation(self, a_client, a_client_user, an_order): Rule(name='$20 base', conditions=[ RuleCondition(variable=RuleCondition.USER_REPUTATION, operator=RuleCondition.GREATER_THAN_EQUAL, condition_value='0') ], consequence=RuleConsequence( consequence_type=RuleConsequence.VALUE, value='20')).save() self.login(a_client, a_client_user.email, a_client_user.password) response = self.get( a_client, 'api/v1/orders/{}/quotation'.format(str(an_order.id))) assert_200(response) assert json.loads(response.data) == {'price': 20}
def test_create_and_get_rule(self, a_client, a_client_user, a_condition_data, a_consequence_data): create_response = self.create_rule( a_client, a_client_user, { 'conditions': [a_condition_data], 'consequence': a_consequence_data, 'name': 'a rule' }) assert_201(create_response) rule = json.loads(create_response.data) get_response = self.get_rule(a_client, a_client_user, Rule.objects.get(id=rule["id"])) assert_200(get_response) rule_returned = json.loads(get_response.data) assert rule_returned['id'] == rule['id']
def test_orders_placed_between_dates_does_not_return_out_of_range_orders( self, a_client, a_client_user, an_order): today = datetime.today() yesterday = today - timedelta(days=1) tomorrow = today + timedelta(days=1) an_order.owner = a_client_user an_order.created = tomorrow an_order.save() self.login(a_client, a_client_user.email, a_client_user.password) params = {'start_date': yesterday, 'end_date': today} url = 'api/v1/orders/placed?' + urllib.parse.urlencode(params) response = self.get(a_client, url) assert_200(response) orders = json.loads(response.data) assert not orders
def test_get_products_by_place(self, a_client, a_client_user, a_product): response = self.get_products_by_place(a_client, a_client_user, a_product.place.id) assert_200(response) products = json.loads(response.data) assert products[0] == { 'id': str(a_product.id), 'name': a_product.name, 'description': a_product.description, 'image': a_product.image, 'price': a_product.price, 'place': { 'id': str(a_product.place.id), 'name': a_product.place.name, 'image': a_product.place.image, 'coordinates': { 'latitude': a_product.place.coordinates.latitude, 'longitude': a_product.place.coordinates.longitude } } }
def test_delete_should_delete_rule(self, a_client, an_admin_user, a_rule): response = self.delete_rule(a_client, an_admin_user, a_rule) assert_200(response) assert not Rule.objects.count()
def test_get_users_from_admin_user(self, a_client, an_admin_user): self.login(a_client, an_admin_user.email, an_admin_user.password) response = self.get_paging(a_client, 'api/v1/users/', 0, 50) assert an_admin_user.type == "BACK_OFFICE" assert_200(response)