Blame tests/test_progit_flask_ui_repo.py

Pierre-Yves Chibon 78ce15
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
"""
Pierre-Yves Chibon 78ce15
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
 Authors:
Pierre-Yves Chibon 78ce15
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
"""
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 78ce15
import pkg_resources
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
import unittest
Pierre-Yves Chibon 78ce15
import shutil
Pierre-Yves Chibon 78ce15
import sys
Pierre-Yves Chibon 78ce15
import os
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
import json
Pierre-Yves Chibon 78ce15
from mock import patch
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 78ce15
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
import progit.lib
Pierre-Yves Chibon 78ce15
import tests
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
class ProgitFlaskRepotests(tests.Modeltests):
Pierre-Yves Chibon 78ce15
    """ Tests for flask app controller of progit """
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
    def setUp(self):
Pierre-Yves Chibon 78ce15
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 78ce15
        super(ProgitFlaskRepotests, self).setUp()
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
        progit.APP.config['TESTING'] = True
Pierre-Yves Chibon 78ce15
        progit.SESSION = self.session
Pierre-Yves Chibon 78ce15
        progit.ui.SESSION = self.session
Pierre-Yves Chibon 78ce15
        progit.ui.app.SESSION = self.session
Pierre-Yves Chibon 78ce15
        progit.ui.repo.SESSION = self.session
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
        progit.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon 78ce15
        progit.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78ce15
            tests.HERE, 'forks')
Pierre-Yves Chibon 78ce15
        progit.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78ce15
            tests.HERE, 'tickets')
Pierre-Yves Chibon 78ce15
        progit.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon 78ce15
            tests.HERE, 'docs')
Pierre-Yves Chibon 78ce15
        self.app = progit.APP.test_client()
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
    def test_add_user(self):
Pierre-Yves Chibon 78ce15
        """ Test the add_user endpoint. """
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
        output = self.app.get('/foo/adduser')
Pierre-Yves Chibon 78ce15
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
        user = tests.FakeUser()
Pierre-Yves Chibon 78ce15
        with tests.user_set(progit.APP, user):
Pierre-Yves Chibon 78ce15
            output = self.app.get('/foo/adduser')
Pierre-Yves Chibon 78ce15
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            tests.create_projects(self.session)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 78ce15
            self.assertEqual(output.status_code, 403)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
        user.username = 'pingou'
Pierre-Yves Chibon 78ce15
        with tests.user_set(progit.APP, user):
Pierre-Yves Chibon 78ce15
            output = self.app.get('/test/adduser')
