def test_ok_interests_request(param_test_ok_interests_request):
    """
    Tests ok clients interests request
    """

    test_context = dict()
    request_arguments, test_store = param_test_ok_interests_request
    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "clients_interests",
        "arguments": request_arguments
    }
    set_valid_auth(request)
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=test_context,
                                        store=test_store)
    assert code == api.OK
    assert len(request_arguments["client_ids"]) == len(response)
    assert all(
        v and isinstance(v, list) and all(isinstance(i, str) for i in v)
        for v in response.values())
    assert test_context.get("nclients") == len(request_arguments["client_ids"])
def test_ok_score_admin_request():
    """
    Tests ok request from admin
    """

    arguments = {"phone": "79175002040", "email": "*****@*****.**"}
    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "online_score",
        "arguments": arguments
    }
    test_context = dict()
    set_valid_auth(request)
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=test_context,
                                        store=KeyValueTestStorage())
    assert code == api.OK
    score = response.get("score")
    assert score == 42
    assert "has" in test_context
    assert len(test_context["has"]) == 2
    assert "phone" in test_context["has"]
    assert "email" in test_context["has"]
Exemple #3
0
 def test_bad_auth(self, request):
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             '',
             ''
         ), ('Forbidden', 403)
     )
 def test_method_handler_bad(self):
     json_dict = self._fixture_load_helper('api_request_bad.json')
     result_bad = api.method_handler(
         json_dict, {'request_id': '8b1aac327f4f4aa59d895f0a67818fee'},
         None)
     self.assertEqual(([
         "email wasn't pass", "first_name wasn't pass",
         "last_name wasn't pass"
     ], 422), result_bad)
Exemple #5
0
 def test_bad_clients_interests(self, request, mocked_check_auth):
     mocked_check_auth.return_value = True
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             {},
             {}
         ),
         ({'client_ids': 'This field must not be an empty.'}, 422)
     )
Exemple #6
0
 def test_bad_meth_online_score(
         self, request, mocked_check_auth):
     mocked_check_auth.return_value = True
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             {},
             {}
         ),
         ('Invalid Request', 422)
     )
def test_bad_auth(param_test_bad_auth):
    """
    Tests response on request with bad authorization token
    """

    _, code = api.method_handler(request={
        "body": param_test_bad_auth,
        "headers": dict()
    },
                                 ctx=dict(),
                                 store=KeyValueTestStorage())
    assert code == api.FORBIDDEN
Exemple #8
0
 def test_clients_interests(self, request, mocked_get_response,
                            mocked_check_auth):
     mocked_get_response.return_value = (self.template_resp, 200)
     mocked_check_auth.return_value = True
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             {},
             {}
         ),
         (self.template_resp, 200)
     )
Exemple #9
0
 def test_online_score(
         self, request, mocked_check_auth, mocked_get_score
 ):
     mocked_check_auth.return_value = True
     mocked_get_score.return_value = 5.0
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             {},
             {}
         ),
         ({'score': 5.0}, 200)
     )
def test_empty_request():
    """
    Tests response on empty body request
    """

    _, code = api.method_handler(request={
        "body": dict(),
        "headers": dict()
    },
                                 ctx=dict(),
                                 store=KeyValueTestStorage())

    assert code == api.INVALID_REQUEST
    def test_method_handler(self, case_data):
        resp, code = api.method_handler({"body": {**case_data['body']} if case_data['body'] else {},
                                         "headers": dict()}, dict(), store=self.store)

        self.assertIsInstance(resp, case_data['instance'])
        self.assertEquals(code, case_data['code'])

        if case_data['details'] == 'OK':
            self.assertTrue('score' in resp.keys())
        elif case_data['details'] == 'Not found':
            self.assertTrue("not found" in resp)
        elif case_data['details'] == 'Forbidden':
            self.assertEquals(resp, "Forbidden")
def test_invalid_method_request(param_test_invalid_method_request):
    """

    :param param_test_invalid_method_request:
    :return:
    """

    set_valid_auth(param_test_invalid_method_request)
    response, code = api.method_handler(request={
        "body": param_test_invalid_method_request,
        "headers": dict()
    },
                                        ctx=dict(),
                                        store=KeyValueTestStorage())
    assert code == api.INVALID_REQUEST
    assert response
def test_invalid_interests_request(param_test_invalid_interests_request):
    """
    Tests invalid clients interests request
    """

    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "clients_interests",
        "arguments": param_test_invalid_interests_request
    }
    set_valid_auth(request)
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=dict(),
                                        store=KeyValueTestStorage())
    assert code == api.INVALID_REQUEST
    assert response
