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
Pierre-Yves Chibon fd7da8
class PagureFlaskGiveRepotests(tests.Modeltests):
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 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 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()
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')
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
Pierre-Yves Chibon fd7da8
if __name__ == '__main__':
Pierre-Yves Chibon fd7da8
    unittest.main(verbosity=2)