Esempio n. 1
0
    def test_no_dns_cn(self):
        # Use a CommonName that is *not* a valid DNSName. By default, this is added as a subjectAltName, which
        # should fail.

        stdin = six.StringIO(self.csr_pem)
        cn = 'foo bar'
        msg = r'^%s: Could not parse CommonName as subjectAlternativeName\.$' % cn

        with self.assertCommandError(msg), self.assertSignal(pre_issue_cert) as pre, \
                self.assertSignal(post_issue_cert) as post:
            self.cmd('sign_cert',
                     ca=self.ca,
                     subject=Subject([('CN', cn)]),
                     cn_in_san=True,
                     stdin=stdin)
        self.assertFalse(pre.called)
        self.assertFalse(post.called)
Esempio n. 2
0
    def test_expiry_too_late(self):
        expires = self.ca.expires + timedelta(days=3)
        time_left = (self.ca.expires - datetime.now()).days
        stdin = six.StringIO(self.csr_pem)

        with self.assertCommandError(
                r'^Certificate would outlive CA, maximum expiry for this CA is {} days\.$'
                .format(time_left)), self.assertSignal(
                    pre_issue_cert) as pre, self.assertSignal(
                        post_issue_cert) as post:
            self.cmd('sign_cert',
                     ca=self.ca,
                     alt=['example.com'],
                     expires=expires,
                     stdin=stdin)
        self.assertFalse(pre.called)
        self.assertFalse(post.called)
 def test_no_san(self):
     # test with no subjectAltNames:
     stdin = six.StringIO(self.csr_pem)
     subject = {'CN': 'example.net'}
     stdout, stderr = self.cmd('sign_cert',
                               subject=subject,
                               cn_in_san=False,
                               alt=[],
                               stdin=stdin)
     cert = Certificate.objects.first()
     self.assertSignature([self.ca], cert)
     self.assertSubject(cert.x509, subject)
     self.assertIssuer(self.ca, cert)
     self.assertAuthorityKeyIdentifier(self.ca, cert)
     self.assertEqual(stdout, 'Please paste the CSR:\n%s' % cert.pub)
     self.assertEqual(stderr, '')
     self.assertEqual(cert.subjectAltName(), '')
Esempio n. 4
0
    def test_makemigrations_dry_run(self):
        """
        Ticket #22676 -- `makemigrations --dry-run` should not ask for defaults.
        """

        class SillyModel(models.Model):
            silly_field = models.BooleanField(default=False)
            silly_date = models.DateField()  # Added field without a default

            class Meta:
                app_label = "migrations"

        out = six.StringIO()
        with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
            call_command("makemigrations", "migrations", dry_run=True, stdout=out)
        # Output the expected changes directly, without asking for defaults
        self.assertIn("Add field silly_date to sillymodel", out.getvalue())
 def test_should_raise_BusinessRuleGenerateException_when_names_not_unique(
         self, mock_for_find):
     # GIVEN
     mock_for_find.return_value = [
         self._get_mock_for_business_class(name='test.business_rule_1'),
         self._get_mock_for_business_class(name='test.business_rule_1'),
     ]
     out = six.StringIO()
     expected_message = 'Not unique names for classes: '
     try:
         # WHEN & THEN
         with self.assertRaisesMessage(BusinessRuleGenerateException,
                                       expected_message):
             call_command(self.COMMAND_NAME, verbosity=2, stdout=out)
     except Exception:
         self._print_output(out)
         raise
Esempio n. 6
0
def test_compile_all(settings):
    out = six.StringIO()
    management.call_command('compilejsi18n', verbosity=1, stdout=out)
    out.seek(0)
    lines = [l.strip() for l in out.readlines()]
    assert len(lines) == 2
    assert lines[0] == "processing language en"
    assert lines[1] == "processing language fr"
    assert os.path.exists(
        os.path.join(settings.STATIC_ROOT, "jsi18n", "en", "djangojs.js"))
    filename = os.path.join(settings.STATICI18N_ROOT, "jsi18n", "fr",
                            "djangojs.js")
    assert os.path.exists(filename)
    with io.open(filename, "r", encoding="utf-8") as fp:
        content = fp.read()
        assert "django.catalog" in content
        assert '"Hello world!": "Bonjour \\u00e0 tous !"' in content
