Blame tests/test_pagure_flask_api_user.py

Pierre-Yves Chibon 8fc633
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
"""
Pierre-Yves Chibon baaf54
 (c) 2015-2018 - Copyright Red Hat Inc
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
 Authors:
Pierre-Yves Chibon 8fc633
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
"""
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 8fc633
import datetime
Pierre-Yves Chibon 219e3b
import os
Pierre-Yves Chibon 219e3b
import pytz
Pierre-Yves Chibon 8fc633
import shutil
Pierre-Yves Chibon 8fc633
import sys
Pierre-Yves Chibon 219e3b
import unittest
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
import json
Pierre-Yves Chibon 8fc633
from mock import patch
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
sys.path.insert(0, os.path.join(os.path.dirname(
Pierre-Yves Chibon 8fc633
    os.path.abspath(__file__)), '..'))
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
import pagure.api
Pierre-Yves Chibon b130e5
import pagure.config
Pierre-Yves Chibon d6ddc4
import pagure.lib.model as model
Pierre-Yves Chibon 930073
import pagure.lib.query
Pierre-Yves Chibon 8fc633
import tests
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
class PagureFlaskApiUSertests(tests.Modeltests):
Pierre-Yves Chibon 8fc633
    """ Tests for the flask API of pagure for issue """
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon f8a683
    maxDiff = None
Pierre-Yves Chibon f8a683
Pierre-Yves Chibon 8fc633
    def setUp(self):
Pierre-Yves Chibon 8fc633
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon 8fc633
        super(PagureFlaskApiUSertests, self).setUp()
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon b130e5
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
    def test_api_users(self):
Pierre-Yves Chibon 8fc633
        """ Test the api_users function.  """
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        output = self.app.get('/api/0/users')
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        self.assertEqual(sorted(data['users']), ['foo', 'pingou'])
Pierre-Yves Chibon 8fc633
        self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
Pierre-Yves Chibon 8fc633
        self.assertEqual(data['total_users'], 2)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        output = self.app.get('/api/0/users?pattern=p')
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        self.assertEqual(data['users'], ['pingou'])
Pierre-Yves Chibon 8fc633
        self.assertEqual(sorted(data.keys()), ['mention', 'total_users', 'users'])
Pierre-Yves Chibon 8fc633
        self.assertEqual(data['total_users'], 1)
Pierre-Yves Chibon 8fc633
Clement Verna 8ed9ee
    def test_api_view_user(self):
Clement Verna 8ed9ee
        """
Clement Verna 8ed9ee
            Test the api_view_user method of the flask api
Clement Verna 8ed9ee
            The tested user has no project or forks.
Clement Verna 8ed9ee
        """
Clement Verna 8ed9ee
        output = self.app.get('/api/0/user/pingou')
Clement Verna 8ed9ee
        self.assertEqual(output.status_code, 200)
Clement Verna 8ed9ee
        exp = {
Clement Verna 8ed9ee
            "forks": [],
Pierre-Yves Chibon c12228
            'forks_pagination': {
Pierre-Yves Chibon 610db7
              'first': 'http://localhost...',
Pierre-Yves Chibon 610db7
              'last': 'http://localhost...',
Pierre-Yves Chibon c12228
              'next': None,
Pierre-Yves Chibon c12228
              'forkpage': 1,
Pierre-Yves Chibon c12228
              'pages': 0,
Pierre-Yves Chibon c12228
              'per_page': 20,
Pierre-Yves Chibon c12228
              'prev': None},
Clement Verna 8ed9ee
            "repos": [],
Pierre-Yves Chibon c12228
            'repos_pagination': {
Pierre-Yves Chibon 610db7
              'first': 'http://localhost...',
Pierre-Yves Chibon 610db7
              'last': 'http://localhost...',
Pierre-Yves Chibon c12228
              'next': None,
Pierre-Yves Chibon c12228
              'repopage': 1,
Pierre-Yves Chibon c12228
              'pages': 0,
Pierre-Yves Chibon c12228
              'per_page': 20,
Pierre-Yves Chibon c12228
              'prev': None},
Ryan Lerch 36ee38
            "user": { "fullname": "PY C",
Ryan Lerch 36ee38
                      "name": "pingou",
Ryan Lerch 36ee38
                      'avatar_url': 'https://seccdn.libravatar.org/avatar/...'}}
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 36ee38
        data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Pierre-Yves Chibon 610db7
        for k in ['forks_pagination', 'repos_pagination']:
Pierre-Yves Chibon 610db7
            for k2 in ['first', 'last']:
Pierre-Yves Chibon 610db7
                self.assertIsNotNone(data[k][k2])
Pierre-Yves Chibon 610db7
                data[k][k2] = 'http://localhost...'
Clement Verna 8ed9ee
        self.assertEqual(data, exp)
Clement Verna 8ed9ee
Clement Verna 8ed9ee
    def test_api_view_user_with_project(self):
Clement Verna 8ed9ee
        """
Clement Verna 8ed9ee
            Test the api_view_user method of the flask api,
Clement Verna 8ed9ee
            this time the user has some project defined.
Clement Verna 8ed9ee
        """
Clement Verna 8ed9ee
        tests.create_projects(self.session)
Clement Verna 8ed9ee
Clement Verna 8ed9ee
        output = self.app.get('/api/0/user/pingou')
Clement Verna 8ed9ee
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon c9e619
        data['repos'][0]['date_created'] = "1490272832"
Clement Verna cd6e30
        data['repos'][0]['date_modified'] = "1490272832"
Pierre-Yves Chibon c9e619
        data['repos'][1]['date_created'] = "1490272832"
Clement Verna cd6e30
        data['repos'][1]['date_modified'] = "1490272832"
Pierre-Yves Chibon c9e619
        data['repos'][2]['date_created'] = "1490272832"
Clement Verna cd6e30
        data['repos'][2]['date_modified'] = "1490272832"
Pierre-Yves Chibon 610db7
        for k in ['forks_pagination', 'repos_pagination']:
Pierre-Yves Chibon 610db7
            for k2 in ['first', 'last']:
Pierre-Yves Chibon 610db7
                self.assertIsNotNone(data[k][k2])
Pierre-Yves Chibon 610db7
                data[k][k2] = 'http://localhost...'
Matt Prahl 6bf79a
        expected_data = {
Clement Verna 8ed9ee
            "forks": [],
Pierre-Yves Chibon c12228
            'forks_pagination': {
Pierre-Yves Chibon 610db7
              'first': 'http://localhost...',
Pierre-Yves Chibon 610db7
              'last': 'http://localhost...',
Pierre-Yves Chibon c12228
              'next': None,
Pierre-Yves Chibon c12228
              'forkpage': 1,
Pierre-Yves Chibon c12228
              'pages': 0,
Pierre-Yves Chibon c12228
              'per_page': 20,
Pierre-Yves Chibon c12228
              'prev': None},
Pierre-Yves Chibon c9e619
            "repos": [
Pierre-Yves Chibon c9e619
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Matt Prahl 6bf79a
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1490272832",
Clement Verna cd6e30
                    "date_modified": "1490272832",
Matt Prahl 6bf79a
                    "description": "test project #1",
Matt Prahl 6bf79a
                    "fullname": "test",
Pierre-Yves Chibon 469e24
                    "url_path": "test",
Matt Prahl 6bf79a
                    "id": 1,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test",
Matt Prahl 6bf79a
                    "namespace": None,
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Clement Verna 8ed9ee
                },
Clement Verna 8ed9ee
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Matt Prahl 6bf79a
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1490272832",
Clement Verna cd6e30
                    "date_modified": "1490272832",
Matt Prahl 6bf79a
                    "description": "test project #2",
Matt Prahl 6bf79a
                    "fullname": "test2",
Pierre-Yves Chibon 469e24
                    "url_path": "test2",
Matt Prahl 6bf79a
                    "id": 2,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test2",
Matt Prahl 6bf79a
                    "namespace": None,
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Clement Verna 8ed9ee
                },
