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