Pierre-Yves Chibon 78ce15
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 78ce15
            self.assertTrue('

Add user

' in output.data)
Pierre-Yves Chibon 78ce15
            self.assertTrue(
Pierre-Yves Chibon 78ce15
                '
    \n
' in output.data)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            csrf_token = output.data.split(
Pierre-Yves Chibon 78ce15
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            data = {
Pierre-Yves Chibon 78ce15
                'user': 'ralph',
Pierre-Yves Chibon 78ce15
            }
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            output = self.app.post('/test/adduser', data=data)
Pierre-Yves Chibon 78ce15
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 78ce15
            self.assertTrue('

Add user

' in output.data)
Pierre-Yves Chibon 78ce15
            self.assertTrue(
Pierre-Yves Chibon 78ce15
                '
    \n
' in output.data)
Pierre-Yves Chibon 78ce15
Pierre-Yves Chibon 78ce15
            data['csrf_token'] = csrf_token
Pierre-Yves Chibon 78ce15
            output = self.app.post('/test/adduser', data=data)
Pierre-Yves Chibon 78ce15
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 78ce15
            self.assertTrue('

Add user

' in output.data)
Pierre-Yves Chibon 78ce15
            self.assertTrue(
Pierre-Yves Chibon 78ce15
                '
  • No user "ralph" found
  • '
    Pierre-Yves Chibon 78ce15
                    in output.data)
    Pierre-Yves Chibon 78ce15
    Pierre-Yves Chibon 78ce15
                data['user'] = 'foo'
    Pierre-Yves Chibon 78ce15
                output = self.app.post(
    Pierre-Yves Chibon 78ce15
                    '/test/adduser', data=data, follow_redirects=True)
    Pierre-Yves Chibon 78ce15
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 78ce15
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 78ce15
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 78ce15
                self.assertTrue(
    Pierre-Yves Chibon 78ce15
                    '
  • User added
  • ' in output.data)
    Pierre-Yves Chibon 78ce15
    Pierre-Yves Chibon 5980f6
        def test_remove_user(self):
    Pierre-Yves Chibon 5980f6
            """ Test the remove_user endpoint. """
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
            output = self.app.post('/foo/dropuser/1')
    Pierre-Yves Chibon 5980f6
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
            user = tests.FakeUser()
    Pierre-Yves Chibon 5980f6
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 5980f6
                output = self.app.post('/foo/dropuser/1')
    Pierre-Yves Chibon 5980f6
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
                tests.create_projects(self.session)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
                output = self.app.post('/test/dropuser/1')
    Pierre-Yves Chibon 5980f6
                self.assertEqual(output.status_code, 403)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
            user.username = 'pingou'
    Pierre-Yves Chibon 5980f6
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 2c15da
                output = self.app.post('/test/settings')
    Pierre-Yves Chibon 2c15da
    Pierre-Yves Chibon 2c15da
                csrf_token = output.data.split(
    Pierre-Yves Chibon 2c15da
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 2c15da
    Pierre-Yves Chibon 2c15da
                data = {'csrf_token': csrf_token}
    Pierre-Yves Chibon 2c15da
    Pierre-Yves Chibon 2c15da
                output = self.app.post(
    Pierre-Yves Chibon 2c15da
                    '/test/dropuser/2', data=data, follow_redirects=True)
    Pierre-Yves Chibon 5980f6
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 7c0d1e
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 7c0d1e
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 5980f6
                self.assertTrue(
    Pierre-Yves Chibon 5980f6
                    '
  • User does not have commit or cannot '
  • Pierre-Yves Chibon 5980f6
                    'loose it right' in output.data)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
            # Add an user to a project
    Pierre-Yves Chibon 5980f6
            repo = progit.lib.get_project(self.session, 'test')
    Pierre-Yves Chibon 5980f6
            msg = progit.lib.add_user_to_project(
    Pierre-Yves Chibon 5980f6
                session=self.session,
    Pierre-Yves Chibon 5980f6
                project=repo,
    Pierre-Yves Chibon 5980f6
                user='foo',
    Pierre-Yves Chibon 5980f6
            )
    Pierre-Yves Chibon 5980f6
            self.session.commit()
    Pierre-Yves Chibon 5980f6
            self.assertEqual(msg, 'User added')
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 5980f6
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 5980f6
                output = self.app.post('/test/dropuser/2', follow_redirects=True)
    Pierre-Yves Chibon 5980f6
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 7c0d1e
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 2c15da
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 2c15da
                self.assertTrue(
    Pierre-Yves Chibon 2c15da
                    '
      \n
    ' in output.data)
    Pierre-Yves Chibon 2c15da
                self.assertFalse(
    Pierre-Yves Chibon 2c15da
                    '
  • User removed
  • ' in output.data)
    Pierre-Yves Chibon 2c15da
    Pierre-Yves Chibon 2c15da
                data = {'csrf_token': csrf_token}
    Pierre-Yves Chibon 2c15da
                output = self.app.post(
    Pierre-Yves Chibon 2c15da
                    '/test/dropuser/2', data=data, follow_redirects=True)
    Pierre-Yves Chibon 2c15da
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 2c15da
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 7c0d1e
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 5980f6
                self.assertTrue(
    Pierre-Yves Chibon 5980f6
                    '
  • User removed
  • ' in output.data)
    Pierre-Yves Chibon 5980f6
    Pierre-Yves Chibon 8cae39
        def test_update_description(self):
    Pierre-Yves Chibon 8cae39
            """ Test the update_description endpoint. """
    Pierre-Yves Chibon 8cae39
            output = self.app.post('/foo/updatedesc')
    Pierre-Yves Chibon 8cae39
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
            user = tests.FakeUser()
    Pierre-Yves Chibon 8cae39
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 8cae39
                output = self.app.post('/foo/updatedesc')
    Pierre-Yves Chibon 8cae39
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
                tests.create_projects(self.session)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
                output = self.app.post('/test/updatedesc')
    Pierre-Yves Chibon 8cae39
                self.assertEqual(output.status_code, 403)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
            user.username = 'pingou'
    Pierre-Yves Chibon 8cae39
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 8cae39
                output = self.app.post('/test/updatedesc', follow_redirects=True)
    Pierre-Yves Chibon 8cae39
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 8cae39
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 8cae39
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 8cae39
                self.assertTrue(
    Pierre-Yves Chibon 8cae39
                    '
      \n
    ' in output.data)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
                csrf_token = output.data.split(
    Pierre-Yves Chibon 8cae39
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 8cae39
                data = {
    Pierre-Yves Chibon 8cae39
                    'description': 'new description for test project #1',
    Pierre-Yves Chibon 8cae39
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon 8cae39
                }
    Pierre-Yves Chibon 8cae39
                output = self.app.post(
    Pierre-Yves Chibon 8cae39
                    '/test/updatedesc', data=data, follow_redirects=True)
    Pierre-Yves Chibon 8cae39
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 8cae39
                self.assertTrue('<header class="repo">' in output.data)</header>
    Pierre-Yves Chibon 8cae39
                self.assertTrue('

    Settings

    ' in output.data)
    Pierre-Yves Chibon 8cae39
                self.assertTrue(
    Pierre-Yves Chibon 8cae39
                    '
  • Description updated
  • '
    Pierre-Yves Chibon 8cae39
                    in output.data)
    Pierre-Yves Chibon 8cae39
    Pierre-Yves Chibon 78ce15
    Pierre-Yves Chibon 78ce15
    if __name__ == '__main__':
    Pierre-Yves Chibon 78ce15
        SUITE = unittest.TestLoader().loadTestsFromTestCase(ProgitFlaskRepotests)
    Pierre-Yves Chibon 78ce15
        unittest.TextTestRunner(verbosity=2).run(SUITE)