From 08b6a4e3d315d1e9a5adb03b8dafa1df19fac7d7 Mon Sep 17 00:00:00 2001 From: Vivek Anand Date: Aug 31 2017 13:50:47 +0000 Subject: Unit test: add ui tests for star project --- diff --git a/tests/test_pagure_flask_ui_star_project.py b/tests/test_pagure_flask_ui_star_project.py new file mode 100644 index 0000000..7b2d417 --- /dev/null +++ b/tests/test_pagure_flask_ui_star_project.py @@ -0,0 +1,268 @@ +# coding=utf-8 + +""" + (c) 2016 - Copyright Red Hat Inc + + Authors: + Pierre-Yves Chibon + +""" + +__requires__ = ['SQLAlchemy >= 0.8'] +import pkg_resources + +import datetime +import json +import unittest +import shutil +import sys +import tempfile +import os + +import pygit2 + +sys.path.insert(0, os.path.join(os.path.dirname( + os.path.abspath(__file__)), '..')) + +import pagure +import pagure.lib +import tests + +class TestStarProjectUI(tests.SimplePagureTest): + def setUp(self): + """ Set up the environnment, ran before every tests. """ + super(TestStarProjectUI, self).setUp() + + pagure.APP.config['TESTING'] = True + pagure.SESSION = self.session + pagure.ui.SESSION = self.session + pagure.ui.app.SESSION = self.session + pagure.ui.filters.SESSION = self.session + pagure.ui.repo.SESSION = self.session + pagure.ui.issues.SESSION = self.session + + 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( + '1', + output.data + ) + elif stars == 0: + self.assertIn( + '0', + 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(pagure.APP, 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(pagure.APP, 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 start """ + + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, 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( + '\n You starred ' + 'this project\n ', + 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( + '\n You unstarred ' + 'this project\n ', + 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(pagure.APP, 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( + '\n You starred ' + 'this project\n ', + 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( + 'Stargazers of test - Pagure', + output.data + ) + self.assertIn( + 'pingou\n ', + output.data + ) + + # make pingou unstar the project + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, 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( + '\n You unstarred ' + 'this project\n ', + 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( + 'Stargazers of test - Pagure', + output.data + ) + self.assertNotIn( + 'pingou\n ', + 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(pagure.APP, 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( + '\n You starred ' + 'this project\n ', + 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( + 'My Starred Projects - Pagure', + output.data + ) + self.assertIn( + 'test\n', + output.data + ) + + # make pingou unstar the project + user = tests.FakeUser() + user.username = 'pingou' + with tests.user_set(pagure.APP, 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( + '\n You unstarred ' + 'this project\n ', + 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( + 'My Starred Projects - Pagure', + output.data + ) + self.assertNotIn( + 'test\n', + output.data + ) + + +if __name__ == '__main__': + unittest.main(verbosity=2)