Passed
Push — beta ( a8392a...918fe2 )
by Dean
02:58
created

OpenSSL   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 35.7%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 6
c 6
b 0
f 0
dl 0
loc 52
ccs 10
cts 28
cp 0.357
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B test_import() 0 32 4
A on_success() 0 13 2
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.head('https://api-v2launch.trakt.tv', timeout=3)
37
        except requests.RequestException, ex:
38
            # Ignore failed requests (server error, network problem, etc..)
39
            log.warn('Request failed: %s', ex, exc_info=True)
40
41
        return {
42
            'versions': {
43
                'pyopenssl': getattr(OpenSSL, '__version__', None)
44
            }
45
        }
46
47 1
    @classmethod
48
    def on_success(cls, metadata):
49
        # Inject pyOpenSSL into requests
50 1
        try:
51 1
            from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3
52 1
            inject_into_urllib3()
53
        except Exception, ex:
54
            log.warn('Unable to inject pyOpenSSL into urllib3 - %s', ex, exc_info=True)
55
            return
56
57
        # Enable secure error reporting
58 1
        from plugin.core.logger.handlers.error_reporter import RAVEN
59
        RAVEN.set_protocol('threaded+requests+https')
60