Exemplo n.º 1
0
 def form_valid(self, form):
     """
     When form valid, save it to view
     """
     url = form.cleaned_data['url']
     crop_feed = form.cleaned_data['crop_feed']
     service = WeiboCaptureService(url, auto_login=True)
     self.user_media_path = utils.generate_user_media_image_path(name='capture', prefix='weibo')
     file_path = os.path.join(settings.MEDIA_ROOT, self.user_media_path)
     try:
         if crop_feed:
             service.capture_feed_to_file(file_path)
         else:
             service.capture_to_file(file_path)
     except TimeoutException:
         logger.warning('WeiboCaptureView.form_valid get TimeoutException')
         os.remove(file_path)
         self.user_media_path = settings.DEFAULT_WEIBO_CAPTURE_IMAGE
     except WeiboNotLoginException:
         logger.error('WeiboCaptureView.form_valid get WeiboNotLoginException')
         self.user_media_path = settings.DEFAULT_WEIBO_CAPTURE_IMAGE
     return super(WeiboCaptureView, self).form_valid(form)
class WeiboCaptureServiceTest(TestCase):
    """
    Test for WeiboCaptureService
    """
    @mock.patch('selenium.webdriver.Chrome')
    def setUp(self, *args):
        redis_lock.reset_all(get_redis_connection("default"))
        self.service = WeiboCaptureService('http://weibo.com/')

    @mock.patch.object(WeiboCaptureService, 'auto_login')
    def test_initial_auto_login_called(self, mock_auto_login, *args):
        WeiboCaptureService(None, auto_login=True)
        mock_auto_login.assert_called_once_with()

    def test_auto_login_when_got_cookie(self, *args):
        self.service.browser = mock.Mock(current_url=self.service.login_success_feature_url)
        self.service.get = mock.Mock()
        self.service.auto_login()
        self.service.get.assert_called_once_with(self.service.login_success_feature_url)
        self.assertTrue(self.service.login_success)

    def test_auto_login_when_no_cookie(self, *args):
        self.service.browser = mock.Mock(current_url='abc')
        self.service.get = mock.Mock()
        self.service.do_login = mock.Mock()
        self.service.auto_login()
        self.service.get.assert_called_once_with(self.service.login_success_feature_url)
        self.service.do_login.assert_called_once_with()

    @mock.patch.object(WeiboCaptureService, 'get')
    @mock.patch.object(WeiboCaptureService, 'find_element_visible_and_clickable')
    @mock.patch.object(WeiboCaptureService, 'select_checkbox')
    @mock.patch.object(WeiboCaptureService, 'fill_input')
    @mock.patch.object(WeiboCaptureService, 'find_element',
                       return_value=mock.Mock(is_displayed=mock.Mock(return_value=False)))
    def test_do_login(self, *args):
        self.service.browser = mock.Mock(current_url=settings.SINA_WEIBO_LOGIN_REDIRECT_PAGE)
        self.service.do_login()
        self.assertTrue(self.service.login_success)

    @mock.patch.object(WeiboCaptureService, 'get')
    @mock.patch.object(WeiboCaptureService, 'find_element_visible_and_clickable')
    @mock.patch.object(WeiboCaptureService, 'select_checkbox')
    @mock.patch.object(WeiboCaptureService, 'fill_input')
    @mock.patch.object(WeiboCaptureService, 'find_element',
                       return_value=mock.Mock(is_displayed=mock.Mock(return_value=False)))
    @mock.patch.object(utils, 'generate_user_media_image_path')
    def test_do_login_failed(self, mock_method, *args):
        mock_method.return_value = 'a_path'
        file_path = os.path.join(settings.MEDIA_ROOT, 'a_path')
        self.service.browser = mock.Mock(current_url='abc')
        self.service.do_login()
        self.service.browser.save_screenshot.assert_called_once_with(file_path)
        self.assertFalse(self.service.login_success)

    @mock.patch.object(Image, 'open')
    def test_capture_to_file_when_login_success(self, mock_open, *args):
        file_path = '/tmp/weibo_capture.png'
        self.service.login_success = True
        self.service.get = mock.Mock()
        self.service.browser = mock.Mock()
        self.service.browser.find_element_by_css_selector.side_effect = NoSuchElementException()
        self.service.find_element_visible_and_clickable = mock.Mock()
        self.service.capture_to_file(file_path)
        self.service.get.assert_called_once_with(self.service.url)
        self.service.browser.save_screenshot.assert_called_once_with(file_path)

    def test_capture_to_file_not_login_raise_exception(self, *args):
        self.assertRaises(WeiboNotLoginException, self.service.capture_to_file, 'abc')

    @mock.patch.object(Image, 'open')
    @mock.patch.object(utils, 'crop_image')
    def test_capture_document_info_to_file(self, mock_crop_image, mock_open, *args):
        file_path = '/tmp/weibo_capture.png'
        self.service.login_success = True
        self.service._fetch_url = mock.Mock()
        self.service.find_element_visible_and_clickable = mock.Mock()
        self.service.find_element = mock.Mock(return_value=mock.Mock(
            location={'x':4, 'y':5},
            size={'width':3, 'height':4}
        ))
        self.service.execute_script = mock.Mock()
        self.service.capture_feed_to_file(file_path)
        mock_crop_image.assert_called_once_with(file_path, 1, 2, 9, 10)

    def test_get_relative_url_by_base64_media_path(self, *args):
        tmp_name = '.gitignore'
        base64_media_path = base64.b64encode(tmp_name)
        self.assertEqual('/media/' + tmp_name, self.service.get_media_relative_path_by(base64_media_path))

    def test_get_relative_url_not_exists(self, *args):
        tmp_name = 'notexists_file'
        base64_media_path = base64.b64encode(tmp_name)
        self.assertEqual(static(settings.DEFAULT_WEIBO_CAPTURE_IMAGE),
                         self.service.get_media_relative_path_by(base64_media_path))