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
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Vivek Anand 08b6a4
import unittest
Vivek Anand 08b6a4
import sys
Vivek Anand 08b6a4
import os
Vivek Anand 08b6a4
Pierre-Yves Chibon 73d120
sys.path.insert(
Pierre-Yves Chibon 73d120
    0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
Pierre-Yves Chibon 73d120
)
Vivek Anand 08b6a4
Pierre-Yves Chibon b130e5
import pagure.config
Pierre-Yves Chibon 930073
import pagure.lib.query
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)
Pierre-Yves Chibon 73d120
        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 """
Pierre-Yves Chibon 73d120
        output = self.app.get("/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',
Pierre-Yves Chibon 73d120
                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',
Pierre-Yves Chibon 73d120
                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
Pierre-Yves Chibon 73d120
        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()
Pierre-Yves Chibon 73d120
        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(
Pierre-Yves Chibon 73d120
                "/test/star/1", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
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()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/2", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
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()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            # try starring the project for pingou
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/1", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You starred this project", 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(
Pierre-Yves Chibon 73d120
                "/test/star/0", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You unstarred this project", 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()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/1", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You starred this project", 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
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/stargazers/")
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Stargazers of test  - Pagure</title>",
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon 94fee4
            'pingou',
Pierre-Yves Chibon 73d120
            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()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/0", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You unstarred this project", 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
Pierre-Yves Chibon 73d120
        output = self.app.get("/test/stargazers/")
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon 73d120
            "<title>Stargazers of test  - Pagure</title>",
Pierre-Yves Chibon 73d120
            output.get_data(as_text=True),
Vivek Anand 08b6a4
        )
Vivek Anand 08b6a4
        self.assertNotIn(
Pierre-Yves Chibon 94fee4
            'pingou',
Pierre-Yves Chibon 73d120
            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 73d120
        output = self.app.get("/user/pingou/stars/")
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
Pierre-Yves Chibon f0babf
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon 4f046d
            'border-0 ml-auto font-weight-bold">0 Project',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon f0babf
Vivek Anand 08b6a4
        # make pingou star the project
Vivek Anand 08b6a4
        # first create pingou
Vivek Anand 08b6a4
        user = tests.FakeUser()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/1", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You starred this project", 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 73d120
        output = self.app.get("/user/pingou/stars/")
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
Vivek Anand 08b6a4
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon 4f046d
            'border-0 ml-auto font-weight-bold">1 Project',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(output_text.count('class="list-group-item"'), 1)
Pierre-Yves Chibon f0babf
        self.assertEqual(
Pierre-Yves Chibon 73d120
            output_text.count('
'), 2
Pierre-Yves Chibon 73d120
        )
Ryan Lerch 6d2e06
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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()
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Vivek Anand 08b6a4
            csrf_token = self.get_csrf()
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Vivek Anand 08b6a4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/test/star/0", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Vivek Anand 08b6a4
            self.assertEqual(output.status_code, 200)
Vivek Anand 08b6a4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You unstarred this project", 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 930073
        self.session = pagure.lib.query.create_session(self.dbpath)
Pierre-Yves Chibon 73d120
        repo = pagure.lib.query.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 73d120
        output = self.app.get("/user/pingou/stars/")
Pierre-Yves Chibon f0babf
        output_text = output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
        self.assertIn("<title>pingou - starred - Pagure</title>", output_text)
Pierre-Yves Chibon f0babf
        self.assertIn(
Pierre-Yves Chibon f0babf
            '
Pierre-Yves Chibon 4f046d
            'border-0 ml-auto font-weight-bold">0 Project',
Pierre-Yves Chibon 73d120
            output_text,
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon 73d120
        self.assertEqual(output_text.count('class="list-group-item"'), 0)
Pierre-Yves Chibon bbecda
        self.assertEqual(
Pierre-Yves Chibon 73d120
            output_text.count('
'), 1
Pierre-Yves Chibon 73d120
        )
Vivek Anand 08b6a4
Vivek Anand 08b6a4
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Vivek Anand 08b6a4
    unittest.main(verbosity=2)