Exemplo n.º 1
0
 def setup(self):
     from imgurup import ZenityImgur
     self.imgur = ZenityImgur()
     self._enter_token_msg = self.imgur._enter_token_msg
     self._auth_url = self.imgur._auth_url
     self._auth_msg = self.imgur._auth_msg
     self._no_album_msg = self.imgur._no_album_msg
     self._albums = [
         {
             'id': '1',
             'title': 'hello',
             'privacy': 'public'
         },
         {
             'id': '2',
             'title': 'hello2',
             'privacy': 'private'
         }
     ]
     self._auth_msg_dialog_args = [
         'zenity',
         '--entry',
         (
             '--text=This is the first time you use this program, '
             'you have to visit this URL in your browser '
             'and copy the PIN code: \n'
         ),
         (
             '--entry-text=https://api.imgur.com/oauth2/authorize?'
             'client_id=55080e3fd8d0644&response_type=pin&state=carlcarl'
         )
     ]
     self._enter_pin_dialog_args = [
         'zenity',
         '--entry',
         '--text=Enter PIN code displayed in the browser: ',
     ]
     self._ask_album_id_dialog_args = [
         'zenity',
         '--list',
         '--text="Choose the album"',
         '--column=No.',
         '--column=Album name',
         '--column=Privacy',
         '1',
         'hello',
         'public',
         '2',
         'hello2',
         'private',
         '3',
         'Do not move to any album',
         'public'
     ]
     self._image_link = 'http://i.imgur.com/xxxxxxx.jpg'
     self._delete_hash = 'xxxxxxxxxxxxxxx'
