|
Pierre-Yves Chibon |
f778bd |
# -*- coding: utf-8 -*-
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
"""
|
|
Pierre-Yves Chibon |
f778bd |
(c) 2018 - Copyright Red Hat Inc
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
Authors:
|
|
Pierre-Yves Chibon |
f778bd |
Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
"""
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
67d1cc |
from __future__ import unicode_literals, absolute_import
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
import copy
|
|
Pierre-Yves Chibon |
f778bd |
import datetime
|
|
Pierre-Yves Chibon |
f778bd |
import unittest
|
|
Pierre-Yves Chibon |
f778bd |
import shutil
|
|
Pierre-Yves Chibon |
f778bd |
import sys
|
|
Pierre-Yves Chibon |
f778bd |
import time
|
|
Pierre-Yves Chibon |
f778bd |
import os
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
import json
|
|
Pierre-Yves Chibon |
f778bd |
from mock import patch, MagicMock
|
|
Pierre-Yves Chibon |
f778bd |
|
|
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 |
f778bd |
|
|
Pierre-Yves Chibon |
930073 |
import pagure.lib.query
|
|
Pierre-Yves Chibon |
f778bd |
import tests
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
class PagureFlaskApiProjectUpdateWatchTests(tests.Modeltests):
|
|
Pierre-Yves Chibon |
f778bd |
""" Tests for the flask API of pagure for changing the watch status on
|
|
Pierre-Yves Chibon |
f778bd |
a project via the API
|
|
Pierre-Yves Chibon |
f778bd |
"""
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
@patch("pagure.lib.notify.send_email", MagicMock(return_value=True))
|
|
Pierre-Yves Chibon |
f778bd |
def setUp(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Set up the environnment, ran before every tests. """
|
|
Pierre-Yves Chibon |
f778bd |
super(PagureFlaskApiProjectUpdateWatchTests, self).setUp()
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
tests.create_projects(self.session)
|
|
Pierre-Yves Chibon |
73d120 |
tests.create_projects_git(os.path.join(self.path, "tickets"))
|
|
Pierre-Yves Chibon |
f778bd |
tests.create_tokens(self.session)
|
|
Pierre-Yves Chibon |
f778bd |
tests.create_tokens_acl(self.session)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
# Create normal issue
|
|
Pierre-Yves Chibon |
73d120 |
repo = pagure.lib.query.get_authorized_project(self.session, "test")
|
|
Pierre-Yves Chibon |
930073 |
msg = pagure.lib.query.new_issue(
|
|
Pierre-Yves Chibon |
f778bd |
session=self.session,
|
|
Pierre-Yves Chibon |
f778bd |
repo=repo,
|
|
Pierre-Yves Chibon |
73d120 |
title="Test issue #1",
|
|
Pierre-Yves Chibon |
73d120 |
content="We should work on this",
|
|
Pierre-Yves Chibon |
73d120 |
user="pingou",
|
|
Pierre-Yves Chibon |
f778bd |
private=False,
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.session.commit()
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(msg.title, "Test issue #1")
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
# Create project-less token for user foo
|
|
Pierre-Yves Chibon |
f778bd |
item = pagure.lib.model.Token(
|
|
Pierre-Yves Chibon |
73d120 |
id="project-less-foo",
|
|
Pierre-Yves Chibon |
f778bd |
user_id=1,
|
|
Pierre-Yves Chibon |
f778bd |
project_id=None,
|
|
Pierre-Yves Chibon |
f778bd |
expiration=datetime.datetime.utcnow()
|
|
Pierre-Yves Chibon |
73d120 |
+ datetime.timedelta(days=30),
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.session.add(item)
|
|
Pierre-Yves Chibon |
f778bd |
self.session.commit()
|
|
Pierre-Yves Chibon |
73d120 |
tests.create_tokens_acl(self.session, token_id="project-less-foo")
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_invalid_project(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
# Invalid project
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/foobar/watchers/update", headers=headers
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 404)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
73d120 |
data, {"error": "Project not found", "error_code": "ENOPROJECT"}
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_change_status_issue_token_not_for_project(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
# Valid token, wrong project
|
|
Pierre-Yves Chibon |
73d120 |
output = self.app.post("/api/0/test2/watchers/update", headers=headers)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 401)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(
|
|
Pierre-Yves Chibon |
73d120 |
pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
73d120 |
self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_no_user_watching(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"status": "42"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 400)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "Invalid or incomplete input submitted",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EINVALIDREQ",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_no_watch_status(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "pingou"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 400)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": 'The watch value of "None" is invalid',
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "ENOCODE",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_invalid_status(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "pingou", "status": "42"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 400)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": 'The watch value of "42" is invalid',
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "ENOCODE",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_invalid_user(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "example", "status": "2"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 401)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "You are not allowed to modify this project",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EMODIFYPROJECTNOTALLOWED",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_other_user(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "foo", "status": "2"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 401)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "You are not allowed to modify this project",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EMODIFYPROJECTNOTALLOWED",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_all_good(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "pingou", "status": 1}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"message": "You are now watching issues and PRs on this project",
|
|
Pierre-Yves Chibon |
73d120 |
"status": "ok",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
@patch("pagure.utils.is_admin", MagicMock(return_value=True))
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_other_user_admin(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "foo", "status": "2"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 200)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"message": "You are now watching commits on this project",
|
|
Pierre-Yves Chibon |
73d120 |
"status": "ok",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
@patch("pagure.utils.is_admin", MagicMock(return_value=True))
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_invalid_user_admin(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"watcher": "example", "status": "2"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 400)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "Invalid or incomplete input submitted",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EINVALIDREQ",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
@patch("pagure.utils.is_admin", MagicMock(return_value=True))
|
|
Pierre-Yves Chibon |
f778bd |
def test_api_update_project_watchers_missing_user_admin(self):
|
|
Pierre-Yves Chibon |
f778bd |
""" Test the api_update_project_watchers method of the flask api. """
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
headers = {"Authorization": "token aaabbbcccddd"}
|
|
Pierre-Yves Chibon |
73d120 |
data = {"status": "2"}
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
output = self.app.post(
|
|
Pierre-Yves Chibon |
73d120 |
"/api/0/test/watchers/update", headers=headers, data=data
|
|
Pierre-Yves Chibon |
73d120 |
)
|
|
Pierre-Yves Chibon |
f778bd |
self.assertEqual(output.status_code, 400)
|
|
Pierre-Yves Chibon |
f778bd |
data = json.loads(output.get_data(as_text=True))
|
|
Pierre-Yves Chibon |
f778bd |
self.assertDictEqual(
|
|
Pierre-Yves Chibon |
f778bd |
data,
|
|
Pierre-Yves Chibon |
f778bd |
{
|
|
Pierre-Yves Chibon |
73d120 |
"error": "Invalid or incomplete input submitted",
|
|
Pierre-Yves Chibon |
73d120 |
"error_code": "EINVALIDREQ",
|
|
Pierre-Yves Chibon |
73d120 |
},
|
|
Pierre-Yves Chibon |
f778bd |
)
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
f778bd |
|
|
Pierre-Yves Chibon |
73d120 |
if __name__ == "__main__":
|
|
Pierre-Yves Chibon |
f778bd |
unittest.main(verbosity=2)
|