Skip to content

Commit 7ffbe92

Browse files
vvgrem@gmail.comvvgrem@gmail.com
authored andcommitted
SharePoint tests: UserProfile tests fixes, README.md updated to reflect migration changes from adal to msal
1 parent 8dc6780 commit 7ffbe92

File tree

6 files changed

+86
-54
lines changed

6 files changed

+86
-54
lines changed

README.md

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,15 @@ the following clients are available:
112112

113113
#### Authentication
114114

115-
In terms of Microsoft Graph API authentication, there is no any dependency to any particular library implementation,
116-
the following libraries are supported at least:
115+
[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency
116+
is used as a default library to obtain token to call Microsoft Graph API. But in terms of Microsoft Graph API
117+
authentication, another Microsoft Authentication Client compliant libraries such [adal](https://github.com/AzureAD/azure-activedirectory-library-for-python)
118+
as are supported as well.
117119

118120
> Note: access token is getting acquired via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)
119121
> in the provided examples
120122
121-
[Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)
123+
Using [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)
122124

123125
```python
124126
import msal
@@ -134,24 +136,24 @@ def acquire_token():
134136
client_id='{client_id}',
135137
client_credential='{client_secret}'
136138
)
137-
result = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
138-
return result
139+
token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
140+
return token
139141

140142

141143
client = GraphClient(acquire_token)
142144

143145
```
144146

145147

146-
[ADAL Python](https://adal-python.readthedocs.io/en/latest/#)
148+
Using [ADAL Python](https://adal-python.readthedocs.io/en/latest/#)
147149

148150
Usage
149151

150152
```python
151153
import adal
152154
from office365.graph_client import GraphClient
153155

154-
def get_token():
156+
def acquire_token():
155157
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
156158
auth_ctx = adal.AuthenticationContext(authority_url)
157159
token = auth_ctx.acquire_token_with_client_credentials(
@@ -160,7 +162,7 @@ def get_token():
160162
"{client_secret}")
161163
return token
162164

163-
client = GraphClient(get_token)
165+
client = GraphClient(acquire_token)
164166

165167
```
166168

@@ -171,19 +173,9 @@ The example demonstrates how to send an email via [Microsoft Graph endpoint](htt
171173
> Note: access token is getting acquired via [Client Credential flow](https://docs.microsoft.com/en-us/azure/active-directory/develop/v2-oauth2-client-creds-grant-flow)
172174
173175
```python
174-
import adal
175176
from office365.graph_client import GraphClient
176177

177-
def get_token():
178-
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
179-
auth_ctx = adal.AuthenticationContext(authority_url)
180-
token = auth_ctx.acquire_token_with_client_credentials(
181-
"https://graph.microsoft.com",
182-
"{client_id}",
183-
"{client_secret}")
184-
return token
185-
186-
client = GraphClient(get_token)
178+
client = GraphClient(acquire_token)
187179

188180
message_json = {
189181
"Message": {
@@ -217,8 +209,26 @@ client.execute_query()
217209

218210
#### Authentication
219211

220-
[ADAL Python](https://adal-python.readthedocs.io/en/latest/#)
221-
library is utilized to authenticate users to Active Directory (AD) and obtain tokens
212+
[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency
213+
is used to obtain token
214+
215+
```python
216+
import msal
217+
218+
def acquire_token():
219+
"""
220+
Acquire token via MSAL
221+
"""
222+
authority_url = 'https://login.microsoftonline.com/{tenant_id_or_name}'
223+
app = msal.ConfidentialClientApplication(
224+
authority=authority_url,
225+
client_id='{client_id}',
226+
client_credential='{client_secret}'
227+
)
228+
token = app.acquire_token_for_client(scopes=["https://graph.microsoft.com/.default"])
229+
return token
230+
```
231+
222232

223233
#### Examples
224234

@@ -231,17 +241,9 @@ which corresponds to [`list available drives` endpoint](https://docs.microsoft.c
231241
232242
```python
233243
from office365.graph_client import GraphClient
234-
def get_token(auth_ctx):
235-
"""Acquire token via client credential flow (ADAL Python library is utilized)"""
236-
token = auth_ctx.acquire_token_with_client_credentials(
237-
"https://graph.microsoft.com",
238-
"{client_id}",
239-
"{client_secret}")
240-
return token
241-
242244

243245
tenant_name = "contoso.onmicrosoft.com"
244-
client = GraphClient(get_token)
246+
client = GraphClient(acquire_token)
245247
drives = client.drives
246248
client.load(drives)
247249
client.execute_query()
@@ -254,7 +256,7 @@ for drive in drives:
254256

255257
```python
256258
from office365.graph_client import GraphClient
257-
client = GraphClient(get_token)
259+
client = GraphClient(acquire_token)
258260
# retrieve drive properties
259261
drive = client.users["{user_id_or_principal_name}"].drive
260262
client.load(drive)
@@ -288,8 +290,8 @@ Refer [OneDrive examples section](examples/onedrive) for a more examples.
288290

289291
#### Authentication
290292

291-
[ADAL Python](https://adal-python.readthedocs.io/en/latest/#)
292-
library is utilized to authenticate users to Active Directory (AD) and obtain tokens
293+
[The Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/) which comes as a dependency
294+
is used to obtain token
293295

294296
#### Examples
295297

@@ -301,30 +303,16 @@ which corresponds to [`Create team` endpoint](https://docs.microsoft.com/en-us/g
301303
```python
302304
from office365.graph_client import GraphClient
303305
tenant_name = "contoso.onmicrosoft.com"
304-
client = GraphClient(tenant_name, get_token)
306+
client = GraphClient(tenant_name, acquire_token)
305307
new_team = client.groups["{group_id}"].add_team()
306308
client.execute_query()
307309
```
308310

309-
where
310-
311-
```python
312-
def get_token(auth_ctx):
313-
"""Acquire token via client credential flow (ADAL Python library is utilized)
314-
:type auth_ctx: adal.AuthenticationContext
315-
"""
316-
token = auth_ctx.acquire_token_with_client_credentials(
317-
"https://graph.microsoft.com",
318-
"{client_id}",
319-
"{client_secret}")
320-
return token
321-
```
322-
323311

324312
# Third Party Libraries and Dependencies
325313
The following libraries will be installed when you install the client library:
326314
* [requests](https://github.com/kennethreitz/requests)
327-
* [adal](https://github.com/AzureAD/azure-activedirectory-library-for-python)
315+
* [Microsoft Authentication Library (MSAL) for Python](https://pypi.org/project/msal/)
328316

329317

330318

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from office365.sharepoint.base_entity import BaseEntity
2+
3+
4+
class SocialAttachment(BaseEntity):
5+
pass
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from office365.sharepoint.base_entity import BaseEntity
2+
3+
4+
class PeopleManager(BaseEntity):
5+
"""Provides methods for operations related to people."""
6+
pass
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
from office365.runtime.client_object import ClientObject
2+
from office365.runtime.resource_path_service_operation import ResourcePathServiceOperation
23

34

45
class UserProfilePropertiesForUser(ClientObject):
5-
pass
6+
7+
def __init__(self, context, account_name, property_names):
8+
params = {
9+
"accountName": account_name,
10+
"propertyNames": property_names
11+
}
12+
13+
super().__init__(context, ResourcePathServiceOperation("SP.UserProfiles.UserProfilePropertiesForUser", params))

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
requests~=2.24.0
22
requests_ntlm [NTLMAuthentication]
33
setuptools~=50.3.0
4+
5+
adal~=1.2.4
6+
msal~=1.5.0
7+
Faker~=4.1.2
8+
astunparse~=1.6.3

tests/sharepoint/test_user_profile.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
1-
from tests.sharepoint.sharepoint_case import SPTestCase
1+
from unittest import TestCase
2+
3+
from office365.runtime.auth.user_credential import UserCredential
4+
from office365.sharepoint.client_context import ClientContext
5+
from office365.sharepoint.userprofiles.userProfilePropertiesForUser import UserProfilePropertiesForUser
6+
from settings import settings
7+
28

39
from office365.sharepoint.userprofiles.profileLoader import ProfileLoader
410

511

6-
class TestUserProfile(SPTestCase):
12+
class TestUserProfile(TestCase):
713
profile_loader = None # type: ProfileLoader
814

15+
@classmethod
16+
def setUpClass(cls):
17+
credentials = UserCredential(settings['user_credentials']['username'],
18+
settings['user_credentials']['password'])
19+
cls.my_client = ClientContext(settings['url']).with_credentials(credentials)
20+
921
def test1_get_profile_loader(self):
10-
profile_loader = ProfileLoader.get_profile_loader(self.client).execute_query()
22+
profile_loader = ProfileLoader.get_profile_loader(self.my_client).execute_query()
1123
self.__class__.profile_loader = profile_loader
1224

1325
def test2_get_profile_loader(self):
@@ -16,4 +28,12 @@ def test2_get_profile_loader(self):
1628

1729
def test3_create_personal_site(self):
1830
user_profile = self.__class__.profile_loader.get_user_profile()
19-
user_profile.create_personal_site_enque(True).execute_query()
31+
up = user_profile.create_personal_site_enque(True).execute_query()
32+
self.assertIsNotNone(up.properties['PublicUrl'])
33+
34+
# def test4_get_user_props(self):
35+
# account_name = settings['user_credentials']['username']
36+
# props = UserProfilePropertiesForUser(self.my_client, account_name, ['PublicUrl'])
37+
# self.my_client.load(props)
38+
# self.my_client.execute_query()
39+
# self.assertIsNotNone(props)

0 commit comments

Comments
 (0)