Blame tests/test_progit_flask_ui_app.py

Pierre-Yves Chibon cfad13
# -*- coding: utf-8 -*-
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
"""
Pierre-Yves Chibon cfad13
 (c) 2015 - Copyright Red Hat Inc
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
 Authors:
Pierre-Yves Chibon cfad13
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
"""
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon cfad13
import pkg_resources
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
import unittest
Pierre-Yves Chibon cfad13
import shutil
Pierre-Yves Chibon cfad13
import sys
Pierre-Yves Chibon cfad13
import os
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
import json
Pierre-Yves Chibon cfad13
from mock import patch
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon cfad13
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
import progit.lib
Pierre-Yves Chibon cfad13
import tests
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
class ProgitFlaskApptests(tests.Modeltests):
Pierre-Yves Chibon 94b37b
    """ Tests for flask app controller of progit """
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
    def setUp(self):
Pierre-Yves Chibon cfad13
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon cfad13
        super(ProgitFlaskApptests, self).setUp()
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
        progit.APP.config['TESTING'] = True
Pierre-Yves Chibon cfad13
        progit.SESSION = self.session
Pierre-Yves Chibon cfad13
        progit.ui.SESSION = self.session
Pierre-Yves Chibon 720115
        progit.ui.app.SESSION = self.session
Pierre-Yves Chibon d2d020
        progit.ui.repo.SESSION = self.session
Pierre-Yves Chibon d2d020
Pierre-Yves Chibon 6e1ebb
        progit.APP.config['GIT_FOLDER'] = tests.HERE
Pierre-Yves Chibon d2d020
        progit.APP.config['FORK_FOLDER'] = os.path.join(
Pierre-Yves Chibon d2d020
            tests.HERE, 'forks')
Pierre-Yves Chibon d2d020
        progit.APP.config['TICKETS_FOLDER'] = os.path.join(
Pierre-Yves Chibon d2d020
            tests.HERE, 'tickets')
Pierre-Yves Chibon d2d020
        progit.APP.config['DOCS_FOLDER'] = os.path.join(
Pierre-Yves Chibon d2d020
            tests.HERE, 'docs')
Pierre-Yves Chibon d2d020
        self.app = progit.APP.test_client()
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
    def test_index(self):
Pierre-Yves Chibon 7827f1
        """ Test the index endpoint. """
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
        output = self.app.get('/')
