diff --git a/api/jobs/rules.py b/api/jobs/rules.py index 317a83e532a32a66ceeeff0d05e13ab551de6f46..b111baf5999797f778fdd95ebbd5c303fe0d7cc3 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 20ce0b428a4ee13c2713d2276ac8aeb6bb1c101e..3e2585774d260cefee660eead839e3a1088050cd 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 a69c7900b05a7c51a53b4f3e1e95f5043a1474b7..0616e3311d13d14691b52df718b3b05a473cb49d 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'