Passed
Push — develop ( 1cbb75...2fa332 )
by Dean
03:08
created

OpenSSL.test_import()   B

Complexity

Conditions 4

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 17.1835

Importance

Changes 6
Bugs 0 Features 0
Metric Value
c 6
b 0
f 0
dl 0
loc 31
ccs 1
cts 16
cp 0.0625
rs 8.5806
cc 4
crap 17.1835
1 1
from plugin.core.libraries.tests.core.base import BaseTest
2
3 1
import logging
4
5 1
log = logging.getLogger(__name__)
6
7
8 1
class OpenSSL(BaseTest):
9 1
    name = 'openssl'
10 1
    optional = True
11
12 1
    @staticmethod
13
    def test_import():
14
        import OpenSSL.SSL
15
16
        # Try construct SSL context
17
        ctx = OpenSSL.SSL.Context(OpenSSL.SSL.SSLv23_METHOD)
18
19
        # Ensure library has SNI support
20
        cnx = OpenSSL.SSL.Connection(ctx)
21
22
        if not hasattr(cnx, 'set_tlsext_host_name'):
23
            raise Exception('Missing SNI extension')
24
25
        # Ensure binding can be imported
26
        from cryptography.hazmat.bindings.openssl.binding import Binding
27
        assert Binding
28
29
        # Ensure secure connections work with requests
30
        from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3
31
        import requests
32
33
        inject_into_urllib3()
34
35
        try:
36
            requests.get('https://api-v2launch.trakt.tv')
37
        except requests.RequestException, ex:
38
            log.warn('Request failed: %s', ex, exc_info=True)
39
40
        return {
41
            'versions': {
42
                'pyopenssl': getattr(OpenSSL, '__version__', None)
43
            }
44
        }
45
46 1
    @classmethod
47
    def on_success(cls, metadata):
48
        # Inject pyOpenSSL into requests
49 1
        try:
50 1
            from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3
51 1
            inject_into_urllib3()
52
        except Exception, ex:
53
            log.warn('Unable to inject pyOpenSSL into urllib3 - %s', ex, exc_info=True)
54
            return
55
56
        # Enable secure error reporting
57 1
        from plugin.core.logger.handlers.error_reporter import RAVEN
58
        RAVEN.set_protocol('threaded+requests+https')
59