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(
Ryan Lerch 6d2e06
                '
Ryan Lerch 6d2e06
                'btn-primary font-weight-bold">1\n',
Aurélien Bompard 626417
                output.get_data(as_text=True)
Vivek Anand 08b6a4
            )
Vivek Anand 08b6a4
        elif stars == 0:
Vivek Anand 08b6a4
            self.assertIn(
Ryan Lerch 6d2e06
                '
Ryan Lerch 6d2e06
                'btn-primary font-weight-bold">0\n',
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(
Ryan Lerch 90916a
                'You starred '
Ryan Lerch 90916a
                'this project',
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(
Ryan Lerch 90916a
                'You unstarred '
Ryan Lerch 90916a
                'this project',
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(
Ryan Lerch 90916a
                'You starred '
Ryan Lerch 90916a
                'this project',
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(
Pierre-Yves Chibon 94fee4
            'pingou',
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(
Ryan Lerch 90916a
                'You unstarred '
Ryan Lerch 90916a
                'this project',
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(
Pierre-Yves Chibon 94fee4
            'pingou',
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
Pierre-Yves Chibon f0babf
        # Check pingou's stars before
Pierre-Yves Chibon f0babf
        output = self.app.get('/user/pingou/stars/')
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon f0babf
        self.assertIn(
Pierre-Yves Chibon f0babf
            "<title>pingou - starred - Pagure</title>", output_text
Pierre-Yves Chibon f0babf
        )
Pierre-Yves Chibon f0babf
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon f0babf
            'border-0 ml-auto font-weight-bold">0 projects',
Pierre-Yves Chibon f0babf
            output_text)
Pierre-Yves Chibon f0babf
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(
Ryan Lerch 90916a
                'You starred '
Ryan Lerch 90916a
                'this project',
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
Pierre-Yves Chibon f0babf
        output = self.app.get('/user/pingou/stars/')
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon f0babf
            "<title>pingou - starred - Pagure</title>", output_text
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon f0babf
            'border-0 ml-auto font-weight-bold">1 projects',
Pierre-Yves Chibon f0babf
            output_text)
Pierre-Yves Chibon f0babf
        self.assertEqual(
Pierre-Yves Chibon f0babf
            output_text.count('class="list-group-item"'), 1)
Pierre-Yves Chibon f0babf
        self.assertEqual(
Pierre-Yves Chibon f0babf
            output_text.count('
'), 2)
Ryan Lerch 6d2e06
Pierre-Yves Chibon f0babf
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon f0babf
        self.assertEqual(len(repo.stargazers), 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(
Pierre-Yves Chibon f0babf
                'You unstarred this project',
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
Pierre-Yves Chibon f0babf
        self.session = pagure.lib.create_session(self.dbpath)
Pierre-Yves Chibon f0babf
        repo = pagure.lib.get_authorized_project(self.session, 'test')
Pierre-Yves Chibon f0babf
        self.assertEqual(len(repo.stargazers), 0)
Pierre-Yves Chibon f0babf
Vivek Anand 08b6a4
        # now, test if test's name comes in pingou's stars
Vivek Anand 08b6a4
        # it shouldn't because, he just unstarred
Pierre-Yves Chibon f0babf
        output = self.app.get('/user/pingou/stars/')
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon f0babf
            "<title>pingou - starred - Pagure</title>", output_text
Vivek Anand 08b6a4
        )
Pierre-Yves Chibon f0babf
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon f0babf
            'border-0 ml-auto font-weight-bold">0 projects',
Pierre-Yves Chibon f0babf
            output_text)
Pierre-Yves Chibon f0babf
        self.assertEqual(
Pierre-Yves Chibon f0babf
            output_text.count('class="list-group-item"'), 0)
Pierre-Yves Chibon bbecda
        self.assertEqual(
Pierre-Yves Chibon f0babf
            output_text.count('
'), 1)
Vivek Anand 08b6a4
Vivek Anand 08b6a4
Vivek Anand 08b6a4
if __name__ == '__main__':
Vivek Anand 08b6a4
    unittest.main(verbosity=2)