Blame tests/test_pagure_flask_ui_star_project.py

Vivek Anand 08b6a4
# coding=utf-8
Vivek Anand 08b6a4
Vivek Anand 08b6a4
"""
Vivek Anand 7746b7
 (c) 2017 - Copyright Red Hat Inc
Vivek Anand 08b6a4
Vivek Anand 08b6a4
 Authors:
Vivek Anand 08b6a4
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Vivek Anand e28e4b
   Vivek Anand <vivekanand1101@gmail.com></vivekanand1101@gmail.com>
Vivek Anand 08b6a4
Vivek Anand 08b6a4
"""
Vivek Anand 08b6a4
Aurélien Bompard dcf6f6
from __future__ import unicode_literals
Aurélien Bompard dcf6f6
Vivek Anand 08b6a4
__requires__ = ['SQLAlchemy >= 0.8']
Vivek Anand 08b6a4
import pkg_resources
Vivek Anand 08b6a4
Vivek Anand 08b6a4
import unittest
Vivek Anand 08b6a4
import sys
Vivek Anand 08b6a4
import os
Vivek Anand 08b6a4
Vivek Anand 08b6a4
sys.path.insert(0, os.path.join(os.path.dirname(
Vivek Anand 08b6a4
    os.path.abspath(__file__)), '..'))
Vivek Anand 08b6a4
Pierre-Yves Chibon b130e5
import pagure.config
Vivek Anand 08b6a4
import pagure.lib
Vivek Anand 08b6a4
import tests
Vivek Anand 08b6a4
Vivek Anand e28e4b
Vivek Anand 08b6a4
class TestStarProjectUI(tests.SimplePagureTest):
Vivek Anand 08b6a4
    def setUp(self):
Vivek Anand 08b6a4
        """ Set up the environnment, ran before every tests. """
Vivek Anand 08b6a4
        super(TestStarProjectUI, self).setUp()
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        tests.create_projects(self.session)
Vivek Anand 08b6a4
        tests.create_projects_git(os.path.join(self.path, 'repos'), bare=True)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def _check_star_count(self, data, stars=1):
Vivek Anand 08b6a4
        """ Check if the star count is correct or not """
Vivek Anand 08b6a4
        output = self.app.get(
Vivek Anand 08b6a4
            '/test/', data=data, follow_redirects=True)
Vivek Anand 08b6a4
        if stars == 1:
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '
Vivek Anand e28e4b
                'btn-sm btn-primary">1',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand 08b6a4
            )
Vivek Anand 08b6a4
        elif stars == 0:
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '
Vivek Anand e28e4b
                'btn-sm btn-primary">0',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand 08b6a4
            )
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_star_project_no_project(self):
Vivek Anand 08b6a4
        """ Test the star_project endpoint. """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # No such project
Vivek Anand 08b6a4
        output = self.app.post('/test42/star/1')
Vivek Anand 08b6a4
        self.assertEqual(output.status_code, 404)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_star_project_no_csrf(self):
Vivek Anand 08b6a4
        """ Test the star_project endpoint for the case when there
Vivek Anand 08b6a4
        is no CSRF token given """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            data = {}
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/1', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 400)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_star_project_invalid_star(self):
Vivek Anand 08b6a4
        """ Test the star_project endpoint for invalid star """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/2', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 400)
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=0)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_star_project_valid_star(self):
Vivek Anand 7746b7
        """ Test the star_project endpoint for correct star """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            # try starring the project for pingou
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/1', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You starred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            # check home page of project for star count
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=1)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            # try unstarring the project for pingou
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/0', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You unstarred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=0)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_repo_stargazers(self):
Vivek Anand 08b6a4
        """ Test the repo_stargazers endpoint of pagure.ui.repo """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # make pingou star the project
Vivek Anand 08b6a4
        # first create pingou
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/1', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You starred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=1)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # now, test if pingou's name comes in repo stargazers
Vivek Anand 08b6a4
        output = self.app.get(
Vivek Anand 08b6a4
            '/test/stargazers/'
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Vivek Anand e28e4b
            '<title>Stargazers of test  - Pagure</title>',
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Vivek Anand e28e4b
            'pingou\n              ',
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # make pingou unstar the project
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/0', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You unstarred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=0)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # now, test if pingou's name comes in repo stargazers
Vivek Anand 08b6a4
        # it shouldn't because, he just unstarred
Vivek Anand 08b6a4
        output = self.app.get(
Vivek Anand 08b6a4
            '/test/stargazers/'
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Vivek Anand e28e4b
            '<title>Stargazers of test  - Pagure</title>',
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertNotIn(
Vivek Anand e28e4b
            'pingou\n              ',
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
Vivek Anand 08b6a4
    def test_user_stars(self):
Vivek Anand 08b6a4
        """ Test the user_stars endpoint of pagure.ui.app """
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # make pingou star the project
Vivek Anand 08b6a4
        # first create pingou
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/1', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You starred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=1)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # now, test if the project 'test' comes in pingou's stars
Vivek Anand 08b6a4
        output = self.app.get(
Vivek Anand 08b6a4
            '/user/pingou/stars'
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon da67fd
            "<title>pingou's starred Projects - Pagure</title>",
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Aurélien Bompard 626417
            '', output.get_data(as_text=True))
Aurélien Bompard 626417
        self.assertEqual(output.get_data(as_text=True).count('class="list-group-item"'), 1)
Pierre-Yves Chibon bbecda
        self.assertEqual(
Aurélien Bompard 626417
            output.get_data(as_text=True).count(''),
Pierre-Yves Chibon bbecda
            1)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # make pingou unstar the project
Vivek Anand 08b6a4
        user = tests.FakeUser()
Vivek Anand 08b6a4
        user.username = 'pingou'
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Vivek Anand 08b6a4
            data = {
Vivek Anand 08b6a4
                'csrf_token': csrf_token,
Vivek Anand 08b6a4
            }
Vivek Anand 08b6a4
            output = self.app.post(
Vivek Anand 08b6a4
                '/test/star/0', data=data, follow_redirects=True)
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Vivek Anand e28e4b
                '\n                      You unstarred '
Vivek Anand e28e4b
                'this project\n                    ',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand e28e4b
            )
Vivek Anand 08b6a4
            self._check_star_count(data=data, stars=0)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
        # now, test if test's name comes in pingou's stars
Vivek Anand 08b6a4
        # it shouldn't because, he just unstarred
Vivek Anand 08b6a4
        output = self.app.get(
Vivek Anand 08b6a4
            '/user/pingou/stars/'
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon da67fd
            "<title>pingou's starred Projects - Pagure</title>",
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertNotIn(
Pierre-Yves Chibon bbecda
            'test\n',
Aurélien Bompard 626417
            output.get_data(as_text=True)
Vivek Anand 08b6a4
        )
Pierre-Yves Chibon bbecda
        self.assertEqual(
Aurélien Bompard 626417
            output.get_data(as_text=True).count(''),
Pierre-Yves Chibon bbecda
            0)
Aurélien Bompard 626417
        self.assertEqual(output.get_data(as_text=True).count('class="list-group-item"'), 0)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
Vivek Anand 08b6a4
if __name__ == '__main__':
Vivek Anand 08b6a4
    unittest.main(verbosity=2)