Clement Verna 8ed9ee
                {
Matt Prahl 6bf79a
                    "access_groups": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "ticket": []},
Matt Prahl 6bf79a
                    "access_users": {
Matt Prahl 6bf79a
                        "admin": [],
Matt Prahl 6bf79a
                        "commit": [],
Matt Prahl 6bf79a
                        "owner": ["pingou"],
Matt Prahl 6bf79a
                        "ticket": []
Matt Prahl 6bf79a
                    },
Matt Prahl 6bf79a
                    "close_status": [
Matt Prahl 6bf79a
                        "Invalid",
Matt Prahl 6bf79a
                        "Insufficient data",
Matt Prahl 6bf79a
                        "Fixed",
Matt Prahl 6bf79a
                        "Duplicate"
Matt Prahl 6bf79a
                    ],
Matt Prahl 6bf79a
                    "custom_keys": [],
Matt Prahl 6bf79a
                    "date_created": "1490272832",
Clement Verna cd6e30
                    "date_modified": "1490272832",
Matt Prahl 6bf79a
                    "description": "namespaced test project",
Matt Prahl 6bf79a
                    "fullname": "somenamespace/test3",
Pierre-Yves Chibon 469e24
                    "url_path": "somenamespace/test3",
Matt Prahl 6bf79a
                    "id": 3,
Matt Prahl 6bf79a
                    "milestones": {},
Matt Prahl 6bf79a
                    "name": "test3",
Matt Prahl 6bf79a
                    "namespace": "somenamespace",
Matt Prahl 6bf79a
                    "parent": None,
Matt Prahl 6bf79a
                    "priorities": {},
Matt Prahl 6bf79a
                    "tags": [],
Matt Prahl 6bf79a
                    "user": {
Matt Prahl 6bf79a
                        "fullname": "PY C",
Matt Prahl 6bf79a
                        "name": "pingou"
Matt Prahl 6bf79a
                    }
Pierre-Yves Chibon c9e619
                }
Clement Verna 8ed9ee
            ],
Pierre-Yves Chibon c12228
            'repos_pagination': {
Pierre-Yves Chibon 610db7
              'first': 'http://localhost...',
Pierre-Yves Chibon 610db7
              'last': 'http://localhost...',
Pierre-Yves Chibon c12228
              'next': None,
Pierre-Yves Chibon c12228
              'repopage': 1,
Pierre-Yves Chibon c12228
              'pages': 1,
Pierre-Yves Chibon c12228
              'per_page': 20,
Pierre-Yves Chibon c12228
              'prev': None},
Clement Verna 8ed9ee
            "user": {
Clement Verna 8ed9ee
                "fullname": "PY C",
Ryan Lerch 36ee38
                "name": "pingou",
Ryan Lerch 36ee38
                'avatar_url': 'https://seccdn.libravatar.org/avatar/...'
Clement Verna 8ed9ee
            }
Clement Verna 8ed9ee
        }
Ryan Lerch 36ee38
        data["user"]["avatar_url"] = "https://seccdn.libravatar.org/avatar/..."
Matt Prahl 6bf79a
        self.assertEqual(data, expected_data)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 8fc633
    def test_api_view_user_activity_stats(self, mockemail):
Pierre-Yves Chibon 8fc633
        """ Test the api_view_user_activity_stats method of the flask user
Pierre-Yves Chibon 8fc633
        api. """
Pierre-Yves Chibon 8fc633
        mockemail.return_value = True
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        tests.create_projects(self.session)
Pierre-Yves Chibon 8fc633
        tests.create_tokens(self.session)
Pierre-Yves Chibon 8fc633
        tests.create_tokens_acl(self.session)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        headers = {'Authorization': 'token aaabbbcccddd'}
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # Create a pull-request
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon 930073
        req = pagure.lib.query.new_pull_request(
Pierre-Yves Chibon 8fc633
            session=self.session,
Pierre-Yves Chibon 8fc633
            repo_from=forked_repo,
Pierre-Yves Chibon 8fc633
            branch_from='master',
Pierre-Yves Chibon 8fc633
            repo_to=repo,
Pierre-Yves Chibon 8fc633
            branch_to='master',
Pierre-Yves Chibon 8fc633
            title='test pull-request',
Pierre-Yves Chibon 8fc633
            user='pingou',
Pierre-Yves Chibon 8fc633
        )
Pierre-Yves Chibon 8fc633
        self.session.commit()
Pierre-Yves Chibon 8fc633
        self.assertEqual(req.id, 1)
Pierre-Yves Chibon 8fc633
        self.assertEqual(req.title, 'test pull-request')
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # Check comments before
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 8fc633
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 8fc633
        self.assertEqual(len(request.comments), 0)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        data = {
Pierre-Yves Chibon 8fc633
            'comment': 'This is a very interesting question',
Pierre-Yves Chibon 8fc633
        }
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # Valid request
Pierre-Yves Chibon 8fc633
        output = self.app.post(
Pierre-Yves Chibon 8fc633
            '/api/0/test/pull-request/1/comment', data=data, headers=headers)
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        self.assertDictEqual(
Pierre-Yves Chibon 8fc633
            data,
Pierre-Yves Chibon 8fc633
            {'message': 'Comment added'}
Pierre-Yves Chibon 8fc633
        )
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # One comment added
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 8fc633
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 8fc633
        self.assertEqual(len(request.comments), 1)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # Close PR
Pierre-Yves Chibon 8fc633
        output = self.app.post(
Pierre-Yves Chibon 8fc633
            '/api/0/test/pull-request/1/close', headers=headers)
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        self.assertDictEqual(
Pierre-Yves Chibon 8fc633
            data,
Pierre-Yves Chibon 8fc633
            {"message": "Pull-request closed!"}
Pierre-Yves Chibon 8fc633
        )
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # PR closed
Aurélien Bompard 13bcde
        self.session.commit()
Pierre-Yves Chibon 930073
        request = pagure.lib.query.search_pull_requests(
Pierre-Yves Chibon 8fc633
            self.session, project_id=1, requestid=1)
Pierre-Yves Chibon 8fc633
        self.assertEqual(request.status, 'Closed')
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        # Finally retrieve the user's logs
Pierre-Yves Chibon 8fc633
        output = self.app.get('/api/0/user/pingou/activity/stats')
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
Pierre-Yves Chibon a8e4e9
        self.assertDictEqual(data, {date: 4})
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 8fc633
    def test_api_view_user_activity_date(self, mockemail):
Pierre-Yves Chibon 8fc633
        """ Test the api_view_user_activity_date method of the flask user
Pierre-Yves Chibon 8fc633
        api. """
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        self.test_api_view_user_activity_stats()
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon fa2aba
        # Invalid date
Pierre-Yves Chibon fa2aba
        output = self.app.get('/api/0/user/pingou/activity/AABB')
Pierre-Yves Chibon fa2aba
        self.assertEqual(output.status_code, 400)
Pierre-Yves Chibon fa2aba
Pierre-Yves Chibon fa2aba
        # Invalid date
