async def check_underscore_upload(self, params, data, access_token, expected_fields, picture_filename=None): """Check that `Mixcloud._upload` works correctly using given arguments. """ self.mixcloud.access_token = access_token response = self.configure_session_method(self.mock_session.post) mock_native_result = self.mixcloud._native_result = Mock() mock_native_result.return_value = self.coroutine() url = urljoin(self.mixcloud._api_root, f'upload/?access_token={access_token}') yarl_url = yarl.URL(url) result = await self.mixcloud._upload(params, data, yarl_url) self.assertEqual(self.mock_session.post.call_count, 1) (call_url, ), kwargs = self.mock_session.post.call_args kwargs = kwargs.copy() call_data = kwargs.pop('data') self.assertFalse(kwargs) self.assertEqual(call_url, yarl_url) if picture_filename is not None: expected_fields.insert(1, (MultiDict({ 'name': 'picture', 'filename': picture_filename }), {}, data._fields[1][-1])) self.assertIsInstance(call_data, aiohttp.FormData) self.assertEqual(call_data._fields, expected_fields) mock_native_result.assert_called_once_with(response) self.assert_access_dict_equal(result, self.sample_dict)
async def test_oembed(self): """`Mixcloud._oembed` must go through `_session.get` of the proper URL and call `_proper_result`. """ xml = '<?xml version="1.0" encoding="utf-8"?><oembed>foo</oembed>' async def coroutine(): """Return sample XML.""" return xml mock_proper_result = self.mixcloud._proper_result = Mock() mock_proper_result.return_value = coroutine() result = await self.mixcloud.oembed('someuser/somemix', height=120, format='xml') url = urljoin(self.mixcloud._mixcloud_root, 'someuser/somemix') self.mock_session.get.assert_called_once_with( self.mixcloud._oembed_root, params={ 'url': url, 'height': 120, 'format': 'xml' }) mock_proper_result.assert_called_once_with(self.response_get) self.assertEqual(result, xml)
async def test_do_action(self): """`Mixcloud._do_action` must, under normal circumstances, make the specified HTTP method request and return an `AccessDict` of received data. """ methods = ['post', 'delete'] for method_name in methods: async def coroutine(): """Return sample `AccessDict`.""" return AccessDict(self.sample_dict, mixcloud=self.mixcloud) method = getattr(self.mock_session, method_name) response = self.configure_session_method(method) self.mixcloud.access_token = '6he8' mock_native_result = self.mixcloud._native_result = Mock() mock_native_result.return_value = coroutine() result = await self.mixcloud._do_action('nick/mymix', 'favorite', method_name) expected_url = urljoin(self.mixcloud._api_root, 'nick/mymix/favorite/?access_token=6he8') method.assert_called_once_with(yarl.URL(expected_url)) mock_native_result.assert_called_once_with(response) self.assert_access_dict_equal(result, self.sample_dict)
async def test_get_params(self): """`Mixcloud.get` must correctly handle GET parameters.""" @self.configure_get_json async def coroutine(): """Dummy coroutine function.""" await self.mixcloud.get('some/resource', foo='bar', height=3) expected = urljoin(self.mixcloud._api_root, 'some/resource?foo=bar&height=3&metadata=1') self.mock_session.get.assert_called_once_with(yarl.URL(expected))
async def test_embed(self): """`Mixcloud._embed` must go through `_session.get` of the proper URL and call `_proper_result`. """ mock_proper_result = self.mixcloud._proper_result = Mock() mock_proper_result.return_value = self.coroutine() result = await self.mixcloud._embed('auser/amix', height=60) url = urljoin(self.mixcloud._api_root, 'auser/amix/embed-json') self.mock_session.get.assert_called_once_with(yarl.URL(url), params={'height': 60}) self.assert_access_dict_equal(result, self.sample_dict)
async def test_get_access_token(self): """`Mixcloud.get` must include access token as a GET parameter, if it is not None. """ @self.configure_get_json async def coroutine(): """Dummy coroutine function.""" self.mixcloud.access_token = 'fwp9' await self.mixcloud.get('foo/bar') expected = urljoin(self.mixcloud._api_root, 'foo/bar?metadata=1&access_token=fwp9') self.mock_session.get.assert_called_once_with(yarl.URL(expected))