|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2019 Michael Schroeder |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +# |
| 23 | + |
| 24 | +from datetime import datetime |
| 25 | +import json |
| 26 | + |
| 27 | +import pytest |
| 28 | + |
| 29 | +from flask import jsonify |
| 30 | + |
| 31 | +req_headers = { |
| 32 | + 'Authorization':'Signature key-id=test-key,algorithm="hmac-sha256",headers="(request-target) date",signature=foob4r=', |
| 33 | + 'Date': datetime.now().strftime('%Y-%m-%dT%H:%M:%S%Z') |
| 34 | +} |
| 35 | + |
| 36 | +def test_node_deny_no_http_signature(client): |
| 37 | + """ Test node that does not have the required HTTP Signature |
| 38 | + in the Authorization header. |
| 39 | + """ |
| 40 | + |
| 41 | + response = client.get('/') |
| 42 | + |
| 43 | + assert response.status_code == 401 |
| 44 | + |
| 45 | +def test_node_status_returns_json(client): |
| 46 | + """ Test node status returning valid json. |
| 47 | + """ |
| 48 | + |
| 49 | + response = client.get('/status', headers=req_headers) |
| 50 | + ret_json = response.get_json() |
| 51 | + |
| 52 | + assert json.dumps(ret_json) |
| 53 | + |
| 54 | +def test_node_status_return_info(client): |
| 55 | + """ Test node status returns proper info. |
| 56 | + """ |
| 57 | + response = client.get('/status', headers=req_headers) |
| 58 | + ret_json = response.get_json() |
| 59 | + |
| 60 | + for item in ['node_name', 'busy', 'job_count']: |
| 61 | + assert item in ret_json |
| 62 | + |
| 63 | +def test_node_runtest_get_verb(client): |
| 64 | + """ Test node RunTest GET verb |
| 65 | + """ |
| 66 | + |
| 67 | + response = client.get('/run-test', headers=req_headers) |
| 68 | + |
| 69 | + assert response.status_code == 405 |
| 70 | + |
| 71 | +def test_node_runtest_post_success(client): |
| 72 | + """ Test node RunTest with valid POST verb |
| 73 | + """ |
| 74 | + |
| 75 | + payload = { |
| 76 | + 'commit_sha': '123456abcdefg' |
| 77 | + } |
| 78 | + |
| 79 | + response = client.post('/run-test', json=payload, headers=req_headers) |
| 80 | + |
| 81 | + assert response.status_code == 200 |
| 82 | + |
| 83 | +def test_node_runtest_post_non_json(client): |
| 84 | + """ Test node RunTest with invalid POST verb |
| 85 | + """ |
| 86 | + |
| 87 | + payload = 'null' |
| 88 | + |
| 89 | + response = client.post('/run-test', data=payload, headers=req_headers) |
| 90 | + |
| 91 | + assert response.status_code == 406 |
| 92 | + |
| 93 | +def test_node_runtest_post_no_commit_sha(client): |
| 94 | + """ Test node RunTest POST with JSON payload not containing a |
| 95 | + 'commit_sha' field. |
| 96 | + """ |
| 97 | + |
| 98 | + payload = { |
| 99 | + 'null': 'null' |
| 100 | + } |
| 101 | + |
| 102 | + response = client.post('/run-test', json=payload, headers=req_headers) |
| 103 | + |
| 104 | + assert response.status_code == 400 |
0 commit comments