test_client.py 1.82 KB
Newer Older
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
1
from unittest import TestCase
2

3
from senpy.testing import patch_requests
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
4
from senpy.client import Client
5
from senpy.models import Results, Plugins, Error
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
6
from senpy.plugins import AnalysisPlugin
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
7
8
9
10
11
12
13
14
15
16


class ModelsTest(TestCase):
    def setUp(self):
        self.host = '0.0.0.0'
        self.port = 5000

    def test_client(self):
        endpoint = 'http://dummy/'
        client = Client(endpoint)
17
        with patch_requests('http://dummy/', Results()):
J. Fernando Sánchez's avatar
J. Fernando Sánchez committed
18
19
            resp = client.analyse('hello')
            assert isinstance(resp, Results)
20
        with patch_requests('http://dummy/', Error('Nothing')):
21
22
23
24
25
            try:
                client.analyse(input='hello', algorithm='NONEXISTENT')
                raise Exception('Exceptions should be raised. This is not golang')
            except Error:
                pass
26

27
28
29
30
31
32
33
34
35
36
37
38
39
    def test_client_post(self):
        endpoint = 'http://dummy/'
        client = Client(endpoint)
        with patch_requests('http://dummy/', Results()):
            resp = client.analyse('hello')
            assert isinstance(resp, Results)
        with patch_requests('http://dummy/', Error('Nothing'), method='POST'):
            try:
                client.analyse(input='hello', method='POST', algorithm='NONEXISTENT')
                raise Exception('Exceptions should be raised. This is not golang')
            except Error:
                pass

40
41
42
43
44
45
    def test_plugins(self):
        endpoint = 'http://dummy/'
        client = Client(endpoint)
        plugins = Plugins()
        p1 = AnalysisPlugin({'name': 'AnalysisP1', 'version': 0, 'description': 'No'})
        plugins.plugins = [p1, ]
46
        with patch_requests('http://dummy/plugins', plugins):
47
48
49
50
            response = client.plugins()
            assert isinstance(response, dict)
            assert len(response) == 1
            assert "AnalysisP1" in response