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 8fc633
 (c) 2015-2016 - 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 8fc633
__requires__ = ['SQLAlchemy >= 0.8']
Pierre-Yves Chibon 8fc633
import pkg_resources
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
import datetime
Pierre-Yves Chibon 8fc633
import unittest
Pierre-Yves Chibon 8fc633
import shutil
Pierre-Yves Chibon 8fc633
import sys
Pierre-Yves Chibon 8fc633
import os
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 8fc633
import pagure.lib
Pierre-Yves Chibon d6ddc4
import pagure.lib.model as model
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 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 8fc633
        pagure.APP.config['TESTING'] = True
Pierre-Yves Chibon 8fc633
        pagure.SESSION = self.session
Pierre-Yves Chibon 8fc633
        pagure.api.SESSION = self.session
Pierre-Yves Chibon 8fc633
        pagure.api.fork.SESSION = self.session
Pierre-Yves Chibon 8fc633
        pagure.api.user.SESSION = self.session
Pierre-Yves Chibon 8fc633
        pagure.lib.SESSION = self.session
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        pagure.APP.config['REQUESTS_FOLDER'] = None
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
        self.app = pagure.APP.test_client()
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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
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
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 8fc633
        repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 8fc633
        forked_repo = pagure.lib.get_project(self.session, 'test')
Pierre-Yves Chibon 8fc633
        req = pagure.lib.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
            requestfolder=None,
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
Pierre-Yves Chibon 8fc633
        request = pagure.lib.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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
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
Pierre-Yves Chibon 8fc633
        request = pagure.lib.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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
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
Pierre-Yves Chibon 8fc633
        request = pagure.lib.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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
Pierre-Yves Chibon 8fc633
        date = datetime.datetime.utcnow().date().strftime('%Y-%m-%d')
Pierre-Yves Chibon f1bbc3
        # There seems to be a difference in the JSON generated between
Pierre-Yves Chibon f1bbc3
        # flask-0.10.1 (F23) and 0.11.1 (jenkins)
Pierre-Yves Chibon 7507e1
        self.assertTrue(
Pierre-Yves Chibon f1bbc3
            data == {date: 4}
Pierre-Yves Chibon f1bbc3
            or
Pierre-Yves Chibon f1bbc3
            data == [[date, 4]]
Pierre-Yves Chibon f1bbc3
        )
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
        }
Pierre-Yves Chibon fa2aba
        self.assertEqual(json.loads(output.data), 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
        }
Pierre-Yves Chibon fa2aba
        self.assertEqual(json.loads(output.data), 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)
Pierre-Yves Chibon 8fc633
        data = json.loads(output.data)
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 8fc633
              "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 8fc633
              "description_mk": "

pingou comment 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 8fc633
              "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 8fc633
              "description_mk": "

pingou comment 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 d6ddc4
        repo = pagure.lib.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)
Pierre-Yves Chibon d6ddc4
        data = json.loads(output.data)
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
Pierre-Yves Chibon 8fc633
Pierre-Yves Chibon 8fc633
if __name__ == '__main__':
Pierre-Yves Chibon 8fc633
    SUITE = unittest.TestLoader().loadTestsFromTestCase(
Pierre-Yves Chibon 8fc633
        PagureFlaskApiUSertests)
Pierre-Yves Chibon 8fc633
    unittest.TextTestRunner(verbosity=2).run(SUITE)