Esempio n. 7
0
 def _dumpdata_assert(self, args, output, format='json', natural_keys=False,
                      use_base_manager=False, exclude_list=[], primary_keys=[]):
     new_io = six.StringIO()
     management.call_command('dumpdata', *args, **{'format': format,
                                                   'stdout': new_io,
                                                   'stderr': new_io,
                                                   'use_natural_keys': natural_keys,
                                                   'use_base_manager': use_base_manager,
                                                   'exclude': exclude_list,
                                                   'primary_keys': primary_keys})
     command_output = new_io.getvalue().strip()
     if format == "json":
         self.assertJSONEqual(command_output, output)
     elif format == "xml":
         self.assertXMLEqual(command_output, output)
     else:
         self.assertEqual(command_output, output)
Esempio n. 8
0
def test_compile_no_use_i18n(settings, locale):
    """Tests compilation when `USE_I18N = False`.

    In this scenario, only the `settings.LANGUAGE_CODE` locale is processed
    (it defaults to `en-us` for Django projects).
    """
    settings.USE_I18N = False
    settings.LANGUAGE_CODE = locale

    out = six.StringIO()
    management.call_command('compilejsi18n', verbosity=1, stdout=out)
    out.seek(0)
    lines = [l.strip() for l in out.readlines()]
    assert len(lines) == 1
    assert lines[0] == "processing language %s" % locale
    assert os.path.exists(
        os.path.join(settings.STATIC_ROOT, "jsi18n", locale, "djangojs.js"))
Esempio n. 9
0
    def test_pre_syncdb_args(self):
        r = PreSyncdbReceiver()
        signals.pre_syncdb.connect(r, sender=models)
        management.call_command('syncdb',
                                database=SYNCDB_DATABASE,
                                verbosity=SYNCDB_VERBOSITY,
                                interactive=SYNCDB_INTERACTIVE,
                                load_initial_data=False,
                                stdout=six.StringIO())

        args = r.call_args
        self.assertEqual(r.call_counter, 1)
        self.assertEqual(set(args), set(PRE_SYNCDB_ARGS))
        self.assertEqual(args['app'], models)
        self.assertEqual(args['verbosity'], SYNCDB_VERBOSITY)
        self.assertEqual(args['interactive'], SYNCDB_INTERACTIVE)
        self.assertEqual(args['db'], 'default')
Esempio n. 10
0
    def test_regression_22823_unmigrated_fk_to_migrated_model(self):
        """
        https://code.djangoproject.com/ticket/22823

        Assuming you have 3 apps, `A`, `B`, and `C`, such that:

        * `A` has migrations
        * `B` has a migration we want to apply
        * `C` has no migrations, but has an FK to `A`

        When we try to migrate "B", an exception occurs because the
        "B" was not included in the ProjectState that is used to detect
        soft-applied migrations.
        """
        call_command("migrate",
                     "migrated_unapplied_app",
                     stdout=six.StringIO())
Esempio n. 11
0
    def test_non_ascii_verbose_name(self):
        username_field = User._meta.get_field('username')
        old_verbose_name = username_field.verbose_name
        username_field.verbose_name = _('u\u017eivatel')
        new_io = six.StringIO()
        try:
            call_command(
                "createsuperuser",
                interactive=True,
                stdout=new_io,
                stdin=MockTTY(),
            )
        finally:
            username_field.verbose_name = old_verbose_name

        command_output = new_io.getvalue().strip()
        self.assertEqual(command_output, 'Superuser created successfully.')
