Blame tests/test_pagure_flask_ui_admin.py

Pierre-Yves Chibon e1214f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - 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 fe5017
import pagure.lib
Pierre-Yves Chibon e1214f
import tests
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon fe5017
class PagureFlaskAdmintests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for flask admin controller of pagure """
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 fe5017
        super(PagureFlaskAdmintests, self).setUp()
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon fe5017
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon fe5017
        pagure.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon 9fae46
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon fe5017
        pagure.ui.admin.SESSION = self.session
Pierre-Yves Chibon e1214f
Jeremy Cline 20109f
        pagure.APP.config['GIT_FOLDER'] = self.path
Pierre-Yves Chibon fe5017
        pagure.APP.config['TICKETS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'tickets')
Pierre-Yves Chibon fe5017
        pagure.APP.config['DOCS_FOLDER'] = os.path.join(
Jeremy Cline 20109f
            self.path, 'docs')
Pierre-Yves Chibon fe5017
        self.app = pagure.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 fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon f1cbab
            output = self.app.post('/admin', follow_redirects=True)
Pierre-Yves Chibon f1cbab
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f1cbab
            self.assertIn(
Pierre-Yves Chibon f1cbab
                '\n                      Access restricted',
Pierre-Yves Chibon f1cbab
                 output.data)
Pierre-Yves Chibon f1cbab
Pierre-Yves Chibon f1cbab
        user.username = 'foo'
Pierre-Yves Chibon f1cbab
        with tests.user_set(pagure.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 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '\n                      Access restricted',
Pierre-Yves Chibon 64f5f4
                 output.data)
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon d0edc3
        user = tests.FakeUser(
Pierre-Yves Chibon d0edc3
            username='pingou',
Pierre-Yves Chibon fe5017
            groups=pagure.APP.config['ADMIN_GROUP'])
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.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(
Patrick Uiterwijk 63cbb9
                'Re-generate user ssh key files' in output.data)
Pierre-Yves Chibon e1214f
Pierre-Yves Chibon fe5017
    @patch('pagure.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 fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 84a65b
            output = self.app.post('/admin/gitolite', follow_redirects=True)
Pierre-Yves Chibon f1cbab
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f1cbab
            self.assertIn(
Pierre-Yves Chibon f1cbab
                '\n                      Access restricted',
Pierre-Yves Chibon f1cbab
                 output.data)
Pierre-Yves Chibon f1cbab
Pierre-Yves Chibon f1cbab
        user.username = 'foo'
Pierre-Yves Chibon f1cbab
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon f1cbab
            output = self.app.post('/admin/gitolite', follow_redirects=True)
Pierre-Yves Chibon f70650
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '\n                      Access restricted',
Pierre-Yves Chibon 64f5f4
                 output.data)
Pierre-Yves Chibon f70650
Pierre-Yves Chibon f70650
        user = tests.FakeUser(
Pierre-Yves Chibon f70650
            username='pingou',
Pierre-Yves Chibon fe5017
            groups=pagure.APP.config['ADMIN_GROUP'])
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.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(
Patrick Uiterwijk 63cbb9
                'Re-generate user ssh key files' 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
                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(
    Patrick Uiterwijk 63cbb9
                    'Re-generate user ssh key files' in output.data)
    Pierre-Yves Chibon 1f5ebb
                self.assertTrue(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Gitolite ACLs updated'
    Pierre-Yves Chibon 1f5ebb
                    in output.data)
    Pierre-Yves Chibon f70650
    Patrick Uiterwijk 63cbb9
        @patch('pagure.generate_user_key_files')
    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 fe5017
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 84a65b
                output = self.app.post('/admin/ssh', follow_redirects=True)
    Pierre-Yves Chibon f1cbab
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon f1cbab
                self.assertIn(
    Pierre-Yves Chibon f1cbab
                    '\n                      Access restricted',
    Pierre-Yves Chibon f1cbab
                     output.data)
    Pierre-Yves Chibon f1cbab
    Pierre-Yves Chibon f1cbab
            user.username = 'foo'
    Pierre-Yves Chibon f1cbab
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon f1cbab
                output = self.app.post('/admin/ssh', follow_redirects=True)
    Pierre-Yves Chibon bd2667
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Access restricted',
    Pierre-Yves Chibon 64f5f4
                     output.data)
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon bd2667
            user = tests.FakeUser(
    Pierre-Yves Chibon bd2667
                username='pingou',
    Pierre-Yves Chibon fe5017
                groups=pagure.APP.config['ADMIN_GROUP'])
    Pierre-Yves Chibon fe5017
            with tests.user_set(pagure.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(
    Patrick Uiterwijk 63cbb9
                    'Re-generate user ssh key files' 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(
    Patrick Uiterwijk 63cbb9
                    'Re-generate user ssh key files' in output.data)
    Pierre-Yves Chibon 1f5ebb
                self.assertTrue(
    Pierre-Yves Chibon 64f5f4
                    '\n                      User key files regenerated'
    Pierre-Yves Chibon 1f5ebb
                    in output.data)
    Pierre-Yves Chibon bd2667
    Pierre-Yves Chibon 2e44eb
        def test_admin_generate_hook_token(self):
    Pierre-Yves Chibon 2e44eb
            """ Test the admin_generate_hook_token endpoint. """
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
            output = self.app.get('/admin/hook_token')
    Pierre-Yves Chibon 2e44eb
            self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
            output = self.app.post('/admin/hook_token')
    Pierre-Yves Chibon 2e44eb
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
            user = tests.FakeUser()
    Pierre-Yves Chibon 2e44eb
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 2e44eb
                output = self.app.post('/admin/hook_token', follow_redirects=True)
    Pierre-Yves Chibon f1cbab
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon f1cbab
                self.assertIn(
    Pierre-Yves Chibon f1cbab
                    '\n                      Access restricted',
    Pierre-Yves Chibon f1cbab
                     output.data)
    Pierre-Yves Chibon f1cbab
    Pierre-Yves Chibon f1cbab
            user.username = 'foo'
    Pierre-Yves Chibon f1cbab
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon f1cbab
                output = self.app.post('/admin/hook_token', follow_redirects=True)
    Pierre-Yves Chibon 2e44eb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Access restricted',
    Pierre-Yves Chibon 64f5f4
                     output.data)
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
            user = tests.FakeUser(
    Pierre-Yves Chibon 2e44eb
                username='pingou',
    Pierre-Yves Chibon 2e44eb
                groups=pagure.APP.config['ADMIN_GROUP'])
    Pierre-Yves Chibon 2e44eb
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 2e44eb
                output = self.app.post('/admin/hook_token', follow_redirects=True)
    Pierre-Yves Chibon 2e44eb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue(
    Patrick Uiterwijk 63cbb9
                    'Re-generate user ssh key files' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue(
    Pierre-Yves Chibon 2e44eb
                    'Re-generate hook-token for every projects' in output.data)
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
                csrf_token = output.data.split(
    Pierre-Yves Chibon 2e44eb
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 2e44eb
                data = {'csrf_token': csrf_token}
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon 2e44eb
                output = self.app.post(
    Pierre-Yves Chibon 2e44eb
                    '/admin/hook_token', data=data, follow_redirects=True)
    Pierre-Yves Chibon 2e44eb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue('

    Admin section

    ' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue('Re-generate gitolite ACLs file' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue(
    Patrick Uiterwijk 63cbb9
                    'Re-generate user ssh key files' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue(
    Pierre-Yves Chibon 2e44eb
                    'Re-generate hook-token for every projects' in output.data)
    Pierre-Yves Chibon 2e44eb
                self.assertTrue(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Hook token all re-generated'
    Pierre-Yves Chibon 2e44eb
                    in output.data)
    Pierre-Yves Chibon 2e44eb
    Pierre-Yves Chibon e1214f
    Pierre-Yves Chibon e1214f
    if __name__ == '__main__':
    Pierre-Yves Chibon fe5017
        SUITE = unittest.TestLoader().loadTestsFromTestCase(PagureFlaskAdmintests)
    Pierre-Yves Chibon e1214f
        unittest.TextTestRunner(verbosity=2).run(SUITE)