1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
|
# -*- coding: utf-8 -*-
"""
(c) 2018 - Copyright Red Hat Inc
Authors:
Pierre-Yves Chibon <pingou@pingoured.fr>
"""
from __future__ import unicode_literals, absolute_import
import json
import unittest
import sys
import os
from unittest.mock import patch, MagicMock
sys.path.insert(
0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "..")
)
import pagure.lib.query # noqa
import tests # noqa
class PagureFlaskIssuesReadOnlytests(tests.Modeltests):
"""Tests for flask issues controller of pagure with read-only tickets"""
@patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
def setUp(self):
""" Set up the environnment, ran before every tests. """
super(PagureFlaskIssuesReadOnlytests, self).setUp()
tests.create_projects(self.session)
tests.create_projects_git(os.path.join(self.path, "repos"))
# Make the project's issue tracker read-only
repo = pagure.lib.query.get_authorized_project(self.session, "test")
settings = repo.settings
settings["issue_tracker_read_only"] = True
repo.settings = settings
self.session.add(repo)
self.session.commit()
# Create a couple of issue
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue #1",
content="We should work on this for the second time",
user="foo",
status="Open",
private=True,
)
self.session.commit()
self.assertEqual(msg.title, "Test issue #1")
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue #2",
content="We should work on this for the second time",
user="foo",
status="Open",
private=False,
)
self.session.commit()
self.assertEqual(msg.title, "Test issue #2")
def test_issue_list_authenticated_commit(self):
"""Test the list of issues when user is authenticated and has
access to the project.
"""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.get("/test/issues")
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn("<title>Issues - test - Pagure</title>", output_text)
self.assertIn(
'<span class="fa fa-fw fa-exclamation-circle"></span>'
" 2 Open Issues\n",
output_text,
)
def test_field_comment(self):
"""Test if the field commit is present on the issue page."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.get("/test/issue/1")
self.assertEqual(output.status_code, 200)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Issue #1: Test issue #1 - test - Pagure</title>",
output_text,
)
self.assertNotIn(
'value="Update Issue" title="Comment and Update Metadata" '
"tabindex=2 />",
output_text,
)
self.assertIn("This issue tracker is read-only.", output_text)
def test_update_ticket(self):
"""Test updating a ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post(
"/test/issue/1/update", data={}, follow_redirects=True
)
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_edit_comment(self):
"""Test editing a comment from a ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post(
"/test/issue/1/comment/1/edit", data={}, follow_redirects=True
)
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_edit_ticket(self):
"""Test editing a ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post(
"/test/issue/1/edit", data={}, follow_redirects=True
)
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_new_issue(self):
"""Test creating a new ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/test/new_issue/", data={})
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_deleting_issue(self):
"""Test deleting a new ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/test/issue/1/drop", data={})
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_uploading_to_issue(self):
"""Test uploading to a new ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/test/issue/1/upload", data={})
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
class PagureFlaskAPIIssuesReadOnlytests(PagureFlaskIssuesReadOnlytests):
"""Tests for flask API issues controller of pagure with read-only tickets"""
@patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
def setUp(self):
""" Set up the environnment, ran before every tests. """
super(PagureFlaskAPIIssuesReadOnlytests, self).setUp()
def test_api_new_issue(self):
"""Test creating a new ticket."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/new_issue", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_change_status_issue(self):
""" Test closing a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/status", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_change_milestone_issue(self):
""" Test change the milestone of a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/milestone", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_comment_issue(self):
""" Test comment on a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/comment", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_assign_issue(self):
""" Test assigning a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/assign", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_subscribe_issue(self):
""" Test subscribing to a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/subscribe", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_update_custom_field(self):
""" Test updating a specific custom fields on a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/custom/foo", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
def test_api_update_custom_fields(self):
""" Test updating custom fields on a ticket. """
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/api/0/test/issue/1/custom", data={})
self.assertEqual(output.status_code, 401)
data = json.loads(output.get_data(as_text=True))
self.assertEqual(
data,
{
"error": "The issue tracker of this project is read-only",
"error_code": "ETRACKERREADONLY",
},
)
class PagureFlaskIssuesAndPRDisabledtests(tests.Modeltests):
"""Tests for flask issues controller of pagure with tickets and PRs
disabled.
"""
@patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
def setUp(self):
""" Set up the environnment, ran before every tests. """
super(PagureFlaskIssuesAndPRDisabledtests, self).setUp()
tests.create_projects(self.session)
tests.create_projects_git(os.path.join(self.path, "repos"))
# Make the project's issue tracker read-only
repo = pagure.lib.query.get_authorized_project(self.session, "test")
settings = repo.settings
settings["pull_requests"] = False
settings["issue_tracker_read_only"] = True
repo.settings = settings
self.session.add(repo)
self.session.commit()
# Create a couple of issue
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue #1",
content="We should work on this for the second time",
user="foo",
status="Open",
private=True,
)
self.session.commit()
self.assertEqual(msg.title, "Test issue #1")
msg = pagure.lib.query.new_issue(
session=self.session,
repo=repo,
title="Test issue #2",
content="We should work on this for the second time",
user="foo",
status="Open",
private=False,
)
self.session.commit()
self.assertEqual(msg.title, "Test issue #2")
def test_edit_tag(self):
"""Test editing a ticket tag."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/test/tag/tag1/edit", data={})
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
def test_drop_tags(self):
"""Test dropping a ticket tag."""
user = tests.FakeUser(username="pingou")
with tests.user_set(self.app.application, user):
output = self.app.post("/test/droptag/", data={})
self.assertEqual(output.status_code, 401)
output_text = output.get_data(as_text=True)
self.assertIn(
"<title>Unauthorized :'( - Pagure</title>", output_text
)
self.assertIn(
"<p>The issue tracker for this project is read-only</p>",
output_text,
)
if __name__ == "__main__":
unittest.main(verbosity=2)
|