Blame tests/test_progit_flask_ui_admin.py

Pierre-Yves Chibon e1214f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
"""
Pierre-Yves Chibon e1214f
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
 Authors:
Pierre-Yves Chibon e1214f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
"""
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon e1214f
import pkg_resources
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
import json
Pierre-Yves Chibon e1214f
import unittest
Pierre-Yves Chibon e1214f
import shutil
Pierre-Yves Chibon e1214f
import sys
Pierre-Yves Chibon e1214f
import os
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
import pygit2
Pierre-Yves Chibon e1214f
from mock import patch
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon e1214f
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
import progit.lib
Pierre-Yves Chibon e1214f
import tests
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
class ProgitFlaskAdmintests(tests.Modeltests):
Pierre-Yves Chibon e1214f
    """ Tests for flask admin controller of progit """
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
    def setUp(self):
Pierre-Yves Chibon e1214f
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon e1214f
        super(ProgitFlaskAdmintests, self).setUp()
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
        progit.APP.config['TESTING'] = True
Pierre-Yves Chibon e1214f
        progit.SESSION = self.session
Pierre-Yves Chibon e1214f
        progit.ui.SESSION = self.session
Pierre-Yves Chibon e1214f
        progit.ui.app.SESSION = self.session
Pierre-Yves Chibon 322d0d
        progit.ui.repo.SESSION = self.session
Pierre-Yves Chibon e1214f
        progit.ui.admin.SESSION = self.session
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
        progit.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon e1214f
        progit.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon e1214f
            tests.HERE, 'forks')
Pierre-Yves Chibon e1214f
        progit.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon e1214f
            tests.HERE, 'tickets')
Pierre-Yves Chibon e1214f
        progit.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon e1214f
            tests.HERE, 'docs')
Pierre-Yves Chibon e1214f
        self.app = progit.APP.test_client()
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
    def test_admin_index(self):
Pierre-Yves Chibon e1214f
        """ Test the admin_index endpoint. """
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
        output = self.app.get('/admin')
Pierre-Yves Chibon e1214f
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
        user = tests.FakeUser()
Pierre-Yves Chibon e1214f
        with tests.user_set(progit.APP, user):
Pierre-Yves Chibon e1214f
            output = self.app.get('/admin', follow_redirects=True)
