test_vmcp_api.py
3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# -*- coding: utf8 -*-
# This file is part of PyBossa.
#
# Copyright (C) 2015 SciFabric LTD.
#
# PyBossa is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PyBossa is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with PyBossa. If not, see <http://www.gnu.org/licenses/>.
import json
from default import flask_app, with_context
from mock import patch, Mock
from test_api import TestAPI
class TestVmcpAPI(TestAPI):
@with_context
def test_vcmp(self):
"""Test VCMP without key fail works."""
if self.flask_app.config.get('VMCP_KEY'):
self.flask_app.config.pop('VMCP_KEY')
res = self.app.get('api/vmcp', follow_redirects=True)
err = json.loads(res.data)
assert res.status_code == 501, err
assert err['status_code'] == 501, err
assert err['status'] == "failed", err
assert err['target'] == "vmcp", err
assert err['action'] == "GET", err
@with_context
@patch.dict(flask_app.config, {'VMCP_KEY': 'invalid.key'})
def test_vmcp_file_not_found(self):
"""Test VMCP with invalid file key works."""
res = self.app.get('api/vmcp', follow_redirects=True)
err = json.loads(res.data)
assert res.status_code == 501, err
assert err['status_code'] == 501, err
assert err['status'] == "failed", err
assert err['target'] == "vmcp", err
assert err['action'] == "GET", err
@with_context
@patch.dict(flask_app.config, {'VMCP_KEY': 'invalid.key'})
def test_vmcp_01(self):
"""Test VMCP errors works"""
# Even though the key does not exists, let's patch it to test
# all the errors
with patch('os.path.exists', return_value=True):
res = self.app.get('api/vmcp', follow_redirects=True)
err = json.loads(res.data)
assert res.status_code == 415, err
assert err['status_code'] == 415, err
assert err['status'] == "failed", err
assert err['target'] == "vmcp", err
assert err['action'] == "GET", err
assert err['exception_msg'] == 'cvm_salt parameter is missing'
@with_context
@patch.dict(flask_app.config, {'VMCP_KEY': 'invalid.key'})
def test_vmcp_02(self):
"""Test VMCP signing works."""
rsa = Mock()
rsa.sign.return_value = 'signed'
with patch('os.path.exists', return_value=True):
with patch('M2Crypto.RSA.load_key', return_value=rsa):
res = self.app.get('api/vmcp?cvm_salt=testsalt',
follow_redirects=True)
out = json.loads(res.data)
assert res.status_code == 200, out
assert out.get('signature') is not None, out
# Now with a post
res = self.app.post('api/vmcp?cvm_salt=testsalt',
follow_redirects=True)
assert res.status_code == 405, res.status_code