Blame tests/test_pagure_flask_ui_groups.py

Pierre-Yves Chibon 19656f
# -*- coding: utf-8 -*-
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
"""
Pierre-Yves Chibon 9fae46
 (c) 2015-2016 - Copyright Red Hat Inc
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
 Authors:
Pierre-Yves Chibon 19656f
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
"""
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 67d1cc
from __future__ import unicode_literals, absolute_import
Aurélien Bompard dcf6f6
Pierre-Yves Chibon 19656f
import unittest
Pierre-Yves Chibon 19656f
import shutil
Pierre-Yves Chibon 19656f
import sys
Pierre-Yves Chibon 19656f
import os
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
import json
Pierre-Yves Chibon 19656f
from mock import patch
Pierre-Yves Chibon 19656f
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
)
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 930073
import pagure.config
Pierre-Yves Chibon 19656f
import tests
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
class PagureFlaskGroupstests(tests.Modeltests):
Pierre-Yves Chibon 19656f
    """ Tests for flask groups controller of pagure """
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 19656f
    def test_group_lists(self):
Pierre-Yves Chibon 19656f
        """ Test the group_lists endpoint. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/groups")
Pierre-Yves Chibon 0a88aa
        self.assertIn(
Pierre-Yves Chibon b1ec6e
            '

\n'

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

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 1',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 860f53
Pierre-Yves Chibon 860f53
        user = tests.FakeUser(
Pierre-Yves Chibon 73d120
            username="pingou", groups=pagure.config.config["ADMIN_GROUP"]
Pierre-Yves Chibon 73d120
        )
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/group/add")
Pierre-Yves Chibon 860f53
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Create new group",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                '<option value="admin">admin</option>',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 860f53
Pierre-Yves Chibon 860f53
            data = {
Pierre-Yves Chibon 73d120
                "group_name": "test_admin_group",
Pierre-Yves Chibon 73d120
                "group_type": "admin",
Pierre-Yves Chibon 73d120
                "display_name": "Test Admin Group",
Pierre-Yves Chibon 73d120
                "description": "This is another group for the tests",
Pierre-Yves Chibon 73d120
                "csrf_token": csrf_token,
Pierre-Yves Chibon 860f53
            }
Pierre-Yves Chibon 860f53
Pierre-Yves Chibon 860f53
            # All good
Pierre-Yves Chibon 860f53
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/add", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 860f53
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 860f53
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `pingou` added to " "the group `test_admin_group`.",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 860f53
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Group `test_admin_group` " "created.",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0a88aa
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 2',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 860f53
Pierre-Yves Chibon 0148d9
    def test_edit_group(self):
Pierre-Yves Chibon 0148d9
        """ Test the edit_group endpoint. """
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 73d120
        output = self.app.get("/group/test_group/edit")
Pierre-Yves Chibon 0148d9
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/group/test_group/edit")
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

Group not found

", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
        self.test_add_group()
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 73d120
        user.username = "foo"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/group/foo/edit")
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

Group not found

", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 73d120
            output = self.app.get("/group/test_group/edit")
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Edit group: test_group - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 0148d9
                '<form action="/group/test_group/edit" method="post">',</form>
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 615c23
                '<label for="description">Description'</label>
Pierre-Yves Chibon 73d120
                "",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
            # Missing CSRF
Pierre-Yves Chibon 0148d9
            data = {
Pierre-Yves Chibon 73d120
                "group_name": "test_group",
Pierre-Yves Chibon 73d120
                "display_name": "Test Group edited",
Pierre-Yves Chibon 73d120
                "description": "This is a group for the tests edited",
Pierre-Yves Chibon 0148d9
            }
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/edit", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Edit group: test_group - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 0148d9
                '<form action="/group/test_group/edit" method="post">',</form>
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 615c23
                '<label for="description">Description'</label>
Pierre-Yves Chibon 73d120
                "",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
            # User not allowed
Pierre-Yves Chibon 73d120
            data["csrf_token"] = csrf_token
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/edit", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Group test_group - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You are not " "allowed to edit this group",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 0148d9
            # Invalid repo
Pierre-Yves Chibon 0148d9
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/bar/edit", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

Group not found

", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 0148d9
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/edit", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "<title>Group test_group - Pagure</title>",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 615c23
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group edited

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Group "Test Group edited" (test_group) edited",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0148d9
Pierre-Yves Chibon 3448fc
    def test_give_group(self):
Pierre-Yves Chibon 3448fc
        """ Test the give_group endpoint. """
Pierre-Yves Chibon 3448fc
Pierre-Yves Chibon 73d120
        output = self.app.post("/group/test_group/give")
Pierre-Yves Chibon 3448fc
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon 3448fc
Pierre-Yves Chibon 3448fc
        user = tests.FakeUser()
Pierre-Yves Chibon 3448fc
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/test_group/give")
Pierre-Yves Chibon 3448fc
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

Group not found

", output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 3448fc
Pierre-Yves Chibon 3448fc
        self.test_add_group()
Pierre-Yves Chibon 3448fc
Pierre-Yves Chibon 73d120
        user.username = "foo"
Pierre-Yves Chibon 3448fc
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/foo/give")
Pierre-Yves Chibon 3448fc
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 73d120
            self.assertIn(
Pierre-Yves Chibon 73d120
                "

Group not found

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

No groups have been created on this pagure instance "

Pierre-Yves Chibon 73d120
                "yet

",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0a88aa
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 0',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon f297f4
        self.test_add_group()
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/foo/delete", follow_redirects=True)
Pierre-Yves Chibon f297f4
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 0a88aa
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

\n'

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

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 1',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon f297f4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/delete", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f297f4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "You are not allowed to " "delete the group test_group",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0a88aa
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 1',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon 73d120
        user.username = "bar"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon f297f4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/delete", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon f297f4
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/delete", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon f297f4
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Group `test_group` has " "been deleted",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 0a88aa
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

\n'

Pierre-Yves Chibon b1ec6e
                '      Groups 0',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon f297f4
Pierre-Yves Chibon 8abb7d
    def test_view_group(self):
Pierre-Yves Chibon 8abb7d
        """ Test the view_group endpoint. """
Pierre-Yves Chibon 73d120
        output = self.app.get("/group/foo")
Pierre-Yves Chibon 8abb7d
        self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 8abb7d
        self.test_add_group()
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 8abb7d
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon 73d120
            output = self.app.get("/group/test_group")
Pierre-Yves Chibon 8abb7d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

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

Test Admin Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 8abb7d
            # No CSRF
Pierre-Yves Chibon 73d120
            data = {"user": "bar"}
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/test_admin_group", data=data)
Pierre-Yves Chibon 8abb7d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Admin Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 8abb7d
            # Invalid user
Pierre-Yves Chibon 73d120
            data = {"user": "bar", "csrf_token": csrf_token}
Pierre-Yves Chibon 8abb7d
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_admin_group", data=data, follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            self.assertIn("No user `bar` found", output.get_data(as_text=True))
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Admin Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon 8abb7d
            # All good
Pierre-Yves Chibon 73d120
            data = {"user": "foo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/test_admin_group", data=data)
Pierre-Yves Chibon 8abb7d
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 8abb7d
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `foo` added to the " "group `test_admin_group`.",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Admin Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon 8abb7d
Pierre-Yves Chibon fa37e7
    def test_group_user_delete(self):
Pierre-Yves Chibon fa37e7
        """ Test the group_user_delete endpoint. """
Pierre-Yves Chibon 73d120
        output = self.app.post("/group/foo/bar/delete")
Pierre-Yves Chibon fa37e7
        self.assertEqual(output.status_code, 302)
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/foo/bar/delete", follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 404)
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
        self.test_add_group()
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
        user = tests.FakeUser()
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/bar/delete", follow_redirects=True
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon 73d120
            output = self.app.get("/new/")
Pierre-Yves Chibon 73d120
            csrf_token = (
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True)
Pierre-Yves Chibon 73d120
                .split('name="csrf_token" type="hidden" value="')[1]
Pierre-Yves Chibon 73d120
                .split('">')[0]
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon 73d120
            data = {"csrf_token": csrf_token}
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/bar/delete",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon 73d120
            self.assertIn("No user `bar` found", output.get_data(as_text=True))
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/foo/delete",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa37e7
            self.assertIn(
Pierre-Yves Chibon 73d120
                "Could not find user " "username",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon 73d120
        user.username = "pingou"
Pierre-Yves Chibon b130e5
        with tests.user_set(self.app.application, user):
Pierre-Yves Chibon fa37e7
            # User not in the group
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/foo/delete",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa37e7
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `foo` could not be " "found in the group `test_group`",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
            # Cannot delete creator
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/foo/delete",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa37e7
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `foo` could not be " "found in the group `test_group`",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
            # Add user foo
Pierre-Yves Chibon 73d120
            data = {"user": "foo", "csrf_token": csrf_token}
Pierre-Yves Chibon 73d120
            output = self.app.post("/group/test_group", data=data)
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa37e7
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `foo` added to the " "group `test_group`.",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon fa37e7
            output = self.app.post(
Pierre-Yves Chibon 73d120
                "/group/test_group/foo/delete",
Pierre-Yves Chibon 73d120
                data=data,
Pierre-Yves Chibon 73d120
                follow_redirects=True,
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
            self.assertEqual(output.status_code, 200)
Pierre-Yves Chibon fa37e7
            self.assertIn(
Pierre-Yves Chibon 73d120
                "User `foo` removed from " "the group `test_group`",
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon eaef3a
            self.assertIn(
Pierre-Yves Chibon b1ec6e
                '

Test Group

',
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True),
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon b1ec6e
            self.assertEqual(
Pierre-Yves Chibon 73d120
                output.get_data(as_text=True).count('
Pierre-Yves Chibon 73d120
            )
Pierre-Yves Chibon fa37e7
Pierre-Yves Chibon 19656f
Pierre-Yves Chibon 73d120
if __name__ == "__main__":
Pierre-Yves Chibon 393f31
    unittest.main(verbosity=2)