| 1 | n/a | """Tests for packaging.command.register.""" |
|---|
| 2 | n/a | import os |
|---|
| 3 | n/a | import getpass |
|---|
| 4 | n/a | import urllib.request |
|---|
| 5 | n/a | import urllib.error |
|---|
| 6 | n/a | import urllib.parse |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | try: |
|---|
| 9 | n/a | import docutils |
|---|
| 10 | n/a | DOCUTILS_SUPPORT = True |
|---|
| 11 | n/a | except ImportError: |
|---|
| 12 | n/a | DOCUTILS_SUPPORT = False |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | from packaging.tests import unittest, support |
|---|
| 15 | n/a | from packaging.tests.support import Inputs |
|---|
| 16 | n/a | from packaging.command import register as register_module |
|---|
| 17 | n/a | from packaging.command.register import register |
|---|
| 18 | n/a | from packaging.errors import PackagingSetupError |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | PYPIRC_NOPASSWORD = """\ |
|---|
| 22 | n/a | [distutils] |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | index-servers = |
|---|
| 25 | n/a | server1 |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | [server1] |
|---|
| 28 | n/a | username:me |
|---|
| 29 | n/a | """ |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | WANTED_PYPIRC = """\ |
|---|
| 32 | n/a | [distutils] |
|---|
| 33 | n/a | index-servers = |
|---|
| 34 | n/a | pypi |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | [pypi] |
|---|
| 37 | n/a | username:tarek |
|---|
| 38 | n/a | password:password |
|---|
| 39 | n/a | """ |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | class FakeOpener: |
|---|
| 43 | n/a | """Fakes a PyPI server""" |
|---|
| 44 | n/a | def __init__(self): |
|---|
| 45 | n/a | self.reqs = [] |
|---|
| 46 | n/a | |
|---|
| 47 | n/a | def __call__(self, *args): |
|---|
| 48 | n/a | return self |
|---|
| 49 | n/a | |
|---|
| 50 | n/a | def open(self, req): |
|---|
| 51 | n/a | self.reqs.append(req) |
|---|
| 52 | n/a | return self |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | def read(self): |
|---|
| 55 | n/a | return 'xxx' |
|---|
| 56 | n/a | |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | class RegisterTestCase(support.TempdirManager, |
|---|
| 59 | n/a | support.EnvironRestorer, |
|---|
| 60 | n/a | support.LoggingCatcher, |
|---|
| 61 | n/a | unittest.TestCase): |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | restore_environ = ['HOME'] |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | def setUp(self): |
|---|
| 66 | n/a | super(RegisterTestCase, self).setUp() |
|---|
| 67 | n/a | self.tmp_dir = self.mkdtemp() |
|---|
| 68 | n/a | self.rc = os.path.join(self.tmp_dir, '.pypirc') |
|---|
| 69 | n/a | os.environ['HOME'] = self.tmp_dir |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | # patching the password prompt |
|---|
| 72 | n/a | self._old_getpass = getpass.getpass |
|---|
| 73 | n/a | |
|---|
| 74 | n/a | def _getpass(prompt): |
|---|
| 75 | n/a | return 'password' |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | getpass.getpass = _getpass |
|---|
| 78 | n/a | self.old_opener = urllib.request.build_opener |
|---|
| 79 | n/a | self.conn = urllib.request.build_opener = FakeOpener() |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | def tearDown(self): |
|---|
| 82 | n/a | getpass.getpass = self._old_getpass |
|---|
| 83 | n/a | urllib.request.build_opener = self.old_opener |
|---|
| 84 | n/a | if hasattr(register_module, 'input'): |
|---|
| 85 | n/a | del register_module.input |
|---|
| 86 | n/a | super(RegisterTestCase, self).tearDown() |
|---|
| 87 | n/a | |
|---|
| 88 | n/a | def _get_cmd(self, metadata=None): |
|---|
| 89 | n/a | if metadata is None: |
|---|
| 90 | n/a | metadata = {'home_page': 'xxx', 'author': 'xxx', |
|---|
| 91 | n/a | 'author_email': 'xxx', |
|---|
| 92 | n/a | 'name': 'xxx', 'version': 'xxx'} |
|---|
| 93 | n/a | pkg_info, dist = self.create_dist(**metadata) |
|---|
| 94 | n/a | return register(dist) |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | def test_create_pypirc(self): |
|---|
| 97 | n/a | # this test makes sure a .pypirc file |
|---|
| 98 | n/a | # is created when requested. |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | # let's create a register instance |
|---|
| 101 | n/a | cmd = self._get_cmd() |
|---|
| 102 | n/a | |
|---|
| 103 | n/a | # we shouldn't have a .pypirc file yet |
|---|
| 104 | n/a | self.assertFalse(os.path.exists(self.rc)) |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | # patching input and getpass.getpass |
|---|
| 107 | n/a | # so register gets happy |
|---|
| 108 | n/a | # Here's what we are faking : |
|---|
| 109 | n/a | # use your existing login (choice 1.) |
|---|
| 110 | n/a | # Username : 'tarek' |
|---|
| 111 | n/a | # Password : 'password' |
|---|
| 112 | n/a | # Save your login (y/N)? : 'y' |
|---|
| 113 | n/a | inputs = Inputs('1', 'tarek', 'y') |
|---|
| 114 | n/a | register_module.input = inputs |
|---|
| 115 | n/a | cmd.ensure_finalized() |
|---|
| 116 | n/a | cmd.run() |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | # we should have a brand new .pypirc file |
|---|
| 119 | n/a | self.assertTrue(os.path.exists(self.rc)) |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | # with the content similar to WANTED_PYPIRC |
|---|
| 122 | n/a | with open(self.rc) as fp: |
|---|
| 123 | n/a | content = fp.read() |
|---|
| 124 | n/a | self.assertEqual(content, WANTED_PYPIRC) |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # now let's make sure the .pypirc file generated |
|---|
| 127 | n/a | # really works : we shouldn't be asked anything |
|---|
| 128 | n/a | # if we run the command again |
|---|
| 129 | n/a | def _no_way(prompt=''): |
|---|
| 130 | n/a | raise AssertionError(prompt) |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | register_module.input = _no_way |
|---|
| 133 | n/a | cmd.show_response = True |
|---|
| 134 | n/a | cmd.finalized = False |
|---|
| 135 | n/a | cmd.ensure_finalized() |
|---|
| 136 | n/a | cmd.run() |
|---|
| 137 | n/a | |
|---|
| 138 | n/a | # let's see what the server received : we should |
|---|
| 139 | n/a | # have 2 similar requests |
|---|
| 140 | n/a | self.assertEqual(len(self.conn.reqs), 2) |
|---|
| 141 | n/a | req1 = dict(self.conn.reqs[0].headers) |
|---|
| 142 | n/a | req2 = dict(self.conn.reqs[1].headers) |
|---|
| 143 | n/a | self.assertEqual(req2['Content-length'], req1['Content-length']) |
|---|
| 144 | n/a | self.assertIn(b'xxx', self.conn.reqs[1].data) |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | def test_password_not_in_file(self): |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | self.write_file(self.rc, PYPIRC_NOPASSWORD) |
|---|
| 149 | n/a | cmd = self._get_cmd() |
|---|
| 150 | n/a | cmd.finalize_options() |
|---|
| 151 | n/a | cmd._set_config() |
|---|
| 152 | n/a | cmd.send_metadata() |
|---|
| 153 | n/a | |
|---|
| 154 | n/a | # dist.password should be set |
|---|
| 155 | n/a | # therefore used afterwards by other commands |
|---|
| 156 | n/a | self.assertEqual(cmd.distribution.password, 'password') |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | def test_registration(self): |
|---|
| 159 | n/a | # this test runs choice 2 |
|---|
| 160 | n/a | cmd = self._get_cmd() |
|---|
| 161 | n/a | inputs = Inputs('2', 'tarek', 'tarek@ziade.org') |
|---|
| 162 | n/a | register_module.input = inputs |
|---|
| 163 | n/a | # let's run the command |
|---|
| 164 | n/a | # FIXME does this send a real request? use a mock server |
|---|
| 165 | n/a | cmd.ensure_finalized() |
|---|
| 166 | n/a | cmd.run() |
|---|
| 167 | n/a | |
|---|
| 168 | n/a | # we should have send a request |
|---|
| 169 | n/a | self.assertEqual(len(self.conn.reqs), 1) |
|---|
| 170 | n/a | req = self.conn.reqs[0] |
|---|
| 171 | n/a | headers = dict(req.headers) |
|---|
| 172 | n/a | self.assertEqual(headers['Content-length'], '628') |
|---|
| 173 | n/a | self.assertIn(b'tarek', req.data) |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | def test_password_reset(self): |
|---|
| 176 | n/a | # this test runs choice 3 |
|---|
| 177 | n/a | cmd = self._get_cmd() |
|---|
| 178 | n/a | inputs = Inputs('3', 'tarek@ziade.org') |
|---|
| 179 | n/a | register_module.input = inputs |
|---|
| 180 | n/a | cmd.ensure_finalized() |
|---|
| 181 | n/a | cmd.run() |
|---|
| 182 | n/a | |
|---|
| 183 | n/a | # we should have send a request |
|---|
| 184 | n/a | self.assertEqual(len(self.conn.reqs), 1) |
|---|
| 185 | n/a | req = self.conn.reqs[0] |
|---|
| 186 | n/a | headers = dict(req.headers) |
|---|
| 187 | n/a | self.assertEqual(headers['Content-length'], '298') |
|---|
| 188 | n/a | self.assertIn(b'tarek', req.data) |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | @unittest.skipUnless(DOCUTILS_SUPPORT, 'needs docutils') |
|---|
| 191 | n/a | def test_strict(self): |
|---|
| 192 | n/a | # testing the strict option: when on, the register command stops if the |
|---|
| 193 | n/a | # metadata is incomplete or if description contains bad reST |
|---|
| 194 | n/a | |
|---|
| 195 | n/a | # empty metadata # XXX this is not really empty.. |
|---|
| 196 | n/a | cmd = self._get_cmd({'name': 'xxx', 'version': 'xxx'}) |
|---|
| 197 | n/a | cmd.ensure_finalized() |
|---|
| 198 | n/a | cmd.strict = True |
|---|
| 199 | n/a | inputs = Inputs('1', 'tarek', 'y') |
|---|
| 200 | n/a | register_module.input = inputs |
|---|
| 201 | n/a | self.assertRaises(PackagingSetupError, cmd.run) |
|---|
| 202 | n/a | |
|---|
| 203 | n/a | # metadata is OK but description is broken |
|---|
| 204 | n/a | metadata = {'home_page': 'xxx', 'author': 'xxx', |
|---|
| 205 | n/a | 'author_email': 'éxéxé', |
|---|
| 206 | n/a | 'name': 'xxx', 'version': '4.2', |
|---|
| 207 | n/a | 'description': 'title\n==\n\ntext'} |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | cmd = self._get_cmd(metadata) |
|---|
| 210 | n/a | cmd.ensure_finalized() |
|---|
| 211 | n/a | cmd.strict = True |
|---|
| 212 | n/a | self.assertRaises(PackagingSetupError, cmd.run) |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | # now something that works |
|---|
| 215 | n/a | metadata['description'] = 'title\n=====\n\ntext' |
|---|
| 216 | n/a | cmd = self._get_cmd(metadata) |
|---|
| 217 | n/a | cmd.ensure_finalized() |
|---|
| 218 | n/a | cmd.strict = True |
|---|
| 219 | n/a | inputs = Inputs('1', 'tarek', 'y') |
|---|
| 220 | n/a | register_module.input = inputs |
|---|
| 221 | n/a | cmd.ensure_finalized() |
|---|
| 222 | n/a | cmd.run() |
|---|
| 223 | n/a | |
|---|
| 224 | n/a | # strict is not by default |
|---|
| 225 | n/a | cmd = self._get_cmd() |
|---|
| 226 | n/a | cmd.ensure_finalized() |
|---|
| 227 | n/a | inputs = Inputs('1', 'tarek', 'y') |
|---|
| 228 | n/a | register_module.input = inputs |
|---|
| 229 | n/a | cmd.ensure_finalized() |
|---|
| 230 | n/a | cmd.run() |
|---|
| 231 | n/a | |
|---|
| 232 | n/a | # and finally a Unicode test (bug #12114) |
|---|
| 233 | n/a | metadata = {'home_page': 'xxx', 'author': '\u00c9ric', |
|---|
| 234 | n/a | 'author_email': 'xxx', 'name': 'xxx', |
|---|
| 235 | n/a | 'version': 'xxx', |
|---|
| 236 | n/a | 'summary': 'Something about esszet \u00df', |
|---|
| 237 | n/a | 'description': 'More things about esszet \u00df'} |
|---|
| 238 | n/a | |
|---|
| 239 | n/a | cmd = self._get_cmd(metadata) |
|---|
| 240 | n/a | cmd.ensure_finalized() |
|---|
| 241 | n/a | cmd.strict = True |
|---|
| 242 | n/a | inputs = Inputs('1', 'tarek', 'y') |
|---|
| 243 | n/a | register_module.input = inputs |
|---|
| 244 | n/a | cmd.ensure_finalized() |
|---|
| 245 | n/a | cmd.run() |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | def test_register_pep345(self): |
|---|
| 248 | n/a | cmd = self._get_cmd({}) |
|---|
| 249 | n/a | cmd.ensure_finalized() |
|---|
| 250 | n/a | cmd.distribution.metadata['Requires-Dist'] = ['lxml'] |
|---|
| 251 | n/a | data = cmd.build_post_data('submit') |
|---|
| 252 | n/a | self.assertEqual(data['metadata_version'], '1.2') |
|---|
| 253 | n/a | self.assertEqual(data['requires_dist'], ['lxml']) |
|---|
| 254 | n/a | |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | def test_suite(): |
|---|
| 257 | n/a | return unittest.makeSuite(RegisterTestCase) |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | if __name__ == "__main__": |
|---|
| 260 | n/a | unittest.main(defaultTest="test_suite") |
|---|