Ejemplo n.º 1
0
class TestFunctionalOfApi(unittest.TestCase):
    def setUp(self):
        self.context = {}
        self.store = Storage()
        self.backup = {}
        self.fulfill()
        self.key_hash = set()

    def add_key_to_hash_key(self, arguments):
        self.key_hash.add(
            create_key_part(
                arguments["arguments"].get("first_name"),
                arguments["arguments"].get("last_name"),
                (datetime.datetime.strptime(
                    arguments["arguments"].get("birthday"), "%d.%m.%Y")
                 if arguments["arguments"].get("birthday") else None),
                score_prefix,
            ))

    def fulfill(self):
        for i, interests in enumerate(POPULAR_INTERESTS):
            key, value = "%s%d" % (interest_prefix, i), json.dumps(interests)
            self.backup.update({i: value})
            self.store._Storage__setkey(key, value, TIME_OF_STORE)

    def tearDown(self):
        self.store.connection.delete_multi(self.key_hash)
        self.store.connection.delete_multi(
            map(lambda key: interest_prefix + str(key), self.backup.keys()))
        self.store.connection.disconnect_all()

    """Functional test to test correctness of interests method request"""

    @cases([
        (
            {
                'login':
                '******',
                'account':
                'Spiderman',
                'method':
                'clients_interests',
                "arguments": {
                    "client_ids": [0, 1, 2, 3]
                },
                'token':
                '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
            },
            dict(zip([0, 1, 2, 3], POPULAR_INTERESTS[0:4])),
        ),
        (
            {
                'login': '******',
                'account': 'admin',
                'method': 'clients_interests',
                "arguments": {
                    "client_ids": [0, 1, 2]
                },
                'token': get_current_admin_token()
            },
            dict(zip([0, 1, 2], POPULAR_INTERESTS[0:3])),
        ),
    ])
    @mock.patch("api.get_interests", func_test_interests)
    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)

    """Functional test to test correctness of online score method request"""

    @cases([
        (
            {
                'login':
                '******',
                'account':
                'Spiderman',
                'method':
                'online_score',
                'arguments': {
                    'first_name': 'Peter',
                    'last_name': 'Parker'
                },
                'token':
                '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
            },
            .5,
        ),
        (
            {
                'login':
                '******',
                'account':
                'TV actress',
                'method':
                'online_score',
                'arguments': {
                    'first_name': 'Mary',
                    'last_name': 'Jane',
                    'gender': 2,
                    'birthday': '11.10.1965'
                },
                'token':
                'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
            },
            2.,
        ),
        (
            {
                'login':
                '******',
                'account':
                'Houston Rockets',
                'method':
                'online_score',
                'arguments': {
                    'height': 225,
                    'weight': '140',
                    'email': '*****@*****.**',
                    'phone': '77777777777'
                },
                'token':
                'f203215d025be91486e287252ff3895ebecd76f1f280faf7d2fee20610fad104dd60975cceadd4b30e40afeea70c0d66cbe5581edc7802674c07bad21c39bb5e'
            },
            3.,
        ),
        (
            {
                'login': '******',
                'account': 'admin',
                'method': 'online_score',
                'arguments': {},
                'token': get_current_admin_token()
            },
            42,
        )
    ])
    @mock.patch("api.get_score", func_test_scoring)
    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})