Esempio n. 12
0
 def test_createsuperuser_command_with_database_option(self):
     """
     changepassword --database should operate on the specified DB.
     """
     new_io = six.StringIO()
     call_command(
         'createsuperuser',
         interactive=False,
         username='******',
         email='*****@*****.**',
         database='other',
         stdout=new_io,
     )
     command_output = new_io.getvalue().strip()
     self.assertEqual(command_output, 'Superuser created successfully.')
     user = User.objects.using('other').get(username='******')
     self.assertEqual(user.email, '*****@*****.**')
Esempio n. 13
0
    def test_swappable_user_missing_required_field(self):
        "A Custom superuser won't be created when a required field isn't provided"
        # We can use the management command to create a superuser
        # We skip validation because the temporary substitution of the
        # swappable User model messes with validation.
        new_io = six.StringIO()
        with self.assertRaises(CommandError):
            call_command(
                "createsuperuser",
                interactive=False,
                username="******",
                stdout=new_io,
                stderr=new_io,
                skip_checks=True
            )

        self.assertEqual(CustomUser._default_manager.count(), 0)
Esempio n. 14
0
 def test_loaddata_not_existent_fixture_file(self):
     stdout_output = six.StringIO()
     with warnings.catch_warnings(record=True) as w:
         warnings.simplefilter("always")
         # With verbosity=2, we get both stdout output and a warning
         management.call_command(
             'loaddata',
             'this_fixture_doesnt_exist',
             verbosity=2,
             stdout=stdout_output,
         )
     self.assertIn("No fixture 'this_fixture_doesnt_exist' in",
                   force_text(stdout_output.getvalue()))
     self.assertEqual(len(w), 1)
     self.assertEqual(
         force_text(w[0].message),
         "No fixture named 'this_fixture_doesnt_exist' found.")
Esempio n. 15
0
 def test_pre_migrate_migrations_only(self):
     """
     If all apps have migrations, pre_migrate should be sent.
     """
     r = PreMigrateReceiver()
     signals.pre_migrate.connect(r, sender=APP_CONFIG)
     stdout = six.StringIO()
     management.call_command('migrate', database=MIGRATE_DATABASE,
         verbosity=MIGRATE_VERBOSITY, interactive=MIGRATE_INTERACTIVE,
         stdout=stdout)
     args = r.call_args
     self.assertEqual(r.call_counter, 1)
     self.assertEqual(set(args), set(PRE_MIGRATE_ARGS))
     self.assertEqual(args['app_config'], APP_CONFIG)
     self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
     self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
     self.assertEqual(args['using'], 'default')
Esempio n. 16
0
def test_compile(settings, locale):
    out = six.StringIO()
    management.call_command('compilejsi18n',
                            verbosity=1,
                            stdout=out,
                            locale=to_locale(locale))
    out.seek(0)
    lines = [l.strip() for l in out.readlines()]

    assert len(lines) == 1
    assert lines[0] == "processing language %s" % to_locale(locale)
    filename = os.path.join(settings.STATICI18N_ROOT, "jsi18n",
                            to_locale(locale), "djangojs.js")
    assert os.path.exists(filename)
    with io.open(filename, "r", encoding="utf-8") as fp:
        content = fp.read()
        assert LOCALIZED_CONTENT[locale] in content
Esempio n. 17
0
    def test_pre_migrate_args(self):
        r = PreMigrateReceiver()
        signals.pre_migrate.connect(r, sender=APP_CONFIG)
        management.call_command('migrate',
                                database=MIGRATE_DATABASE,
                                verbosity=MIGRATE_VERBOSITY,
                                interactive=MIGRATE_INTERACTIVE,
                                load_initial_data=False,
                                stdout=six.StringIO())

        args = r.call_args
        self.assertEqual(r.call_counter, 1)
        self.assertEqual(set(args), set(PRE_MIGRATE_ARGS))
        self.assertEqual(args['app_config'], APP_CONFIG)
        self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
        self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
        self.assertEqual(args['using'], 'default')
