File: test_auth.py

package info (click to toggle)
python-msrest 0.6.21-5
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 836 kB
  • sloc: python: 8,599; makefile: 8; sh: 1
file content (145 lines) | stat: -rw-r--r-- 4,962 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#--------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. All rights reserved.
#
# The MIT License (MIT)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the ""Software""), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#--------------------------------------------------------------------------

import os
import sys
import json
import isodate
from datetime import datetime
import base64
from base64 import b64decode
import unittest
try:
    from unittest import mock
except ImportError:
    import mock

from msrest.authentication import (
    BasicAuthentication,
    BasicTokenAuthentication,
    OAuthTokenAuthentication,
    ApiKeyCredentials,
    CognitiveServicesCredentials,
    TopicCredentials,
    DomainCredentials
)

from requests import Request, PreparedRequest


class TestAuthentication(unittest.TestCase):

    def setUp(self):

        self.request = mock.create_autospec(Request)
        self.request.headers = {}
        self.request.cookies = {}
        self.request.auth = None
        self.request.url = "http://my_endpoint.com"
        self.request.method = 'GET'
        self.request.files = None
        self.request.data = None
        self.request.json = None
        self.request.params = {}
        self.request.hooks = {}

        return super(TestAuthentication, self).setUp()

    def test_basic_auth(self):

        basic = BasicAuthentication("username", "password")
        session = basic.signed_session()

        req = session.auth(self.request)
        self.assertTrue('Authorization' in req.headers)
        self.assertTrue(req.headers['Authorization'].startswith('Basic '))

    def test_basic_token_auth(self):

        token = {
            'access_token': '123456789'
        }
        basic = BasicTokenAuthentication(token)
        basic.set_token() # Just check that this does not raise
        session = basic.signed_session()

        req = session.prepare_request(self.request)
        assert 'Authorization' in req.headers
        assert req.headers['Authorization'] == 'Bearer 123456789'

    def test_token_auth(self):

        token = {
            'access_token': '123456789'
        }
        auth = OAuthTokenAuthentication("client_id", token)
        session = auth.signed_session()

        request = PreparedRequest()
        request.prepare("GET", "https://example.org")
        session.auth(request)
        assert request.headers == {'Authorization': 'Bearer 123456789'}

    def test_apikey_auth(self):
        auth = ApiKeyCredentials(
            in_headers={
                'testheader' : 'testheadervalue'
            }
        )
        session = auth.signed_session()
        prep_req = session.prepare_request(self.request)
        self.assertDictContainsSubset({'testheader' : 'testheadervalue'}, prep_req.headers)

        auth = ApiKeyCredentials(
            in_query={
                'testquery' : 'testparamvalue'
            }
        )
        session = auth.signed_session()
        prep_req = session.prepare_request(self.request)
        assert "testquery=testparamvalue" in prep_req.path_url

    def test_cs_auth(self):
        auth = CognitiveServicesCredentials("mysubkey")
        session = auth.signed_session()
        prep_req = session.prepare_request(self.request)
        self.assertDictContainsSubset({'Ocp-Apim-Subscription-Key' : 'mysubkey'}, prep_req.headers)

    def test_eventgrid_auth(self):
        auth = TopicCredentials("mytopickey")
        session = auth.signed_session()
        prep_req = session.prepare_request(self.request)
        self.assertDictContainsSubset({'aeg-sas-key' : 'mytopickey'}, prep_req.headers)

    def test_eventgrid_domain_auth(self):
        auth = DomainCredentials("mydomainkey")
        session = auth.signed_session()
        prep_req = session.prepare_request(self.request)
        self.assertDictContainsSubset({'aeg-sas-key' : 'mydomainkey'}, prep_req.headers)

if __name__ == '__main__':
    unittest.main()