File: test_e2e_examples.py

package info (click to toggle)
python-adal 1.2.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,344 kB
  • sloc: xml: 12,790; python: 4,656; makefile: 14
file content (134 lines) | stat: -rw-r--r-- 6,167 bytes parent folder | download | duplicates (4)
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
#------------------------------------------------------------------------------
#
# Copyright (c) Microsoft Corporation. 
# All rights reserved.
# 
# This code is licensed under the MIT License.
# 
# 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 unittest
import base64
import json
import adal

try:
    from tests.config import ACQUIRE_TOKEN_WITH_USERNAME_PASSWORD as user_pass_params
    from tests.config import ACQUIRE_TOKEN_WITH_CLIENT_CREDENTIALS as client_cred_params

    #per http://stackoverflow.com/questions/12487532/how-do-i-skip-a-whole-python-unittest-module-at-run-time

    class TestE2EExamples(unittest.TestCase):

        def setUp(self):
            self.assertIsNotNone(user_pass_params['password'], "This test cannot work without you adding a password")
            return super(TestE2EExamples, self).setUp()

        def test_acquire_token_with_user_pass_explicit(self):
            resource = '00000002-0000-0000-c000-000000000000'
            client_id_xplat = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'
            authority = user_pass_params['authorityHostUrl'] + '/' + user_pass_params['tenant']

            context = adal.AuthenticationContext(authority)
            token_response = context.acquire_token_with_username_password(
                resource, user_pass_params['username'], user_pass_params['password'],
                client_id_xplat)
            self.validate_token_response_username_password(token_response)

        def test_acquire_token_with_client_creds(self):
            resource = '00000002-0000-0000-c000-000000000000'
            context = adal.AuthenticationContext(client_cred_params['authority'])
            token_response = context.acquire_token_with_client_credentials(
                 resource,
                 client_cred_params['clientId'],
                 client_cred_params['secret'])

            self.validate_token_response_client_credentials(token_response)

        @unittest.skip('https://github.com/AzureAD/azure-activedirectory-library-for-python-priv/issues/46')
        def test_acquire_token_with_authorization_code(self):
            self.fail("Not Yet Implemented")

        def test_acquire_token_with_refresh_token(self):
            authority = user_pass_params['authorityHostUrl'] + '/' + user_pass_params['tenant']
            resource = '00000002-0000-0000-c000-000000000000'
            client_id_xplat = '04b07795-8ddb-461a-bbee-02f9e1bf7b46'

            # Get token using username password first
            context = adal.AuthenticationContext(authority)
            token_response = context.acquire_token_with_username_password(
                resource, user_pass_params['username'], user_pass_params['password'],
                client_id_xplat)
            self.validate_token_response_username_password(token_response)

            # Use returned refresh token to acquire a new token.
            refresh_token = token_response['refreshToken']
            context = adal.AuthenticationContext(authority)
            token_response2 = context.acquire_token_with_refresh_token(refresh_token, client_id_xplat, resource)
            self.validate_token_response_refresh_token(token_response2)

        @unittest.skip('https://github.com/AzureAD/azure-activedirectory-library-for-python-priv/issues/47')
        def test_acquire_token_with_client_certificate(self):
            self.fail("Not Yet Implemented")


        # Validation Methods
        def validate_keys_in_dict(self, dict, keys):
            for i in keys:
                self.assertIn(i, dict)

        def validate_token_response_username_password(self, token_response):
            self.validate_keys_in_dict(
                token_response,
                [
                    'accessToken', 'expiresIn', 'expiresOn', 'familyName', 'givenName',
                    'refreshToken', 'resource', 'tenantId', 'tokenType',
                ]
            )

        def validate_token_response_client_credentials(self, token_response):
            self.validate_keys_in_dict(
                token_response,
                ['accessToken', 'expiresIn', 'expiresOn', 'resource', 'tokenType']
            )

        @unittest.skip('https://github.com/AzureAD/azure-activedirectory-library-for-python-priv/issues/46')
        def validate_token_response_authorization_code(self, token_response):
            self.fail("Not Yet Implemented")

        def validate_token_response_refresh_token(self, token_response):
            self.validate_keys_in_dict(
                token_response,
                [
                    'accessToken', 'expiresIn', 'expiresOn', 'refreshToken', 'resource', 'tokenType'
                ]
            )

        @unittest.skip('https://github.com/AzureAD/azure-activedirectory-library-for-python-priv/issues/47')
        def validate_token_response_client_certificate(self, token_response):
            self.fail("Not Yet Implemented")

except:
    print ("WARNING: E2E example testing were skipped, for missing 'config.py'.")


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