def test_invalid_phone_score_request(param_test_invalid_phone_score_request):
    """
    Tests invalid phone score request
    """
    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "online_score",
        "arguments": param_test_invalid_phone_score_request
    }
    set_valid_auth(request)
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=dict(),
                                        store=KeyValueTestStorage())
    assert code == api.INVALID_REQUEST
    assert "phone" in response
    assert len(response) == 1
Exemple #15
0
 def test_bad_online_score(
         self, request, mocked_check_auth, mocked_get_score
 ):
     mocked_check_auth.return_value = True
     mocked_get_score.return_value = 5.0
     self.assertEqual(
         api.method_handler(
             {"body": request, "headers": ''},
             {},
             {}
         ),
         ({
              'OnlineScoreRequest': {
                  'pair_not_exist': "Invalid set of fields "
                                    "(('phone', 'email'), "
                                    "('first_name', 'last_name'), "
                                    "('gender', 'birthday'))"
              }
           },
          422
          )
     )
def test_invalid_date_interests_request(
        param_test_invalid_date_interests_request):
    """
    Tests clients_interests request with invalid date
    """

    request_arguments, test_store = param_test_invalid_date_interests_request
    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "clients_interests",
        "arguments": request_arguments
    }
    set_valid_auth(request)
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=dict(),
                                        store=test_store)
    assert code == api.INVALID_REQUEST
    assert "date" in response
    assert len(response) == 1
def test_ok_score_request(param_test_ok_score_request):
    """
    Tests ok score request
    """

    request = {
        "account": "horns&hoofs",
        "login": "******",
        "method": "online_score",
        "arguments": param_test_ok_score_request
    }

    set_valid_auth(request)
    test_context = dict()
    response, code = api.method_handler(request={
        "body": request,
        "headers": dict()
    },
                                        ctx=test_context,
                                        store=KeyValueTestStorage())
    assert code == api.OK
    score = response.get("score")
    assert isinstance(score, (int, float)) and score >= 0
    assert sorted(test_context["has"]) == sorted(param_test_ok_score_request)
Exemple #18
0
def get_response(request, headers, context, store):
    return api.method_handler({
        "body": request,
        "headers": headers
    }, context, store)
 def get_response(self, request):
     return api.method_handler({"body": request, "headers": self.headers}, self.context, self.store)
Exemple #20
0
 def get_response(self, request, store=None):
     self.context = {}
     return api.method_handler({
         "body": request,
         "headers": self.headers
     }, self.context, store)
Exemple #21
0
 def get_response(self, request):
     data = {"body": request, "headers": self.headers}
     return api.method_handler(data, self.context, self.store)
Exemple #22
0
 def get_response(self, request):
     return api.method_handler({'body': request, 'headers': self.headers}, self.context,
                               self.store)
Exemple #23
0
 def test_score_handler_is_correct(self, storage, score):
     request = {"body": storage}
     self.add_key_to_hash_key(request["body"])
     response, code = method_handler(request, self.context, self.store)
     self.assertEqual(code, OK)
     self.assertEqual(response, {"score": score})
Exemple #24
0
 def test_interest_handler_is_correct(self, storage, answers):
     request = {"body": storage}
     response, code = method_handler(request, self.context, self.store)
     self.assertEqual(code, OK)
     for key, value in response.items():
         self.assertEqual(answers[key], value)
Exemple #25
0
 def test_empty_or_partial_filled_request(self, request):
     with self.assertRaises(TooMuchErrors):
         method_handler(request, self.context, self.store)
Exemple #26
0
 def test_score(self, request_data):
     response, code = api.method_handler(
         {
             "body": request_data,
             "headers": self.headers
         }, {}, None)
Exemple #27
0
 def test_forbidden_access(self, storage):
     request = create_request(storage)
     self.assertEqual(method_handler(request, self.context, []),
                      (None, FORBIDDEN))
Exemple #28
0
 def test_no_arguments_in_request(self, storage):
     request = create_request(storage)
     with self.assertRaises(NoArgumentsError):
         method_handler(request, self.context, [])
Exemple #29
0
 def get_response(self, request):
     return api.method_handler({
         "body": request,
         "headers": self.headers
     }, self.context, self.settings)
 def test_method_handler_good(self):
     json_dict = self._fixture_load_helper('api_request_good.json')
     result_good = api.method_handler(
         json_dict, {'request_id': '8b1aac327f4f4aa59d895f0a67818fee'},
         None)
     self.assertEqual(({'score': 5.0}, 200), result_good)