Pierre-Yves Chibon fa2aba
        output = self.app.get('/api/0/user/pingou/activity/2016asd')
Pierre-Yves Chibon fa2aba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa2aba
        exp = {
Pierre-Yves Chibon fa2aba
          "activities": [],
Pierre-Yves Chibon fa2aba
          "date": "2016-01-01"
Pierre-Yves Chibon fa2aba
        }
Aurélien Bompard 626417
        self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
Pierre-Yves Chibon fa2aba
Pierre-Yves Chibon fa2aba
        # Date parsed, just not really as expected
Pierre-Yves Chibon fa2aba
        output = self.app.get('/api/0/user/pingou/activity/20161245')
Pierre-Yves Chibon fa2aba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa2aba
        exp = {
Pierre-Yves Chibon fa2aba
          "activities": [],
Pierre-Yves Chibon fa2aba
          "date": "1970-08-22"
Pierre-Yves Chibon fa2aba
        }
Aurélien Bompard 626417
        self.assertEqual(json.loads(output.get_data(as_text=True)), exp)
Pierre-Yves Chibon fa2aba
Pierre-Yves Chibon 8fc633
        date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
Pierre-Yves Chibon 8fc633
        # Retrieve the user's logs for today
Pierre-Yves Chibon 8fc633
        output = self.app.get('/api/0/user/pingou/activity/%s' % date)
Pierre-Yves Chibon 8fc633
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 8fc633
        exp = {
Pierre-Yves Chibon 8fc633
          "activities": [
Pierre-Yves Chibon 8fc633
            {
Pierre-Yves Chibon 8fc633
              "date": date,
Pierre-Yves Chibon 8fc633
              "date_created": "1477558752",
Pierre-Yves Chibon 263695
              "type": "pull-request",
Pierre-Yves Chibon f8a683
              "description_mk": "

pingou created PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 1,
Pierre-Yves Chibon a3362c
              "ref_id": "1",
Pierre-Yves Chibon a3362c
              "type": "created",
Pierre-Yves Chibon 8fc633
              "user": {
Pierre-Yves Chibon 8fc633
                "fullname": "PY C",
Pierre-Yves Chibon 8fc633
                "name": "pingou"
Pierre-Yves Chibon 8fc633
              }
Pierre-Yves Chibon 8fc633
            },
Pierre-Yves Chibon 8fc633
            {
Pierre-Yves Chibon 8fc633
              "date": date,
Pierre-Yves Chibon 8fc633
              "date_created": "1477558752",
Pierre-Yves Chibon 263695
              "type": "pull-request",
Pierre-Yves Chibon f8a683
              "description_mk": "

pingou commented on PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 2,
Pierre-Yves Chibon a3362c
              "ref_id": "1",
Pierre-Yves Chibon a3362c
              "type": "commented",
Pierre-Yves Chibon 8fc633
              "user": {
Pierre-Yves Chibon 8fc633
                "fullname": "PY C",
Pierre-Yves Chibon 8fc633
                "name": "pingou"
Pierre-Yves Chibon 8fc633
              }
Pierre-Yves Chibon 8fc633
            },
Pierre-Yves Chibon 8fc633
            {
Pierre-Yves Chibon 8fc633
              "date": date,
Pierre-Yves Chibon 8fc633
              "date_created": "1477558752",
Pierre-Yves Chibon 263695
              "type": "pull-request",
Pierre-Yves Chibon f8a683
              "description_mk": "

pingou closed PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 3,
Pierre-Yves Chibon a3362c
              "ref_id": "1",
Pierre-Yves Chibon a3362c
              "type": "closed",
Pierre-Yves Chibon 8fc633
              "user": {
Pierre-Yves Chibon 8fc633
                "fullname": "PY C",
Pierre-Yves Chibon 8fc633
                "name": "pingou"
Pierre-Yves Chibon 8fc633
              }
Pierre-Yves Chibon 8fc633
            },
Pierre-Yves Chibon 8fc633
            {
Pierre-Yves Chibon 8fc633
              "date": date,
Pierre-Yves Chibon 8fc633
              "date_created": "1477558752",
Pierre-Yves Chibon 263695
              "type": "pull-request",
Pierre-Yves Chibon f8a683
              "description_mk": "

pingou commented on PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 4,
Pierre-Yves Chibon a3362c
              "ref_id": "1",
Pierre-Yves Chibon a3362c
              "type": "commented",
Pierre-Yves Chibon 8fc633
              "user": {
Pierre-Yves Chibon 8fc633
                "fullname": "PY C",
Pierre-Yves Chibon 8fc633
                "name": "pingou"
Pierre-Yves Chibon 8fc633
              }
Pierre-Yves Chibon 8fc633
            }
Pierre-Yves Chibon fa2aba
          ],
Pierre-Yves Chibon fa2aba
          "date": date,
Pierre-Yves Chibon 8fc633
        }
Pierre-Yves Chibon 8fc633
        for idx, act in enumerate(data['activities']):
Pierre-Yves Chibon 8fc633
            act['date_created'] = '1477558752'
Pierre-Yves Chibon 8fc633
            data['activities'][idx] = act
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        self.assertEqual(data, exp)
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon d6ddc4
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon d6ddc4
    def test_api_view_user_activity_date_1_activity(self, mockemail):
Pierre-Yves Chibon d6ddc4
        """ Test the api_view_user_activity_date method of the flask user
Pierre-Yves Chibon d6ddc4
        api when the user only did one action. """
Pierre-Yves Chibon d6ddc4
Pierre-Yves Chibon d6ddc4
        tests.create_projects(self.session)
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon d6ddc4
Pierre-Yves Chibon d6ddc4
        now = datetime.datetime.utcnow()
Pierre-Yves Chibon d6ddc4
        date = now.date().strftime('%Y-%m-%d')
Pierre-Yves Chibon d6ddc4
        # Create a single commit log
Pierre-Yves Chibon d6ddc4
        log = model.PagureLog(
Pierre-Yves Chibon d6ddc4
            user_id=1,
Pierre-Yves Chibon d6ddc4
            user_email='foo@bar.com',
Pierre-Yves Chibon d6ddc4
            project_id=1,
Pierre-Yves Chibon d6ddc4
            log_type='committed',
Pierre-Yves Chibon d6ddc4
            ref_id='githash',
Pierre-Yves Chibon d6ddc4
            date=now.date(),
Pierre-Yves Chibon d6ddc4
            date_created=now
Pierre-Yves Chibon d6ddc4
        )
Pierre-Yves Chibon d6ddc4
        self.session.add(log)
Pierre-Yves Chibon d6ddc4
        self.session.commit()
Pierre-Yves Chibon d6ddc4
Pierre-Yves Chibon d6ddc4
        # Retrieve the user's logs for today
Pierre-Yves Chibon d6ddc4
        output = self.app.get(
Pierre-Yves Chibon d6ddc4
            '/api/0/user/pingou/activity/%s?grouped=1' % date)
Pierre-Yves Chibon d6ddc4
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon d6ddc4
        exp = {
Pierre-Yves Chibon d6ddc4
          "activities": [
Pierre-Yves Chibon d6ddc4
            {
Pierre-Yves Chibon d6ddc4
              "description_mk": "

pingou committed on test#githash

"
Pierre-Yves Chibon d6ddc4
            }
Pierre-Yves Chibon d6ddc4
          ],
Pierre-Yves Chibon d6ddc4
          "date": date,
Pierre-Yves Chibon d6ddc4
        }