Esempio n. 18
0
 def test_migrate_record_squashed(self):
     """
     Running migrate for a squashed migration should record as run
     if all of the replaced migrations have been run (#25231).
     """
     recorder = MigrationRecorder(connection)
     recorder.record_applied("migrations", "0001_initial")
     recorder.record_applied("migrations", "0002_second")
     out = six.StringIO()
     call_command("migrate", "migrations", verbosity=0)
     call_command("showmigrations", "migrations", stdout=out, no_color=True)
     self.assertEqual(
         'migrations\n'
         ' [x] 0001_squashed_0002 (2 squashed migrations)\n',
         out.getvalue().lower())
     self.assertIn(("migrations", "0001_squashed_0002"),
                   recorder.applied_migrations())
Esempio n. 19
0
def streamTest(format, self):
    obj = ComplexModel(field1='first', field2='second', field3='third')
    obj.save_base(raw=True)

    # Serialize the test database to a stream
    for stream in (six.StringIO(), HttpResponse()):
        serializers.serialize(format, [obj], indent=2, stream=stream)

        # Serialize normally for a comparison
        string_data = serializers.serialize(format, [obj], indent=2)

        # Check that the two are the same
        if isinstance(stream, six.StringIO):
            self.assertEqual(string_data, stream.getvalue())
        else:
            self.assertEqual(string_data, stream.content.decode('utf-8'))
        stream.close()
Esempio n. 20
0
    def test_dumpdata_loaddata_cycle(self):
        """
        Test a dumpdata/loaddata cycle with geographic data.
        """
        out = six.StringIO()
        original_data = list(City.objects.all().order_by('name'))
        call_command('dumpdata', 'geoapp.City', stdout=out)
        result = out.getvalue()
        houston = City.objects.get(name='Houston')
        self.assertIn('"point": "%s"' % houston.point.ewkt, result)

        # Reload now dumped data
        with tempfile.NamedTemporaryFile(mode='w', suffix='.json') as tmp:
            tmp.write(result)
            tmp.seek(0)
            call_command('loaddata', tmp.name, verbosity=0)
        self.assertListEqual(original_data, list(City.objects.all().order_by('name')))
Esempio n. 21
0
    def test_simple_migration(self):
        """
        Tests serializing a simple migration.
        """
        fields = {
            'charfield': models.DateTimeField(default=datetime.datetime.utcnow),
            'datetimefield': models.DateTimeField(default=datetime.datetime.utcnow),
        }

        options = {
            'verbose_name': 'My model',
            'verbose_name_plural': 'My models',
        }

        migration = type(str("Migration"), (migrations.Migration,), {
            "operations": [
                migrations.CreateModel("MyModel", tuple(fields.items()), options, (models.Model,)),
                migrations.CreateModel("MyModel2", tuple(fields.items()), bases=(models.Model,)),
                migrations.CreateModel(
                    name="MyModel3", fields=tuple(fields.items()), options=options, bases=(models.Model,)
                ),
                migrations.DeleteModel("MyModel"),
                migrations.AddField("OtherModel", "datetimefield", fields["datetimefield"]),
            ],
            "dependencies": [("testapp", "some_other_one")],
        })
        writer = MigrationWriter(migration)
        output = writer.as_string()
        # It should NOT be unicode.
        self.assertIsInstance(output, six.binary_type, "Migration as_string returned unicode")
        # We don't test the output formatting - that's too fragile.
        # Just make sure it runs for now, and that things look alright.
        result = self.safe_exec(output)
        self.assertIn("Migration", result)
        # In order to preserve compatibility with Python 3.2 unicode literals
        # prefix shouldn't be added to strings.
        tokens = tokenize.generate_tokens(six.StringIO(str(output)).readline)
        for token_type, token_source, (srow, scol), __, line in tokens:
            if token_type == tokenize.STRING:
                self.assertFalse(
                    token_source.startswith('u'),
                    "Unicode literal prefix found at %d:%d: %r" % (
                        srow, scol, line.strip()
                    )
                )
