Esempio n. 1
0
    def test_pagination(self):
        self.app.config['SERVER_NAME'] = 'www.example.com'
        self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True
        self.app.config['SITEMAP_MAX_URL_COUNT'] = 10
        sitemap = Sitemap(app=self.app)
        now = datetime.now().isoformat()

        @self.app.route('/')
        def index():
            pass

        @self.app.route('/first')
        def first():
            pass

        @self.app.route('/second')
        def second():
            pass

        @self.app.route('/<username>')
        def user(username):
            pass

        @sitemap.register_generator
        def user():
            for number in range(20):
                yield 'user', {'username': '******'.format(number)}

        directory = mkdtemp()
        manager = Manager(self.app)
        manager.add_command('sitemap', script.Sitemap())

        try:
            manager.handle('manage.py', ['sitemap', '-o', directory])

            with self.app.test_client() as c:
                data = c.get('/sitemap.xml').data
                data1 = c.get('/sitemap1.xml').data

                assert b('sitemapindex') in data
                assert len(data1) > 0

                with open(os.path.join(directory, 'sitemap.xml'), 'r') as f:
                    assert b(f.read()) == data

                with open(os.path.join(directory, 'sitemap1.xml'), 'r') as f:
                    assert b(f.read()) == data1
        finally:
            shutil.rmtree(directory)
Esempio n. 2
0
class EmailSendingTests(ViewTestCase):
    BASE_APP_CONFIG = ViewTestCase.BASE_APP_CONFIG.copy()

    BASE_APP_CONFIG.update(
        MAIL_USERNAME='******',
        NOI_DEPLOY='noi.org',
        SECURITY_EMAIL_SENDER='*****@*****.**',
        MAIL_SUPPRESS_SEND=True,
    )

    def setUp(self):
        super(EmailSendingTests, self).setUp()
        self.manager = Manager(self.app)
        self.manager.add_command('noi1', noi1.Noi1Command)
        self.mail = self.app.extensions.get('mail')

        noi1.set_users_from_json([SAMPLE_USER])
        self.user = noi1.add_user_to_db(SAMPLE_USER, password='******')
        db.session.commit()

        assert self.user.noi1_migration_info.email_sent_at is None

    def run_command(self, *args):
        self.manager.handle('', ['noi1'] + list(args))

    def test_send_all_migration_instructions_works(self):
        with self.mail.record_messages() as outbox:
            self.run_command('send_all_migration_instructions')
            self.assertEqual(len(outbox), 1)

        with self.mail.record_messages() as outbox:
            self.run_command('send_all_migration_instructions')
            self.assertEqual(len(outbox), 0)

    def test_send_migration_instructions_works(self):
        with self.mail.record_messages() as outbox:
            self.run_command('send_migration_instructions',
                             '*****@*****.**')
            self.assertEqual(len(outbox), 1)
            msg = outbox[0]
            self.assertEqual(msg.sender, '*****@*****.**')
            self.assertEqual(msg.recipients, ['*****@*****.**'])
            assert 'https://noi.org' in msg.body

            delta = (datetime.datetime.now() -
                     self.user.noi1_migration_info.email_sent_at)

            assert delta.total_seconds() < 60
Esempio n. 3
0
def prepare_app():
    create_db_and_user()
    # python3 manage.py db upgrade
    db.drop_all()
    db.create_all()

    # python3 manage.py setup
    manager = Manager(app)
    manager.add_command('setup', setup.Setup)
    manager.handle('manage.py', ['setup', '--headless'])

    app.reload_dbconfig()
    register_views()

    yield  # Wait until tests are done
    cleanup()
Esempio n. 4
0
    def test_parse_templates(self):
        """Test the --parse-templates option.
        """
        # Create a file in the app's templates directory
        self.app.template_folder = self.path('templates')
        self.create_files({
            'templates/template.html': """
            {% assets "in", output="output" %}
                {{ ASSET_URL }}
            {% endassets %}
            """,
            'in': "foo"
        })

        # Run the build command with --parse-templates, which should pick
        # up the bundle we defined in above template.
        mgmt = Manager(self.app)
        mgmt.add_command('assets', ManageAssets(log=stdout_log))
        mgmt.handle('test', ['assets', '--parse-templates', 'build'])

        assert self.exists('output')
Esempio n. 5
0
    def test_parse_templates(self):
        """Test the --parse-templates option.
        """
        # Create a file in the app's templates directory
        self.app.template_folder = self.path('templates')
        self.create_files({
            'templates/template.html': """
            {% assets "in", output="output" %}
                {{ ASSET_URL }}
            {% endassets %}
            """,
            'in': "foo"
        })

        # Run the build command with --parse-templates, which should pick
        # up the bundle we defined in above template.
        mgmt = Manager(self.app)
        mgmt.add_command('assets', ManageAssets(log=stdout_log))
        mgmt.handle('test', ['assets', '--parse-templates', 'build'])

        assert self.exists('output')
Esempio n. 6
0
    def test_pagination(self):
        self.app.config['SERVER_NAME'] = 'www.example.com'
        self.app.config['SITEMAP_INCLUDE_RULES_WITHOUT_PARAMS'] = True
        self.app.config['SITEMAP_MAX_URL_COUNT'] = 10
        sitemap = Sitemap(app=self.app)
        now = datetime.now().isoformat()

        @self.app.route('/')
        def index():
            pass

        @self.app.route('/first')
        def first():
            pass

        @self.app.route('/second')
        def second():
            pass

        @self.app.route('/<username>')
        def user(username):
            pass

        @sitemap.register_generator
        def user():
            for number in range(20):
                yield 'user', {'username': '******'.format(number)}

        directory = mkdtemp()
        runner = CliRunner()

        try:
            result = runner.invoke(
                self.app.cli,
                ['sitemap', '-o', directory, '-v'],
                obj=ScriptInfo(create_app=lambda _: self.app),
            )
            assert 'sitemap1.xml\nsitemap2.xml\nsitemap3.xml\nsitemap.xml' \
                in result.output
            # assert result.exit_code == 0

            with self.app.test_client() as c:
                data = c.get('/sitemap.xml').data
                data1 = c.get('/sitemap1.xml').data

                assert b'sitemapindex' in data
                assert len(data1) > 0

                with open(os.path.join(directory, 'sitemap.xml'), 'rb') as f:
                    assert f.read() == data

                with open(os.path.join(directory, 'sitemap1.xml'), 'rb') as f:
                    assert f.read() == data1
        finally:
            shutil.rmtree(directory)

        directory = mkdtemp()
        manager = Manager(self.app)
        manager.add_command('sitemap', script.Sitemap())

        try:
            manager.handle('manage.py', ['sitemap', '-o', directory])

            with self.app.test_client() as c:
                data = c.get('/sitemap.xml').data
                data1 = c.get('/sitemap1.xml').data

                assert b'sitemapindex' in data
                assert len(data1) > 0

                with open(os.path.join(directory, 'sitemap.xml'), 'rb') as f:
                    assert f.read() == data

                with open(os.path.join(directory, 'sitemap1.xml'), 'rb') as f:
                    assert f.read() == data1
        finally:
            shutil.rmtree(directory)