Pierre-Yves Chibon d6ddc4
        self.assertEqual(data, exp)
Pierre-Yves Chibon d6ddc4
Adam Williamson f99ac7
    @patch('pagure.lib.notify.send_email')
Adam Williamson f99ac7
    def test_api_view_user_activity_timezone_negative(self, mockemail):
Adam Williamson 8a161a
        """Test api_view_user_activity{_stats,_date} with the America/
Adam Williamson 8a161a
        New York timezone, which is 5 hours behind UTC in winter and
Adam Williamson 8a161a
        4 hours behind UTC in summer (daylight savings). The events
Pierre-Yves Chibon 219e3b
        will occur on XXXX-02-15 in UTC, but on XXXX-02-14 local.
Adam Williamson f99ac7
        """
Adam Williamson f99ac7
        tests.create_projects(self.session)
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Adam Williamson f99ac7
Pierre-Yves Chibon 219e3b
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 219e3b
        year = today.year
Pierre-Yves Chibon 219e3b
        if today.year == 2 and today.date <=15:
Pierre-Yves Chibon 219e3b
            year = year - 1
Pierre-Yves Chibon 219e3b
        elif today.year < 2:
Pierre-Yves Chibon 219e3b
            year = year - 1
Pierre-Yves Chibon 219e3b
        dateobj = datetime.datetime(year, 2, 15, 3, 30)
Pierre-Yves Chibon 219e3b
        utcdate = '%s-02-15' % year
Pierre-Yves Chibon 219e3b
        # the Unix timestamp for YYYY-02-15 12:00 UTC
Pierre-Yves Chibon 219e3b
        utcts = str(int(
Pierre-Yves Chibon 219e3b
            (
Pierre-Yves Chibon 219e3b
                datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
Pierre-Yves Chibon 219e3b
                - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
Pierre-Yves Chibon 219e3b
            ).total_seconds()
Pierre-Yves Chibon 219e3b
        ))
Pierre-Yves Chibon 219e3b
        localdate = '%s-02-14' % today.year
Pierre-Yves Chibon 219e3b
        # the Unix timestamp for YYYY-02-15 18:00 America/New_York
Pierre-Yves Chibon 219e3b
        localts = str(int(
Pierre-Yves Chibon 219e3b
            (
Pierre-Yves Chibon 219e3b
                datetime.datetime(
Pierre-Yves Chibon 219e3b
                    year, 2, 14, 17, 0, tzinfo=pytz.timezone('America/New_York'))
Pierre-Yves Chibon 219e3b
                - datetime.datetime(
Pierre-Yves Chibon 219e3b
                    1970, 1, 1 , tzinfo=pytz.timezone('America/New_York'))
Pierre-Yves Chibon 219e3b
            ).total_seconds()
Pierre-Yves Chibon 219e3b
        ))
Adam Williamson f99ac7
        # Create a single commit log
Adam Williamson f99ac7
        log = model.PagureLog(
Adam Williamson f99ac7
            user_id=1,
Adam Williamson f99ac7
            user_email='foo@bar.com',
Adam Williamson f99ac7
            project_id=1,
Adam Williamson f99ac7
            log_type='committed',
Adam Williamson f99ac7
            ref_id='githash',
Adam Williamson f99ac7
            date=dateobj.date(),
Adam Williamson f99ac7
            date_created=dateobj
Adam Williamson f99ac7
        )
Adam Williamson f99ac7
        self.session.add(log)
Adam Williamson f99ac7
        self.session.commit()
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's stats with no timezone specified (==UTC)
Adam Williamson f99ac7
        output = self.app.get('/api/0/user/pingou/activity/stats')
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        # date in output should be UTC date
Adam Williamson f99ac7
        self.assertDictEqual(data, {utcdate: 1})
Adam Williamson 8a161a
        # Now in timestamp format...
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp')
Adam Williamson 8a161a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson 8a161a
        # timestamp in output should be UTC ts
Adam Williamson 8a161a
        self.assertDictEqual(data, {utcts: 1})
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's stats with local timezone specified
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?tz=America/New_York')
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        # date in output should be local date
Adam Williamson f99ac7
        self.assertDictEqual(data, {localdate: 1})
Adam Williamson 8a161a
        # Now in timestamp format...
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp&tz=America/New_York')
Adam Williamson 8a161a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson 8a161a
        # timestamp in output should be local ts
Adam Williamson 8a161a
        self.assertDictEqual(data, {localts: 1})
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's logs for 2018-02-15 with no timezone
Adam Williamson f99ac7
        output = self.app.get(
Adam Williamson f99ac7
            '/api/0/user/pingou/activity/%s?grouped=1' % utcdate)
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        exp = {
Adam Williamson f99ac7
          "activities": [
Adam Williamson f99ac7
            {
Adam Williamson f99ac7
              "description_mk": "

pingou committed on test#githash

"
Adam Williamson f99ac7
            }
Adam Williamson f99ac7
          ],
Adam Williamson f99ac7
          "date": utcdate,
Adam Williamson f99ac7
        }
Adam Williamson f99ac7
        self.assertEqual(data, exp)
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Now retrieve the user's logs for 2018-02-14 with local time
Adam Williamson f99ac7
        output = self.app.get(
Adam Williamson 8a161a
            '/api/0/user/pingou/activity/%s?grouped=1&tz=America/New_York' % localdate)
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        exp['date'] = localdate
Adam Williamson f99ac7
        self.assertEqual(data, exp)
Adam Williamson f99ac7
Adam Williamson f99ac7
    @patch('pagure.lib.notify.send_email')
Adam Williamson f99ac7
    def test_api_view_user_activity_timezone_positive(self, mockemail):
Adam Williamson 8a161a
        """Test api_view_user_activity{_stats,_date} with the Asia/
Adam Williamson 8a161a
        Dubai timezone, which is 4 hours ahead of UTC. The events will
Pierre-Yves Chibon 219e3b
        occur on XXXX-02-15 in UTC, but on XXXX-02-16 in local time.
Adam Williamson f99ac7
        """
Adam Williamson f99ac7
        tests.create_projects(self.session)
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Adam Williamson f99ac7
Pierre-Yves Chibon 219e3b
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 219e3b
        year = today.year
Pierre-Yves Chibon 219e3b
        if today.year == 2 and today.date <=15:
Pierre-Yves Chibon 219e3b
            year = year - 1
Pierre-Yves Chibon 219e3b
        elif today.year < 2:
Pierre-Yves Chibon 219e3b
            year = year - 1
Pierre-Yves Chibon 219e3b
        dateobj = datetime.datetime(year, 2, 15, 22, 30)
Pierre-Yves Chibon 219e3b
        utcdate = '%s-02-15' % year
Pierre-Yves Chibon 219e3b
        # the Unix timestamp for YYYY-02-15 12:00 UTC
Pierre-Yves Chibon 219e3b
        utcts = str(int(
Pierre-Yves Chibon 219e3b
            (
Pierre-Yves Chibon 219e3b
                datetime.datetime(year, 2, 15, 12, 0, tzinfo=pytz.UTC)
Pierre-Yves Chibon 219e3b
                - datetime.datetime(1970, 1, 1, tzinfo=pytz.UTC)
Pierre-Yves Chibon 219e3b
            ).total_seconds()
Pierre-Yves Chibon 219e3b
        ))
Pierre-Yves Chibon 219e3b
        localdate = '%s-02-16' % year
Pierre-Yves Chibon 219e3b
        # the Unix timestamp for YYYY-02-16 9:00 Asia/Dubai