Esempio n. 22
0
    def test_from_stdin(self):
        stdin = six.StringIO(self.csr_pem)
        subject = OrderedDict([('CN', 'example.com')])
        stdout, stderr = self.cmd('sign_cert', subject=subject, stdin=stdin)
        self.assertEqual(stderr, '')

        cert = Certificate.objects.first()
        self.assertSignature([self.ca], cert)
        self.assertSubject(cert.x509, subject)
        self.assertEqual(stdout, 'Please paste the CSR:\n%s' % cert.pub)

        self.assertEqual(
            cert.keyUsage(),
            (True, ['digitalSignature', 'keyAgreement', 'keyEncipherment']))
        self.assertEqual(cert.extendedKeyUsage(), (False, ['serverAuth']))
        self.assertEqual(cert.subjectAltName(), (False, ['DNS:example.com']))
        self.assertIssuer(self.ca, cert)
        self.assertAuthorityKeyIdentifier(self.ca, cert)
Esempio n. 23
0
    def test_args(self):
        pre_migrate_receiver = Receiver(signals.pre_migrate)
        post_migrate_receiver = Receiver(signals.post_migrate)
        management.call_command(
            'migrate', database=MIGRATE_DATABASE, verbosity=MIGRATE_VERBOSITY,
            interactive=MIGRATE_INTERACTIVE, stdout=six.StringIO(),
        )

        for receiver in [pre_migrate_receiver, post_migrate_receiver]:
            args = receiver.call_args
            self.assertEqual(receiver.call_counter, 1)
            self.assertEqual(set(args), set(SIGNAL_ARGS))
            self.assertEqual(args['app_config'], APP_CONFIG)
            self.assertEqual(args['verbosity'], MIGRATE_VERBOSITY)
            self.assertEqual(args['interactive'], MIGRATE_INTERACTIVE)
            self.assertEqual(args['using'], 'default')
            self.assertEqual(args['plan'], [])
            self.assertIsInstance(args['apps'], migrations.state.StateApps)
    def test_extensions(self):
        stdin = six.StringIO(self.csr_pem)
        stdout, stderr = self.cmd('sign_cert', subject={'CN': 'example.com'},
                                  key_usage='critical,keyCertSign',
                                  ext_key_usage='clientAuth',
                                  alt=['URI:https://example.net'],
                                  tls_features='OCSPMustStaple',
                                  stdin=stdin)
        self.assertEqual(stderr, '')

        cert = Certificate.objects.first()
        self.assertSignature([self.ca], cert)
        self.assertSubject(cert.x509, {'CN': 'example.com'})
        self.assertEqual(stdout, 'Please paste the CSR:\n%s' % cert.pub)
        self.assertEqual(cert.keyUsage(), (True, ['keyCertSign']))
        self.assertEqual(cert.extendedKeyUsage(), (False, ['clientAuth']))
        self.assertEqual(cert.subjectAltName(), (False, ['DNS:example.com', 'URI:https://example.net']))
        self.assertEqual(cert.TLSFeature(), (False, ['OCSP Must-Staple']))
Esempio n. 25
0
    def test_makemigrations_non_interactive_no_field_rename(self):
        """
        Makes sure that makemigrations adds and removes a possible field rename in non-interactive mode.
        """
        class SillyModel(models.Model):
            silly_rename = models.BooleanField(default=False)

            class Meta:
                app_label = "migrations"

        out = six.StringIO()
        try:
            with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
                call_command("makemigrations", "migrations", interactive=False, stdout=out)
        except CommandError:
            self.fail("Makemigrations failed while running non-interactive questioner")
        self.assertIn("Remove field silly_field from sillymodel", force_text(out.getvalue()))
        self.assertIn("Add field silly_rename to sillymodel", force_text(out.getvalue()))
Esempio n. 26
0
 def test_makemigrations_interactive_by_default(self):
     """
     Makes sure that the user is prompted to merge by default if there are
     conflicts and merge is True. Answer negative to differentiate it from
     behavior when --noinput is specified.
     """
     # Monkeypatch interactive questioner to auto reject
     out = six.StringIO()
     with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
         try:
             with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
                 call_command("makemigrations", "migrations", merge=True, stdout=out)
                 merge_file = os.path.join(migration_dir, '0003_merge.py')
                 # This will fail if interactive is False by default
                 self.assertFalse(os.path.exists(merge_file))
         except CommandError:
             self.fail("Makemigrations failed while running interactive questioner")
         self.assertNotIn("Created new merge migration", out.getvalue())
