|
Patrick Uiterwijk |
b2cb9c |
# -*- coding: utf-8 -*-
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
"""
|
|
Patrick Uiterwijk |
b2cb9c |
(c) 2015-2018 - Copyright Red Hat Inc
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
Authors:
|
|
Patrick Uiterwijk |
b2cb9c |
Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
|
|
Patrick Uiterwijk |
b2cb9c |
Patrick Uiterwijk <patrick@puiterwijk.org></patrick@puiterwijk.org>
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
"""
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Pierre-Yves Chibon |
67d1cc |
from __future__ import unicode_literals, absolute_import
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
import datetime
|
|
Patrick Uiterwijk |
b2cb9c |
import os
|
|
Patrick Uiterwijk |
b2cb9c |
import shutil
|
|
Patrick Uiterwijk |
b2cb9c |
import sys
|
|
Patrick Uiterwijk |
b2cb9c |
import tempfile
|
|
Patrick Uiterwijk |
b2cb9c |
import time
|
|
Patrick Uiterwijk |
b2cb9c |
import unittest
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
import pygit2
|
|
Patrick Uiterwijk |
b2cb9c |
import six
|
|
Patrick Uiterwijk |
b2cb9c |
from mock import patch, MagicMock
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
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 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
import pagure.lib.git
|
|
Pierre-Yves Chibon |
930073 |
import pagure.lib.query
|
|
Patrick Uiterwijk |
b2cb9c |
import tests
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
from pagure.lib.repo import PagureRepo
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
class PagureLibGitAuthtests(tests.Modeltests):
|
|
Patrick Uiterwijk |
b2cb9c |
""" Tests for pagure.lib.git_auth """
|
|
Pierre-Yves Chibon |
73d120 |
|
|
Pierre-Yves Chibon |
73d120 |
config_values = {"authbackend": "test_auth"}
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
def setUp(self):
|
|
Patrick Uiterwijk |
b2cb9c |
super(PagureLibGitAuthtests, self).setUp()
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
tests.create_projects(self.session)
|
|
Patrick Uiterwijk |
b2cb9c |
tests.create_tokens(self.session)
|
|
Patrick Uiterwijk |
b2cb9c |
tests.create_tokens_acl(self.session)
|
|
Pierre-Yves Chibon |
73d120 |
self.create_project_full("hooktest")
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
def test_edit_with_all_allowed(self):
|
|
Patrick Uiterwijk |
b2cb9c |
"""Tests that editing a file is possible if ACLs say allowed."""
|
|
Patrick Uiterwijk |
b2cb9c |
user = tests.FakeUser()
|
|
Pierre-Yves Chibon |
73d120 |
user.username = "pingou"
|
|
Patrick Uiterwijk |
b2cb9c |
with tests.user_set(self.app.application, user):
|
|
Patrick Uiterwijk |
b2cb9c |
# Add some content to the git repo
|
|
Patrick Uiterwijk |
b2cb9c |
tests.add_content_git_repo(
|
|
Pierre-Yves Chibon |
73d120 |
os.path.join(self.path, "repos", "hooktest.git")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
data = {
|
|
Pierre-Yves Chibon |
73d120 |
"content": "foo\n bar\n baz",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_title": "test commit",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_message": "Online commits from the gure.lib.get",
|
|
Pierre-Yves Chibon |
73d120 |
"email": "bar@pingou.com",
|
|
Pierre-Yves Chibon |
73d120 |
"branch": "master",
|
|
Pierre-Yves Chibon |
73d120 |
"csrf_token": self.get_csrf(),
|
|
Patrick Uiterwijk |
b2cb9c |
}
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/hooktest/edit/master/f/sources",
|
|
Pierre-Yves Chibon |
73d120 |
data=data,
|
|
Pierre-Yves Chibon |
73d120 |
follow_redirects=True,
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertIn(
|
|
Pierre-Yves Chibon |
73d120 |
"<title>Commits - hooktest - Pagure</title>", output_text
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertIn("test commit", output_text)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Check file after the commit
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.get("/hooktest/raw/master/f/sources")
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(output_text, "foo\n bar\n baz")
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
def test_edit_with_all_denied(self):
|
|
Patrick Uiterwijk |
b2cb9c |
"""Tests that editing a file is not possible if ACLs say denied."""
|
|
Patrick Uiterwijk |
b2cb9c |
self.set_auth_status(False)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
user = tests.FakeUser()
|
|
Pierre-Yves Chibon |
73d120 |
user.username = "pingou"
|
|
Patrick Uiterwijk |
b2cb9c |
with tests.user_set(self.app.application, user):
|
|
Patrick Uiterwijk |
b2cb9c |
# Add some content to the git repo
|
|
Patrick Uiterwijk |
b2cb9c |
tests.add_content_git_repo(
|
|
Pierre-Yves Chibon |
73d120 |
os.path.join(self.path, "repos", "hooktest.git")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
data = {
|
|
Pierre-Yves Chibon |
73d120 |
"content": "foo\n bar\n baz",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_title": "test commit",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_message": "Online commits from the gure.lib.get",
|
|
Pierre-Yves Chibon |
73d120 |
"email": "bar@pingou.com",
|
|
Pierre-Yves Chibon |
73d120 |
"branch": "master",
|
|
Pierre-Yves Chibon |
73d120 |
"csrf_token": self.get_csrf(),
|
|
Patrick Uiterwijk |
b2cb9c |
}
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/hooktest/edit/master/f/sources",
|
|
Pierre-Yves Chibon |
73d120 |
data=data,
|
|
Pierre-Yves Chibon |
73d120 |
follow_redirects=True,
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertIn(
|
|
Slavek Kabrda |
536c18 |
"Remote hook declined the push: "
|
|
Pierre-Yves Chibon |
dddc2b |
"Denied push for ref 'refs/heads/master' for user 'pingou'",
|
|
Pierre-Yves Chibon |
73d120 |
output_text,
|
|
Patrick Uiterwijk |
b2cb9c |
)
|
|
Pierre-Yves Chibon |
dddc2b |
self.assertIn("All changes have been rejected", output_text)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Check file after the commit:
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.get("/hooktest/raw/master/f/sources")
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(output_text, "foo\n bar")
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
def test_edit_pr(self):
|
|
Patrick Uiterwijk |
b2cb9c |
"""Tests the ACLs if they only accept PRs."""
|
|
Pierre-Yves Chibon |
73d120 |
self.set_auth_status(
|
|
Pierre-Yves Chibon |
73d120 |
{"refs/heads/master": "pronly", "refs/heads/source": True}
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
user = tests.FakeUser()
|
|
Pierre-Yves Chibon |
73d120 |
user.username = "pingou"
|
|
Patrick Uiterwijk |
b2cb9c |
with tests.user_set(self.app.application, user):
|
|
Patrick Uiterwijk |
b2cb9c |
# Add some content to the git repo
|
|
Patrick Uiterwijk |
b2cb9c |
tests.add_content_git_repo(
|
|
Pierre-Yves Chibon |
73d120 |
os.path.join(self.path, "repos", "hooktest.git")
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Try editing master branch, should fail (only PRs allowed)
|
|
Patrick Uiterwijk |
b2cb9c |
data = {
|
|
Pierre-Yves Chibon |
73d120 |
"content": "foo\n bar\n baz",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_title": "test commit",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_message": "Online commits from the gure.lib.get",
|
|
Pierre-Yves Chibon |
73d120 |
"email": "bar@pingou.com",
|
|
Pierre-Yves Chibon |
73d120 |
"branch": "master",
|
|
Pierre-Yves Chibon |
73d120 |
"csrf_token": self.get_csrf(),
|
|
Patrick Uiterwijk |
b2cb9c |
}
|
|
Patrick Uiterwijk |
b2cb9c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/hooktest/edit/master/f/sources",
|
|
Pierre-Yves Chibon |
73d120 |
data=data,
|
|
Pierre-Yves Chibon |
73d120 |
follow_redirects=True,
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertIn(
|
|
Slavek Kabrda |
536c18 |
"Remote hook declined the push: "
|
|
Pierre-Yves Chibon |
dddc2b |
"Denied push for ref 'refs/heads/master' for user 'pingou'",
|
|
Pierre-Yves Chibon |
73d120 |
output_text,
|
|
Patrick Uiterwijk |
b2cb9c |
)
|
|
Pierre-Yves Chibon |
dddc2b |
self.assertIn("All changes have been rejected", output_text)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Change something in the "source" branch
|
|
Patrick Uiterwijk |
b2cb9c |
data = {
|
|
Pierre-Yves Chibon |
73d120 |
"content": "foo\n bar\n baz",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_title": "test commit",
|
|
Pierre-Yves Chibon |
73d120 |
"commit_message": "Online commits from the gure.lib.get",
|
|
Pierre-Yves Chibon |
73d120 |
"email": "bar@pingou.com",
|
|
Pierre-Yves Chibon |
73d120 |
"branch": "source",
|
|
Pierre-Yves Chibon |
73d120 |
"csrf_token": self.get_csrf(),
|
|
Patrick Uiterwijk |
b2cb9c |
}
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/hooktest/edit/master/f/sources",
|
|
Pierre-Yves Chibon |
73d120 |
data=data,
|
|
Pierre-Yves Chibon |
73d120 |
follow_redirects=True,
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertIn(
|
|
Pierre-Yves Chibon |
73d120 |
"<title>Commits - hooktest - Pagure</title>", output_text
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertIn("test commit", output_text)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Check file after the commit:
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.get("/hooktest/raw/source/f/sources")
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(output_text, "foo\n bar\n baz")
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Create the PRs
|
|
Pierre-Yves Chibon |
73d120 |
project = pagure.lib.query.get_authorized_project(
|
|
Pierre-Yves Chibon |
73d120 |
self.session, "hooktest"
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
930073 |
req = pagure.lib.query.new_pull_request(
|
|
Patrick Uiterwijk |
b2cb9c |
session=self.session,
|
|
Patrick Uiterwijk |
b2cb9c |
repo_from=project,
|
|
Patrick Uiterwijk |
b2cb9c |
branch_from="source",
|
|
Patrick Uiterwijk |
b2cb9c |
repo_to=project,
|
|
Pierre-Yves Chibon |
73d120 |
branch_to="master",
|
|
Pierre-Yves Chibon |
73d120 |
title="PR to master",
|
|
Pierre-Yves Chibon |
73d120 |
user="pingou",
|
|
Patrick Uiterwijk |
b2cb9c |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.session.add(req)
|
|
Patrick Uiterwijk |
b2cb9c |
self.session.commit()
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Check file before the merge
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.get("/hooktest/raw/master/f/sources")
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(output_text, "foo\n bar")
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Try to merge (should work)
|
|
Patrick Uiterwijk |
b2cb9c |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/hooktest/pull-request/1/merge",
|
|
Pierre-Yves Chibon |
73d120 |
data=data,
|
|
Pierre-Yves Chibon |
73d120 |
follow_redirects=True,
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertIn(
|
|
Pierre-Yves Chibon |
73d120 |
"<title>Overview - hooktest - Pagure</title>", output_text
|
|
Patrick Uiterwijk |
b2cb9c |
)
|
|
Patrick Uiterwijk |
b2cb9c |
|
|
Patrick Uiterwijk |
b2cb9c |
# Check file after the merge
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.get("/hooktest/raw/master/f/sources")
|
|
Patrick Uiterwijk |
b2cb9c |
self.assertEqual(output.status_code, 200)
|
|
Patrick Uiterwijk |
b2cb9c |
output_text = output.get_data(as_text=True)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(output_text, "foo\n bar\n baz")
|