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 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 8fc633
        self.assertEqual(
Pierre-Yves Chibon 8fc633
            data,
Pierre-Yves Chibon 8fc633
            {
Pierre-Yves Chibon 8fc633
                '%s 00:00:00' % date: 4
Pierre-Yves Chibon 8fc633
            }
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_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 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 8fc633
              "description": "pingou created PR test#1",
Pierre-Yves Chibon 8fc633
              "description_mk": "

pingou created PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 1,
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 8fc633
              "description": "pingou comment on PR test#1",
Pierre-Yves Chibon 8fc633
              "description_mk": "

pingou comment on PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 2,
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 8fc633
              "description": "pingou closed PR test#1",
Pierre-Yves Chibon 8fc633
              "description_mk": "

pingou closed PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 3,
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 8fc633
              "description": "pingou comment on PR test#1",
Pierre-Yves Chibon 8fc633
              "description_mk": "

pingou comment on PR test#1

",
Pierre-Yves Chibon 8fc633
              "id": 4,
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
        }
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 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)