# -*- coding: utf-8 -*- """ (c) 2015-2016 - Copyright Red Hat Inc Authors: Pierre-Yves Chibon """ from __future__ import unicode_literals, absolute_import import unittest import shutil import sys import os import json from mock import patch sys.path.insert( 0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..") ) import pagure.config import tests class PagureFlaskGroupstests(tests.Modeltests): """ Tests for flask groups controller of pagure """ def test_group_lists(self): """ Test the group_lists endpoint. """ output = self.app.get("/groups") self.assertIn( '

\n' ' Groups 0', output.get_data(as_text=True), ) def test_add_group(self): """ Test the add_group endpoint. """ output = self.app.get("/group/add") self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.get("/group/add") self.assertEqual(output.status_code, 403) user.username = "pingou" with tests.user_set(self.app.application, user): output = self.app.get("/group/add") self.assertEqual(output.status_code, 200) self.assertIn( "Create new group", output.get_data(as_text=True), ) self.assertNotIn( '', output.get_data(as_text=True), ) csrf_token = ( output.get_data(as_text=True) .split('name="csrf_token" type="hidden" value="')[1] .split('">')[0] ) data = {} # Insufficient input output = self.app.post("/group/add", data=data) self.assertEqual(output.status_code, 200) self.assertIn( "Create new group", output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count("This field is required."), 3, ) data = { "group_name": "test_group", "display_name": "Test Group", "description": "This is a group for the tests", } # Missing CSRF output = self.app.post("/group/add", data=data) self.assertEqual(output.status_code, 200) self.assertIn( "Create new group", output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count("This field is required."), 0, ) data["csrf_token"] = csrf_token # All good output = self.app.post( "/group/add", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "User `pingou` added to " "the group `test_group`.", output.get_data(as_text=True), ) self.assertIn( "Group `test_group` created.", output.get_data(as_text=True) ) self.assertIn( '

\n' ' Groups 1', output.get_data(as_text=True), ) user = tests.FakeUser( username="pingou", groups=pagure.config.config["ADMIN_GROUP"] ) with tests.user_set(self.app.application, user): output = self.app.get("/group/add") self.assertEqual(output.status_code, 200) self.assertIn( "Create new group", output.get_data(as_text=True), ) self.assertIn( '', output.get_data(as_text=True), ) data = { "group_name": "test_admin_group", "group_type": "admin", "display_name": "Test Admin Group", "description": "This is another group for the tests", "csrf_token": csrf_token, } # All good output = self.app.post( "/group/add", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "User `pingou` added to " "the group `test_admin_group`.", output.get_data(as_text=True), ) self.assertIn( "Group `test_admin_group` " "created.", output.get_data(as_text=True), ) self.assertIn( '

\n' ' Groups 2', output.get_data(as_text=True), ) def test_edit_group(self): """ Test the edit_group endpoint. """ output = self.app.get("/group/test_group/edit") self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.get("/group/test_group/edit") self.assertEqual(output.status_code, 404) self.assertIn( "

Group not found

", output.get_data(as_text=True) ) self.test_add_group() user.username = "foo" with tests.user_set(self.app.application, user): output = self.app.get("/group/foo/edit") self.assertEqual(output.status_code, 404) self.assertIn( "

Group not found

