Blame tests/test_pagure_flask_api_project_update_watch.py

Pierre-Yves Chibon f778bd
# -*- coding: utf-8 -*-
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
"""
Pierre-Yves Chibon f778bd
 (c) 2018 - Copyright Red Hat Inc
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
 Authors:
Pierre-Yves Chibon f778bd
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
"""
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
import copy
Pierre-Yves Chibon f778bd
import datetime
Pierre-Yves Chibon f778bd
import unittest
Pierre-Yves Chibon f778bd
import shutil
Pierre-Yves Chibon f778bd
import sys
Pierre-Yves Chibon f778bd
import time
Pierre-Yves Chibon f778bd
import os
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
import json
Pierre-Yves Chibon f778bd
from mock import patch, MagicMock
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon f778bd
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon f778bd
import tests
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
class PagureFlaskApiProjectUpdateWatchTests(tests.Modeltests):
Pierre-Yves Chibon f778bd
    """ Tests for the flask API of pagure for changing the watch status on
Pierre-Yves Chibon f778bd
    a project via the API
Pierre-Yves Chibon f778bd
    """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    @patch('pagure.lib.notify.send_email', MagicMock(return_value=True))
Pierre-Yves Chibon f778bd
    def setUp(self):
Pierre-Yves Chibon f778bd
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon f778bd
        super(PagureFlaskApiProjectUpdateWatchTests, self).setUp()
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        tests.create_projects(self.session)
Pierre-Yves Chibon f778bd
        tests.create_projects_git(os.path.join(self.path, 'tickets'))
Pierre-Yves Chibon f778bd
        tests.create_tokens(self.session)
Pierre-Yves Chibon f778bd
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        # Create normal issue
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon f778bd
            session=self.session,
Pierre-Yves Chibon f778bd
            repo=repo,
Pierre-Yves Chibon f778bd
            title='Test issue #1',
Pierre-Yves Chibon f778bd
            content='We should work on this',
Pierre-Yves Chibon f778bd
            user='pingou',
Pierre-Yves Chibon f778bd
            private=False,
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
        self.session.commit()
Pierre-Yves Chibon f778bd
        self.assertEqual(msg.title, 'Test issue #1')
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        # Create project-less token for user foo
Pierre-Yves Chibon f778bd
        item = pagure.lib.model.Token(
Pierre-Yves Chibon f778bd
            id='project-less-foo',
Pierre-Yves Chibon f778bd
            user_id=1,
Pierre-Yves Chibon f778bd
            project_id=None,
Pierre-Yves Chibon f778bd
            expiration=datetime.datetime.utcnow()
Pierre-Yves Chibon f778bd
            + datetime.timedelta(days=30)
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
        self.session.add(item)
Pierre-Yves Chibon f778bd
        self.session.commit()
Pierre-Yves Chibon f778bd
        tests.create_tokens_acl(self.session, token_id='project-less-foo')
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_invalid_project(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        # Invalid project
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/foobar/watchers/update', headers=headers)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
              "error": "Project not found",
Pierre-Yves Chibon f778bd
              "error_code": "ENOPROJECT",
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_change_status_issue_token_not_for_project(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        # Valid token, wrong project
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test2/watchers/update', headers=headers)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.name,
Pierre-Yves Chibon f778bd
                         data['error_code'])
Pierre-Yves Chibon f778bd
        self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data['error'])
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_no_user_watching(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'status': '42',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'Invalid or incomplete input submitted',
Pierre-Yves Chibon f778bd
                u'error_code': u'EINVALIDREQ'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_no_watch_status(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'pingou',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'The watch value of "None" is invalid',
Pierre-Yves Chibon f778bd
                u'error_code': u'ENOCODE'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_invalid_status(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'pingou',
Pierre-Yves Chibon f778bd
            'status': '42',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'The watch value of "42" is invalid',
Pierre-Yves Chibon f778bd
                u'error_code': u'ENOCODE'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_invalid_user(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'example',
Pierre-Yves Chibon f778bd
            'status': '2',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'You are not allowed to modify this project',
Pierre-Yves Chibon f778bd
                u'error_code': u'EMODIFYPROJECTNOTALLOWED'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_other_user(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'foo',
Pierre-Yves Chibon f778bd
            'status': '2',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 401)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'You are not allowed to modify this project',
Pierre-Yves Chibon f778bd
                u'error_code': u'EMODIFYPROJECTNOTALLOWED'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_all_good(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'pingou',
Pierre-Yves Chibon f778bd
            'status': 1,
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'message': u'You are now watching issues and PRs on this project',
Pierre-Yves Chibon f778bd
                u'status': u'ok'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    @patch('pagure.utils.is_admin', MagicMock(return_value=True))
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_other_user_admin(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'foo',
Pierre-Yves Chibon f778bd
            'status': '2',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'message': u'You are now watching commits on this project',
Pierre-Yves Chibon f778bd
                u'status': u'ok'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    @patch('pagure.utils.is_admin', MagicMock(return_value=True))
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_invalid_user_admin(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'watcher': 'example',
Pierre-Yves Chibon f778bd
            'status': '2',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'Invalid or incomplete input submitted',
Pierre-Yves Chibon f778bd
                u'error_code': u'EINVALIDREQ'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
    @patch('pagure.utils.is_admin', MagicMock(return_value=True))
Pierre-Yves Chibon f778bd
    def test_api_update_project_watchers_missing_user_admin(self):
Pierre-Yves Chibon f778bd
        """ Test the api_update_project_watchers method of the flask api. """
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon f778bd
        data = {
Pierre-Yves Chibon f778bd
            'status': '2',
Pierre-Yves Chibon f778bd
        }
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
        output = self.app.post(
Pierre-Yves Chibon f778bd
            '/api/0/test/watchers/update', headers=headers, data=data)
Pierre-Yves Chibon f778bd
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon f778bd
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f778bd
        self.assertDictEqual(
Pierre-Yves Chibon f778bd
            data,
Pierre-Yves Chibon f778bd
            {
Pierre-Yves Chibon f778bd
                u'error': u'Invalid or incomplete input submitted',
Pierre-Yves Chibon f778bd
                u'error_code': u'EINVALIDREQ'
Pierre-Yves Chibon f778bd
            }
Pierre-Yves Chibon f778bd
        )
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
Pierre-Yves Chibon f778bd
if __name__ == '__main__':
Pierre-Yves Chibon f778bd
    unittest.main(verbosity=2)