Pierre-Yves Chibon 219e3b
        localts = str(int(
Pierre-Yves Chibon 219e3b
            (
Pierre-Yves Chibon 219e3b
                datetime.datetime(
Pierre-Yves Chibon 219e3b
                    year, 2, 16, 8, 0, tzinfo=pytz.timezone('Asia/Dubai'))
Pierre-Yves Chibon 219e3b
                - datetime.datetime(
Pierre-Yves Chibon 219e3b
                    1970, 1, 1 , tzinfo=pytz.timezone('Asia/Dubai'))
Pierre-Yves Chibon 219e3b
            ).total_seconds()
Pierre-Yves Chibon 219e3b
        ))
Adam Williamson f99ac7
        # Create a single commit log
Adam Williamson f99ac7
        log = model.PagureLog(
Adam Williamson f99ac7
            user_id=1,
Adam Williamson f99ac7
            user_email='foo@bar.com',
Adam Williamson f99ac7
            project_id=1,
Adam Williamson f99ac7
            log_type='committed',
Adam Williamson f99ac7
            ref_id='githash',
Adam Williamson f99ac7
            date=dateobj.date(),
Adam Williamson f99ac7
            date_created=dateobj
Adam Williamson f99ac7
        )
Adam Williamson f99ac7
        self.session.add(log)
Adam Williamson f99ac7
        self.session.commit()
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's stats with no timezone specified (==UTC)
Adam Williamson f99ac7
        output = self.app.get('/api/0/user/pingou/activity/stats')
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        # date in output should be UTC date
Adam Williamson f99ac7
        self.assertDictEqual(data, {utcdate: 1})
Adam Williamson 8a161a
        # Now in timestamp format...
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp')
Adam Williamson 8a161a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson 8a161a
        # timestamp in output should be UTC ts
Adam Williamson 8a161a
        self.assertDictEqual(data, {utcts: 1})
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's stats with local timezone specified
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?tz=Asia/Dubai')
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        # date in output should be local date
Adam Williamson f99ac7
        self.assertDictEqual(data, {localdate: 1})
Adam Williamson 8a161a
        # Now in timestamp format...
Adam Williamson 8a161a
        output = self.app.get('/api/0/user/pingou/activity/stats?format=timestamp&tz=Asia/Dubai')
Adam Williamson 8a161a
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson 8a161a
        # timestamp in output should be local ts
Adam Williamson 8a161a
        self.assertDictEqual(data, {localts: 1})
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Retrieve the user's logs for 2018-02-15 with no timezone
Adam Williamson f99ac7
        output = self.app.get(
Adam Williamson f99ac7
            '/api/0/user/pingou/activity/%s?grouped=1' % utcdate)
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        exp = {
Adam Williamson f99ac7
          "activities": [
Adam Williamson f99ac7
            {
Adam Williamson f99ac7
              "description_mk": "

pingou committed on test#githash

"
Adam Williamson f99ac7
            }
Adam Williamson f99ac7
          ],
Adam Williamson f99ac7
          "date": utcdate,
Adam Williamson f99ac7
        }
Adam Williamson f99ac7
        self.assertEqual(data, exp)
Adam Williamson f99ac7
Adam Williamson 8a161a
        # Now retrieve the user's logs for 2018-02-16 with local time
Adam Williamson f99ac7
        output = self.app.get(
Adam Williamson 8a161a
            '/api/0/user/pingou/activity/%s?grouped=1&tz=Asia/Dubai' % localdate)
Adam Williamson f99ac7
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Adam Williamson f99ac7
        exp['date'] = localdate
Adam Williamson f99ac7
        self.assertEqual(data, exp)
Adam Williamson f99ac7
Pierre-Yves Chibon 8fc633
Ryan Lerch 397681
class PagureFlaskApiUsertestrequests(tests.Modeltests):
Ryan Lerch 397681
    """ Tests for the user requests endpoints """
Ryan Lerch 397681
Ryan Lerch 397681
    maxDiff = None
Ryan Lerch 397681
Ryan Lerch 397681
    def setUp(self):
Ryan Lerch 397681
        """ Set up the environnment, ran before every tests. """
Ryan Lerch 397681
        super(PagureFlaskApiUsertestrequests, self).setUp()
Ryan Lerch 397681
Pierre-Yves Chibon b130e5
        pagure.config.config['REQUESTS_FOLDER'] = None
Ryan Lerch 397681
Ryan Lerch 397681
        tests.create_projects(self.session)
Ryan Lerch 397681
Ryan Lerch 397681
        # Create few pull-requests
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='open pullrequest by user foo on repo test',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
        )
Pierre-Yves Chibon 65d34a
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='open pullrequest by user foo on repo test2',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='closed pullrequest by user foo on repo test',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
            status='Closed',
Ryan Lerch 397681
        )
Pierre-Yves Chibon 65d34a
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='closed pullrequest by user foo on repo test2',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
            status='Closed',
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='merged pullrequest by user foo on repo test',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
            status='Merged',
Ryan Lerch 397681
        )
Pierre-Yves Chibon 65d34a
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='merged pullrequest by user foo on repo test2',
Ryan Lerch 397681
            user='foo',
Ryan Lerch 397681
            status='Merged',
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='open pullrequest by user pingou on repo test',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='open pullrequest by user pingou on repo test2',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='closed pullrequest by user pingou on repo test',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
            status="Closed",
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='closed pullrequest by user pingou on repo test2',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
            status="Closed",
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='merged pullrequest by user pingou on repo test',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
            status="Merged",
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Pierre-Yves Chibon 930073
        repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        forked_repo = pagure.lib.query.get_authorized_project(self.session, 'test2')
Pierre-Yves Chibon 930073
        pagure.lib.query.new_pull_request(
Ryan Lerch 397681
            session=self.session,
Ryan Lerch 397681
            repo_from=forked_repo,
Ryan Lerch 397681
            branch_from='master',
Ryan Lerch 397681
            repo_to=repo,
Ryan Lerch 397681
            branch_to='master',
Ryan Lerch 397681
            title='merged pullrequest by user pingou on repo test2',
Ryan Lerch 397681
            user='pingou',
Ryan Lerch 397681
            status="Merged",
Ryan Lerch 397681
        )
Ryan Lerch 397681
        self.session.commit()
Ryan Lerch 397681
Ryan Lerch 397681
    @patch('pagure.lib.notify.send_email')
Ryan Lerch 397681
    def test_api_view_user_requests_filed(self, mockemail):
Ryan Lerch 397681
        """ Test the api_view_user_requests_filed method of the flask user
Ryan Lerch 397681
        api """
Ryan Lerch 397681
Ryan Lerch 397681
        # First we test without the status parameter. It should default to `open`
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/filed')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "open pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "open pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "open")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `open`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/filed?status=open')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "open pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "open pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "open")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `closed`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/filed?status=closed')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "closed pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "closed pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "closed")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `merged`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/filed?status=merged')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "merged")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Finally, test with the status parameter set to `all`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/filed?status=all')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['user']['name'], "pingou")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user pingou on repo test2")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['title'], "closed pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['title'], "closed pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['title'], "open pullrequest by user pingou on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['title'], "open pullrequest by user pingou on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "all")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Pierre-Yves Chibon 254b06
        # Test page 2 with the status parameter set to `all`.
Pierre-Yves Chibon 254b06
        output = self.app.get(
Pierre-Yves Chibon 254b06
            '/api/0/user/pingou/requests/filed?status=all&page=2')
