{"action":"created","issue":{"url":"https://api.github.com/repos/aio-libs/aiohttp/issues/2904","repository_url":"https://api.github.com/repos/aio-libs/aiohttp","labels_url":"https://api.github.com/repos/aio-libs/aiohttp/issues/2904/labels{/name}","comments_url":"https://api.github.com/repos/aio-libs/aiohttp/issues/2904/comments","events_url":"https://api.github.com/repos/aio-libs/aiohttp/issues/2904/events","html_url":"https://github.com/aio-libs/aiohttp/issues/2904","id":310623788,"number":2904,"title":"Patching internal functions in unittesting","user":{"login":"d21d3q","id":490844,"avatar_url":"https://avatars3.githubusercontent.com/u/490844?v=4","gravatar_id":"","url":"https://api.github.com/users/d21d3q","html_url":"https://github.com/d21d3q","followers_url":"https://api.github.com/users/d21d3q/followers","following_url":"https://api.github.com/users/d21d3q/following{/other_user}","gists_url":"https://api.github.com/users/d21d3q/gists{/gist_id}","starred_url":"https://api.github.com/users/d21d3q/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/d21d3q/subscriptions","organizations_url":"https://api.github.com/users/d21d3q/orgs","repos_url":"https://api.github.com/users/d21d3q/repos","events_url":"https://api.github.com/users/d21d3q/events{/privacy}","received_events_url":"https://api.github.com/users/d21d3q/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-04-02T21:51:58Z","updated_at":"2018-04-03T08:03:41Z","closed_at":null,"author_association":"NONE","body":"I am trying to write unit test of endpoint which uses [`aiohttp_security`](https://github.com/aio-libs/aiohttp-security) and mock call to [`authorized_userid`](https://github.com/aio-libs/aiohttp-security/blob/6e0cedaac762fdbd8ee8f3857506c1d2d40837ff/aiohttp_security/api.py#L49) in order to simulate logged in user. \r\n\r\nSo here is what I am doing:\r\n```python\r\nfrom aiohttp.test_utils import AioHTTPTestCase, unittest_run_loop\r\nfrom my_app import make_app\r\nfrom mock import patch\r\n\r\nasync def fake_authorised_userid(request):\r\n    return 1\r\n\r\nclass AppTest(AioHTTPTestCase):\r\n    async def get_application(self):\r\n        return make_app()\r\n\r\n    def setUp(self):\r\n        super().setUp()\r\n        # here I am setting up db\r\n\r\n    @unittest_run_loop\r\n    @patch('aiohttp_security.authorised_userid', side_effect=fake_authorised_userid)\r\n    # @patch('aiohttp_security', return_value=1)\r\n    async def test_foo(self, mock):\r\n        # endpoint is marked with login_required\r\n        # and also makes call to authorised_userid\r\n        request = await self.client.get('/some/endpoint')\r\n        assert mock.called\r\n        assert request.status == 200\r\n```\r\nand patch doesn't work as expected.\r\nI am running tests with `pytest`.\r\nIs that issue related to `aiohttp` or `asyncio` in general or just to `aiohttp_security`?"},"comment":{"url":"https://api.github.com/repos/aio-libs/aiohttp/issues/comments/378163807","html_url":"https://github.com/aio-libs/aiohttp/issues/2904#issuecomment-378163807","issue_url":"https://api.github.com/repos/aio-libs/aiohttp/issues/2904","id":378163807,"user":{"login":"asvetlov","id":356399,"avatar_url":"https://avatars0.githubusercontent.com/u/356399?v=4","gravatar_id":"","url":"https://api.github.com/users/asvetlov","html_url":"https://github.com/asvetlov","followers_url":"https://api.github.com/users/asvetlov/followers","following_url":"https://api.github.com/users/asvetlov/following{/other_user}","gists_url":"https://api.github.com/users/asvetlov/gists{/gist_id}","starred_url":"https://api.github.com/users/asvetlov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/asvetlov/subscriptions","organizations_url":"https://api.github.com/users/asvetlov/orgs","repos_url":"https://api.github.com/users/asvetlov/repos","events_url":"https://api.github.com/users/asvetlov/events{/privacy}","received_events_url":"https://api.github.com/users/asvetlov/received_events","type":"User","site_admin":false},"created_at":"2018-04-03T08:03:41Z","updated_at":"2018-04-03T08:03:41Z","author_association":"OWNER","body":"I have several questions.\r\n1. If you use `pytest` why do you write tests in `unittest` style?\r\n2. Why do you need mocking at all? Does creation a test app with custom test [authorization policy] (http://aiohttp-security.readthedocs.io/en/latest/reference.html#aiohttp_security.AbstractAuthorizationPolicy) and use it everywhere. Mock overusage is a strong smell of bad design decisions."}}
{"actor":{"display_login":"asvetlov"}}