Pierre-Yves Chibon e1214f
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e1214f
            self.assertTrue(
Pierre-Yves Chibon e1214f
                '
  • Access restricted
  • ' in output.data)
    Pierre-Yves Chibon e1214f
    Pierre-Yves Chibon d0edc3
            user = tests.FakeUser(
    Pierre-Yves Chibon d0edc3
                username='pingou',
    Pierre-Yves Chibon d0edc3
                groups=progit.APP.config['ADMIN_GROUP'])
    Pierre-Yves Chibon e1214f
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon e1214f
                output = self.app.get('/admin', follow_redirects=True)
    Pierre-Yves Chibon e1214f
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon e1214f
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon e1214f
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon e1214f
                self.assertTrue(
    Pierre-Yves Chibon e1214f
                    'Re-generate ssh authorized_key file' in output.data)
    Pierre-Yves Chibon e1214f
    Pierre-Yves Chibon f70650
        @patch('progit.lib.git.write_gitolite_acls')
    Pierre-Yves Chibon f70650
        def test_admin_generate_acl(self, wga):
    Pierre-Yves Chibon f70650
            """ Test the admin_generate_acl endpoint. """
    Pierre-Yves Chibon f70650
            wga.return_value = True
    Pierre-Yves Chibon f70650
    Pierre-Yves Chibon f70650
            output = self.app.get('/admin/gitolite')
    Pierre-Yves Chibon 84a65b
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
            output = self.app.post('/admin/gitolite')
    Pierre-Yves Chibon f70650
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon f70650
    Pierre-Yves Chibon f70650
            user = tests.FakeUser()
    Pierre-Yves Chibon f70650
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 84a65b
                output = self.app.post('/admin/gitolite', follow_redirects=True)
    Pierre-Yves Chibon f70650
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f70650
                self.assertTrue(
    Pierre-Yves Chibon f70650
                    '
  • Access restricted
  • ' in output.data)
    Pierre-Yves Chibon f70650
    Pierre-Yves Chibon f70650
            user = tests.FakeUser(
    Pierre-Yves Chibon f70650
                username='pingou',
    Pierre-Yves Chibon f70650
                groups=progit.APP.config['ADMIN_GROUP'])
    Pierre-Yves Chibon f70650
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 84a65b
                output = self.app.post('/admin/gitolite', follow_redirects=True)
    Pierre-Yves Chibon 84a65b
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 84a65b
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertTrue(
    Pierre-Yves Chibon 84a65b
                    'Re-generate ssh authorized_key file' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertFalse(
    Pierre-Yves Chibon 84a65b
                    '
  • Gitolite ACLs updated
  • '
    Pierre-Yves Chibon 84a65b
                    in output.data)
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
                csrf_token = output.data.split(
    Pierre-Yves Chibon 84a65b
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
                data = {'csrf_token': csrf_token}
    Pierre-Yves Chibon 84a65b
                output = self.app.post(
    Pierre-Yves Chibon 84a65b
                    '/admin/gitolite', data=data, follow_redirects=True)
    Pierre-Yves Chibon f70650
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f70650
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon f70650
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon f70650
                self.assertTrue(
    Pierre-Yves Chibon f70650
                    'Re-generate ssh authorized_key file' in output.data)
    Pierre-Yves Chibon 1f5ebb
                self.assertTrue(
    Pierre-Yves Chibon 1f5ebb
                    '
  • Gitolite ACLs updated
  • '
    Pierre-Yves Chibon 1f5ebb
                    in output.data)
    Pierre-Yves Chibon f70650
    Pierre-Yves Chibon bd2667
        @patch('progit.generate_authorized_key_file')
    Pierre-Yves Chibon bd2667
        def test_admin_refresh_ssh(self, gakf):
    Pierre-Yves Chibon bd2667
            """ Test the admin_refresh_ssh endpoint. """
    Pierre-Yves Chibon bd2667
            gakf.return_value = True
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon bd2667
            output = self.app.get('/admin/ssh')
    Pierre-Yves Chibon 84a65b
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
            output = self.app.post('/admin/ssh')
    Pierre-Yves Chibon bd2667
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon bd2667
            user = tests.FakeUser()
    Pierre-Yves Chibon bd2667
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 84a65b
                output = self.app.post('/admin/ssh', follow_redirects=True)
    Pierre-Yves Chibon bd2667
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon bd2667
                self.assertTrue(
    Pierre-Yves Chibon bd2667
                    '
  • Access restricted
  • ' in output.data)
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon bd2667
            user = tests.FakeUser(
    Pierre-Yves Chibon bd2667
                username='pingou',
    Pierre-Yves Chibon bd2667
                groups=progit.APP.config['ADMIN_GROUP'])
    Pierre-Yves Chibon bd2667
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 84a65b
                output = self.app.post('/admin/ssh', follow_redirects=True)
    Pierre-Yves Chibon 84a65b
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 84a65b
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertTrue(
    Pierre-Yves Chibon 84a65b
                    'Re-generate ssh authorized_key file' in output.data)
    Pierre-Yves Chibon 84a65b
                self.assertFalse(
    Pierre-Yves Chibon 84a65b
                    '
  • Authorized file updated
  • '
    Pierre-Yves Chibon 84a65b
                    in output.data)
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
                csrf_token = output.data.split(
    Pierre-Yves Chibon 84a65b
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 84a65b
    Pierre-Yves Chibon 84a65b
                data = {'csrf_token': csrf_token}
    Pierre-Yves Chibon 84a65b
                output = self.app.post(
    Pierre-Yves Chibon 84a65b
                    '/admin/ssh', data=data, follow_redirects=True)
    Pierre-Yves Chibon bd2667
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon bd2667
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon bd2667
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon bd2667
                self.assertTrue(
    Pierre-Yves Chibon bd2667
                    'Re-generate ssh authorized_key file' in output.data)
    Pierre-Yves Chibon 1f5ebb
                self.assertTrue(
    Pierre-Yves Chibon 1f5ebb
                    '
  • Authorized file updated
  • '
    Pierre-Yves Chibon 1f5ebb
                    in output.data)
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon e1214f
    Pierre-Yves Chibon e1214f
    if __name__ == '__main__':
    Pierre-Yves Chibon e1214f
        SUITE = unittest.TestLoader().loadTestsFromTestCase(ProgitFlaskAdmintests)
    Pierre-Yves Chibon e1214f
        unittest.TextTestRunner(verbosity=2).run(SUITE)