Esempio n. 27
0
    def _test_output(self, verbosity):
        runner = DiscoverRunner(debug_sql=True, verbosity=0)
        suite = runner.test_suite()
        suite.addTest(self.FailingTest())
        suite.addTest(self.ErrorTest())
        suite.addTest(self.PassingTest())
        old_config = runner.setup_databases()
        stream = six.StringIO()
        resultclass = runner.get_resultclass()
        runner.test_runner(
            verbosity=verbosity,
            stream=stream,
            resultclass=resultclass,
        ).run(suite)
        runner.teardown_databases(old_config)

        stream.seek(0)
        return stream.read()
Esempio n. 28
0
    def test_basic_usage(self):
        "Check the operation of the createsuperuser management command"
        # We can use the management command to create a superuser
        new_io = six.StringIO()
        call_command(
            "createsuperuser",
            interactive=False,
            username="******",
            email="*****@*****.**",
            stdout=new_io
        )
        command_output = new_io.getvalue().strip()
        self.assertEqual(command_output, 'Superuser created successfully.')
        u = User.objects.get(username="******")
        self.assertEqual(u.email, '*****@*****.**')

        # created password should be unusable
        self.assertFalse(u.has_usable_password())
Esempio n. 29
0
    def test_squashmigrations_valid_start(self):
        """
        squashmigrations accepts a starting migration.
        """
        out = six.StringIO()
        with self.temporary_migration_module(module="migrations.test_migrations_no_changes") as migration_dir:
            call_command("squashmigrations", "migrations", "0002", "0003",
                         interactive=False, verbosity=1, stdout=out)

            squashed_migration_file = os.path.join(migration_dir, "0002_second_squashed_0003_third.py")
            with codecs.open(squashed_migration_file, "r", encoding="utf-8") as fp:
                content = fp.read()
                self.assertIn("        ('migrations', '0001_initial')", content)
                self.assertNotIn("initial = True", content)
        out = force_text(out.getvalue())
        self.assertNotIn(" - 0001_initial", out)
        self.assertIn(" - 0002_second", out)
        self.assertIn(" - 0003_third", out)
    def serialize(self, queryset, **options):
        """
        Serialize a queryset.
        """
        self.options = options

        self.stream = options.pop("stream", six.StringIO())
        self.selected_fields = options.pop("fields", None)
        self.use_natural_keys = options.pop("use_natural_keys", False)
        if self.use_natural_keys and RemovedInDjango19Warning is not None:
            warnings.warn(
                "``use_natural_keys`` is deprecated; use ``use_natural_foreign_keys`` instead.",
                RemovedInDjango19Warning)
        self.use_natural_foreign_keys = options.pop(
            'use_natural_foreign_keys', False) or self.use_natural_keys
        self.use_natural_primary_keys = options.pop('use_natural_primary_keys',
                                                    False)

        self.start_serialization()
        self.first = True
        for obj in queryset:
            self.start_object(obj)
            # Use the concrete parent class' _meta instead of the object's _meta
            # This is to avoid local_fields problems for proxy models. Refs #17717.
            concrete_model = obj._meta.concrete_model
            # only one change local_fields -> fields for supporting nested models
            for field in concrete_model._meta.fields:
                if field.serialize:
                    if field.rel is None:
                        if self.selected_fields is None or field.attname in self.selected_fields:
                            self.handle_field(obj, field)
                    else:
                        if self.selected_fields is None or field.attname[:
                                                                         -3] in self.selected_fields:
                            self.handle_fk_field(obj, field)
            for field in concrete_model._meta.many_to_many:
                if field.serialize:
                    if self.selected_fields is None or field.attname in self.selected_fields:
                        self.handle_m2m_field(obj, field)
            self.end_object(obj)
            if self.first:
                self.first = False
        self.end_serialization()
        return self.getvalue()