Ejemplo n.º 2
0
class TestHandlers(unittest.TestCase):
    """Test class for testing work of handlers"""
    def setUp(self):
        self.context = {}
        self.store = Storage()
        self.backup = {}
        self.fulfill()
        self.key_hash = set()

    def add_key_to_hash_key(self, arguments):
        self.key_hash.add(
            create_key_part(
                arguments["arguments"].get("first_name"),
                arguments["arguments"].get("last_name"),
                (datetime.datetime.strptime(
                    arguments["arguments"].get("birthday"), "%d.%m.%Y")
                 if arguments["arguments"].get("birthday") else None),
                score_prefix,
            ))

    def fulfill(self):
        for i in range(1, 11):
            interests = random.sample(SOME_INTERESTS, 2)
            key, value = "%s%d" % (interest_prefix, i), json.dumps(interests)
            self.backup.update({i: value})
            self.store._Storage__setkey(key, value, TIME_OF_STORE)

    def tearDown(self):
        self.store.connection.delete_multi(self.key_hash)
        self.store.connection.delete_multi(
            map(lambda key: interest_prefix + str(key), self.backup.keys()))
        self.store.connection.disconnect_all()

    """Test handler MethodRequest"""

    @cases([
        {
            "account": "account",
            "login": "******",
            "token": "token",
            "arguments": {},
            "method": "method"
        },
        {
            "login": "******",
            "token": "token",
            "arguments": {},
            "method": "method"
        },
        {
            "login": "",
            "token": "",
            "arguments": {},
            "method": "method"
        },
    ])
    def test_init_method_request(self, arguments):
        MethodRequest([], self.context, arguments)

    @cases([
        (
            # Test that method cannot be null
            {
                "account": "account",
                "login": "******",
                "token": "token",
                "arguments": {},
                "method": ""
            },
            ['method cannot be null']),
        (
            # Test that request should contain login field
            {
                "token": "token",
                "arguments": {},
                "method": "method"
            },
            ["login is required"]),
        (
            # Test that request should contain arguments field and method is not null
            {
                "login": "",
                "token": "",
                "method": ""
            },
            ["arguments is required", "method cannot be null"]),
    ])
    @mock.patch("api.MethodRequest.raise_error_if_there_are_some")
    def test_bad_init_method_request(self, arguments, errors, _):
        method_request = MethodRequest([], self.context, arguments)
        self.assertSetEqual(set(method_request.errors), set(errors))

    """Test handler ClientsInterestsRequest"""

    @cases([
        (
            # Test there are no problem with initialization and
            # right number in context["nclients"]
            {
                "arguments": {
                    "client_ids": [1, 2, 3]
                },
                "method": "method"
            },
            3,
        ),
        (
            {
                "arguments": {
                    "client_ids": list(range(1000)),
                    "date": "09.11.2017"
                }
            },
            1000,
        ),
        (
            {
                "login": "",
                "token": "",
                "arguments": {
                    "client_ids": [10000]
                }
            },
            1,
        )
    ])
    def test_init_clients_interests_request(self, arguments, nclients):
        ClientsInterestsRequest([], self.context, arguments["arguments"])
        self.assertEqual(self.context["nclients"], nclients)

    # Test exception cases (empty list, wrong format)
    @cases([
        {
            "arguments": {
                "client_ids": []
            },
            "method": "method"
        },
        {
            "arguments": {
                "client_ids": 10
            },
            "method": "method"
        },
        {
            "arguments": {
                "data": "11.11.2111"
            },
            "method": "method"
        },
    ])
    def test_exception_init_clients_interests_request(self, arguments):
        with self.assertRaises(TooMuchErrors):
            ClientsInterestsRequest([], self.context, arguments["arguments"])

    @mock.patch("api.get_interests", func_test_interests)
    def test_process_clients_interests_request(self):
        cli_request = ClientsInterestsRequest(
            self.store, self.context, {"client_ids": list(range(1, 11))})
        result, code = cli_request.process()
        self.assertEqual(code, OK)
        for key, value in result.items():
            self.assertEqual(self.backup[key], json.dumps(value))

    @mock.patch("api.get_interests", func_test_interests)
    def test_process_clients_interests_request_no_keys(self):
        cli_request = ClientsInterestsRequest(
            self.store, self.context, {"client_ids": list(range(12, 21))})

        with self.assertRaises(NoSuchElementError):
            result, _ = cli_request.process()

    """Test handler OnlineScoreRequest"""

    @cases([
        (
            # Test there are no problem with initialization and
            # field case is correct
            {
                "arguments": {
                    "first_name": "first_name",
                    "last_name": "last_name"
                }
            },
            ["first_name", "last_name"],
        ),
        (
            {
                "arguments": {
                    "first_name": "first_name",
                    "last_name": "last_name",
                    "phone": "79178761213"
                }
            },
            ["first_name", "last_name", "phone"],
        ),
        ({
            "arguments": {
                "email": "[email protected]",
                "phone": "79178761213"
            }
        }, ["email", "phone"]),
        ({
            "arguments": {
                "gender": 1,
                "birthday": "11.09.1987",
                "email": "[email protected]"
            }
        }, ["gender", "birthday", "email"]),
    ])
    def test_init_online_score_request(self, arguments, has):
        OnlineScoreRequest(self.store, self.context, arguments["arguments"])
        self.assertSetEqual(set(self.context["has"]), set(has))

    @cases([
        (
            # Test there are no sufficient fields to apply score method
            {
                "arguments": {
                    "first_name": "first_name",
                    "phone": "78923429421"
                }
            }, ),
        ({
            "arguments": {
                "last_name": "last_name",
                "phone": "79178761213"
            }
        }, ),
        ({
            "arguments": {
                "email": "[email protected]",
                "gender": 2
            }
        }, ),
        ({
            "arguments": {
                "birthday": "11.09.1987",
                "email": "[email protected]"
            }
        }, ),
    ])
    def test_bad_init_online_score_request(self, arguments):
        with self.assertRaises(TooLessInformationError):
            OnlineScoreRequest(self.store, self.context,
                               arguments["arguments"])

    @cases([
        (
            # Test correctness of score function
            {
                "arguments": {
                    "first_name": "first_name",
                    "last_name": "last_name"
                }
            },
            .5,
        ),
        (
            {
                "arguments": {
                    "first_name": "N",
                    "last_name": "A",
                    "phone": "79178761213"
                }
            },
            2.,
        ),
        (
            {
                "arguments": {
                    "email": "[email protected]",
                    "phone": "79178761213"
                }
            },
            3.,
        ),
        (
            {
                "arguments": {
                    "gender": 1,
                    "birthday": "11.09.1987",
                    "email": "[email protected]"
                }
            },
            3.,
        ),
    ])
    @mock.patch("api.get_score", func_test_scoring)
    def test_calculate_score_request(self, arguments, score):
        self.add_key_to_hash_key(arguments)
        online_request = OnlineScoreRequest(self.store, self.context,
                                            arguments["arguments"])
        self.assertEqual(online_request.process(), ({"score": score}, OK))

    """Test authorization function"""

    @cases([
        # Right cases
        {
            'login':
            '******',
            'account':
            'Spiderman',
            'token':
            '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
        },
        {
            'login':
            '******',
            'account':
            'TV actress',
            'token':
            'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
        },
        {
            'login':
            '******',
            'account':
            'Houston Rockets',
            'token':
            'f203215d025be91486e287252ff3895ebecd76f1f280faf7d2fee20610fad104dd60975cceadd4b30e40afeea70c0d66cbe5581edc7802674c07bad21c39bb5e'
        },
        {
            'login': '******',
            'account': 'admin',
            'token': get_current_admin_token()
        },
    ])
    def test_right_authorization(self, storage):
        auth_request = create_request(storage)["body"]
        self.assertTrue(check_auth(auth_request))

    @cases([
        {
            'login':
            '******',
            'account':
            'Hulk',
            'token':
            '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
        },
        {
            'login':
            '******',
            'account':
            'TV actress',
            'token':
            'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
        },
        {
            'login': '******',
            'account': 'Houston Rockets',
            'token': 'YaoMingFromChinaWhoIsVeryTallButCantBeAuthorizedThisTime'
        },
        {
            'login': '******',
            'account': 'admin',
            'token': 'IDontNeedTokenToAuthenticateIHackIt!!'
        },
    ])
    def test_bad_authorization(self, storage):
        auth_request = create_request(storage)["body"]
        self.assertFalse(check_auth(auth_request))

    """Test method_handler function which routes handlers"""

    @cases([
        {
            'login':
            '******',
            'account':
            'Hulk',
            'token':
            '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
        },
        {
            'login':
            '******',
            'account':
            'TV actress',
            'token':
            'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
        },
        {
            'login': '******',
            'account': 'Houston Rockets',
            'token': 'YaoMingFromChinaWhoIsVeryTallButCantBeAuthorizedThisTime'
        },
        {
            'login': '******',
            'account': 'admin',
            'token': 'IDontNeedTokenToAuthenticateIHackIt!!'
        },
    ])
    @mock.patch("api.MethodRequest", lambda _x, _y, body: body)
    def test_forbidden_access(self, storage):
        request = create_request(storage)
        self.assertEqual(method_handler(request, self.context, []),
                         (None, FORBIDDEN))

    @cases([
        {
            'login':
            '******',
            'account':
            'Spiderman',
            'token':
            '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
        },
        {
            'login':
            '******',
            'account':
            'TV actress',
            'token':
            'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
        },
        {
            'login':
            '******',
            'account':
            'Houston Rockets',
            'token':
            'f203215d025be91486e287252ff3895ebecd76f1f280faf7d2fee20610fad104dd60975cceadd4b30e40afeea70c0d66cbe5581edc7802674c07bad21c39bb5e'
        },
        {
            'login': '******',
            'account': 'admin',
            'token': get_current_admin_token()
        },
    ])
    @mock.patch("api.MethodRequest", lambda _x, _y, body: body)
    def test_no_method_in_request(self, storage):
        request = create_request(storage)
        with self.assertRaises(NoMethodError):
            method_handler(request, self.context, [])

    @cases([
        {
            'login':
            '******',
            'account':
            'Spiderman',
            'method':
            'online_score',
            'token':
            '4de1853f30330c85fb3dc5fc5b1fb2239981e5e0fe1bcfb7137feee75eb9beeef21a63c4652ba576461d7fb60ec9083a7c3cb35345cdf3c798748bd287d975b2'
        },
        {
            'login':
            '******',
            'account':
            'TV actress',
            'method':
            'clients_interests',
            'token':
            'dce3e3f520c9294c4ac7fd6e3434be2ed5de97423350f7c509f6321dc6c8a38fa386b1d8a9cbd2f27c6f9c8164e3e11fd369547d988123f31132a69c6e986de7'
        },
        {
            'login':
            '******',
            'account':
            'Houston Rockets',
            'method':
            'online_score',
            'token':
            'f203215d025be91486e287252ff3895ebecd76f1f280faf7d2fee20610fad104dd60975cceadd4b30e40afeea70c0d66cbe5581edc7802674c07bad21c39bb5e'
        },
        {
            'login': '******',
            'account': 'admin',
            'method': 'clients_interests',
            'token': get_current_admin_token()
        },
    ])
    @mock.patch("api.MethodRequest", lambda _x, _y, body: body)
    def test_no_arguments_in_request(self, storage):
        request = create_request(storage)
        with self.assertRaises(NoArgumentsError):
            method_handler(request, self.context, [])

    """Test partial filled requests, which should cause errors"""

    @cases([
        {
            "body": {}
        },
        {
            "body": {
                "login": "******"
            }
        },
        {
            "body": {
                "login": "******",
                "token": "123"
            }
        },
        {
            "body": {
                "login": "******",
                "token": "123",
                "method": "method"
            }
        },
    ])
    def test_empty_or_partial_filled_request(self, request):
        with self.assertRaises(TooMuchErrors):
            method_handler(request, self.context, self.store)

    """END"""