Blame tests/test_pagure_flask_ui_app_give_project.py

Pierre-Yves Chibon fd7da8
# -*- coding: utf-8 -*-
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
"""
Pierre-Yves Chibon fd7da8
 (c) 2017 - Copyright Red Hat Inc
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
 Authors:
Pierre-Yves Chibon fd7da8
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
"""
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon fd7da8
import pkg_resources
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
import unittest
Pierre-Yves Chibon fd7da8
import shutil
Pierre-Yves Chibon fd7da8
import sys
Pierre-Yves Chibon fd7da8
import tempfile
Pierre-Yves Chibon fd7da8
import os
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
from mock import patch, MagicMock
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon fd7da8
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
import pagure
Pierre-Yves Chibon fd7da8
import pagure.lib
Pierre-Yves Chibon fd7da8
import tests
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
Clement Verna 109c4b
class PagureFlaskGiveRepotests(tests.SimplePagureTest):
Pierre-Yves Chibon fd7da8
    """ Tests for give a project on pagure """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def setUp(self):
Pierre-Yves Chibon fd7da8
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon fd7da8
        super(PagureFlaskGiveRepotests, self).setUp()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon fd7da8
        pagure.SESSION = self.session
Pierre-Yves Chibon fd7da8
        pagure.ui.SESSION = self.session
Pierre-Yves Chibon fd7da8
        pagure.ui.app.SESSION = self.session
Pierre-Yves Chibon fd7da8
        pagure.ui.filters.SESSION = self.session
Pierre-Yves Chibon fd7da8
        pagure.ui.repo.SESSION = self.session
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        pagure.APP.config['VIRUS_SCAN_ATTACHMENTS'] = False
Pierre-Yves Chibon fd7da8
        pagure.APP.config['UPLOAD_FOLDER_URL'] = '/releases/'
Pierre-Yves Chibon fd7da8
        pagure.APP.config['UPLOAD_FOLDER_PATH'] = os.path.join(
Pierre-Yves Chibon fd7da8
            self.path, 'releases')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        tests.create_projects(self.session)
Patrick Uiterwijk 170974
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def _check_user(self, user='pingou'):
Pierre-Yves Chibon fd7da8
        project = pagure.get_authorized_project(
Pierre-Yves Chibon fd7da8
            self.session, project_name='test')
