Blame tests/test_pagure_flask_ui_app.py

Pierre-Yves Chibon cfad13
# -*- coding: utf-8 -*-
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
"""
Pierre-Yves Chibon f66c8c
 (c) 2015-2017 - 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 170fc9
import datetime
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 5833b3
import six
Pierre-Yves Chibon cfad13
import json
Pierre-Yves Chibon f66c8c
import pygit2
Pierre-Yves Chibon ce6f30
from mock import patch, MagicMock
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 fe5017
import pagure.lib
Pierre-Yves Chibon cfad13
import tests
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon cfad13
Pierre-Yves Chibon fe5017
class PagureFlaskApptests(tests.Modeltests):
Pierre-Yves Chibon fe5017
    """ Tests for flask app controller of pagure """
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 fe5017
        super(PagureFlaskApptests, self).setUp()
Pierre-Yves Chibon cfad13
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 d2d020
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 016b9a
        self.assertIn(
Pierre-Yves Chibon 0c83a8
            '

All Projects '

Pierre-Yves Chibon a57a5c
            '0', 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 016b9a
        self.assertIn(
Pierre-Yves Chibon 0c83a8
            '

All Projects '

clime afed57
            '3', output.data)
Pierre-Yves Chibon 0ebfa3
Pierre-Yves Chibon c17bfb
        # Add a 3rd project with a long description
Pierre-Yves Chibon fe5017
        item = pagure.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',
clime afed57
            hook_token='aaabbbeeefff',
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 fb5b9d
        user = tests.FakeUser(username='foo')
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 0ebfa3
            output = self.app.get('/?repopage=abc&forkpage=def')
Pierre-Yves Chibon 016b9a
            self.assertIn(
Pierre-Yves Chibon fb5b9d
                'Projects 1',
Pierre-Yves Chibon 016b9a
                output.data)
Pierre-Yves Chibon 016b9a
            self.assertIn(
Pierre-Yves Chibon fb5b9d
                'Forks 0',
Pierre-Yves Chibon 016b9a
                output.data)
Pierre-Yves Chibon 016b9a
            self.assertEqual(
Pierre-Yves Chibon fb5b9d
                output.data.count('

No group found

'), 1)
Pierre-Yves Chibon fb5b9d
            self.assertEqual(
Pierre-Yves Chibon a0ff38
                output.data.count('
'), 6)
Vivek Anand 272efb
Vivek Anand 272efb
    def test_watch_list(self):
Vivek Anand 272efb
        ''' Test for watch list of a user '''
Vivek Anand 272efb
Vivek Anand 272efb
        user = tests.FakeUser(username='pingou')
Vivek Anand 272efb
        with tests.user_set(pagure.APP, user):
Vivek Anand 272efb
            output = self.app.get('/')
Vivek Anand 272efb
            self.assertIn(
Vivek Anand 272efb
                '
You have no projects
',
Vivek Anand 272efb
                output.data)
Vivek Anand 272efb
            self.assertIn(
Vivek Anand 272efb
                '

You have no forks

',
Vivek Anand 272efb
                output.data)
Vivek Anand 272efb
            self.assertIn(
Vivek Anand 272efb
                '

No project in watch list

',
Vivek Anand 272efb
                output.data)
Vivek Anand 272efb
Vivek Anand 272efb
            tests.create_projects(self.session)
Vivek Anand 272efb
Vivek Anand 272efb
            output = self.app.get('/')
Vivek Anand 272efb
            self.assertIn(
clime afed57
                'My Projects 3',
Vivek Anand 272efb
                output.data)
Vivek Anand 272efb
            self.assertIn(
Vivek Anand 272efb
                'My Forks 0',
Vivek Anand 272efb
                output.data)
Vivek Anand 272efb
            self.assertIn(
clime afed57
                'My Watch List 3',
Vivek Anand 272efb
                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 016b9a
        self.assertIn(
Pierre-Yves Chibon 5c51fc
            '

\n Users '

Pierre-Yves Chibon 5c51fc
            '2', output.data)
Pierre-Yves Chibon 016b9a
        self.assertIn(
Pierre-Yves Chibon 016b9a
            '',
Pierre-Yves Chibon 016b9a
            output.data)
Pierre-Yves Chibon 016b9a
        self.assertIn(
Pierre-Yves Chibon 016b9a
            '',
Pierre-Yves Chibon 016b9a
            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 016b9a
        self.assertIn(
Pierre-Yves Chibon fb5b9d
            'Projects 0',
Pierre-Yves Chibon a57a5c
            output.data)
Pierre-Yves Chibon a57a5c
        self.assertIn(
Pierre-Yves Chibon fb5b9d
            'Forks 0',
Pierre-Yves Chibon a57a5c
            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 fe5017
            pagure.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 016b9a
        self.assertIn(
clime afed57
            'Projects 3',
Pierre-Yves Chibon a57a5c
            output.data)
Pierre-Yves Chibon 016b9a
        self.assertIn(
Pierre-Yves Chibon fb5b9d
            'Forks 0', output.data)
Pierre-Yves Chibon 045b56
Vivek Anand 3b0299
Vivek Anand 3b0299
    def test_new_project_when_turned_off(self):
Vivek Anand 3b0299
        """ Test the new_project endpoint when new project creation is