Pierre-Yves Chibon cfad13
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon daa3de
        self.assertTrue('

All Projects (0)

' in output.data)
Pierre-Yves Chibon daa3de
Pierre-Yves Chibon daa3de
        tests.create_projects(self.session)
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon 0ebfa3
        output = self.app.get('/?page=abc')
Pierre-Yves Chibon 0ebfa3
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ebfa3
        self.assertTrue('

All Projects (2)

' in output.data)
Pierre-Yves Chibon 0ebfa3
Pierre-Yves Chibon c17bfb
        # Add a 3rd project with a long description
Pierre-Yves Chibon c17bfb
        item = progit.lib.model.Project(
Pierre-Yves Chibon c17bfb
            user_id=2,  # foo
Pierre-Yves Chibon c17bfb
            name='test3',
Pierre-Yves Chibon c17bfb
            description='test project #3 with a very long description',
Pierre-Yves Chibon c17bfb
        )
Pierre-Yves Chibon c17bfb
        self.session.add(item)
Pierre-Yves Chibon c17bfb
        self.session.commit()
Pierre-Yves Chibon c17bfb
Pierre-Yves Chibon 0ebfa3
        user = tests.FakeUser()
Pierre-Yves Chibon 0ebfa3
        with tests.user_set(progit.APP, user):
Pierre-Yves Chibon 0ebfa3
            output = self.app.get('/?repopage=abc&forkpage=def')
Pierre-Yves Chibon 0ebfa3
            self.assertTrue(
Pierre-Yves Chibon 0ebfa3
                '<section class="project_list" id="repos">' in output.data)</section>
Pierre-Yves Chibon 0ebfa3
            self.assertTrue('

My Forks (0)

' in output.data)
Pierre-Yves Chibon 0ebfa3
            self.assertTrue(
Pierre-Yves Chibon 0ebfa3
                '<section class="project_list" id="myforks">' in output.data)</section>
Pierre-Yves Chibon 0ebfa3
            self.assertTrue('

My Projects (0)

' in output.data)
Pierre-Yves Chibon 0ebfa3
            self.assertTrue(
Pierre-Yves Chibon 0ebfa3
                '<section class="project_list" id="myrepos">' in output.data)</section>
Pierre-Yves Chibon c17bfb
            self.assertTrue('

All Projects (3)

' in output.data)
Pierre-Yves Chibon 0ebfa3
Pierre-Yves Chibon 78d005
    def test_view_users(self):
Pierre-Yves Chibon 7827f1
        """ Test the view_users endpoint. """
Pierre-Yves Chibon 78d005
Pierre-Yves Chibon 78d005
        output = self.app.get('/users/?page=abc')
Pierre-Yves Chibon 78d005
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 78d005
        self.assertTrue('

2 users registered.

' in output.data)
Pierre-Yves Chibon 78d005
        self.assertTrue('' in output.data)
Pierre-Yves Chibon 78d005
        self.assertTrue('' in output.data)
Pierre-Yves Chibon 78d005
Pierre-Yves Chibon 045b56
    def test_view_user(self):
Pierre-Yves Chibon 7827f1
        """ Test the view_user endpoint. """
Pierre-Yves Chibon 045b56
Pierre-Yves Chibon 045b56
        output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
Pierre-Yves Chibon 045b56
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 045b56
        self.assertTrue(
Pierre-Yves Chibon 045b56
            '<section class="project_list" id="repos">' in output.data)</section>
Pierre-Yves Chibon 8bcb94
        self.assertTrue('

Projects (0)

' in output.data)
Pierre-Yves Chibon 045b56
        self.assertTrue(
Pierre-Yves Chibon 045b56
            '<section class="project_list" id="forks">' in output.data)</section>
Pierre-Yves Chibon 8bcb94
        self.assertTrue('

Forks (0)

' in output.data)
Pierre-Yves Chibon 045b56
Pierre-Yves Chibon 8bcb94
        tests.create_projects(self.session)
Pierre-Yves Chibon 293dcb
        self.gitrepos = tests.create_projects_git(
Pierre-Yves Chibon 8bcb94
            progit.APP.config['GIT_FOLDER'])
Pierre-Yves Chibon 8bcb94
Pierre-Yves Chibon 8bcb94
        output = self.app.get('/user/pingou?repopage=abc&forkpage=def')
Pierre-Yves Chibon 8bcb94
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 8bcb94
        self.assertTrue(
Pierre-Yves Chibon 8bcb94
            '<section class="project_list" id="repos">' in output.data)</section>
Pierre-Yves Chibon 8bcb94
        self.assertTrue('

Projects (2)

' in output.data)
Pierre-Yves Chibon 8bcb94
        self.assertTrue(
Pierre-Yves Chibon 8bcb94
            '<section class="project_list" id="forks">' in output.data)</section>
Pierre-Yves Chibon 8bcb94
        self.assertTrue('

Forks (0)

' in output.data)
Pierre-Yves Chibon 045b56
Pierre-Yves Chibon e5e107
    def test_new_project(self):
Pierre-Yves Chibon e5e107
        """ Test the new_project endpoint. """
Pierre-Yves Chibon e5e107
        # Before
Pierre-Yves Chibon e5e107
        projects = progit.lib.search_projects(self.session)
Pierre-Yves Chibon e5e107
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon e5e107
            os.path.join(tests.HERE, 'project#1.git')))
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon e5e107
            os.path.join(tests.HERE, 'tickets', 'project#1.git')))
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon e5e107
            os.path.join(tests.HERE, 'docs', 'project#1.git')))
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
        user = tests.FakeUser()
Pierre-Yves Chibon e5e107
        with tests.user_set(progit.APP, user):
Pierre-Yves Chibon e5e107
            output = self.app.get('/new/')
Pierre-Yves Chibon e5e107
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e5e107
            self.assertTrue('

New project

' in output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
            csrf_token = output.data.split(
Pierre-Yves Chibon e5e107
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
            data = {
Pierre-Yves Chibon e5e107
                'description': 'Project #1',
Pierre-Yves Chibon e5e107
            }
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon e5e107
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e5e107
            self.assertTrue('

New project

' in output.data)
Pierre-Yves Chibon e5e107
            self.assertTrue(
Pierre-Yves Chibon e5e107
                'This field is required.'
Pierre-Yves Chibon e5e107
                in output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
            data['name'] = 'project#1'
Pierre-Yves Chibon e5e107
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon e5e107
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e5e107
            self.assertTrue('

New project

' in output.data)
Pierre-Yves Chibon e5e107
            self.assertFalse(
Pierre-Yves Chibon e5e107
                'This field is required.'
Pierre-Yves Chibon e5e107
                in output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
            data['csrf_token'] =  csrf_token
Pierre-Yves Chibon e5e107
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon e5e107
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon e5e107
            self.assertTrue('

New project

' in output.data)
Pierre-Yves Chibon e5e107
            self.assertTrue(
Pierre-Yves Chibon e5e107
                '
  • No user "username" found
  • '
    Pierre-Yves Chibon e5e107
                    in output.data)
    Pierre-Yves Chibon e5e107
    Pierre-Yves Chibon e5e107
            user.username = 'foo'
    Pierre-Yves Chibon e5e107
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon e5e107
                data['csrf_token'] =  csrf_token
    Pierre-Yves Chibon e5e107
                output = self.app.post('/new/', data=data, follow_redirects=True)
    Pierre-Yves Chibon e5e107
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon e5e107
                self.assertTrue('

    Project #1

    ' in output.data)
    Pierre-Yves Chibon e5e107
                self.assertTrue(
    Pierre-Yves Chibon e5e107
                    '
  • Project "project#1" created
  • '
    Pierre-Yves Chibon e5e107
                    in output.data)
    Pierre-Yves Chibon e5e107
    Pierre-Yves Chibon e5e107
            # After
    Pierre-Yves Chibon e5e107
            projects = progit.lib.search_projects(self.session)
    Pierre-Yves Chibon e5e107
            self.assertEqual(len(projects), 1)
    Pierre-Yves Chibon e5e107
            self.assertTrue(os.path.exists(
    Pierre-Yves Chibon e5e107
                os.path.join(tests.HERE, 'project#1.git')))
    Pierre-Yves Chibon e5e107
            self.assertTrue(os.path.exists(
    Pierre-Yves Chibon e5e107
                os.path.join(tests.HERE, 'tickets', 'project#1.git')))
    Pierre-Yves Chibon e5e107
            self.assertTrue(os.path.exists(
    Pierre-Yves Chibon e5e107
                os.path.join(tests.HERE, 'docs', 'project#1.git')))
    Pierre-Yves Chibon e5e107
    Pierre-Yves Chibon 7958b1
        def test_user_settings(self):
    Pierre-Yves Chibon 7958b1
            """ Test the user_settings endpoint. """
    Pierre-Yves Chibon 7958b1
            self.test_new_project()
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
            user = tests.FakeUser()
    Pierre-Yves Chibon 7958b1
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 7958b1
                output = self.app.get('/settings/')
    Pierre-Yves Chibon 7958b1
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 7958b1
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
            user.username = 'foo'
    Pierre-Yves Chibon 7958b1
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 7958b1
                output = self.app.get('/settings/')
    Pierre-Yves Chibon 7958b1
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 7958b1
                self.assertTrue("

    foo's settings

    " in output.data)
    Pierre-Yves Chibon 7958b1
                self.assertTrue(
    Pierre-Yves Chibon 7958b1
                    '<textarea id="ssh_key" name="ssh_key"></textarea>'
    Pierre-Yves Chibon 7958b1
                    in output.data)
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
                csrf_token = output.data.split(
    Pierre-Yves Chibon 7958b1
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
                data = {
    Pierre-Yves Chibon 7958b1
                    'ssh_key': 'this is my ssh key',
    Pierre-Yves Chibon 7958b1
                }
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
                output = self.app.post('/settings/', data=data)
    Pierre-Yves Chibon 7958b1
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 7958b1
                self.assertTrue("

    foo's settings

    " in output.data)
    Pierre-Yves Chibon 7958b1
                self.assertTrue(
    Pierre-Yves Chibon 7958b1
                    '<textarea id="ssh_key" name="ssh_key">this is my ssh key'</textarea>
    Pierre-Yves Chibon 7958b1
                    '' in output.data)
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
                data['csrf_token'] =  csrf_token
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 7958b1
                output = self.app.post(
    Pierre-Yves Chibon 7958b1
                    '/settings/', data=data, follow_redirects=True)
    Pierre-Yves Chibon 7958b1
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 7958b1
                self.assertTrue(
    Pierre-Yves Chibon 7958b1
                    '
  • Public ssh key updated
  • '
    Pierre-Yves Chibon 7958b1
                    in output.data)
    Pierre-Yves Chibon 7958b1
                self.assertTrue(
    Pierre-Yves Chibon 7958b1
                    '<section class="project_list" id="repos">' in output.data)</section>
    Pierre-Yves Chibon 7958b1
                self.assertTrue(
    Pierre-Yves Chibon 7958b1
                    '<section class="project_list" id="forks">' in output.data)</section>
    Pierre-Yves Chibon 7958b1
    Pierre-Yves Chibon 1ea2d8
        def test_markdown_preview(self):
    Pierre-Yves Chibon 1ea2d8
            """ Test the markdown_preview endpoint. """
    Pierre-Yves Chibon 1ea2d8
    Pierre-Yves Chibon 1ea2d8
            data = {
    Pierre-Yves Chibon 1ea2d8
                'content': 'test\n----\n\n * 1\n * item 2'
    Pierre-Yves Chibon 1ea2d8
            }
    Pierre-Yves Chibon 1ea2d8
    Pierre-Yves Chibon 1ea2d8
            output = self.app.post(
    Pierre-Yves Chibon 1ea2d8
                '/markdown/', data=data)
    Pierre-Yves Chibon 1ea2d8
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 1ea2d8
            self.assertTrue(
    Pierre-Yves Chibon 1ea2d8
                '

    You should be redirected automatically to target URL: '

    Pierre-Yves Chibon 1ea2d8
                ''
    Pierre-Yves Chibon 1ea2d8
                '/login/?next=http%3A%2F%2Flocalhost%2Fmarkdown%2F.  '
    Pierre-Yves Chibon 1ea2d8
                'If not click the link.' in output.data)
    Pierre-Yves Chibon 1ea2d8
    Pierre-Yves Chibon 1ea2d8
            user = tests.FakeUser()
    Pierre-Yves Chibon 1ea2d8
            with tests.user_set(progit.APP, user):
    Pierre-Yves Chibon 1ea2d8
                output = self.app.post('/markdown/', data=data)
    Pierre-Yves Chibon 1ea2d8
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 1ea2d8
                exp = """

    test

    Pierre-Yves Chibon 1ea2d8
      Pierre-Yves Chibon 1ea2d8
    • 1
    • Pierre-Yves Chibon 1ea2d8
    • item 2
    • Pierre-Yves Chibon 1ea2d8
      """
      Pierre-Yves Chibon 1ea2d8
                  self.assertEqual(output.data, exp)
      Pierre-Yves Chibon 1ea2d8
      Pierre-Yves Chibon cfad13
      Pierre-Yves Chibon cfad13
      if __name__ == '__main__':
      Pierre-Yves Chibon cfad13
          SUITE = unittest.TestLoader().loadTestsFromTestCase(ProgitFlaskApptests)
      Pierre-Yves Chibon cfad13
          unittest.TextTestRunner(verbosity=2).run(SUITE)