Pierre-Yves Chibon fd7da8
        self.assertEqual(project.user.user, user)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_no_project(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        # No such project
Pierre-Yves Chibon fd7da8
        output = self.app.post('/test42/give')
Pierre-Yves Chibon fd7da8
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_no_csrf(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'pingou'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # Missing CSRF
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon fd7da8
                '<title>Overview - test - Pagure</title>',
Pierre-Yves Chibon fd7da8
                output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_invalid_user(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'pingou'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # Invalid user
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foobar',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon 0575a5
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon fd7da8
                '

No such user foobar found

',
Pierre-Yves Chibon fd7da8
                output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_not_owner(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'foo'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # User isn't the admin
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon fd7da8
                '

You are not allowed to change the settings for this '

Pierre-Yves Chibon fd7da8
                'project

', output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_not_admin(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'foo'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # User isn't the admin
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon fd7da8
                '

You are not allowed to change the settings for this '

Pierre-Yves Chibon fd7da8
                'project

', output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    def test_give_project_not_owner(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
        project = pagure.get_authorized_project(
Pierre-Yves Chibon fd7da8
            self.session, project_name='test')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        msg = pagure.lib.add_user_to_project(
Pierre-Yves Chibon fd7da8
            self.session,
Pierre-Yves Chibon fd7da8
            project=project,
Pierre-Yves Chibon fd7da8
            new_user='foo',
Pierre-Yves Chibon fd7da8
            user='pingou',
Pierre-Yves Chibon fd7da8
            access='admin')
Pierre-Yves Chibon fd7da8
        self.session.commit()
Pierre-Yves Chibon fd7da8
        self.assertEqual(msg, 'User added')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'foo'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # User isn't the owner
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon fd7da8
                '

You are not allowed to give this project

',
Pierre-Yves Chibon fd7da8
                output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    @patch.dict('pagure.APP.config', {'PAGURE_ADMIN_USERS': 'foo'})
Pierre-Yves Chibon a324b4
    @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
Pierre-Yves Chibon fd7da8
    def test_give_project_not_owner_but_admin(self):
Pierre-Yves Chibon 1544e0
        """ Test the give_project endpoint.
Pierre-Yves Chibon 1544e0
Pierre-Yves Chibon 1544e0
        Test giving a project when the person giving the project is a pagure
Pierre-Yves Chibon 1544e0
        admin (instance wide admin) but not a project admin.
Pierre-Yves Chibon 1544e0
        """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'foo'
Pierre-Yves Chibon fd7da8
        user.cla_done = True
Pierre-Yves Chibon fd7da8
        user.groups = ['foo']
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # User isn't the owner but is an instance admin
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon 0575a5
                '\n                      The project has been '
Pierre-Yves Chibon 0575a5
                'transferred to foo\n',
Pierre-Yves Chibon fd7da8
                output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user('foo')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
    @patch.dict('pagure.APP.config', {'PAGURE_ADMIN_USERS': 'foo'})
Pierre-Yves Chibon a324b4
    @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
Pierre-Yves Chibon fd7da8
    def test_give_project(self):
Pierre-Yves Chibon fd7da8
        """ Test the give_project endpoint. """
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
        user = tests.FakeUser()
mprahl 10dcea
        user.username = 'pingou'
mprahl 10dcea
        with tests.user_set(pagure.APP, user):
mprahl 10dcea
            csrf_token = self.get_csrf()
mprahl 10dcea
mprahl 10dcea
            self._check_user()
mprahl 10dcea
mprahl 10dcea
            # All good
mprahl 10dcea
            data = {
mprahl 10dcea
                'user': 'foo',
mprahl 10dcea
                'csrf_token': csrf_token,
mprahl 10dcea
            }
mprahl 10dcea
mprahl 10dcea
            output = self.app.post(
mprahl 10dcea
                '/test/give', data=data, follow_redirects=True)
mprahl 10dcea
            self.assertEqual(output.status_code, 200)
mprahl 10dcea
            self.assertIn(
mprahl 10dcea
                '\n                      The project has been '
mprahl 10dcea
                'transferred to foo\n',
mprahl 10dcea
                output.data)
mprahl 10dcea
mprahl 10dcea
            self._check_user('foo')
mprahl 10dcea
            # Make sure that the user giving the project is still an admin
mprahl 10dcea
            project = pagure.get_authorized_project(
mprahl 10dcea
                self.session, project_name='test')
mprahl 10dcea
            self.assertEqual(len(project.users), 1)
mprahl 10dcea
            self.assertEqual(project.users[0].user, 'pingou')
mprahl 10dcea
mprahl 10dcea
    @patch.dict('pagure.APP.config', {'PAGURE_ADMIN_USERS': 'foo'})
Pierre-Yves Chibon a324b4
    @patch('pagure.lib.git.generate_gitolite_acls', MagicMock())
mprahl 10dcea
    def test_give_project_already_user(self):
mprahl 10dcea
        """ Test the give_project endpoint when the new main_admin is already
mprahl 10dcea
        a committer on the project. """
mprahl 10dcea
mprahl 10dcea
        project = pagure.lib._get_project(self.session, 'test')
mprahl 10dcea
        pagure.lib.add_user_to_project(
mprahl 10dcea
            self.session, project,
mprahl 10dcea
            new_user='foo',
mprahl 10dcea
            user='pingou',
mprahl 10dcea
            access='commit'
mprahl 10dcea
        )
mprahl 10dcea
        self.session.commit()
mprahl 10dcea
        user = tests.FakeUser()
Pierre-Yves Chibon fd7da8
        user.username = 'pingou'
Pierre-Yves Chibon fd7da8
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon fd7da8
            csrf_token = self.get_csrf()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user()
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            # All good
Pierre-Yves Chibon fd7da8
            data = {
Pierre-Yves Chibon fd7da8
                'user': 'foo',
Pierre-Yves Chibon fd7da8
                'csrf_token': csrf_token,
Pierre-Yves Chibon fd7da8
            }
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            output = self.app.post(
Pierre-Yves Chibon fd7da8
                '/test/give', data=data, follow_redirects=True)
Pierre-Yves Chibon fd7da8
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fd7da8
            self.assertIn(
Pierre-Yves Chibon 0575a5
                '\n                      The project has been '
Pierre-Yves Chibon 0575a5
                'transferred to foo\n',
Pierre-Yves Chibon fd7da8
                output.data)
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
            self._check_user('foo')
mprahl 8f40ee
            # Make sure that the user giving the project is still an admin
mprahl 8f40ee
            project = pagure.get_authorized_project(
mprahl 8f40ee
                self.session, project_name='test')
mprahl 8f40ee
            self.assertEqual(len(project.users), 1)
mprahl 8f40ee
            self.assertEqual(project.users[0].user, 'pingou')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
if __name__ == '__main__':
Pierre-Yves Chibon fd7da8
    unittest.main(verbosity=2)