Vivek Anand 3b0299
        not allowed in the pagure instance. """
Vivek Anand 3b0299
Vivek Anand 3b0299
        #turn the project creation off
Vivek Anand 3b0299
        pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
Vivek Anand 3b0299
Vivek Anand 3b0299
        # Before
Vivek Anand 3b0299
        projects = pagure.lib.search_projects(self.session)
Vivek Anand 3b0299
        self.assertEqual(len(projects), 0)
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'tickets', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'requests', 'project-1.git')))
Vivek Anand 3b0299
Vivek Anand 3b0299
        user = tests.FakeUser()
Vivek Anand 3b0299
        with tests.user_set(pagure.APP, user):
Vivek Anand 3b0299
            output = self.app.get('/new/')
Vivek Anand 3b0299
            self.assertEqual(output.status_code, 404)
Vivek Anand 3b0299
Vivek Anand 3b0299
            #just get the csrf token
Vivek Anand 3b0299
            pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
Vivek Anand 3b0299
            output = self.app.get('/new/')
Vivek Anand 3b0299
            pagure.APP.config['ENABLE_NEW_PROJECTS'] = False
Vivek Anand 3b0299
Vivek Anand 3b0299
            csrf_token = output.data.split(
Vivek Anand 3b0299
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Vivek Anand 3b0299
Vivek Anand 3b0299
            data = {
Vivek Anand 3b0299
                'description': 'Project #1',
Vivek Anand 3b0299
                'name': 'project-1',
Vivek Anand 3b0299
            }
Vivek Anand 3b0299
Vivek Anand 3b0299
        user.username = 'foo'
Vivek Anand 3b0299
        with tests.user_set(pagure.APP, user):
Vivek Anand 3b0299
            data['csrf_token'] =  csrf_token
Vivek Anand 3b0299
            output = self.app.post('/new/', data=data, follow_redirects=True)
Vivek Anand 3b0299
            self.assertEqual(output.status_code, 404)
Vivek Anand 3b0299
Vivek Anand 3b0299
        #After
Vivek Anand 3b0299
        projects = pagure.lib.search_projects(self.session)
Vivek Anand 3b0299
        self.assertEqual(len(projects), 0)
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'tickets', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'project-1.git')))
Vivek Anand 3b0299
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'requests', 'project-1.git')))
Vivek Anand 3b0299
Vivek Anand 3b0299
        pagure.APP.config['ENABLE_NEW_PROJECTS'] = True
Vivek Anand 3b0299
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 fe5017
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon e5e107
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'project#1.git')))
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'tickets', 'project#1.git')))
Pierre-Yves Chibon e5e107
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'project#1.git')))
Pierre-Yves Chibon ed8d1c
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'requests', 'project#1.git')))
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
        user = tests.FakeUser()
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon e5e107
            output = self.app.get('/new/')
Pierre-Yves Chibon e5e107
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon a82942
            self.assertIn(
Pierre-Yves Chibon a82942
                u'Create new Project', 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 016b9a
            self.assertIn(
Pierre-Yves Chibon a82942
                u'Create new Project', output.data)
Pierre-Yves Chibon a82942
            self.assertIn(
Pierre-Yves Chibon a82942
                u'<small>\n            This field is required. \n'</small>
Pierre-Yves Chibon c3b75f
                '          ', output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon b53d8c
            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 016b9a
            self.assertIn('Create new Project', output.data)
Pierre-Yves Chibon 016b9a
            self.assertNotIn(
Pierre-Yves Chibon a82942
                u'<small>\n            This field is required. \n'</small>
Pierre-Yves Chibon c3b75f
                '          ', 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 016b9a
            self.assertIn('Create new Project', output.data)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon a82942
                u'\n                      No user '
Pierre-Yves Chibon c3b75f
                '"username" found\n                    ',
Pierre-Yves Chibon 64f5f4
                output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
        user.username = 'foo'
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.APP, user):
Patrick Uiterwijk e56b25
            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 98f2af
            self.assertIn(
Pierre-Yves Chibon a82942
                u'
\nProject #1
',
Pierre-Yves Chibon 98f2af
                output.data)
Pierre-Yves Chibon a82942
            self.assertIn(u'

This repo is brand new!

', output.data)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon a82942
                u'<title>Overview - project-1 - Pagure</title>', output.data)
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon e5e107
        # After
Pierre-Yves Chibon fe5017
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon e5e107
        self.assertEqual(len(projects), 1)
Pierre-Yves Chibon e5e107
        self.assertTrue(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'project-1.git')))
Pierre-Yves Chibon e5e107
        self.assertTrue(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'tickets', 'project-1.git')))
Pierre-Yves Chibon e5e107
        self.assertTrue(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'project-1.git')))
Pierre-Yves Chibon ed8d1c
        self.assertTrue(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'requests', 'project-1.git')))
Pierre-Yves Chibon e5e107
Pierre-Yves Chibon f1cd93
    @patch.dict('pagure.APP.config', {'PRIVATE_PROJECTS': True})
Pierre-Yves Chibon f1cd93
    def test_new_project_private(self):
Pierre-Yves Chibon f1cd93
        """ Test the new_project endpoint for a private project. """
Pierre-Yves Chibon f1cd93
        # Before
Pierre-Yves Chibon f1cd93
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon f1cd93
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon f1cd93
        self.assertFalse(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'foo', 'project#1.git')))
Pierre-Yves Chibon f1cd93
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'tickets', 'foo', 'project#1.git')))
Pierre-Yves Chibon f1cd93
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'docs', 'foo', 'project#1.git')))
Pierre-Yves Chibon f1cd93
        self.assertFalse(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'requests', 'foo', 'project#1.git')))
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
        user = tests.FakeUser()
Pierre-Yves Chibon f1cd93
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon f1cd93
            output = self.app.get('/new/')
Pierre-Yves Chibon f1cd93
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'Create new Project', output.data)
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
            csrf_token = output.data.split(
Pierre-Yves Chibon f1cd93
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
            data = {
Pierre-Yves Chibon f1cd93
                'description': 'Project #1',
Pierre-Yves Chibon f1cd93
                'private': True,
Pierre-Yves Chibon f1cd93
            }
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon f1cd93
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'Create new Project', output.data)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'<small>\n            This field is required. \n'</small>
Pierre-Yves Chibon f1cd93
                '          ', output.data)
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
            data['name'] = 'project-1'
Pierre-Yves Chibon f1cd93
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon f1cd93
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f1cd93
            self.assertIn('Create new Project', output.data)
Pierre-Yves Chibon f1cd93
            self.assertNotIn(
Pierre-Yves Chibon f1cd93
                u'<small>\n            This field is required. \n'</small>
Pierre-Yves Chibon f1cd93
                '          ', output.data)
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
            data['csrf_token'] =  csrf_token
Pierre-Yves Chibon f1cd93
            output = self.app.post('/new/', data=data)
Pierre-Yves Chibon f1cd93
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f1cd93
            self.assertIn('Create new Project', output.data)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'\n                      No user '
Pierre-Yves Chibon f1cd93
                '"username" found\n                    ',
Pierre-Yves Chibon f1cd93
                output.data)
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
        user.username = 'foo'
Pierre-Yves Chibon f1cd93
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon f1cd93
            data['csrf_token'] =  csrf_token
Pierre-Yves Chibon f1cd93
            output = self.app.post('/new/', data=data, follow_redirects=True)
Pierre-Yves Chibon f1cd93
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'
\nProject #1
',
Pierre-Yves Chibon f1cd93
                output.data)
Pierre-Yves Chibon f1cd93
            self.assertIn(u'

This repo is brand new!

', output.data)
Pierre-Yves Chibon f1cd93
            self.assertIn(
Pierre-Yves Chibon f1cd93
                u'<title>Overview - foo/project-1 - Pagure</title>', output.data)
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon f1cd93
        # After
Pierre-Yves Chibon f1cd93
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon f1cd93
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon f1cd93
        projects = pagure.lib.search_projects(self.session, private=True)
Pierre-Yves Chibon f1cd93
        self.assertEqual(len(projects), 1)
Pierre-Yves Chibon f1cd93
        self.assertTrue(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'foo', 'project-1.git')))
Pierre-Yves Chibon f1cd93
        self.assertTrue(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'tickets', 'foo', 'project-1.git')))
Pierre-Yves Chibon f1cd93
        self.assertTrue(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'docs', 'foo', 'project-1.git')))
Pierre-Yves Chibon f1cd93
        self.assertTrue(os.path.exists(
Pierre-Yves Chibon f1cd93
            os.path.join(self.path, 'requests', 'foo', 'project-1.git')))
Pierre-Yves Chibon f1cd93
Pierre-Yves Chibon 5833b3
    def test_non_ascii_new_project(self):
Pierre-Yves Chibon 5833b3
        """ Test the new_project endpoint with a non-ascii project. """
Pierre-Yves Chibon 5833b3
        # Before
Pierre-Yves Chibon 5833b3
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 5833b3
        self.assertEqual(len(projects), 0)
Pierre-Yves Chibon 5833b3
        self.assertFalse(os.path.exists(
Patrick Uiterwijk 170974
            os.path.join(self.path, 'repos', 'project-1.git')))
Pierre-Yves Chibon 5833b3
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'tickets', 'project-1.git')))
Pierre-Yves Chibon 5833b3
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'docs', 'project-1.git')))
Pierre-Yves Chibon 5833b3
        self.assertFalse(os.path.exists(
Jeremy Cline 20109f
            os.path.join(self.path, 'requests', 'project-1.git')))
Pierre-Yves Chibon 5833b3
Pierre-Yves Chibon 5833b3
        user = tests.FakeUser()
Pierre-Yves Chibon 5833b3
        user.username = 'foo'
Pierre-Yves Chibon 5833b3
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 5833b3
            output = self.app.get('/new/')
Pierre-Yves Chibon 5833b3
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5833b3
            self.assertIn(
Pierre-Yves Chibon 5833b3
                b'Create new Project', output.data)
Pierre-Yves Chibon 5833b3
Pierre-Yves Chibon 5833b3
            csrf_token = output.data.decode('utf-8').split(
Pierre-Yves Chibon 5833b3
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon 5833b3
Pierre-Yves Chibon 5833b3
            data = {
Pierre-Yves Chibon 5833b3
                'description': 'Prõjéctö #1',
Pierre-Yves Chibon 5833b3
                'name': 'project-1',
Pierre-Yves Chibon 5833b3
                'csrf_token':  csrf_token,
Pierre-Yves Chibon a219ad
                'create_readme': True,
Pierre-Yves Chibon 5833b3
            }
Pierre-Yves Chibon 5833b3
            output = self.app.post('/new/', data=data, follow_redirects=True)
Pierre-Yves Chibon a219ad
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 5833b3
            self.assertIn(
Pierre-Yves Chibon 5833b3
                '
\nPrõjéctö #1
',
Pierre-Yves Chibon 5833b3
                output.data if six.PY2 else output.data.decode('utf-8'))
Pierre-Yves Chibon 5833b3
            self.assertIn(
Pierre-Yves Chibon a219ad
                '''<section class="readme"></section>
Shengjing Zhu 539c7f
              

project-1

Pierre-Yves Chibon a219ad

Prõjéctö #1

Pierre-Yves Chibon a219ad
            ''', output.data if six.PY2 else output.data.decode('utf-8'))
Pierre-Yves Chibon 5833b3
Pierre-Yves Chibon 645b66
            data = {
Pierre-Yves Chibon 645b66
                'description': 'Мой первый суперский репозиторий',
Pierre-Yves Chibon 645b66
                'name': 'project-2',
Pierre-Yves Chibon 645b66
                'csrf_token':  csrf_token,
Pierre-Yves Chibon 645b66
                'create_readme': True,
Pierre-Yves Chibon 645b66
            }
Pierre-Yves Chibon 645b66
            output = self.app.post('/new/', data=data, follow_redirects=True)
Pierre-Yves Chibon 645b66
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 645b66
            self.assertIn(
Pierre-Yves Chibon 645b66
                '
\nМой первый суперский репозиторий
',
Pierre-Yves Chibon 645b66
                output.data if six.PY2 else output.data.decode('utf-8'))
Pierre-Yves Chibon 645b66
            self.assertIn(
Pierre-Yves Chibon 645b66
                '''<section class="readme"></section>
Shengjing Zhu 539c7f
              

project-2

Pierre-Yves Chibon 645b66

Мой первый суперский репозиторий

Pierre-Yves Chibon 645b66
            ''', output.data if six.PY2 else output.data.decode('utf-8'))
Pierre-Yves Chibon 645b66
Pierre-Yves Chibon 5833b3
        # After
Pierre-Yves Chibon 5833b3
        projects = pagure.lib.search_projects(self.session)
Pierre-Yves Chibon 645b66
        self.assertEqual(len(projects), 2)
Pierre-Yves Chibon 645b66
        for project in ['project-1', 'project-2']:
Pierre-Yves Chibon 645b66
            self.assertTrue(os.path.exists(
Patrick Uiterwijk 170974
                os.path.join(self.path, 'repos', '%s.git' % project)))
Pierre-Yves Chibon 645b66
            self.assertTrue(os.path.exists(
Jeremy Cline 20109f
                os.path.join(self.path, 'tickets', '%s.git' % project)))
Pierre-Yves Chibon 645b66
            self.assertTrue(os.path.exists(
Jeremy Cline 20109f
                os.path.join(self.path, 'docs', '%s.git' % project)))
Pierre-Yves Chibon 645b66
            self.assertTrue(os.path.exists(
Jeremy Cline 20109f
                os.path.join(self.path, 'requests', '%s.git' % project)))
Pierre-Yves Chibon 5833b3
Pierre-Yves Chibon 893463
    @patch('pagure.ui.app.admin_session_timedout')
Pierre-Yves Chibon 893463
    def test_user_settings(self, ast):
Pierre-Yves Chibon 7958b1
        """ Test the user_settings endpoint. """
Pierre-Yves Chibon 893463
        ast.return_value = False
Pierre-Yves Chibon 7958b1
        self.test_new_project()
Pierre-Yves Chibon 7958b1
Pierre-Yves Chibon 7958b1
        user = tests.FakeUser()
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.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 fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon 7958b1
            output = self.app.get('/settings/')
Pierre-Yves Chibon 7958b1
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '
\n Basic Information\n'
Pierre-Yves Chibon 64f5f4
                '      ', output.data)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
Pierre-Yves Chibon 64f5f4
                '', 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 = {
Patrick Uiterwijk f888fd
                'ssh_key': 'blah'
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 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '
\n Basic Information\n'
Pierre-Yves Chibon 64f5f4
                '      ', 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)
Patrick Uiterwijk f888fd
            self.assertIn('Invalid SSH keys', output.data)
Patrick Uiterwijk f888fd
            self.assertIn(
Patrick Uiterwijk f888fd
                '
\n Basic Information\n'
Patrick Uiterwijk f888fd
                '      ', output.data)
Patrick Uiterwijk f888fd
            self.assertIn('>blah', output.data)
Patrick Uiterwijk f888fd
Patrick Uiterwijk f888fd
            csrf_token = output.data.split(
Patrick Uiterwijk f888fd
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Patrick Uiterwijk f888fd
Patrick Uiterwijk f888fd
            data = {
Patrick Uiterwijk f888fd
                'ssh_key': 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQDUkub32fZnNI'
Patrick Uiterwijk f888fd
                           '1zJYs43vhhx3c6IcYo4yzhw1gQ37BLhrrNeS6x8l5PKX4J8ZP5'
Patrick Uiterwijk f888fd
                           '1XhViPaLbeOpl94Vm5VSCbLy0xtY9KwLhMkbKj7g6vvfxLm2sT'
Patrick Uiterwijk f888fd
                           'Osb15j4jzIkUYYgIE7cHhZMCLWR6UA1c1HEzo6mewMDsvpQ9wk'
Patrick Uiterwijk f888fd
                           'cDnAuXjK3Q==',
Patrick Uiterwijk f888fd
                'csrf_token': csrf_token
Patrick Uiterwijk f888fd
            }
Patrick Uiterwijk f888fd
Patrick Uiterwijk f888fd
            output = self.app.post(
Patrick Uiterwijk f888fd
                '/settings/', data=data, follow_redirects=True)
Patrick Uiterwijk f888fd
            self.assertEqual(output.status_code, 200)
Patrick Uiterwijk f888fd
            self.assertIn('Public ssh key updated', output.data)
Pierre-Yves Chibon 016b9a
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '
\n Basic Information\n'
Pierre-Yves Chibon 64f5f4
                '      ', output.data)
Pierre-Yves Chibon 016b9a
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
Patrick Uiterwijk f888fd
                'ssh-rsa AAAA', output.data)
Pierre-Yves Chibon 7958b1
Pierre-Yves Chibon 893463
            ast.return_value = True
Pierre-Yves Chibon 893463
            output = self.app.get('/settings/')
Pierre-Yves Chibon 893463
            self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 893463
Pierre-Yves Chibon 80e0f2
    def patched_commit_exists(user, namespace, repo, githash):
Pierre-Yves Chibon 80e0f2
        ''' Patched version of pagure.pfmarkdown._commit_exists to enforce
Pierre-Yves Chibon 80e0f2
        returning true on some given hash without having us actually check
Pierre-Yves Chibon 80e0f2
        the git repos.
Pierre-Yves Chibon 80e0f2
        '''
Pierre-Yves Chibon 80e0f2
        if githash in ['9364354', '9364354a', '9364354a4555ba17aa60f0dc844d70b74eb1aecd']:
Pierre-Yves Chibon 80e0f2
            return True
Pierre-Yves Chibon 80e0f2
        else:
Pierre-Yves Chibon 80e0f2
            return False
Pierre-Yves Chibon 80e0f2
Pierre-Yves Chibon 80e0f2
    @patch(
Pierre-Yves Chibon 80e0f2
        'pagure.pfmarkdown._commit_exists',
Pierre-Yves Chibon 80e0f2
        MagicMock(side_effect=patched_commit_exists))
Pierre-Yves Chibon f66c8c
    def test_patched_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 f629ce
        # CSRF missing
Pierre-Yves Chibon f629ce
        output = self.app.post('/markdown/', data=data)
Pierre-Yves Chibon f629ce
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon 1ea2d8
Pierre-Yves Chibon 1ea2d8
        user = tests.FakeUser()
Pierre-Yves Chibon f629ce
        user.username = 'foo'
Pierre-Yves Chibon fe5017
        with tests.user_set(pagure.APP, user):
Pierre-Yves Chibon f629ce
            output = self.app.get('/settings/')
Pierre-Yves Chibon 1ea2d8
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '
\n Basic Information\n'
Pierre-Yves Chibon 64f5f4
                '      ', output.data)
Pierre-Yves Chibon 64f5f4
            self.assertIn(
Pierre-Yves Chibon 64f5f4
                '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
Pierre-Yves Chibon 64f5f4
                '', output.data)
Pierre-Yves Chibon f629ce
Pierre-Yves Chibon f629ce
            csrf_token = output.data.split(
Pierre-Yves Chibon f629ce
                'name="csrf_token" type="hidden" value="')[1].split('">')[0]
Pierre-Yves Chibon f629ce
Pierre-Yves Chibon f629ce
        # With CSRF
Pierre-Yves Chibon f629ce
        data['csrf_token'] = csrf_token
Pierre-Yves Chibon f629ce
        output = self.app.post('/markdown/', data=data)
Pierre-Yves Chibon f629ce
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f629ce
        exp = """

