From 85f7de39e23016516360750f285848e20a0a974a Mon Sep 17 00:00:00 2001
From: Ambrus Simon <ambrussimon@invenshure.com>
Date: Mon, 22 Jan 2018 16:11:12 +0100
Subject: [PATCH] Add rule.disabled support

---
 api/jobs/rules.py                            |  2 +-
 raml/schemas/definitions/rule.json           | 14 ++++----
 tests/integration_tests/python/test_rules.py | 38 ++++++++++++++++++++
 3 files changed, 47 insertions(+), 7 deletions(-)

diff --git a/api/jobs/rules.py b/api/jobs/rules.py
index 317a83e5..b111baf5 100644
--- a/api/jobs/rules.py
+++ b/api/jobs/rules.py
@@ -267,7 +267,7 @@ def get_rules_for_container(db, container):
         return get_rules_for_container(db, project)
     else:
         # Assume container is a project, or a collection (which currently cannot have a rules property)
-        result = list(db.project_rules.find({'project_id': str(container['_id'])}))
+        result = list(db.project_rules.find({'project_id': str(container['_id']), 'disabled': {'$ne': True}}))
 
         if not result:
             return []
diff --git a/raml/schemas/definitions/rule.json b/raml/schemas/definitions/rule.json
index 20ce0b42..3e258577 100644
--- a/raml/schemas/definitions/rule.json
+++ b/raml/schemas/definitions/rule.json
@@ -32,7 +32,8 @@
                 "alg":        { "type": "string" },
                 "name":       { "type": "string" },
                 "any":        { "$ref": "#/definitions/rule-items" },
-                "all":        { "$ref": "#/definitions/rule-items" }
+                "all":        { "$ref": "#/definitions/rule-items" },
+                "disabled":   { "type": "boolean" }
             },
             "additionalProperties": false
         },
@@ -40,11 +41,12 @@
         "rule-output": {
             "type": "object",
             "properties": {
-                "_id":  { "type": "string" },
-                "alg":  { "type": "string" },
-                "name": { "type": "string" },
-                "any":  { "$ref": "#/definitions/rule-items" },
-                "all":  { "$ref": "#/definitions/rule-items" }
+                "_id":      { "type": "string" },
+                "alg":      { "type": "string" },
+                "name":     { "type": "string" },
+                "any":      { "$ref": "#/definitions/rule-items" },
+                "all":      { "$ref": "#/definitions/rule-items" },
+                "disabled": { "type": "boolean" }
             }
         }
     }
diff --git a/tests/integration_tests/python/test_rules.py b/tests/integration_tests/python/test_rules.py
index a69c7900..0616e331 100644
--- a/tests/integration_tests/python/test_rules.py
+++ b/tests/integration_tests/python/test_rules.py
@@ -439,3 +439,41 @@ def test_rules(randstr, data_builder, file_form, as_root, as_admin, with_user, a
     assert r.ok
 
     # TODO add and test 'new-style' rules
+
+
+def test_disabled_rules(randstr, data_builder, api_db, as_admin, file_form):
+    # Create gear, project and *disabled* rule triggering on any csv (once enabled)
+    gear_name = randstr()
+    gear = data_builder.create_gear(gear={'name': gear_name, 'version': '0.0.1'})
+    project = data_builder.create_project()
+    r = as_admin.post('/projects/' + project + '/rules', json={
+        'alg': gear_name,
+        'name': 'csv-job-trigger-rule',
+        'any': [],
+        'all': [{'type': 'file.type', 'value': 'tabular data'}],
+        'disabled': True,
+    })
+    assert r.ok
+    rule = r.json()['_id']
+
+    # Upload 1st file (while rule is disabled)
+    r = as_admin.post('/projects/' + project + '/files', files=file_form('test1.csv'))
+    assert r.ok
+
+    # Verify that no jobs were created
+    gear_jobs = [job for job in api_db.jobs.find({'gear_id': gear})]
+    assert len(gear_jobs) == 0
+
+    # Enable rule
+    r = as_admin.put('/projects/' + project + '/rules/' + rule, json={'disabled': False})
+    assert r.ok
+
+    # Upload 1st file (rule is now enabled)
+    r = as_admin.post('/projects/' + project + '/files', files=file_form('test2.csv'))
+    assert r.ok
+
+    # Verify that a job was created
+    gear_jobs = [job for job in api_db.jobs.find({'gear_id': gear})]
+    assert len(gear_jobs) == 1
+    assert len(gear_jobs[0]['inputs']) == 1
+    assert gear_jobs[0]['inputs'][0]['name'] == 'test2.csv'
-- 
GitLab