Blame tests/test_pagure_lib_star_project.py

Vivek Anand 327a4f
# coding=utf-8
Vivek Anand 327a4f
"""
Vivek Anand 327a4f
 (c) 2015-2017 - Copyright Red Hat Inc
Vivek Anand 327a4f
Vivek Anand 327a4f
 Authors:
Vivek Anand 327a4f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Vivek Anand 327a4f
   Vivek Anand <vivekanand1101@gmail.com></vivekanand1101@gmail.com>
Vivek Anand 327a4f
Vivek Anand 327a4f
"""
Vivek Anand 327a4f
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Vivek Anand 327a4f
__requires__ = ['SQLAlchemy >= 0.8']
Vivek Anand 327a4f
import pkg_resources
Vivek Anand 327a4f
Vivek Anand 327a4f
import unittest
Vivek Anand 327a4f
import sys
Vivek Anand 327a4f
import os
Vivek Anand 327a4f
Vivek Anand 327a4f
sys.path.insert(0, os.path.join(os.path.dirname(
Vivek Anand 327a4f
    os.path.abspath(__file__)), '..'))
Vivek Anand 327a4f
Vivek Anand 327a4f
import pagure
Vivek Anand 327a4f
import pagure.lib
Vivek Anand 327a4f
import tests
Vivek Anand 327a4f
Vivek Anand e28e4b
Vivek Anand 327a4f
class TestStarProjectLib(tests.SimplePagureTest):
Vivek Anand 327a4f
    ''' Test the star project feature of pagure '''
Vivek Anand 327a4f
Vivek Anand 327a4f
    def setUp(self):
Vivek Anand 327a4f
        """ Set up the environnment for running each star project lib tests """
Vivek Anand 327a4f
        super(TestStarProjectLib, self).setUp()
Vivek Anand 327a4f
        tests.create_projects(self.session)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_update_star_project(self):
Vivek Anand 327a4f
        ''' Test the update_star_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        # test with invalud Star object, should return None
Vivek Anand 327a4f
        msg = pagure.lib.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None,
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test starring the project
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        msg = pagure.lib.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            '1',
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test unstarring the project
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        msg = pagure.lib.update_star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            '0',
Vivek Anand 327a4f
            'pingou',
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_star_project(self):
Vivek Anand 327a4f
        ''' Test the _star_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test with not all arguments present
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        msg = pagure.lib._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Vivek Anand 327a4f
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_unstar_project(self):
Vivek Anand 327a4f
        ''' Test the _unstar_project endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # test with not all arguments present
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        msg = pagure.lib._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            None
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, None)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # the user hasn't starred the project before
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(msg, 'You never starred the project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star it for testing
Vivek Anand 327a4f
        msg = pagure.lib._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # the user starred and wishes to unstar
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_get_stargazer_obj(self):
Vivek Anand 327a4f
        ''' Test the _get_stargazer_obj test of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star the project first
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # get the object now
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        star_obj = pagure.lib._get_stargazer_obj(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(isinstance(star_obj, pagure.lib.model.Star), True)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # unstar it and then try to get the object
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # we don't store if the user has unstarred, we delete the obj
Vivek Anand 327a4f
        # so, we should get anything back in the query
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand 327a4f
        star_obj = pagure.lib._get_stargazer_obj(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(star_obj is None, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
    def test_has_starred(self):
Vivek Anand 327a4f
        ''' Test the has_starred endpoint of pagure.lib '''
Vivek Anand 327a4f
Vivek Anand 327a4f
        # star the project
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._star_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You starred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 1)
Vivek Anand 327a4f
Vivek Anand 327a4f
        has_starred = pagure.lib.has_starred(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            'pingou'
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(has_starred is True, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # unstar it and then test for has_starred
Vivek Anand e28e4b
        repo_obj = pagure.lib._get_project(self.session, 'test')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand 327a4f
        msg = pagure.lib._unstar_project(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand e28e4b
            user_obj,
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.session.commit()
Vivek Anand 327a4f
        self.assertEqual(msg, 'You unstarred this project')
Vivek Anand e28e4b
        user_obj = pagure.lib.get_user(self.session, 'pingou')
Vivek Anand e28e4b
        self.assertEqual(len(user_obj.stars), 0)
Vivek Anand 327a4f
Vivek Anand 327a4f
        # check now, it should return False
Vivek Anand 327a4f
        has_starred = pagure.lib.has_starred(
Vivek Anand 327a4f
            self.session,
Vivek Anand e28e4b
            repo_obj,
Vivek Anand 327a4f
            'pingou'
Vivek Anand 327a4f
        )
Vivek Anand 327a4f
        self.assertEqual(has_starred is False, True)
Vivek Anand 327a4f
Vivek Anand 327a4f
Vivek Anand 327a4f
if __name__ == '__main__':
Vivek Anand 327a4f
    unittest.main(verbosity=2)