| |
@@ -1284,6 +1284,123 @@
|
| |
self.assertDictEqual(data, {"message": "Pull-request closed!"})
|
| |
|
| |
@patch("pagure.lib.notify.send_email")
|
| |
+ def test_api_pull_request_reopen(self, send_email):
|
| |
+ """Test the api_pull_request_reopen method of the flask api."""
|
| |
+ send_email.return_value = True
|
| |
+
|
| |
+ tests.create_projects(self.session)
|
| |
+ tests.create_tokens(self.session)
|
| |
+ tests.create_tokens_acl(self.session)
|
| |
+
|
| |
+ # Create the pull-request to close and reopen
|
| |
+ repo = pagure.lib.query.get_authorized_project(self.session, "test")
|
| |
+ forked_repo = pagure.lib.query.get_authorized_project(
|
| |
+ self.session, "test"
|
| |
+ )
|
| |
+ req = pagure.lib.query.new_pull_request(
|
| |
+ session=self.session,
|
| |
+ repo_from=forked_repo,
|
| |
+ branch_from="master",
|
| |
+ repo_to=repo,
|
| |
+ branch_to="master",
|
| |
+ title="test pull-request",
|
| |
+ user="pingou",
|
| |
+ )
|
| |
+ self.session.commit()
|
| |
+ self.assertEqual(req.id, 1)
|
| |
+ self.assertEqual(req.title, "test pull-request")
|
| |
+
|
| |
+ headers = {"Authorization": "token aaabbbcccddd"}
|
| |
+
|
| |
+ # Invalid project
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/foo/pull-request/1/close", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 404)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertDictEqual(
|
| |
+ data, {"error": "Project not found", "error_code": "ENOPROJECT"}
|
| |
+ )
|
| |
+
|
| |
+ # Valid token, wrong project
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/test2/pull-request/1/close", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 401)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertEqual(
|
| |
+ pagure.api.APIERROR.EINVALIDTOK.name, data["error_code"]
|
| |
+ )
|
| |
+ self.assertEqual(pagure.api.APIERROR.EINVALIDTOK.value, data["error"])
|
| |
+
|
| |
+ # Invalid PR
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/test/pull-request/2/close", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 404)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertDictEqual(
|
| |
+ data, {"error": "Pull-Request not found", "error_code": "ENOREQ"}
|
| |
+ )
|
| |
+
|
| |
+ # Create a token for foo for this project
|
| |
+ item = pagure.lib.model.Token(
|
| |
+ id="foobar_token",
|
| |
+ user_id=2,
|
| |
+ project_id=1,
|
| |
+ expiration=datetime.datetime.utcnow()
|
| |
+ + datetime.timedelta(days=30),
|
| |
+ )
|
| |
+ self.session.add(item)
|
| |
+ self.session.commit()
|
| |
+
|
| |
+ # Allow the token to close and reopen PR
|
| |
+ acls = pagure.lib.query.get_acls(self.session)
|
| |
+ for acl in acls:
|
| |
+ if acl.name == "pull_request_close":
|
| |
+ break
|
| |
+ item = pagure.lib.model.TokenAcl(
|
| |
+ token_id="foobar_token", acl_id=acl.id
|
| |
+ )
|
| |
+ self.session.add(item)
|
| |
+ self.session.commit()
|
| |
+
|
| |
+ headers = {"Authorization": "token foobar_token"}
|
| |
+
|
| |
+ # User not admin
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/test/pull-request/1/close", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 403)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertDictEqual(
|
| |
+ data,
|
| |
+ {
|
| |
+ "error": "You are not allowed to merge/close pull-request "
|
| |
+ "for this project",
|
| |
+ "error_code": "ENOPRCLOSE",
|
| |
+ },
|
| |
+ )
|
| |
+
|
| |
+ headers = {"Authorization": "token aaabbbcccddd"}
|
| |
+
|
| |
+ # Close PR
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/test/pull-request/1/close", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 200)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertDictEqual(data, {"message": "Pull-request closed!"})
|
| |
+
|
| |
+ # Reopen PR
|
| |
+ output = self.app.post(
|
| |
+ "/api/0/test/pull-request/1/reopen", headers=headers
|
| |
+ )
|
| |
+ self.assertEqual(output.status_code, 200)
|
| |
+ data = json.loads(output.get_data(as_text=True))
|
| |
+ self.assertDictEqual(data, {"message": "Pull-request reopened!"})
|
| |
+
|
| |
+ @patch("pagure.lib.notify.send_email")
|
| |
def test_api_pull_request_merge_pr_disabled(self, send_email):
|
| |
"""Test the api_pull_request_merge method of the flask api when PR
|
| |
are disabled."""
|
| |
Signed-off-by: Matej Focko mfocko@redhat.com