test

Pierre-Yves Chibon 1ea2d8
    Pierre-Yves Chibon 1ea2d8
  • 1
  • Pierre-Yves Chibon 1ea2d8
  • item 2
  • Pierre-Yves Chibon 1ea2d8
    """
    Pierre-Yves Chibon f629ce
            self.assertEqual(output.data, exp)
    Pierre-Yves Chibon 1ea2d8
    Pierre-Yves Chibon 82e2f3
            tests.create_projects(self.session)
    Pierre-Yves Chibon 82e2f3
            texts = [
    Pierre-Yves Chibon 82e2f3
                'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
    Pierre-Yves Chibon 82e2f3
                'Cf commit 936435',  # 6 chars - not long enough
    Pierre-Yves Chibon 82e2f3
                'Cf commit 9364354',  # 7 chars - long enough
    Pierre-Yves Chibon 82e2f3
                'Cf commit 9364354a',  # 8 chars - still long enough
    Pierre-Yves Chibon 82e2f3
                'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd',  # 40 chars
    Pierre-Yves Chibon 82e2f3
            ]
    Pierre-Yves Chibon 82e2f3
            expected = [
    Pierre-Yves Chibon 82e2f3
                # 'pingou committed on test#9364354a4555ba17aa60f0dc844d70b74eb1aecd',
    Pierre-Yves Chibon 82e2f3
                '

    pingou committed on

    Pierre-Yves Chibon 82e2f3
                'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
    Pierre-Yves Chibon 82e2f3
                '>test#9364354a4555ba17aa60f0dc844d70b74eb1aecd

    ',
    Pierre-Yves Chibon 82e2f3
                # 'Cf commit 936435',
    Pierre-Yves Chibon 82e2f3
                '

    Cf commit 936435

    ',
    Pierre-Yves Chibon 82e2f3
                # 'Cf commit 9364354',
    Pierre-Yves Chibon 82e2f3
                #'

    Cf commit 9364354

    ',
    Pierre-Yves Chibon 82e2f3
                '

    Cf commit

    Pierre-Yves Chibon 82e2f3
                'title="Commit 9364354"> 9364354

    ',
    Pierre-Yves Chibon 82e2f3
                # 'Cf commit 9364354a',
    Pierre-Yves Chibon 82e2f3
                '

    Cf commit

    Pierre-Yves Chibon 82e2f3
                'title="Commit 9364354a"> 9364354

    ',
    Pierre-Yves Chibon 82e2f3
                # 'Cf commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd',
    Pierre-Yves Chibon 82e2f3
                '

    Cf commit

    Pierre-Yves Chibon 82e2f3
                'title="Commit 9364354a4555ba17aa60f0dc844d70b74eb1aecd"'
    Pierre-Yves Chibon 82e2f3
                '> 9364354

    ',
    Pierre-Yves Chibon 82e2f3
            ]
    Pierre-Yves Chibon 82e2f3
    Pierre-Yves Chibon 82e2f3
            with pagure.APP.app_context():
    Pierre-Yves Chibon 82e2f3
                for idx, text in enumerate(texts):
    Pierre-Yves Chibon 82e2f3
                    data = {
    Pierre-Yves Chibon 82e2f3
                        'content': text,
    Pierre-Yves Chibon 82e2f3
                        'csrf_token': csrf_token,
    Pierre-Yves Chibon 82e2f3
                    }
    Pierre-Yves Chibon 82e2f3
                    output = self.app.post('/markdown/?repo=test', data=data)
    Pierre-Yves Chibon 82e2f3
                    self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 82e2f3
                    self.assertEqual(expected[idx], output.data)
    Pierre-Yves Chibon 82e2f3
    Pierre-Yves Chibon 80e0f2
        def test_markdown_preview(self):
    Pierre-Yves Chibon f66c8c
            """ Test the markdown_preview endpoint with a non-existing commit.
    Pierre-Yves Chibon f66c8c
            """
    Pierre-Yves Chibon 80e0f2
    Pierre-Yves Chibon 80e0f2
            user = tests.FakeUser()
    Pierre-Yves Chibon 80e0f2
            user.username = 'foo'
    Pierre-Yves Chibon 80e0f2
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 80e0f2
                output = self.app.get('/settings/')
    Pierre-Yves Chibon 80e0f2
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 80e0f2
                self.assertIn(
    Pierre-Yves Chibon 80e0f2
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 80e0f2
                    '      ', output.data)
    Pierre-Yves Chibon 80e0f2
    Pierre-Yves Chibon 80e0f2
                csrf_token = output.data.split(
    Pierre-Yves Chibon 80e0f2
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 80e0f2
    Pierre-Yves Chibon 80e0f2
            tests.create_projects(self.session)
    Patrick Uiterwijk 170974
            tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
    Pierre-Yves Chibon 80e0f2
            text = 'Cf commit 9364354a4555ba17aa60f0d'
    Pierre-Yves Chibon 80e0f2
            exp = '

    Cf commit 9364354a4555ba17aa60f0d

    '
    Pierre-Yves Chibon 80e0f2
    Pierre-Yves Chibon 80e0f2
            with pagure.APP.app_context():
    Pierre-Yves Chibon 80e0f2
                data = {
    Pierre-Yves Chibon 80e0f2
                    'content': text,
    Pierre-Yves Chibon 80e0f2
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon 80e0f2
                }
    Pierre-Yves Chibon 80e0f2
                output = self.app.post('/markdown/?repo=test', data=data)
    Pierre-Yves Chibon 80e0f2
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 80e0f2
                self.assertEqual(exp, output.data)
    Pierre-Yves Chibon 80e0f2
    Pierre-Yves Chibon f66c8c
        def test_markdown_preview_valid_commit(self):
    Pierre-Yves Chibon f66c8c
            """ Test the markdown_preview endpoint with an existing commit. """
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
            user = tests.FakeUser()
    Pierre-Yves Chibon f66c8c
            user.username = 'foo'
    Pierre-Yves Chibon f66c8c
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon f66c8c
                output = self.app.get('/settings/')
    Pierre-Yves Chibon f66c8c
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f66c8c
                self.assertIn(
    Pierre-Yves Chibon f66c8c
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon f66c8c
                    '      ', output.data)
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
                csrf_token = output.data.split(
    Pierre-Yves Chibon f66c8c
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
            tests.create_projects(self.session)
    Patrick Uiterwijk 170974
            tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
    Patrick Uiterwijk 170974
            repopath = os.path.join(self.path, 'repos', 'test.git')
    Pierre-Yves Chibon f66c8c
            tests.add_content_git_repo(repopath)
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
            repo = pygit2.Repository(repopath)
    Pierre-Yves Chibon f66c8c
            first_commit = repo.revparse_single('HEAD')
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
            text = 'Cf commit %s' % first_commit.oid.hex
    Pierre-Yves Chibon f66c8c
            exp = '

    Cf commit {1}'\

    Pierre-Yves Chibon f66c8c
            '

    '.format(first_commit.oid.hex, first_commit.oid.hex[:7])
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon f66c8c
            with pagure.APP.app_context():
    Pierre-Yves Chibon f66c8c
                data = {
    Pierre-Yves Chibon f66c8c
                    'content': text,
    Pierre-Yves Chibon f66c8c
                    'csrf_token': csrf_token,
    Pierre-Yves Chibon f66c8c
                }
    Pierre-Yves Chibon f66c8c
                output = self.app.post('/markdown/?repo=test', data=data)
    Pierre-Yves Chibon f66c8c
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon f66c8c
                self.assertEqual(exp, output.data)
    Pierre-Yves Chibon f66c8c
    Pierre-Yves Chibon c87f24
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon c87f24
        def test_remove_user_email(self, ast):
    Pierre-Yves Chibon c87f24
            """ Test the remove_user_email endpoint. """
    Pierre-Yves Chibon c87f24
            ast.return_value = False
    Pierre-Yves Chibon c87f24
            self.test_new_project()
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
            user = tests.FakeUser()
    Pierre-Yves Chibon c87f24
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon c87f24
                output = self.app.post('/settings/email/drop')
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon c87f24
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
            user.username = 'foo'
    Pierre-Yves Chibon c87f24
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon c87f24
                output = self.app.post('/settings/')
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'</textarea>
    Pierre-Yves Chibon 64f5f4
                    '', output.data)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                csrf_token = output.data.split(
    Pierre-Yves Chibon c87f24
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                data = {
    Pierre-Yves Chibon c87f24
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon c87f24
                }
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                output = self.app.post(
    Pierre-Yves Chibon c87f24
                    '/settings/email/drop', data=data, follow_redirects=True)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon c87f24
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
    Pierre-Yves Chibon 64f5f4
                    '', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      You must always have at least one email',
    Pierre-Yves Chibon c87f24
                    output.data)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
            user.username = 'pingou'
    Pierre-Yves Chibon c87f24
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon c87f24
                output = self.app.post('/settings/')
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '<textarea class="form-control form-control-error" id="ssh_key" name="ssh_key">'</textarea>
    Pierre-Yves Chibon 64f5f4
                    '', output.data)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                csrf_token = output.data.split(
    Pierre-Yves Chibon c87f24
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                data = {
    Pierre-Yves Chibon c87f24
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon c87f24
                }
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                output = self.app.post(
    Pierre-Yves Chibon c87f24
                    '/settings/email/drop', data=data, follow_redirects=True)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                data = {
    Pierre-Yves Chibon c87f24
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon c87f24
                    'email': 'foobar@pingou.com',
    Pierre-Yves Chibon c87f24
                }
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                output = self.app.post(
    Pierre-Yves Chibon c87f24
                    '/settings/email/drop', data=data, follow_redirects=True)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon c87f24
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      You do not have the '
    Pierre-Yves Chibon 64f5f4
                    'email: foobar@pingou.com, nothing to remove', output.data)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                data = {
    Pierre-Yves Chibon c87f24
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon c87f24
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon c87f24
                }
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                output = self.app.post(
    Pierre-Yves Chibon c87f24
                    '/settings/email/drop', data=data, follow_redirects=True)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.data.count('foo@pingou.com'), 0)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.data.count('bar@pingou.com'), 3)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                output = self.app.post(
    Pierre-Yves Chibon c87f24
                    '/settings/email/drop', data=data, follow_redirects=True)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.data.count('foo@pingou.com'), 0)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.data.count('bar@pingou.com'), 3)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon c87f24
                ast.return_value = True
    Pierre-Yves Chibon c87f24
                output = self.app.post('/settings/email/drop', data=data)
    Pierre-Yves Chibon c87f24
                self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon c87f24
    Pierre-Yves Chibon ca16bb
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon ca16bb
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 2c7572
        def test_add_api_user_email(self, ast, send_email):
    Pierre-Yves Chibon 2c7572
            """ Test the add_api_user_email endpoint. """
    Pierre-Yves Chibon ca16bb
            send_email.return_value = True
    Pierre-Yves Chibon ca16bb
            ast.return_value = False
    Pierre-Yves Chibon ca16bb
            self.test_new_project()
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
            user = tests.FakeUser()
    Pierre-Yves Chibon ca16bb
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon ca16bb
                output = self.app.post('/settings/email/add')
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon ca16bb
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
            user.username = 'foo'
    Pierre-Yves Chibon ca16bb
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon ca16bb
                output = self.app.post('/settings/email/add')
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
    Pierre-Yves Chibon 64f5f4
                self.assertTrue("Add new email" in output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    Pierre-Yves Chibon 64f5f4
                    'name="email" type="text" value="">', output.data)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
            user.username = 'pingou'
    Pierre-Yves Chibon ca16bb
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon ca16bb
                output = self.app.post('/settings/email/add')
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertTrue("Add new email" in output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    Pierre-Yves Chibon 64f5f4
                    'name="email" type="text" value="">', output.data)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                csrf_token = output.data.split(
    Pierre-Yves Chibon ca16bb
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                data = {
    Pierre-Yves Chibon 64f5f4
                    'email': 'foo2@pingou.com',
    Pierre-Yves Chibon ca16bb
                }
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                output = self.app.post(
    Pierre-Yves Chibon ca16bb
                    '/settings/email/add', data=data, follow_redirects=True)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertTrue("Add new email" in output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertEqual(output.data.count('foo2@pingou.com'), 1)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                # New email
    Pierre-Yves Chibon ca16bb
                data = {
    Pierre-Yves Chibon ca16bb
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon ca16bb
                    'email': 'foobar@pingou.com',
    Pierre-Yves Chibon ca16bb
                }
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                output = self.app.post(
    Pierre-Yves Chibon ca16bb
                    '/settings/email/add', data=data, follow_redirects=True)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon ca16bb
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Email pending validation',
    Pierre-Yves Chibon ca16bb
                    output.data)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.data.count('bar@pingou.com'), 5)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.data.count('foobar@pingou.com'), 2)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon 15ef6d
                # Email already pending
    Pierre-Yves Chibon 15ef6d
                output = self.app.post(
    Pierre-Yves Chibon 15ef6d
                    '/settings/email/add', data=data, follow_redirects=True)
    Pierre-Yves Chibon 15ef6d
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 15ef6d
                self.assertIn(
    Pierre-Yves Chibon 15ef6d
                    '
    \n '
    Pierre-Yves Chibon 15ef6d
                    'Add new email', output.data)
    Pierre-Yves Chibon 15ef6d
                self.assertIn(
    Pierre-Yves Chibon 15ef6d
                    '\n                      This email is already '
    Pierre-Yves Chibon 15ef6d
                    'pending confirmation', output.data)
    Pierre-Yves Chibon 15ef6d
    Pierre-Yves Chibon ca16bb
                # User already has this email
    Pierre-Yves Chibon ca16bb
                data = {
    Pierre-Yves Chibon ca16bb
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon ca16bb
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon ca16bb
                }
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                output = self.app.post(
    Pierre-Yves Chibon ca16bb
                    '/settings/email/add', data=data, follow_redirects=True)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertTrue("Add new email" in output.data)
    Pierre-Yves Chibon 8558c8
                self.assertTrue(
    Pierre-Yves Chibon 64f5f4
                    'Invalid value, can't be any of: bar@pingou.com, '
    Pierre-Yves Chibon 8558c8
                    'foo@pingou.com. ' in output.data
    Pierre-Yves Chibon 8558c8
                    or
    Pierre-Yves Chibon 8558c8
                    'Invalid value, can't be any of: foo@pingou.com, '
    Pierre-Yves Chibon 8558c8
                    'bar@pingou.com. ' in output.data
    Pierre-Yves Chibon 8558c8
                )
    Pierre-Yves Chibon 15ef6d
                self.assertEqual(output.data.count('foo@pingou.com'), 6)
    Pierre-Yves Chibon 15ef6d
                self.assertEqual(output.data.count('bar@pingou.com'), 5)
    Pierre-Yves Chibon 64f5f4
                self.assertEqual(output.data.count('foobar@pingou.com'), 0)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                # Email registered by someone else
    Pierre-Yves Chibon ca16bb
                data = {
    Pierre-Yves Chibon ca16bb
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon ca16bb
                    'email': 'foo@bar.com',
    Pierre-Yves Chibon ca16bb
                }
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                output = self.app.post(
    Pierre-Yves Chibon ca16bb
                    '/settings/email/add', data=data, follow_redirects=True)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertTrue("Add new email" in output.data)
    Pierre-Yves Chibon ca16bb
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    'Invalid value, can't be any of: foo@bar.com. ',
    Pierre-Yves Chibon 64f5f4
                    output.data)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon ca16bb
                ast.return_value = True
    Pierre-Yves Chibon ca16bb
                output = self.app.post('/settings/email/add', data=data)
    Pierre-Yves Chibon ca16bb
                self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon ca16bb
    Pierre-Yves Chibon 5cae35
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 5cae35
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 5cae35
        def test_set_default_email(self, ast, send_email):
    Pierre-Yves Chibon 5cae35
            """ Test the set_default_email endpoint. """
    Pierre-Yves Chibon 5cae35
            send_email.return_value = True
    Pierre-Yves Chibon 5cae35
            ast.return_value = False
    Pierre-Yves Chibon 5cae35
            self.test_new_project()
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
            user = tests.FakeUser()
    Pierre-Yves Chibon 5cae35
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 5cae35
                output = self.app.post('/settings/email/default')
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 5cae35
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
            user.username = 'pingou'
    Pierre-Yves Chibon 5cae35
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 5cae35
                output = self.app.get('/settings/')
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
    Pierre-Yves Chibon 64f5f4
                    '', output.data)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                csrf_token = output.data.split(
    Pierre-Yves Chibon 5cae35
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                data = {
    Pierre-Yves Chibon 5cae35
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon 5cae35
                }
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                output = self.app.post(
    Pierre-Yves Chibon 5cae35
                    '/settings/email/default', data=data, follow_redirects=True)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                # Set invalid default email
    Pierre-Yves Chibon 5cae35
                data = {
    Pierre-Yves Chibon 5cae35
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon 5cae35
                    'email': 'foobar@pingou.com',
    Pierre-Yves Chibon 5cae35
                }
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                output = self.app.post(
    Pierre-Yves Chibon 5cae35
                    '/settings/email/default', data=data, follow_redirects=True)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 5cae35
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      You do not have the '
    Pierre-Yves Chibon 64f5f4
                    'email: foobar@pingou.com, nothing to set',
    Pierre-Yves Chibon 64f5f4
                    output.data)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                # Set default email
    Pierre-Yves Chibon 5cae35
                data = {
    Pierre-Yves Chibon 5cae35
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon 5cae35
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon 5cae35
                }
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                output = self.app.post(
    Pierre-Yves Chibon 5cae35
                    '/settings/email/default', data=data, follow_redirects=True)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 64f5f4
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 5cae35
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Default email set to: '
    Pierre-Yves Chibon 64f5f4
                    'foo@pingou.com', output.data)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 5cae35
                ast.return_value = True
    Pierre-Yves Chibon 5cae35
                output = self.app.post('/settings/email/default', data=data)
    Pierre-Yves Chibon 5cae35
                self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 5cae35
    Pierre-Yves Chibon 496605
        @patch('pagure.lib.notify.send_email')
    Pierre-Yves Chibon 496605
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 496605
        def test_reconfirm_email(self, ast, send_email):
    Pierre-Yves Chibon 496605
            """ Test the reconfirm_email endpoint. """
    Pierre-Yves Chibon 496605
            send_email.return_value = True
    Pierre-Yves Chibon 496605
            ast.return_value = False
    Pierre-Yves Chibon 496605
            self.test_new_project()
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
            # Add a pending email to pingou
    Pierre-Yves Chibon 496605
            userobj = pagure.lib.search_user(self.session, username='pingou')
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
            self.assertEqual(len(userobj.emails), 2)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
            email_pend = pagure.lib.model.UserEmailPending(
    Pierre-Yves Chibon 496605
                user_id=userobj.id,
    Pierre-Yves Chibon 496605
                email='foo@fp.o',
    Pierre-Yves Chibon 496605
                token='abcdef',
    Pierre-Yves Chibon 496605
            )
    Pierre-Yves Chibon 496605
            self.session.add(email_pend)
    Pierre-Yves Chibon 496605
            self.session.commit()
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
            user = tests.FakeUser()
    Pierre-Yves Chibon 496605
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 496605
                output = self.app.post('/settings/email/resend')
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 496605
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
            user.username = 'pingou'
    Pierre-Yves Chibon 496605
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 496605
                output = self.app.get('/settings/')
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 496605
                    '      ', output.data)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '<textarea class="form-control" id="ssh_key" name="ssh_key">'</textarea>
    Pierre-Yves Chibon 496605
                    '', output.data)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                csrf_token = output.data.split(
    Pierre-Yves Chibon 496605
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                data = {
    Pierre-Yves Chibon 496605
                    'email': 'foo@pingou.com',
    Pierre-Yves Chibon 496605
                }
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                output = self.app.post(
    Pierre-Yves Chibon 496605
                    '/settings/email/resend', data=data, follow_redirects=True)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 496605
                    '      ', output.data)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                # Set invalid default email
    Pierre-Yves Chibon 496605
                data = {
    Pierre-Yves Chibon 496605
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon 496605
                    'email': 'foobar@pingou.com',
    Pierre-Yves Chibon 496605
                }
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                output = self.app.post(
    Pierre-Yves Chibon 496605
                    '/settings/email/resend', data=data, follow_redirects=True)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 496605
                    '      ', output.data)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 37f6c2
                    '\n                      This email address has '
    Pierre-Yves Chibon 37f6c2
                    'already been confirmed', output.data)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                # Validate a non-validated email
    Pierre-Yves Chibon 496605
                data = {
    Pierre-Yves Chibon 496605
                    'csrf_token':  csrf_token,
    Pierre-Yves Chibon 496605
                    'email': 'foo@fp.o',
    Pierre-Yves Chibon 496605
                }
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                output = self.app.post(
    Pierre-Yves Chibon 496605
                    '/settings/email/resend', data=data, follow_redirects=True)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 496605
                    '      ', output.data)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.data.count('foo@pingou.com'), 4)
    Pierre-Yves Chibon 496605
                self.assertIn(
    Pierre-Yves Chibon 496605
                    '\n                      Confirmation email re-sent',
    Pierre-Yves Chibon 496605
                    output.data)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 496605
                ast.return_value = True
    Pierre-Yves Chibon 496605
                output = self.app.post('/settings/email/resend', data=data)
    Pierre-Yves Chibon 496605
                self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 496605
    Pierre-Yves Chibon 2497e3
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 2497e3
        def test_confirm_email(self, ast):
    Pierre-Yves Chibon 2497e3
            """ Test the confirm_email endpoint. """
    Pierre-Yves Chibon 2497e3
            output = self.app.get('/settings/email/confirm/foobar')
    Pierre-Yves Chibon 2497e3
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            ast.return_value = False
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            # Add a pending email to pingou
    Pierre-Yves Chibon 2497e3
            userobj = pagure.lib.search_user(self.session, username='pingou')
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            self.assertEqual(len(userobj.emails), 2)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            email_pend = pagure.lib.model.UserEmailPending(
    Pierre-Yves Chibon 2497e3
                user_id=userobj.id,
    Pierre-Yves Chibon 2497e3
                email='foo@fp.o',
    Pierre-Yves Chibon 2497e3
                token='abcdef',
    Pierre-Yves Chibon 2497e3
            )
    Pierre-Yves Chibon 2497e3
            self.session.add(email_pend)
    Pierre-Yves Chibon 2497e3
            self.session.commit()
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            user = tests.FakeUser()
    Pierre-Yves Chibon 2497e3
            user.username = 'pingou'
    Pierre-Yves Chibon 2497e3
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 2497e3
                # Wrong token
    Pierre-Yves Chibon 2497e3
                output = self.app.get(
    Pierre-Yves Chibon 2497e3
                    '/settings/email/confirm/foobar', follow_redirects=True)
    Pierre-Yves Chibon 2497e3
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 2497e3
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 2497e3
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      No email associated with this token.',
    Pierre-Yves Chibon 2497e3
                    output.data)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
                # Confirm email
    Pierre-Yves Chibon 2497e3
                output = self.app.get(
    Pierre-Yves Chibon 2497e3
                    '/settings/email/confirm/abcdef', follow_redirects=True)
    Pierre-Yves Chibon 2497e3
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 2497e3
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '
    \n Basic Information\n'
    Pierre-Yves Chibon 64f5f4
                    '      ', output.data)
    Pierre-Yves Chibon 2497e3
                self.assertIn(
    Pierre-Yves Chibon 64f5f4
                    '\n                      Email validated',
    Pierre-Yves Chibon 64f5f4
                    output.data)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            userobj = pagure.lib.search_user(self.session, username='pingou')
    Pierre-Yves Chibon 2497e3
            self.assertEqual(len(userobj.emails), 3)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon 2497e3
            ast.return_value = True
    Pierre-Yves Chibon 2497e3
            output = self.app.get('/settings/email/confirm/foobar')
    Pierre-Yves Chibon 2497e3
            self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 2497e3
    Pierre-Yves Chibon cfad13
    Jeremy Cline babe37
        def test_view_my_requests_no_user(self):
    Pierre-Yves Chibon ce6f30
            """Test the view_user_requests endpoint."""
    Jeremy Cline babe37
            output = self.app.get('/user/somenonexistentuser/requests')
    Jeremy Cline babe37
            self.assertEqual(output.status_code, 404)
    Jeremy Cline babe37
    Pierre-Yves Chibon ce6f30
        @patch(
    Pierre-Yves Chibon ce6f30
            'pagure.lib.git.update_git', MagicMock(return_value=True))
    Pierre-Yves Chibon ce6f30
        @patch(
    Pierre-Yves Chibon ce6f30
            'pagure.lib.notify.send_email', MagicMock(return_value=True))
    Jeremy Cline babe37
        def test_view_my_requests(self):
    Pierre-Yves Chibon ce6f30
            """Test the view_user_requests endpoint. """
    Pierre-Yves Chibon ce6f30
            # Create the PR
    Pierre-Yves Chibon ce6f30
            tests.create_projects(self.session)
    Farhaan Bukhsh 940880
            repo = pagure.lib._get_project(self.session, 'test')
    Pierre-Yves Chibon ce6f30
            req = pagure.lib.new_pull_request(
    Pierre-Yves Chibon ce6f30
                session=self.session,
    Pierre-Yves Chibon ce6f30
                repo_from=repo,
    Pierre-Yves Chibon ce6f30
                branch_from='dev',
    Pierre-Yves Chibon ce6f30
                repo_to=repo,
    Pierre-Yves Chibon ce6f30
                branch_to='master',
    Pierre-Yves Chibon ce6f30
                title='test pull-request #1',
    Pierre-Yves Chibon ce6f30
                user='pingou',
    Pierre-Yves Chibon ce6f30
                requestfolder=None,
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.commit()
    Pierre-Yves Chibon ce6f30
            self.assertEqual(req.id, 1)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(req.title, 'test pull-request #1')
    Pierre-Yves Chibon ce6f30
    Jeremy Cline 6492c1
            output = self.app.get('/user/pingou/requests')
    Jeremy Cline 6492c1
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon ce6f30
            self.assertIn('test pull-request #1', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(
    Pierre-Yves Chibon ce6f30
                output.data.count('
    Pierre-Yves Chibon ce6f30
                1)
    Jeremy Cline babe37
    Pierre-Yves Chibon ce6f30
            # Add a PR in a fork
    Pierre-Yves Chibon ce6f30
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon ce6f30
                user_id=1,  # pingou
    Pierre-Yves Chibon ce6f30
                name='test_fork',
    Pierre-Yves Chibon ce6f30
                description='test project #1',
    Pierre-Yves Chibon ce6f30
                is_fork=True,
    Pierre-Yves Chibon ce6f30
                parent_id=1,
    Pierre-Yves Chibon ce6f30
                hook_token='aaabbbttt',
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.add(item)
    Farhaan Bukhsh 940880
            repo = pagure.lib._get_project(
    Pierre-Yves Chibon ce6f30
                self.session, 'test_fork', user='pingou')
    Pierre-Yves Chibon ce6f30
    Pierre-Yves Chibon ce6f30
            req = pagure.lib.new_pull_request(
    Pierre-Yves Chibon ce6f30
                session=self.session,
    Pierre-Yves Chibon ce6f30
                repo_from=repo,
    Pierre-Yves Chibon ce6f30
                branch_from='dev',
    Pierre-Yves Chibon ce6f30
                repo_to=repo,
    Pierre-Yves Chibon ce6f30
                branch_to='master',
    Pierre-Yves Chibon ce6f30
                title='test pull-request #2',
    Pierre-Yves Chibon ce6f30
                user='pingou',
    Pierre-Yves Chibon ce6f30
                requestfolder=None,
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.commit()
    Pierre-Yves Chibon ce6f30
            self.assertEqual(req.id, 1)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(req.title, 'test pull-request #2')
    Jeremy Cline babe37
    Pierre-Yves Chibon ce6f30
            output = self.app.get('/user/pingou/requests')
    Pierre-Yves Chibon ce6f30
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon ce6f30
            self.assertIn('test pull-request #1', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertIn('test pull-request #2', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(
    Pierre-Yves Chibon ce6f30
                output.data.count('
    Pierre-Yves Chibon ce6f30
                2)
    Jeremy Cline babe37
    Jeremy Cline babe37
        def test_view_my_issues_no_user(self):
    Pierre-Yves Chibon ce6f30
            """Test the view_user_issues endpoint with a missing user."""
    Jeremy Cline babe37
            output = self.app.get('/user/somenonexistentuser/issues')
    Jeremy Cline babe37
            self.assertEqual(output.status_code, 404)
    Jeremy Cline babe37
    Pierre-Yves Chibon ce6f30
        @patch(
    Pierre-Yves Chibon ce6f30
            'pagure.lib.git.update_git', MagicMock(return_value=True))
    Pierre-Yves Chibon ce6f30
        @patch(
    Pierre-Yves Chibon ce6f30
            'pagure.lib.notify.send_email', MagicMock(return_value=True))
    Jeremy Cline babe37
        def test_view_my_issues(self):
    Pierre-Yves Chibon ce6f30
            """Test the view_user_issues endpoint when the user exists."""
    Pierre-Yves Chibon ce6f30
            # Create the issue
    Pierre-Yves Chibon ce6f30
            tests.create_projects(self.session)
    Farhaan Bukhsh 940880
            repo = pagure.lib._get_project(self.session, 'test')
    Pierre-Yves Chibon ce6f30
            msg = pagure.lib.new_issue(
    Pierre-Yves Chibon ce6f30
                session=self.session,
    Pierre-Yves Chibon ce6f30
                repo=repo,
    Pierre-Yves Chibon ce6f30
                title='Test issue #1',
    Pierre-Yves Chibon ce6f30
                content='We should work on this for the second time',
    Pierre-Yves Chibon ce6f30
                user='pingou',
    Pierre-Yves Chibon ce6f30
                status='Open',
    Pierre-Yves Chibon ce6f30
                ticketfolder=None
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.commit()
    Pierre-Yves Chibon ce6f30
            self.assertEqual(msg.title, 'Test issue #1')
    Pierre-Yves Chibon ce6f30
    Pierre-Yves Chibon ce6f30
            output = self.app.get('/user/pingou/issues')
    Pierre-Yves Chibon ce6f30
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon ce6f30
            self.assertIn('Test issue #1', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(
    Pierre-Yves Chibon ce6f30
                output.data.count('
    Pierre-Yves Chibon ce6f30
                1)
    Pierre-Yves Chibon ce6f30
    Pierre-Yves Chibon ce6f30
            # Add an issue in a fork
    Pierre-Yves Chibon ce6f30
            item = pagure.lib.model.Project(
    Pierre-Yves Chibon ce6f30
                user_id=2,  # foo
    Pierre-Yves Chibon ce6f30
                name='test_fork',
    Pierre-Yves Chibon ce6f30
                description='test project #1',
    Pierre-Yves Chibon ce6f30
                is_fork=True,
    Pierre-Yves Chibon ce6f30
                parent_id=1,
    Pierre-Yves Chibon ce6f30
                hook_token='aaabbbttt',
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.add(item)
    Farhaan Bukhsh 940880
            repo = pagure.lib._get_project(self.session, 'test_fork', user='foo')
    Pierre-Yves Chibon ce6f30
    Pierre-Yves Chibon ce6f30
            msg = pagure.lib.new_issue(
    Pierre-Yves Chibon ce6f30
                session=self.session,
    Pierre-Yves Chibon ce6f30
                repo=repo,
    Pierre-Yves Chibon ce6f30
                title='Test issue #2',
    Pierre-Yves Chibon ce6f30
                content='We should work on this for the second time',
    Pierre-Yves Chibon ce6f30
                user='pingou',
    Pierre-Yves Chibon ce6f30
                status='Open',
    Pierre-Yves Chibon ce6f30
                ticketfolder=None
    Pierre-Yves Chibon ce6f30
            )
    Pierre-Yves Chibon ce6f30
            self.session.commit()
    Pierre-Yves Chibon ce6f30
            self.assertEqual(msg.title, 'Test issue #2')
    Pierre-Yves Chibon ce6f30
    Mark Reynolds 300f0f
            # Test the assigned issue table.  Create issue then set the assignee
    Mark Reynolds 300f0f
            msg = pagure.lib.new_issue(
    Mark Reynolds 300f0f
                session=self.session,
    Mark Reynolds 300f0f
                repo=repo,
    Mark Reynolds 300f0f
                title='Test issue #3',
    Mark Reynolds 300f0f
                content='This issue created by foo, but assigned to pingou',
    Mark Reynolds 300f0f
                user='foo',
    Mark Reynolds 300f0f
                status='Open',
    Mark Reynolds 300f0f
                ticketfolder=None
    Mark Reynolds 300f0f
            )
    Mark Reynolds 300f0f
            self.session.commit()
    Mark Reynolds 300f0f
            self.assertEqual(msg.title, 'Test issue #3')
    Mark Reynolds 300f0f
    Mark Reynolds eabdc8
            msg = pagure.lib.add_issue_assignee(
    Mark Reynolds 300f0f
                session=self.session,
    Mark Reynolds 300f0f
                issue=msg,
    Mark Reynolds 300f0f
                assignee='pingou',
    Mark Reynolds 300f0f
                user='foo',
    Pierre-Yves Chibon 5b8052
                ticketfolder=None)
    Mark Reynolds 300f0f
            self.session.commit()
    Pierre-Yves Chibon 5b8052
            self.assertEqual(msg, 'Issue assigned to pingou')
    Pierre-Yves Chibon ce6f30
    Jeremy Cline 6492c1
            output = self.app.get('/user/pingou/issues')
    Jeremy Cline 6492c1
            self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon ce6f30
            self.assertIn('Test issue #1', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertIn('Test issue #2', output.data)
    Mark Reynolds 300f0f
            self.assertIn('Test issue #3', output.data)
    Pierre-Yves Chibon ce6f30
            self.assertEqual(
    Mark Reynolds 300f0f
                output.data.count('
    Jeremy Cline babe37
    Vivek Anand 611221
        def test_view_my_issues_tickets_turned_off(self):
    Vivek Anand 611221
            """Test the view_user_issues endpoint when the user exists and
    Vivek Anand 611221
            and ENABLE_TICKETS is False """
    Vivek Anand 611221
    Vivek Anand 611221
            # Turn off the tickets instance wide
    Vivek Anand 611221
            pagure.APP.config['ENABLE_TICKETS'] = False
    Vivek Anand 611221
    Vivek Anand 611221
            output = self.app.get('/user/pingou/issues')
    Vivek Anand 611221
            self.assertEqual(output.status_code, 404)
    Vivek Anand a98392
            pagure.APP.config['ENABLE_TICKETS'] = True
    Vivek Anand 611221
    Pierre-Yves Chibon 170fc9
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 170fc9
        def test_add_user_token(self, ast):
    Pierre-Yves Chibon 170fc9
            """ Test the add_user_token endpoint. """
    Pierre-Yves Chibon 170fc9
            ast.return_value = False
    Pierre-Yves Chibon 170fc9
            self.test_new_project()
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
            user = tests.FakeUser()
    Pierre-Yves Chibon 170fc9
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 170fc9
                output = self.app.get('/settings/token/new/')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 170fc9
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
            user.username = 'foo'
    Pierre-Yves Chibon 170fc9
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 170fc9
                output = self.app.get('/settings/token/new')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '
    \n '
    Pierre-Yves Chibon 170fc9
                    'Create a new token\n', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<input name="acls" type="checkbox" value="create_project">',
    Pierre-Yves Chibon 170fc9
                    output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                csrf_token = output.data.split(
    Pierre-Yves Chibon 170fc9
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                data = {
    Pierre-Yves Chibon 170fc9
                    'acls': ['create_project', 'fork_project']
    Pierre-Yves Chibon 170fc9
                }
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # missing CSRF
    Pierre-Yves Chibon 170fc9
                output = self.app.post('/settings/token/new', data=data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<title>Create token - Pagure</title>', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '
    \n '
    Pierre-Yves Chibon 170fc9
                    'Create a new token\n', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<input name="acls" type="checkbox" value="create_project">',
    Pierre-Yves Chibon 170fc9
                    output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                data = {
    Pierre-Yves Chibon 170fc9
                    'acls': ['new_project'],
    Pierre-Yves Chibon 170fc9
                    'csrf_token':  csrf_token
    Pierre-Yves Chibon 170fc9
                }
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # Invalid ACLs
    Pierre-Yves Chibon 170fc9
                output = self.app.post('/settings/token/new', data=data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<title>Create token - Pagure</title>', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '
    \n '
    Pierre-Yves Chibon 170fc9
                    'Create a new token\n', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<input name="acls" type="checkbox" value="create_project">',
    Pierre-Yves Chibon 170fc9
                    output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                data = {
    Pierre-Yves Chibon 170fc9
                    'acls': ['create_project', 'fork_project'],
    Pierre-Yves Chibon 170fc9
                    'csrf_token':  csrf_token
    Pierre-Yves Chibon 170fc9
                }
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # All good
    Pierre-Yves Chibon 170fc9
                output = self.app.post(
    Pierre-Yves Chibon 170fc9
                    '/settings/token/new', data=data, follow_redirects=True)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '<title>foo\'s settings - Pagure</title>', output.data)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    '\n                      Token created\n',
    Pierre-Yves Chibon 170fc9
                    output.data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(
    Pierre-Yves Chibon 170fc9
                    output.data.count(
    Pierre-Yves Chibon 170fc9
                        'Valid'
    Pierre-Yves Chibon 170fc9
                        ' until: '), 1)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                ast.return_value = True
    Pierre-Yves Chibon 170fc9
                output = self.app.get('/settings/token/new')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 302)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
        @patch('pagure.ui.app.admin_session_timedout')
    Pierre-Yves Chibon 170fc9
        def test_revoke_api_user_token(self, ast):
    Pierre-Yves Chibon 170fc9
            """ Test the revoke_api_user_token endpoint. """
    Pierre-Yves Chibon 170fc9
            ast.return_value = False
    Pierre-Yves Chibon 170fc9
            self.test_new_project()
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
            user = tests.FakeUser()
    Pierre-Yves Chibon 170fc9
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 170fc9
                # Token doesn't exist
    Pierre-Yves Chibon 170fc9
                output = self.app.post('/settings/token/revoke/foobar')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 170fc9
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # Create the foobar API token but associated w/ the user 'foo'
    Pierre-Yves Chibon 170fc9
                item = pagure.lib.model.Token(
    Pierre-Yves Chibon 170fc9
                    id='foobar',
    Pierre-Yves Chibon 170fc9
                    user_id=2,  # foo
    Pierre-Yves Chibon 170fc9
                    expiration=datetime.datetime.utcnow() \
    Pierre-Yves Chibon 170fc9
                        + datetime.timedelta(days=30)
    Pierre-Yves Chibon 170fc9
                )
    Pierre-Yves Chibon 170fc9
                self.session.add(item)
    Pierre-Yves Chibon 170fc9
                self.session.commit()
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # Token not associated w/ this user
    Pierre-Yves Chibon 170fc9
                output = self.app.post('/settings/token/revoke/foobar')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 404)
    Pierre-Yves Chibon 170fc9
                self.assertTrue('

    Page not found (404)

    ' in output.data)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
            user.username = 'foo'
    Pierre-Yves Chibon 170fc9
            with tests.user_set(pagure.APP, user):
    Pierre-Yves Chibon 170fc9
                # Missing CSRF token
    Pierre-Yves Chibon 170fc9
                output = self.app.post(
    Pierre-Yves Chibon 170fc9
                    '/settings/token/revoke/foobar', follow_redirects=True)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    "<title>foo's settings - Pagure</title>", output.data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(
    Pierre-Yves Chibon 170fc9
                    output.data.count(
    Pierre-Yves Chibon 170fc9
                        'Valid'
    Pierre-Yves Chibon 170fc9
                        ' until: '), 1)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                csrf_token = output.data.split(
    Pierre-Yves Chibon 170fc9
                    'name="csrf_token" type="hidden" value="')[1].split('">')[0]
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                data = {
    Pierre-Yves Chibon 170fc9
                    'csrf_token': csrf_token
    Pierre-Yves Chibon 170fc9
                }
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # All good - token is deleted
    Pierre-Yves Chibon 170fc9
                output = self.app.post(
    Pierre-Yves Chibon 170fc9
                    '/settings/token/revoke/foobar', data=data,
    Pierre-Yves Chibon 170fc9
                    follow_redirects=True)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    "<title>foo's settings - Pagure</title>", output.data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(
    Pierre-Yves Chibon 170fc9
                    output.data.count(
    Pierre-Yves Chibon 170fc9
                        'Valid'
    Pierre-Yves Chibon 170fc9
                        ' until: '), 0)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                user = pagure.lib.get_user(self.session, key='foo')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(len(user.tokens), 1)
    Pierre-Yves Chibon 170fc9
                expiration_dt = user.tokens[0].expiration
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # Token was already deleted - no changes
    Pierre-Yves Chibon 170fc9
                output = self.app.post(
    Pierre-Yves Chibon 170fc9
                    '/settings/token/revoke/foobar', data=data,
    Pierre-Yves Chibon 170fc9
                    follow_redirects=True)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 200)
    Pierre-Yves Chibon 170fc9
                self.assertIn(
    Pierre-Yves Chibon 170fc9
                    "<title>foo's settings - Pagure</title>", output.data)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(
    Pierre-Yves Chibon 170fc9
                    output.data.count(
    Pierre-Yves Chibon 170fc9
                        'Valid'
    Pierre-Yves Chibon 170fc9
                        ' until: '), 0)
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                # Ensure the expiration date did not change
    Pierre-Yves Chibon 170fc9
                user = pagure.lib.get_user(self.session, key='foo')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(len(user.tokens), 1)
    Pierre-Yves Chibon 170fc9
                self.assertEqual(
    Pierre-Yves Chibon 170fc9
                    expiration_dt, user.tokens[0].expiration
    Pierre-Yves Chibon 170fc9
                )
    Pierre-Yves Chibon 170fc9
    Pierre-Yves Chibon 170fc9
                ast.return_value = True
    Pierre-Yves Chibon 170fc9
                output = self.app.get('/settings/token/new')
    Pierre-Yves Chibon 170fc9
                self.assertEqual(output.status_code, 302)
    Jeremy Cline babe37
    Pierre-Yves Chibon cfad13
    if __name__ == '__main__':
    Pierre-Yves Chibon 170fc9
        unittest.main(verbosity=2)