", output.get_data(as_text=True) ) output = self.app.get("/group/test_group/edit") self.assertEqual(output.status_code, 200) self.assertIn( "Edit group: test_group - Pagure", output.get_data(as_text=True), ) self.assertIn( '
', output.get_data(as_text=True), ) self.assertIn( ' ", output.get_data(as_text=True), ) csrf_token = ( output.get_data(as_text=True) .split('name="csrf_token" type="hidden" value="')[1] .split('">')[0] ) # Missing CSRF data = { "group_name": "test_group", "display_name": "Test Group edited", "description": "This is a group for the tests edited", } output = self.app.post( "/group/test_group/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "Edit group: test_group - Pagure", output.get_data(as_text=True), ) self.assertIn( '', output.get_data(as_text=True), ) self.assertIn( ' ", output.get_data(as_text=True), ) # User not allowed data["csrf_token"] = csrf_token output = self.app.post( "/group/test_group/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "Group test_group - Pagure", output.get_data(as_text=True), ) self.assertIn( "You are not " "allowed to edit this group", output.get_data(as_text=True), ) self.assertIn( '

Test Group

', output.get_data(as_text=True), ) user.username = "pingou" with tests.user_set(self.app.application, user): # Invalid repo output = self.app.post( "/group/bar/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 404) self.assertIn( "

Group not found

", output.get_data(as_text=True) ) output = self.app.post( "/group/test_group/edit", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "Group test_group - Pagure", output.get_data(as_text=True), ) self.assertIn( '

Test Group edited

', output.get_data(as_text=True), ) self.assertIn( "Group "Test Group edited" (test_group) edited", output.get_data(as_text=True), ) def test_give_group(self): """ Test the give_group endpoint. """ output = self.app.post("/group/test_group/give") self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.post("/group/test_group/give") self.assertEqual(output.status_code, 404) self.assertIn( "

Group not found

", output.get_data(as_text=True) ) self.test_add_group() user.username = "foo" with tests.user_set(self.app.application, user): output = self.app.post("/group/foo/give") self.assertEqual(output.status_code, 404) self.assertIn( "

Group not found

", output.get_data(as_text=True) ) output = self.app.post("/group/test_group/give") self.assertEqual(output.status_code, 403) csrf_token = self.get_csrf() user.username = "pingou" with tests.user_set(self.app.application, user): # Missing CSRF data = {"username": "invalid"} output = self.app.post( "/group/test_group/give", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "Group test_group - Pagure", output_text ) self.assertIn( 'administered by pingou', output_text, ) # User not found data["csrf_token"] = csrf_token output = self.app.post( "/group/test_group/give", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "Group test_group - Pagure", output_text ) self.assertIn( " No user invalid found to give this group to", output_text, ) self.assertIn( 'administered by pingou', output_text, ) # Working data["username"] = "foo" output = self.app.post( "/group/test_group/give", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) output_text = output.get_data(as_text=True) self.assertIn( "Group test_group - Pagure", output_text ) self.assertIn(" Group given", output_text) self.assertIn( 'administered by foo', output_text ) def test_group_delete(self): """ Test the group_delete endpoint. """ output = self.app.post("/group/foo/delete") self.assertEqual(output.status_code, 302) user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.post("/group/foo/delete", follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( "

No groups have been created on this pagure instance " "yet

", output.get_data(as_text=True), ) self.assertIn( '

\n' ' Groups 0', output.get_data(as_text=True), ) self.test_add_group() with tests.user_set(self.app.application, user): output = self.app.post("/group/foo/delete", follow_redirects=True) self.assertEqual(output.status_code, 200) self.assertIn( '

\n' ' Groups 1', output.get_data(as_text=True), ) output = self.app.get("/new/") csrf_token = ( output.get_data(as_text=True) .split('name="csrf_token" type="hidden" value="')[1] .split('">')[0] ) user.username = "foo" with tests.user_set(self.app.application, user): data = {"csrf_token": csrf_token} output = self.app.post( "/group/bar/delete", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "No group `bar` found", output.get_data(as_text=True) ) self.assertIn( '

\n' ' Groups 1', output.get_data(as_text=True), ) output = self.app.post( "/group/test_group/delete", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "You are not allowed to " "delete the group test_group", output.get_data(as_text=True), ) self.assertIn( '

\n' ' Groups 1', output.get_data(as_text=True), ) user.username = "bar" with tests.user_set(self.app.application, user): output = self.app.post( "/group/test_group/delete", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 404) user.username = "pingou" with tests.user_set(self.app.application, user): output = self.app.post( "/group/test_group/delete", data=data, follow_redirects=True ) self.assertEqual(output.status_code, 200) self.assertIn( "Group `test_group` has " "been deleted", output.get_data(as_text=True), ) self.assertIn( '

\n' ' Groups 0', output.get_data(as_text=True), ) def test_view_group(self): """ Test the view_group endpoint. """ output = self.app.get("/group/foo") self.assertEqual(output.status_code, 404) self.test_add_group() user = tests.FakeUser() with tests.user_set(self.app.application, user): output = self.app.get("/group/test_group") self.assertEqual(output.status_code, 200) self.assertIn( '

Test Group

', output.get_data(as_text=True), ) output = self.app.get("/group/test_admin_group") self.assertEqual(output.status_code, 404) user = tests.FakeUser( username="pingou", groups=pagure.config.config["ADMIN_GROUP"] ) with tests.user_set(self.app.application, user): # Admin can see group of type admins output = self.app.get("/group/test_admin_group") self.assertEqual(output.status_code, 200) self.assertIn( '

Test Admin Group

', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('')[0] ) # No CSRF data = {"user": "bar"} output = self.app.post("/group/test_admin_group", data=data) self.assertEqual(output.status_code, 200) self.assertIn( '

Test Admin Group

', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('
Test Admin Group

', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Admin Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('')[0] ) data = {"csrf_token": csrf_token} output = self.app.post( "/group/test_group/bar/delete", data=data, follow_redirects=True, ) self.assertEqual(output.status_code, 200) self.assertIn("No user `bar` found", output.get_data(as_text=True)) self.assertIn( '

Test Group

', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('
Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('Test Group', output.get_data(as_text=True), ) self.assertEqual( output.get_data(as_text=True).count('