<commit_before>import sys

import fixtures
import imgurpython
import testtools

from unittest import mock

import imgur_cli.cli as cli
from imgur_cli import exceptions

FAKE_ENV = {'IMGUR_CLIENT_ID': 'client_id',
            'IMGUR_CLIENT_SECRET': 'client_secret',
            'IMGUR_ACCESS_TOKEN': 'access_token',
            'IMGUR_REFRESH_TOKEN': 'refresh_token',
            'IMGUR_MASHAPE_KEY': 'mashape_key'}


class DevNull:
    def write(self, data):
        pass


class TestImgurCli(testtools.TestCase):

    def setUp(self):
        super(TestImgurCli, self).setUp()
        self.mock_client = mock.patch('imgur_cli.cli.imgurpython.ImgurClient')
        self._client = self.mock_client.start()
        self.mock_output = mock.patch('imgur_cli.cli_api.generate_output')
        self.mock_output.start()
        self.old_stderr = sys.stderr
        sys.stderr = DevNull()

    def tearDown(self):
        super(TestImgurCli, self).tearDown()
        self.mock_client.stop()
        self.mock_output.stop()
        sys.stderr = self.old_stderr

    def cli(self, argv, exclude=None):
        self.make_env(exclude)
        _cli = cli.ImgurCli()
        _cli.main(argv)
        return _cli

    def assertParser(self, _cli, parser_args, argv):
        """
        Assertion method for subparsers, subcommands and positional arguments.
        Parameter argv is a list where the first item is the name of the subparser,
        followed by the subcommand and the remaining items are positional arguments
        """
        number_of_parsing_levels = 2
        cmd = 'cmd_{0}_{1}'.format(argv[0], argv[1]).replace('-', '_')
        self.assertEqual(parser_args.func.__name__, cmd)
        self.assertTrue(argv[1] in _cli.subcommands)
        self.assertTrue(argv[0] in _cli.subparsers)
        number_of_positional_arguments = len(argv) - number_of_parsing_levels
        if number_of_positional_arguments:
            positional_args = []
            for item in parser_args.func.arguments:
                arg = item[0][0]
                if arg.startswith('--'):
                    break
                positional_args.append(arg)
            for index, arg in enumerate(positional_args,
                                        start=number_of_parsing_levels):
                args_type = (
                    parser_args.func.arguments[index - number_of_parsing_levels][1]
                    .get('type')
                )
                if args_type:
                    self.assertEqual(getattr(parser_args, arg),
                                     args_type(argv[index]))
                else:
                    self.assertEqual(getattr(parser_args, arg), argv[index])
                self.assertRaises(SystemExit, self.cli, argv[:index])

    def make_env(self, exclude=None):
        env = {key: value for key, value in FAKE_ENV.items() if key != exclude}
        self.useFixture(fixtures.MonkeyPatch('os.environ', env))

    def test_imgur_credentials_env(self):
        self.make_env()
        expected = ('client_id', 'client_secret', 'access_token', 'refresh_token',
                    'mashape_key')
        imgur_credentials = cli.imgur_credentials()
        self.assertEqual(expected, imgur_credentials)
        self.make_env(exclude='IMGUR_MASHAPE_KEY')
        expected = ('client_id', 'client_secret', 'access_token', 'refresh_token',
                    None)
        imgur_credentials = cli.imgur_credentials()
        self.assertEqual(expected, imgur_credentials)
        self.make_env(exclude='IMGUR_CLIENT_ID')
        self.assertRaises(imgurpython.client.ImgurClientError,
                          cli.imgur_credentials)
        self.make_env(exclude='IMGUR_CLIENT_SECRET')
        self.assertRaises(imgurpython.client.ImgurClientError,
                          cli.imgur_credentials)

    def test_help_unknown_command(self):
        self.assertRaises(exceptions.CommandError, self.cli, ['help', 'foofoo'])

    def test_account_user(self):
        argv = ['account', 'user', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account.called)

    def test_account_gallery_favorites(self):
        argv = ['account', 'gallery-favorites', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_gallery_favorites.called)

    def test_account_favorites(self):
        argv = ['account', 'favorites', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_favorites.called)

    def test_account_submissions(self):
        argv = ['account', 'submissions', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_submissions.called)

    def test_account_submissions(self):
        argv = ['account', 'settings', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_settings.called)

    def test_account_change_settings(self):
        argv = ['account', 'change-settings', 'ue90', '--username', 'ueg1990']
        self._client.return_value.allowed_account_fields = {'bio', 'public_images',
                                                            'messaging_enabled',
                                                            'album_privacy',
                                                            'accepted_gallery_terms',
                                                            'username'}
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.change_account_settings.called)
        expected_args = 'ue90', {'username': 'ueg1990'}
        _cli.client.change_account_settings.assert_called_with(*expected_args)

    def test_account_email_verification_status(self):
        argv = ['account', 'verification-status', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_email_verification_status.called)

    def test_account_send_verification_email(self):
        argv = ['account', 'send-verification', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.send_verification_email.called)

    def test_account_albums(self):
        argv = ['account', 'albums', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_albums.called)

    def test_album(self):
        argv = ['album', 'id', '123']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_album.called)

    def test_album_images(self):
        argv = ['album', 'images', '123']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertEqual(parser_args.output_file, None)
        self.assertTrue(_cli.client.get_album_images.called)
        self.assertRaises(SystemExit, self.cli,
                          [argv[0], '--output-file', 'dummy.json'])

    def test_album_create(self):
        argv = ['album', 'create', '--title', 'test']
        self._client.return_value.allowed_album_fields = {'ids', 'title',<commit_msg>Add unit test for account album-ids<commit_after>import sys

import fixtures
import imgurpython
import testtools

from unittest import mock

import imgur_cli.cli as cli
from imgur_cli import exceptions

FAKE_ENV = {'IMGUR_CLIENT_ID': 'client_id',
            'IMGUR_CLIENT_SECRET': 'client_secret',
            'IMGUR_ACCESS_TOKEN': 'access_token',
            'IMGUR_REFRESH_TOKEN': 'refresh_token',
            'IMGUR_MASHAPE_KEY': 'mashape_key'}


class DevNull:
    def write(self, data):
        pass


class TestImgurCli(testtools.TestCase):

    def setUp(self):
        super(TestImgurCli, self).setUp()
        self.mock_client = mock.patch('imgur_cli.cli.imgurpython.ImgurClient')
        self._client = self.mock_client.start()
        self.mock_output = mock.patch('imgur_cli.cli_api.generate_output')
        self.mock_output.start()
        self.old_stderr = sys.stderr
        sys.stderr = DevNull()

    def tearDown(self):
        super(TestImgurCli, self).tearDown()
        self.mock_client.stop()
        self.mock_output.stop()
        sys.stderr = self.old_stderr

    def cli(self, argv, exclude=None):
        self.make_env(exclude)
        _cli = cli.ImgurCli()
        _cli.main(argv)
        return _cli

    def assertParser(self, _cli, parser_args, argv):
        """
        Assertion method for subparsers, subcommands and positional arguments.
        Parameter argv is a list where the first item is the name of the subparser,
        followed by the subcommand and the remaining items are positional arguments
        """
        number_of_parsing_levels = 2
        cmd = 'cmd_{0}_{1}'.format(argv[0], argv[1]).replace('-', '_')
        self.assertEqual(parser_args.func.__name__, cmd)
        self.assertTrue(argv[1] in _cli.subcommands)
        self.assertTrue(argv[0] in _cli.subparsers)
        number_of_positional_arguments = len(argv) - number_of_parsing_levels
        if number_of_positional_arguments:
            positional_args = []
            for item in parser_args.func.arguments:
                arg = item[0][0]
                if arg.startswith('--'):
                    break
                positional_args.append(arg)
            for index, arg in enumerate(positional_args,
                                        start=number_of_parsing_levels):
                args_type = (
                    parser_args.func.arguments[index - number_of_parsing_levels][1]
                    .get('type')
                )
                if args_type:
                    self.assertEqual(getattr(parser_args, arg),
                                     args_type(argv[index]))
                else:
                    self.assertEqual(getattr(parser_args, arg), argv[index])
                self.assertRaises(SystemExit, self.cli, argv[:index])

    def make_env(self, exclude=None):
        env = {key: value for key, value in FAKE_ENV.items() if key != exclude}
        self.useFixture(fixtures.MonkeyPatch('os.environ', env))

    def test_imgur_credentials_env(self):
        self.make_env()
        expected = ('client_id', 'client_secret', 'access_token', 'refresh_token',
                    'mashape_key')
        imgur_credentials = cli.imgur_credentials()
        self.assertEqual(expected, imgur_credentials)
        self.make_env(exclude='IMGUR_MASHAPE_KEY')
        expected = ('client_id', 'client_secret', 'access_token', 'refresh_token',
                    None)
        imgur_credentials = cli.imgur_credentials()
        self.assertEqual(expected, imgur_credentials)
        self.make_env(exclude='IMGUR_CLIENT_ID')
        self.assertRaises(imgurpython.client.ImgurClientError,
                          cli.imgur_credentials)
        self.make_env(exclude='IMGUR_CLIENT_SECRET')
        self.assertRaises(imgurpython.client.ImgurClientError,
                          cli.imgur_credentials)

    def test_help_unknown_command(self):
        self.assertRaises(exceptions.CommandError, self.cli, ['help', 'foofoo'])

    def test_account_user(self):
        argv = ['account', 'user', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account.called)

    def test_account_gallery_favorites(self):
        argv = ['account', 'gallery-favorites', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_gallery_favorites.called)

    def test_account_favorites(self):
        argv = ['account', 'favorites', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_favorites.called)

    def test_account_submissions(self):
        argv = ['account', 'submissions', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_submissions.called)

    def test_account_submissions(self):
        argv = ['account', 'settings', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_settings.called)

    def test_account_change_settings(self):
        argv = ['account', 'change-settings', 'ue90', '--username', 'ueg1990']
        self._client.return_value.allowed_account_fields = {'bio', 'public_images',
                                                            'messaging_enabled',
                                                            'album_privacy',
                                                            'accepted_gallery_terms',
                                                            'username'}
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.change_account_settings.called)
        expected_args = 'ue90', {'username': 'ueg1990'}
        _cli.client.change_account_settings.assert_called_with(*expected_args)

    def test_account_email_verification_status(self):
        argv = ['account', 'verification-status', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_email_verification_status.called)

    def test_account_send_verification_email(self):
        argv = ['account', 'send-verification', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.send_verification_email.called)

    def test_account_albums(self):
        argv = ['account', 'albums', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_albums.called)

    def test_account_album_ids(self):
        argv = ['account', 'album-ids', 'me']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_account_album_ids.called)

    def test_album(self):
        argv = ['album', 'id', '123']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertTrue(_cli.client.get_album.called)

    def test_album_images(self):
        argv = ['album', 'images', '123']
        _cli = self.cli(argv)
        parser_args = _cli.parser.parse_args(argv)
        self.assertParser(_cli, parser_args, argv)
        self.assertEqual(parser_args.output_file, None)
        self.assertTrue(_cli.client.get_album_images.called)
        self.assertRaises(SystemExit, self.cli,
                          [argv[0], '--output-file', 'dummy.json'])

    def test_album_create(self):
        argv = ['album', 'create', '--title', 'test']
        self._client.return_value.allowed_album_fields = {'ids', 'title',
{'fraction_non_alphanumeric': 0.1093030396070003, 'fraction_numerical': 0.0035615597175314706, 'mean_word_length': 2.409966499162479, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '610464', 'n_tokens_mistral': 4898, 'n_tokens_neox': 4779, 'n_words': 816}