Pierre-Yves Chibon 254b06
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 254b06
Pierre-Yves Chibon 254b06
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Pierre-Yves Chibon 254b06
        self.assertEqual(data['args']['page'], 2)
Pierre-Yves Chibon 254b06
Ryan Lerch 397681
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_filed_created(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        thedaybefore = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                thedaybefore.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&created=%s..%s' % (
Pierre-Yves Chibon 0ccdba
                thedaybefore.isoformat(), tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_filed_updated(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_filed_closed(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/filed?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 13b47e
    def test_api_view_user_requests_filed_foo(self, mockemail):
Pierre-Yves Chibon 13b47e
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 13b47e
        api """
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
        # Default data returned
Pierre-Yves Chibon 13b47e
        output = self.app.get(
Pierre-Yves Chibon 13b47e
            '/api/0/user/foo/requests/filed?status=all&per_page=6')
Pierre-Yves Chibon 13b47e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13b47e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 13b47e
        self.assertEqual(
Pierre-Yves Chibon 13b47e
            sorted(data.keys()),
Pierre-Yves Chibon 13b47e
            [u'args', u'pagination', u'requests', u'total_requests'])
Pierre-Yves Chibon 13b47e
        # There are 6 PRs, that's 1 page at 6 results per page
Pierre-Yves Chibon 13b47e
        self.assertEqual(data['pagination']['pages'], 1)
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 13b47e
    def test_api_view_user_requests_filed_foo_grp_access(self, mockemail):
Pierre-Yves Chibon 13b47e
        """ Test when the user has accessed to some PRs via a group. """
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
        # Add the user to a group
Pierre-Yves Chibon 13b47e
        msg = pagure.lib.query.add_group(
Pierre-Yves Chibon 13b47e
            self.session,
Pierre-Yves Chibon 13b47e
            group_name='some_group',
Pierre-Yves Chibon 13b47e
            display_name='Some Group',
Pierre-Yves Chibon 13b47e
            description=None,
Pierre-Yves Chibon 13b47e
            group_type='bar',
Pierre-Yves Chibon 13b47e
            user='pingou',
Pierre-Yves Chibon 13b47e
            is_admin=False,
Pierre-Yves Chibon 13b47e
            blacklist=[],
Pierre-Yves Chibon 13b47e
        )
Pierre-Yves Chibon 13b47e
        self.session.commit()
Pierre-Yves Chibon 13b47e
        # Add the group to the project `test2`
Pierre-Yves Chibon 13b47e
        project = pagure.lib.query._get_project(self.session, 'test2')
Pierre-Yves Chibon 13b47e
        msg = pagure.lib.query.add_group_to_project(
Pierre-Yves Chibon 13b47e
            session=self.session,
Pierre-Yves Chibon 13b47e
            project=project,
Pierre-Yves Chibon 13b47e
            new_group='some_group',
Pierre-Yves Chibon 13b47e
            user='pingou',
Pierre-Yves Chibon 13b47e
        )
Pierre-Yves Chibon 13b47e
        self.session.commit()
Pierre-Yves Chibon 13b47e
        self.assertEqual(msg, 'Group added')
Pierre-Yves Chibon 13b47e
        # Add foo to the group
Pierre-Yves Chibon 13b47e
        group = pagure.lib.query.search_groups(
Pierre-Yves Chibon 13b47e
            self.session, group_name='some_group')
Pierre-Yves Chibon 13b47e
        result = pagure.lib.query.add_user_to_group(
Pierre-Yves Chibon 13b47e
            self.session, 'foo', group, 'pingou', True)
Pierre-Yves Chibon 13b47e
        self.session.commit()
Pierre-Yves Chibon 13b47e
        self.assertEqual(
Pierre-Yves Chibon 13b47e
            result, 'User `foo` added to the group `some_group`.')
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
        # Query the API for foo's filed PRs
Pierre-Yves Chibon 13b47e
        output = self.app.get(
Pierre-Yves Chibon 13b47e
            '/api/0/user/foo/requests/filed?status=all&per_page=6')
Pierre-Yves Chibon 13b47e
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 13b47e
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 13b47e
        self.assertEqual(
Pierre-Yves Chibon 13b47e
            sorted(data.keys()),
Pierre-Yves Chibon 13b47e
            [u'args', u'pagination', u'requests', u'total_requests'])
Pierre-Yves Chibon 13b47e
        # There are 6 PRs, that's 1 page at 6 results per page
Pierre-Yves Chibon 13b47e
        self.assertEqual(data['pagination']['pages'], 1)
Pierre-Yves Chibon 13b47e
Pierre-Yves Chibon 13b47e
    @patch('pagure.lib.notify.send_email')
Ryan Lerch 397681
    def test_api_view_user_requests_actionable(self, mockemail):
Ryan Lerch 397681
        """ Test the api_view_user_requests_actionable method of the flask user
Ryan Lerch 397681
        api """
Ryan Lerch 397681
Ryan Lerch 397681
        # First we test without the status parameter. It should default to `open`
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/actionable')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "open pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "open pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "open")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `open`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/actionable?status=open')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "open pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "open pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "open")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `closed`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/actionable?status=closed')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "closed pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "closed pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "closed")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Next test with the status parameter set to `merged`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/actionable?status=merged')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 2)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "merged")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Ryan Lerch 397681
        # Finally, test with the status parameter set to `all`.
Ryan Lerch 397681
        output = self.app.get(
Ryan Lerch 397681
            '/api/0/user/pingou/requests/actionable?status=all')
Ryan Lerch 397681
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Ryan Lerch 397681
Ryan Lerch 397681
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['user']['name'], "foo")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][1]['status'], "Merged")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['status'], "Closed")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['status'], "Open")
Ryan Lerch 397681
        self.assertEqual(data['requests'][0]['title'], "merged pullrequest by user foo on repo test2")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['requests'][1]['title'], "merged pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['requests'][2]['title'], "closed pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][3]['title'], "closed pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['requests'][4]['title'], "open pullrequest by user foo on repo test2")
Ryan Lerch 397681
        self.assertEqual(data['requests'][5]['title'], "open pullrequest by user foo on repo test")
Ryan Lerch 397681
        self.assertEqual(data['args']['status'], "all")
Pierre-Yves Chibon 65d34a
        self.assertEqual(data['args']['page'], 1)
Ryan Lerch 397681
Pierre-Yves Chibon 254b06
        # Test page 2 with the status parameter set to `all`.
Pierre-Yves Chibon 254b06
        output = self.app.get(
Pierre-Yves Chibon 254b06
            '/api/0/user/pingou/requests/actionable?status=all&page=2')
Pierre-Yves Chibon 254b06
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 254b06
Pierre-Yves Chibon 254b06
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon fc03a3
        self.assertEqual(
Pierre-Yves Chibon fc03a3
            sorted(data.keys()),
Pierre-Yves Chibon fc03a3
            [u'args', u'pagination', u'requests', u'total_requests'])
Pierre-Yves Chibon 254b06
        self.assertEqual(data['args']['page'], 2)
Pierre-Yves Chibon 254b06
Ryan Lerch 397681
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_actionable_created(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        thedaybefore = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                thedaybefore.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=..%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&created=%s..%s' % (
Pierre-Yves Chibon 0ccdba
                thedaybefore.isoformat(), tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_actionable_updated(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&updated=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 6)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
    @patch('pagure.lib.notify.send_email')
Pierre-Yves Chibon 0ccdba
    def test_api_view_user_requests_actionable_closed(self, mockemail):
Pierre-Yves Chibon 0ccdba
        """ Test the api_view_user_requests_filed method of the flask user
Pierre-Yves Chibon 0ccdba
        api with the created parameter """
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                today.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                yesterday.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 0ccdba
        output = self.app.get(
Pierre-Yves Chibon 0ccdba
            '/api/0/user/pingou/requests/actionable?status=all&closed=%s' % (
Pierre-Yves Chibon 0ccdba
                tomorrow.isoformat()))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0ccdba
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 0ccdba
        self.assertEqual(len(data['requests']), 0)
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon 0ccdba
Pierre-Yves Chibon f8e0d1
class PagureFlaskApiUsertestissues(tests.Modeltests):
Pierre-Yves Chibon f8e0d1
    """ Tests for the user issues endpoints """
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
    maxDiff = None
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
    def setUp(self):
Pierre-Yves Chibon f8e0d1
        """ Set up the environnment, ran before every tests. """
Pierre-Yves Chibon f8e0d1
        super(PagureFlaskApiUsertestissues, self).setUp()
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon b130e5
        pagure.config.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
        tests.create_projects(self.session)
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon 930073
        repo = pagure.lib.query._get_project(self.session, 'test')
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
        # Create issues to play with
Pierre-Yves Chibon 930073
        msg = pagure.lib.query.new_issue(
Pierre-Yves Chibon f8e0d1
            session=self.session,
Pierre-Yves Chibon f8e0d1
            repo=repo,
Pierre-Yves Chibon f8e0d1
            title='Test issue',
Pierre-Yves Chibon f8e0d1
            content='We should work on this',
Pierre-Yves Chibon f8e0d1
            user='pingou',
Pierre-Yves Chibon f8e0d1
        )
Pierre-Yves Chibon f8e0d1
        self.session.commit()
Pierre-Yves Chibon f8e0d1
        self.assertEqual(msg.title, 'Test issue')
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
    def test_user_issues_empty(self):
Pierre-Yves Chibon f8e0d1
        """ Return the list of issues associated with the specified user. """
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
        output = self.app.get('/api/0/user/foo/issues')
Pierre-Yves Chibon f8e0d1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 610db7
        for k in ['pagination_issues_assigned', 'pagination_issues_created']:
Pierre-Yves Chibon 610db7
            for k2 in ['first', 'last']:
Pierre-Yves Chibon 610db7
                self.assertIsNotNone(data[k][k2])
Pierre-Yves Chibon 610db7
                data[k][k2] = None
Pierre-Yves Chibon f8e0d1
        self.assertEqual(
Pierre-Yves Chibon f8e0d1
            data,
Pierre-Yves Chibon f8e0d1
            {
Pierre-Yves Chibon f8e0d1
              "args": {
Pierre-Yves Chibon f8e0d1
                "assignee": True,
Pierre-Yves Chibon f8e0d1
                "author": True,
Pierre-Yves Chibon 872898
                "closed": None,
Pierre-Yves Chibon 872898
                "created": None,
Pierre-Yves Chibon f8e0d1
                "milestones": [],
Pierre-Yves Chibon f8e0d1
                "no_stones": None,
Pierre-Yves Chibon f8e0d1
                "order": None,
Pierre-Yves Chibon f8e0d1
                "order_key": None,
Pierre-Yves Chibon f8e0d1
                "page": 1,
Pierre-Yves Chibon f8e0d1
                "since": None,
Pierre-Yves Chibon f8e0d1
                "status": None,
Pierre-Yves Chibon 872898
                "tags": [],
Pierre-Yves Chibon 872898
                "updated": None,
Pierre-Yves Chibon f8e0d1
              },
Pierre-Yves Chibon f8e0d1
              "issues_assigned": [],
Pierre-Yves Chibon f8e0d1
              "issues_created": [],
Pierre-Yves Chibon c12228
              'pagination_issues_assigned': {
Pierre-Yves Chibon 610db7
                'first': None,
Pierre-Yves Chibon 610db7
                'last': None,
Pierre-Yves Chibon c12228
                'next': None,
Pierre-Yves Chibon c12228
                'page': 1,
Pierre-Yves Chibon c12228
                'pages': 0,
Pierre-Yves Chibon c12228
                'per_page': 20,
Pierre-Yves Chibon c12228
                'prev': None},
Pierre-Yves Chibon c12228
              'pagination_issues_created': {
Pierre-Yves Chibon 610db7
                'first': None,
Pierre-Yves Chibon 610db7
                'last': None,
Pierre-Yves Chibon c12228
                'next': None,
Pierre-Yves Chibon c12228
                'page': 1,
Pierre-Yves Chibon c12228
                'pages': 0,
Pierre-Yves Chibon c12228
                'per_page': 20,
Pierre-Yves Chibon c12228
                'prev': None},
Pierre-Yves Chibon f8e0d1
              "total_issues_assigned": 0,
Pierre-Yves Chibon f8e0d1
              "total_issues_assigned_pages": 1,
Pierre-Yves Chibon f8e0d1
              "total_issues_created": 0,
Pierre-Yves Chibon f8e0d1
              "total_issues_created_pages": 1
Pierre-Yves Chibon f8e0d1
            }
Pierre-Yves Chibon f8e0d1
        )
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
    def test_user_issues(self):
Pierre-Yves Chibon f8e0d1
        """ Return the list of issues associated with the specified user. """
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon f8e0d1
        output = self.app.get('/api/0/user/pingou/issues')
Pierre-Yves Chibon f8e0d1
        self.assertEqual(output.status_code, 200)
Aurélien Bompard 626417
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon f8e0d1
        issues = []
Pierre-Yves Chibon f8e0d1
        for issue in data['issues_created']:
Pierre-Yves Chibon f8e0d1
            issue['date_created'] = '1513111778'
Pierre-Yves Chibon f8e0d1
            issue['last_updated'] = '1513111778'
Pierre-Yves Chibon f8e0d1
            issue['project']['date_created'] = '1513111778'
Pierre-Yves Chibon f8e0d1
            issue['project']['date_modified'] = '1513111778'
Pierre-Yves Chibon f8e0d1
            issues.append(issue)
Pierre-Yves Chibon f8e0d1
        data['issues_created'] = issues
Pierre-Yves Chibon 610db7
        for k in ['pagination_issues_assigned', 'pagination_issues_created']:
Pierre-Yves Chibon 610db7
            for k2 in ['first', 'last']:
Pierre-Yves Chibon 610db7
                self.assertIsNotNone(data[k][k2])
Pierre-Yves Chibon 610db7
                data[k][k2] = None
Pierre-Yves Chibon f8e0d1
        self.assertEqual(
Pierre-Yves Chibon f8e0d1
            data,
Pierre-Yves Chibon f8e0d1
            {
Pierre-Yves Chibon f8e0d1
              "args": {
Pierre-Yves Chibon f8e0d1
                "assignee": True,
Pierre-Yves Chibon f8e0d1
                "author": True,
Pierre-Yves Chibon 872898
                "closed": None,
Pierre-Yves Chibon 872898
                "created": None,
Pierre-Yves Chibon f8e0d1
                "milestones": [],
Pierre-Yves Chibon f8e0d1
                "no_stones": None,
Pierre-Yves Chibon f8e0d1
                "order": None,
Pierre-Yves Chibon f8e0d1
                "order_key": None,
Pierre-Yves Chibon f8e0d1
                "page": 1,
Pierre-Yves Chibon f8e0d1
                "since": None,
Pierre-Yves Chibon f8e0d1
                "status": None,
Pierre-Yves Chibon 872898
                "tags": [],
Pierre-Yves Chibon 872898
                "updated": None,
Pierre-Yves Chibon f8e0d1
              },
Pierre-Yves Chibon f8e0d1
              "issues_assigned": [],
Pierre-Yves Chibon f8e0d1
              "issues_created": [
Pierre-Yves Chibon f8e0d1
                {
Pierre-Yves Chibon f8e0d1
                  "assignee": None,
Pierre-Yves Chibon f8e0d1
                  "blocks": [],
Pierre-Yves Chibon f8e0d1
                  "close_status": None,
Pierre-Yves Chibon f8e0d1
                  "closed_at": None,
Clement Verna 81c130
                  "closed_by": None,
Pierre-Yves Chibon f8e0d1
                  "comments": [],
Pierre-Yves Chibon f8e0d1
                  "content": "We should work on this",
Pierre-Yves Chibon f8e0d1
                  "custom_fields": [],
Pierre-Yves Chibon f8e0d1
                  "date_created": "1513111778",
Pierre-Yves Chibon f8e0d1
                  "depends": [],
Pierre-Yves Chibon f8e0d1
                  "id": 1,
Pierre-Yves Chibon f8e0d1
                  "last_updated": "1513111778",
Pierre-Yves Chibon f8e0d1
                  "milestone": None,
Pierre-Yves Chibon f8e0d1
                  "priority": None,
Pierre-Yves Chibon f8e0d1
                  "private": False,
Pierre-Yves Chibon f8e0d1
                  "project": {
Pierre-Yves Chibon f8e0d1
                    "access_groups": {
Pierre-Yves Chibon f8e0d1
                      "admin": [],
Pierre-Yves Chibon f8e0d1
                      "commit": [],
Pierre-Yves Chibon f8e0d1
                      "ticket": []
Pierre-Yves Chibon f8e0d1
                    },
Pierre-Yves Chibon f8e0d1
                    "access_users": {
Pierre-Yves Chibon f8e0d1
                      "admin": [],
Pierre-Yves Chibon f8e0d1
                      "commit": [],
Pierre-Yves Chibon f8e0d1
                      "owner": [
Pierre-Yves Chibon f8e0d1
                        "pingou"
Pierre-Yves Chibon f8e0d1
                      ],
Pierre-Yves Chibon f8e0d1
                      "ticket": []
Pierre-Yves Chibon f8e0d1
                    },
Pierre-Yves Chibon f8e0d1
                    "close_status": [
Pierre-Yves Chibon f8e0d1
                      "Invalid",
Pierre-Yves Chibon f8e0d1
                      "Insufficient data",
Pierre-Yves Chibon f8e0d1
                      "Fixed",
Pierre-Yves Chibon f8e0d1
                      "Duplicate"
Pierre-Yves Chibon f8e0d1
                    ],
Pierre-Yves Chibon f8e0d1
                    "custom_keys": [],
Pierre-Yves Chibon f8e0d1
                    "date_created": "1513111778",
Pierre-Yves Chibon f8e0d1
                    "date_modified": "1513111778",
Pierre-Yves Chibon f8e0d1
                    "description": "test project #1",
Pierre-Yves Chibon f8e0d1
                    "fullname": "test",
Pierre-Yves Chibon f8e0d1
                    "id": 1,
Pierre-Yves Chibon f8e0d1
                    "milestones": {},
Pierre-Yves Chibon f8e0d1
                    "name": "test",
Pierre-Yves Chibon f8e0d1
                    "namespace": None,
Pierre-Yves Chibon f8e0d1
                    "parent": None,
Pierre-Yves Chibon f8e0d1
                    "priorities": {},
Pierre-Yves Chibon f8e0d1
                    "tags": [],
Pierre-Yves Chibon f8e0d1
                    "url_path": "test",
Pierre-Yves Chibon f8e0d1
                    "user": {
Pierre-Yves Chibon f8e0d1
                      "fullname": "PY C",
Pierre-Yves Chibon f8e0d1
                      "name": "pingou"
Pierre-Yves Chibon f8e0d1
                    }
Pierre-Yves Chibon f8e0d1
                  },
Pierre-Yves Chibon f8e0d1
                  "status": "Open",
Pierre-Yves Chibon f8e0d1
                  "tags": [],
Pierre-Yves Chibon f8e0d1
                  "title": "Test issue",
Pierre-Yves Chibon f8e0d1
                  "user": {
Pierre-Yves Chibon f8e0d1
                    "fullname": "PY C",
Pierre-Yves Chibon f8e0d1
                    "name": "pingou"
Pierre-Yves Chibon f8e0d1
                  }
Pierre-Yves Chibon f8e0d1
                }
Pierre-Yves Chibon f8e0d1
              ],
Pierre-Yves Chibon c12228
              'pagination_issues_assigned': {
Pierre-Yves Chibon 610db7
                'first': None,
Pierre-Yves Chibon 610db7
                'last': None,
Pierre-Yves Chibon c12228
                'next': None,
Pierre-Yves Chibon c12228
                'page': 1,
Pierre-Yves Chibon c12228
                'pages': 0,
Pierre-Yves Chibon c12228
                'per_page': 20,
Pierre-Yves Chibon c12228
                'prev': None},
Pierre-Yves Chibon c12228
              'pagination_issues_created': {
Pierre-Yves Chibon 610db7
                'first': None,
Pierre-Yves Chibon 610db7
                'last': None,
Pierre-Yves Chibon c12228
                'next': None,
Pierre-Yves Chibon c12228
                'page': 1,
Pierre-Yves Chibon c12228
                'pages': 1,
Pierre-Yves Chibon c12228
                'per_page': 20,
Pierre-Yves Chibon c12228
                'prev': None},
Pierre-Yves Chibon f8e0d1
              "total_issues_assigned": 0,
Pierre-Yves Chibon f8e0d1
              "total_issues_assigned_pages": 1,
Pierre-Yves Chibon f8e0d1
              "total_issues_created": 1,
Pierre-Yves Chibon f8e0d1
              "total_issues_created_pages": 1
Pierre-Yves Chibon f8e0d1
            }
Pierre-Yves Chibon f8e0d1
        )
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon 872898
    def test_user_issues_created(self):
Pierre-Yves Chibon 872898
        """ Return the list of issues associated with the specified user
Pierre-Yves Chibon 872898
        and play with the created filter. """
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        today = datetime.datetime.utcnow().date()
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=%s' % (today.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 1)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        yesterday = today - datetime.timedelta(days=1)
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=%s' % (yesterday.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 1)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        tomorrow = today + datetime.timedelta(days=1)
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=%s' % (tomorrow.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 0)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=..%s' % (yesterday.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 0)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=%s..%s' % (
Pierre-Yves Chibon 872898
                yesterday.isoformat(), today.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 0)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon 872898
        output = self.app.get(
Pierre-Yves Chibon 872898
            '/api/0/user/pingou/issues?created=%s..%s' % (
Pierre-Yves Chibon 872898
                yesterday.isoformat(), tomorrow.isoformat()))
Pierre-Yves Chibon 872898
        self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 872898
        data = json.loads(output.get_data(as_text=True))
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_assigned"], 0)
Pierre-Yves Chibon 872898
        self.assertEqual(data["total_issues_created"], 1)
Pierre-Yves Chibon 872898
Pierre-Yves Chibon f8e0d1
Pierre-Yves Chibon 8fc633
if __name__ == '__main__':
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)