Exemplo n.º 2
0
class TestZenityImgur:

    def setup(self):
        from imgurup import ZenityImgur
        self.imgur = ZenityImgur()
        self._enter_token_msg = self.imgur._enter_token_msg
        self._auth_url = self.imgur._auth_url
        self._auth_msg = self.imgur._auth_msg
        self._no_album_msg = self.imgur._no_album_msg
        self._albums = [
            {
                'id': '1',
                'title': 'hello',
                'privacy': 'public'
            },
            {
                'id': '2',
                'title': 'hello2',
                'privacy': 'private'
            }
        ]
        self._auth_msg_dialog_args = [
            'zenity',
            '--entry',
            (
                '--text=This is the first time you use this program, '
                'you have to visit this URL in your browser '
                'and copy the PIN code: \n'
            ),
            (
                '--entry-text=https://api.imgur.com/oauth2/authorize?'
                'client_id=55080e3fd8d0644&response_type=pin&state=carlcarl'
            )
        ]
        self._enter_pin_dialog_args = [
            'zenity',
            '--entry',
            '--text=Enter PIN code displayed in the browser: ',
        ]
        self._ask_album_id_dialog_args = [
            'zenity',
            '--list',
            '--text="Choose the album"',
            '--column=No.',
            '--column=Album name',
            '--column=Privacy',
            '1',
            'hello',
            'public',
            '2',
            'hello2',
            'private',
            '3',
            'Do not move to any album',
            'public'
        ]
        self._image_link = 'http://i.imgur.com/xxxxxxx.jpg'
        self._delete_hash = 'xxxxxxxxxxxxxxx'

    @pytest.fixture(scope='function')
    def mock_subprocess(self, request):
        m = mock.patch('imgurup.subprocess')
        ret = m.start()
        request.addfinalizer(m.stop)
        return ret

    def test_show_error_and_exit(self, mock_subprocess):
        mock_subprocess.Popen.return_value.returncode = 0
        with pytest.raises(SystemExit):
            self.imgur.show_error_and_exit(1)

    def test_get_error_dialog_args(self):
        result = self.imgur.get_error_dialog_args()
        args = [
            'zenity',
            '--error',
            '--text=Error',
        ]
        assert result == args

    def test_get_auth_msg_dialog_args(self):
        result = self.imgur.get_auth_msg_dialog_args(self._auth_msg, self._auth_url)
        assert result == self._auth_msg_dialog_args

    def test_get_enter_pin_dialog_args(self):
        result = self.imgur.get_enter_pin_dialog_args(self._enter_token_msg)
        assert result == self._enter_pin_dialog_args


    @pytest.fixture(scope='function')
    def mock_get_auth_msg_dialog_args(self, request):
        m = mock.patch('imgurup.ZenityImgur.get_auth_msg_dialog_args')
        ret = m.start()
        request.addfinalizer(m.stop)
        return ret

    def test_ask_pin(
            self,
            mock_get_auth_msg_dialog_args,
            mock_subprocess
    ):
        def _test_ask_pin(args, stdout, stderr):
            from mock import MagicMock
            m = MagicMock()
            if args == self._auth_msg_dialog_args:
                m.communicate = lambda: ['']
            elif args == self._enter_pin_dialog_args:
                m.communicate = lambda: ['XXXXXX']
            return m

        mock_get_auth_msg_dialog_args.return_value = self._auth_msg_dialog_args
        mock_subprocess.Popen.side_effect = _test_ask_pin
        pin = self.imgur.ask_pin(
            self._auth_msg,
            self._auth_url,
            self._enter_token_msg
        )
        assert pin == 'XXXXXX'
        mock_subprocess.Popen.assert_has_calls(
            [
                call(
                    self._auth_msg_dialog_args,
                    stdout=mock_subprocess.PIPE,
                    stderr=mock_subprocess.PIPE
                ),
                call(
                    self._enter_pin_dialog_args,
                    stdout=mock_subprocess.PIPE,
                    stderr=mock_subprocess.PIPE
                )
            ]
        )

    def test_get_ask_image_path_dialog_args(self):
        result = self.imgur.get_ask_image_path_dialog_args()
        args = [
            'zenity',
            '--file-selection',
        ]
        assert result == args

    def test_ask_image_path(self, mock_subprocess):
        # Fail case
        mock_subprocess.Popen.return_value.communicate = lambda: ['']
        with pytest.raises(SystemExit):
            self.imgur.ask_image_path()
        # Success case
        mock_subprocess.Popen.return_value.communicate = lambda: ['/tmp/test.jpg']
        image_path = self.imgur.ask_image_path()
        assert image_path == '/tmp/test.jpg'

    def test_get_ask_album_id_dialog_args(self):
        no_album_msg = self._no_album_msg
        result = self.imgur.get_ask_album_id_dialog_args(self._albums, no_album_msg)
        assert result == self._ask_album_id_dialog_args

    @pytest.fixture(scope='function')
    def mock_get_ask_album_id_dialog_args(self, request):
        m = mock.patch('imgurup.ZenityImgur.get_ask_album_id_dialog_args')
        ret = m.start()
        request.addfinalizer(m.stop)
        return ret

    def test_ask_album_id(
            self,
            mock_get_ask_album_id_dialog_args,
            mock_subprocess
    ):
        mock_get_ask_album_id_dialog_args.return_value = self._ask_album_id_dialog_args
        mock_subprocess.Popen.return_value.communicate = lambda: ['1']
        result = self.imgur.ask_album_id(self._albums)
        assert result == '1'
        mock_subprocess.Popen.return_value.communicate = lambda: ['']
        with pytest.raises(SystemExit):
            self.imgur.ask_album_id(self._albums)

    def test_get_show_link_dialog_args(self):
        links = (
            'Link: http://i.imgur.com/xxxxxxx.jpg\n'
            'Delete link: http://imgur.com/delete/xxxxxxxxxxxxxxx'
        )
        result = self.imgur.get_show_link_dialog_args(links)
        args = [
            'zenity',
            '--info',
            (
                '--text=Link: http://i.imgur.com/xxxxxxx.jpg\n'
                'Delete link: http://imgur.com/delete/xxxxxxxxxxxxxxx'
            )
        ]
        assert result == args

    def test_show_link(self, mock_subprocess):
        self.imgur.show_link(self._image_link, self._delete_hash)
        mock_subprocess.Popen.assert_has_calls(
            [
                call(
                    [
                        'zenity',
                        '--info',
                        (
                            '--text=Link: http://i.imgur.com/xxxxxxx.jpg\n'
                            'Delete link: http://imgur.com/delete/xxxxxxxxxxxxxxx'
                        )
                    ],
                    stdout=mock_subprocess.PIPE,
                    stderr=mock_subprocess.PIPE
                )
            ]
        )