Completed
Push — master ( 7ac7e1...e63902 )
by dotzero
01:43
created

AuthTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 73
rs 10
wmc 10
1
#!/usr/bin/env python
2
# -*- coding: utf-8 -*-
3
#
4
# Copyright (c) 2016 dotzero <[email protected]>
5
#
6
# Permission is hereby granted, free of charge, to any person obtaining a copy
7
# of this software and associated documentation files (the "Software"), to deal
8
# in the Software without restriction, including without limitation the rights
9
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
# copies of the Software, and to permit persons to whom the Software is
11
# furnished to do so, subject to the following conditions:
12
#
13
# The above copyright notice and this permission notice shall be included
14
# in all copies or substantial portions of the Software.
15
#
16
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
# SOFTWARE.
23
24
"""This module contains a object that represents Tests for Auth"""
25
26
import unittest
27
import sys
28
29
sys.path.append('.')
30
31
import habrahabr
32
from tests.base import BaseTest
33
34
35
class AuthTest(BaseTest, unittest.TestCase):
36
    """This object represents Tests for Auth."""
37
38
    def setUp(self):
39
        self.auth = habrahabr.Auth()
40
41
    def test_auth_init_empty(self):
42
        """Test Auth.__init__() method"""
43
        auth = habrahabr.Auth()
44
45
        self.assertEqual(auth._client, None)
46
        self.assertEqual(auth._token, None)
47
        self.assertEqual(auth._api_key, None)
48
        self.assertNotEqual(auth._endpoint, None)
49
50
    def test_auth_init_oauth(self):
51
        """Test Auth.__init__(client, token) method"""
52
        auth = habrahabr.Auth(client='foo.bar', token='foobar')
53
54
        self.assertEqual(auth._client, 'foo.bar')
55
        self.assertEqual(auth._token, 'foobar')
56
        self.assertEqual(auth._api_key, None)
57
58
    def test_auth_init_apikey(self):
59
        """Test Auth.__init__(api_key) method"""
60
        auth = habrahabr.Auth(api_key='foobar')
61
62
        self.assertEqual(auth._client, None)
63
        self.assertEqual(auth._token, None)
64
        self.assertEqual(auth._api_key, 'foobar')
65
66
    def test_set_request_client(self):
67
        """Test Auth.set_request_client(client) method"""
68
        self.auth.set_request_client('foo.bar')
69
        self.assertEqual(self.auth._client, 'foo.bar')
70
71
    def test_set_request_token(self):
72
        """Test Auth.set_request_token(token) method"""
73
        self.auth.set_request_token('foobar')
74
        self.assertEqual(self.auth._token, 'foobar')
75
76
    def test_set_request_apikey(self):
77
        """Test Auth.set_request_apikey(api_key) method"""
78
        self.auth.set_request_apikey('foobar')
79
        self.assertEqual(self.auth._api_key, 'foobar')
80
81
    def test_get_request_endpoint(self):
82
        """Test Auth.auth.get_request_endpoint() method"""
83
        url = self.auth.get_request_endpoint()
84
        self.assertEqual(self.auth._endpoint, url)
85
86
    def test_get_request_headers(self):
87
        """Test Auth.get_request_headers() method"""
88
        auth = habrahabr.Auth(client='foo.bar', token='foobar')
89
        headers = auth.get_request_headers()
90
        self.assertTrue(self.is_dict(headers))
91
        self.assertEqual(headers['client'], 'foo.bar')
92
        self.assertEqual(headers['token'], 'foobar')
93
94
        auth = habrahabr.Auth(api_key='foobar')
95
        headers = auth.get_request_headers()
96
        self.assertTrue(self.is_dict(headers))
97
        self.assertEqual(headers['apikey'], 'foobar')
98
99
    def test_get_authorization_url(self):
100
        """Test Auth.get_authorization_url() method"""
101
        self.auth.set_request_client('foo.bar')
102
        url = self.auth.get_authorization_url('https://tmtm.ru/', 'token')
103
104
        self.assertTrue('/auth/o/login/' in url)
105
        self.assertTrue('response_type=token' in url)
106
        self.assertTrue('client_id=foo.bar' in url)
107
        self.assertTrue('redirect_uri=https%3A%2F%2Ftmtm.ru%2F' in url)
108
109
110
if __name__ == '__main__':
111
    unittest.main()
112