[copr] master: Allow altering actions by backend (0ef0ef6)

bkabrda at fedoraproject.org bkabrda at fedoraproject.org
Thu Feb 21 09:13:49 UTC 2013


Repository : http://git.fedorahosted.org/cgit/copr.git

On branch  : master

>---------------------------------------------------------------

commit 0ef0ef68b8209101f7511cf4c36eb344e1144695
Author: Bohuslav Kabrda <bkabrda at redhat.com>
Date:   Thu Feb 21 10:12:30 2013 +0100

    Allow altering actions by backend


>---------------------------------------------------------------

 .../coprs/views/backend_ns/backend_general.py      |   25 +++++++
 coprs_frontend/tests/coprs_test_case.py            |   24 +++++++
 .../test_views/test_backend_ns/test_general.py     |   68 ++++++++++++++++++++
 3 files changed, 117 insertions(+), 0 deletions(-)

diff --git a/coprs_frontend/coprs/views/backend_ns/backend_general.py b/coprs_frontend/coprs/views/backend_ns/backend_general.py
index f6ce69a..27baca6 100644
--- a/coprs_frontend/coprs/views/backend_ns/backend_general.py
+++ b/coprs_frontend/coprs/views/backend_ns/backend_general.py
@@ -47,3 +47,28 @@ def waiting_actions():
     actions = models.Action.query.filter(models.Action.backend_result==helpers.BackendResultEnum('waiting')).all()
 
     return flask.jsonify({'actions': [action.to_dict(options={'__columns_except__': ['backend_result', 'backend_message']}) for action in actions]})
+
+# TODO: this is very similar to update_builds, we should pull out the common functionality into a single function
+ at backend_ns.route('/update_actions/', methods=['POST', 'PUT'])
+ at misc.backend_authenticated
+def update_actions():
+    to_update = {}
+    for action in flask.request.json['actions']:
+        to_update[action['id']] = action
+
+    if not to_update:
+        return json.dumps({'warning': 'No parsed actions'})
+
+    existing = {}
+    for action in models.Action.query.filter(models.Action.id.in_(to_update.keys())).all():
+        existing[action.id] = action
+
+    non_existing_ids = list(set(to_update.keys()) - set(existing.keys()))
+
+    for i, action in existing.items():
+        existing[i].backend_result = to_update[i]['backend_result']
+        existing[i].backend_message = to_update[i]['backend_message']
+
+    db.session.commit()
+
+    return flask.jsonify({'updated_actions_ids': list(existing.keys()), 'non_existing_actions_ids': non_existing_ids})
diff --git a/coprs_frontend/tests/coprs_test_case.py b/coprs_frontend/tests/coprs_test_case.py
index 82e7a38..dcb0cb9 100644
--- a/coprs_frontend/tests/coprs_test_case.py
+++ b/coprs_frontend/tests/coprs_test_case.py
@@ -1,5 +1,6 @@
 import base64
 import os
+import time
 
 import pytest
 
@@ -96,3 +97,26 @@ class CoprsTestCase(object):
         self.cp3 = models.CoprPermission(copr = self.c3, user = self.u1, copr_builder = helpers.PermissionEnum('request'), copr_admin = helpers.PermissionEnum('approved'))
 
         self.db.session.add_all([self.cp1, self.cp2, self.cp3])
+
+    @pytest.fixture
+    def f_actions(self):
+        self.a1 = models.Action(action_type=helpers.ActionTypeEnum('rename'),
+                                object_type='copr',
+                                object_id=self.c1.id,
+                                old_value='{0}/{1}'.format(self.c1.owner.name, self.c1.name),
+                                new_value='{0}/new_name'.format(self.c1.owner.name),
+                                created_on=int(time.time()))
+        self.a2 = models.Action(action_type=helpers.ActionTypeEnum('rename'),
+                                object_type='copr',
+                                object_id=self.c2.id,
+                                old_value='{0}/{1}'.format(self.c2.owner.name, self.c2.name),
+                                new_value='{0}/new_name2'.format(self.c2.owner.name),
+                                created_on=int(time.time()))
+        self.a3 = models.Action(action_type=helpers.ActionTypeEnum('delete'),
+                                object_type='copr',
+                                object_id=100,
+                                old_value='asd/qwe',
+                                new_value=None,
+                                backend_result=helpers.BackendResultEnum('success'),
+                                created_on=int(time.time()))
+        self.db.session.add_all([self.a1, self.a2, self.a3])
diff --git a/coprs_frontend/tests/test_views/test_backend_ns/test_general.py b/coprs_frontend/tests/test_views/test_backend_ns/test_general.py
index d5433d7..5b021d8 100644
--- a/coprs_frontend/tests/test_views/test_backend_ns/test_general.py
+++ b/coprs_frontend/tests/test_views/test_backend_ns/test_general.py
@@ -127,3 +127,71 @@ class TestUpdateBuilds(CoprsTestCase):
         assert len(signals_received) == 1
         self.db.session.add(self.b2)
         assert signals_received[0].id == 2
+
+class TestWaitingActions(CoprsTestCase):
+    def test_no_waiting_builds(self):
+        assert '"actions": []' in self.tc.get('/backend/waiting_actions/').data
+
+    def test_waiting_build_only_lists_not_started_or_ended(self, f_users, f_coprs, f_actions, f_db):
+        r = self.tc.get('/backend/waiting_actions/')
+        assert len(json.loads(r.data)['actions']) == 2
+
+class TestUpdateActions(CoprsTestCase):
+    data1 = """
+{
+  "actions":[
+    {
+      "id": 1,
+      "backend_result": 1,
+      "backend_message": "no problem"
+    }
+  ]
+}"""
+    data2 = """
+{
+  "actions":[
+    {
+      "id": 1,
+      "backend_result": 1,
+      "backend_message": null
+    },
+    {
+      "id": 2,
+      "backend_result": 2,
+      "backend_message": "problem!"
+    },
+    {
+      "id": 100,
+      "backend_result": 123,
+      "backend_message": "wheeeee!"
+    }
+  ]
+}"""
+
+    def test_update_one_action(self, f_users, f_coprs, f_actions, f_db):
+       r = self.tc.post('/backend/update_actions/',
+                         content_type='application/json',
+                         headers=self.auth_header,
+                         data=self.data1)
+       assert json.loads(r.data)["updated_actions_ids"] == [1]
+       assert json.loads(r.data)["non_existing_actions_ids"] == []
+
+       updated = self.models.Action.query.filter(self.models.Action.id==1).first()
+       assert updated.backend_result == 1
+       assert updated.backend_message == "no problem"
+
+    def test_update_more_existent_and_non_existent_builds(self, f_users, f_coprs, f_actions, f_db):
+       r = self.tc.post('/backend/update_actions/',
+                         content_type='application/json',
+                         headers=self.auth_header,
+                         data=self.data2)
+       assert sorted(json.loads(r.data)["updated_actions_ids"]) == [1, 2]
+       assert json.loads(r.data)["non_existing_actions_ids"] == [100]
+
+       updated = self.models.Action.query.filter(self.models.Action.id==1).first()
+       assert updated.backend_result == 1
+       assert updated.backend_message == None
+
+       updated2 = self.models.Action.query.filter(self.models.Action.id==2).first()
+       assert updated2.backend_result == 2
+       assert updated2.backend_message == "problem!"



More information about the copr-devel mailing list