tests.test_auth.TestAuth.test_no_parameters()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
#!/usr/bin/env python3
2
# -*- coding: utf-8 -*-
3
"""
4
Created on Thu Jul 12 11:54:50 2018
5
6
@author: Paolo Cozzi <[email protected]>
7
"""
8
9
import datetime
10
import python_jwt
11
12
from unittest.mock import patch, Mock
13
from unittest import TestCase
14
15
16
from pyUSIrest.auth import Auth
17
from pyUSIrest.exceptions import USIConnectionError
18
19
20
def generate_token(now=None, domains=['subs.test-team-1']):
21
    """A function to generate a 'fake' token"""
22
23
    if not now:
24
        now = int(datetime.datetime.now().timestamp())
25
26
    claims = {
27
        'iss': 'https://explore.aai.ebi.ac.uk/sp',
28
        'iat': now,
29
        'exp': now+3600,
30
        'sub': 'usr-f1801430-51e1-4718-8fca-778887087bad',
31
        'email': '[email protected]',
32
        'nickname': 'foo',
33
        'name': 'Foo Bar',
34
        'domains': domains
35
    }
36
37
    return python_jwt.generate_jwt(
38
        claims,
39
        algorithm='RS256')
40
41
42
class TestAuth(TestCase):
43
    @classmethod
44
    def setup_class(cls):
45
        cls.mock_get_patcher = patch('pyUSIrest.auth.requests.get')
46
        cls.mock_get = cls.mock_get_patcher.start()
47
48
    @classmethod
49
    def teardown_class(cls):
50
        cls.mock_get_patcher.stop()
51
52
    def setUp(self):
53
        # Configure the mock to return a response with an OK status code.
54
        self.mock_get.return_value = Mock()
55
        self.mock_get.return_value.text = generate_token()
56
        self.mock_get.return_value.status_code = 200
57
        self.now = int(datetime.datetime.now().timestamp())
58
59
    def test_login(self):
60
        # Call the service, which will send a request to the server.
61
        auth = Auth(user='foo', password='bar')
62
63
        # If the request is sent successfully, then I expect a response to
64
        # be returned.
65
        self.assertIsInstance(auth.__str__(), str)
66
        self.assertFalse(auth.is_expired())
67
68
    def test_with_tocken(self):
69
        auth = Auth(token=generate_token())
70
71
        # If the request is sent successfully, then I expect a response to
72
        # be returned.
73
        self.assertIsInstance(auth.__str__(), str)
74
        self.assertFalse(auth.is_expired())
75
76
    def test_get_tocken(self):
77
        # Call the service, which will send a request to the server.
78
        auth = Auth(user='foo', password='bar')
79
        token = auth.token
80
81
        self.assertIsInstance(token, str)
82
83
    def test_invalid_status(self):
84
        self.mock_get.return_value = Mock()
85
        self.mock_get.return_value.text = generate_token()
86
        self.mock_get.return_value.status_code = 400
87
88
        self.assertRaisesRegex(
89
            USIConnectionError, "Got status", Auth, user='foo', password='bar')
90
91
    def test_expired(self):
92
        self.mock_get.return_value = Mock()
93
        self.mock_get.return_value.text = generate_token(
94
            now=self.now-10000)
95
        self.mock_get.return_value.status_code = 200
96
97
        # Call the service, which will send a request to the server.
98
        auth = Auth(user='foo', password='bar')
99
100
        self.assertIsInstance(auth.__str__(), str)
101
        self.assertTrue(auth.is_expired())
102
103
    def test_no_parameters(self):
104
        self.assertRaisesRegex(
105
            ValueError,
106
            "You need to provide user/password or a valid token",
107
            Auth)
108
109
    def test_get_durations(self):
110
        self.mock_get.return_value = Mock()
111
        self.mock_get.return_value.text = generate_token(
112
            now=self.now-3300)
113
        self.mock_get.return_value.status_code = 200
114
115
        # Call the service, which will send a request to the server.
116
        auth = Auth(user='foo', password='bar')
117
118
        # get duration
119
        duration = auth.get_duration()
120
121
        # get remaining seconds
122
        seconds = int(duration.total_seconds())
123
124
        self.assertAlmostEqual(seconds, 300, delta=10)
125
126
    def test_get_domains(self):
127
        self.mock_get.return_value = Mock()
128
        self.mock_get.return_value.text = generate_token(
129
            domains=['subs.test-team-1', 'subs.test-team-2'])
130
        self.mock_get.return_value.status_code = 200
131
132
        auth = Auth(user='foo', password='bar')
133
134
        test_domains = auth.get_domains()
135
136
        self.assertEqual(
137
            test_domains, ['subs.test-team-1', 'subs.test-team-2'])
138