Projet

Général

Profil

0002-tests-add-basic-testing-of-wscall-workflow.patch

Frédéric Péters, 25 février 2015 14:28

Télécharger (4,09 ko)

Voir les différences:

Subject: [PATCH 2/2] tests: add basic testing of wscall workflow

 tests/test_workflows.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++-
 tests/utilities.py      | 33 +++++++++++++++++++++++++++++++-
 2 files changed, 82 insertions(+), 2 deletions(-)
tests/test_workflows.py
14 14
from wcs.wf.register_comment import RegisterCommenterWorkflowStatusItem
15 15
from wcs.wf.remove import RemoveWorkflowStatusItem
16 16
from wcs.wf.roles import AddRoleWorkflowStatusItem, RemoveRoleWorkflowStatusItem
17
from wcs.wf.wscall import WebserviceCallStatusItem
17 18

  
18
from utilities import create_temporary_pub, MockSubstitutionVariables, emails
19
from utilities import (create_temporary_pub, MockSubstitutionVariables, emails,
20
        http_requests)
19 21

  
20 22
def setup_module(module):
21 23
    cleanup()
......
325 327
    item.perform(formdata)
326 328
    assert emails.count() == 1
327 329
    assert emails.get('foobar')['kwargs']['email_rcpt'] == ['xyz@localhost']
330

  
331
def test_webservice_call():
332
    pub.substitutions.feed(MockSubstitutionVariables())
333

  
334
    formdef = FormDef()
335
    formdef.name = 'baz'
336
    formdef.fields = []
337
    formdef.store()
338

  
339
    formdata = formdef.data_class()()
340
    formdata.just_created()
341
    formdata.store()
342

  
343
    item = WebserviceCallStatusItem()
344
    item.url = 'http://remote.example.net'
345
    item.perform(formdata)
346
    assert http_requests.get_last('url') == 'http://remote.example.net'
347
    assert http_requests.get_last('method') == 'POST'
348

  
349
    item = WebserviceCallStatusItem()
350
    item.url = 'http://remote.example.net'
351
    item.post = False
352
    item.perform(formdata)
353
    assert http_requests.get_last('url') == 'http://remote.example.net'
354
    assert http_requests.get_last('method') == 'GET'
355

  
356
    item = WebserviceCallStatusItem()
357
    item.url = 'http://remote.example.net'
358
    item.post = False
359
    item.request_signature_key = 'xxx'
360
    item.perform(formdata)
361
    assert 'signature=' in http_requests.get_last('url')
362
    assert http_requests.get_last('method') == 'GET'
363

  
364
    item = WebserviceCallStatusItem()
365
    item.url = 'http://remote.example.net'
366
    item.post = False
367
    item.request_signature_key = '[empty]'
368
    item.perform(formdata)
369
    assert not 'signature=' in http_requests.get_last('url')
370

  
371
    item = WebserviceCallStatusItem()
372
    item.url = 'http://remote.example.net'
373
    item.post = False
374
    item.request_signature_key = '[bar]'
375
    item.perform(formdata)
376
    assert 'signature=' in http_requests.get_last('url')
tests/utilities.py
58 58

  
59 59
class MockSubstitutionVariables(object):
60 60
    def get_substitution_variables(self):
61
        return {'bar': 'Foobar', 'foo': '1 < 3', 'email': 'sub@localhost'}
61
        return {'bar': 'Foobar', 'foo': '1 < 3', 'email': 'sub@localhost',
62
                'empty': ''}
63

  
64

  
65
class HttpRequestsMocking(object):
66
    def __init__(self):
67
        self.requests = []
68
        import wcs.qommon.misc
69
        import qommon.misc
70
        wcs.qommon.misc._http_request = self.http_request
71
        qommon.misc._http_request = self.http_request
72

  
73
    def http_request(self, url, method='GET', body=None, headers={}, timeout=None):
74
        self.requests.append(
75
                {'url': url,
76
                 'method': method,
77
                 'body': body,
78
                 'headers': headers,
79
                 'timeout': timeout})
80

  
81
        response = ''
82
        status = 200
83
        data = ''
84
        return None, status, data, None
85

  
86
    def get_last(self, attribute):
87
        return self.requests[-1][attribute]
88

  
89
    def empty(self):
90
        self.requests = []
91

  
92
http_requests = HttpRequestsMocking()
62
-