Debian Codegraph @

tests/apps/socialaccount/providers/linkedin_oauth2/tests.py

raw
1 from http import HTTPStatus
2 from json import loads
3
4 from django.test import TestCase
5 from django.test.utils import override_settings
6
7 from allauth.socialaccount.models import SocialAccount
8 from allauth.socialaccount.providers.base import ProviderException
9 from allauth.socialaccount.providers.linkedin_oauth2.provider import (
10 LinkedInOAuth2Provider,
11 )
12 from tests.apps.socialaccount.base import OAuth2TestsMixin
13 from tests.mocking import MockedResponse
14
15
16 class LinkedInOAuth2Tests(OAuth2TestsMixin, TestCase):
17 provider_id = LinkedInOAuth2Provider.id
18
19 def get_mocked_response(self):
20 return [
21 MockedResponse(
22 HTTPStatus.OK,
23 """
24 {}
25 """,
26 ),
27 MockedResponse(
28 HTTPStatus.OK,
29 """
30 {
31 "profilePicture": {
32 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
33 },
34 "id": "1234567",
35 "lastName": {
36 "preferredLocale": {
37 "language": "en",
38 "country": "US"
39 },
40 "localized": {
41 "en_US": "Penners"
42 }
43 },
44 "firstName": {
45 "preferredLocale": {
46 "language": "en",
47 "country": "US"
48 },
49 "localized": {
50 "en_US": "Raymond"
51 }
52 }
53 }
54 """,
55 ),
56 ]
57
58 def get_expected_to_str(self):
59 return "Raymond Penners"
60
61 def test_data_to_str(self):
62 data = {
63 "emailAddress": "[email protected]",
64 "firstName": "John",
65 "id": "a1b2c3d4e",
66 "lastName": "Doe",
67 "pictureUrl": "https://media.licdn.com/mpr/foo",
68 "pictureUrls": {
69 "_total": 1,
70 "values": ["https://media.licdn.com/foo"],
71 },
72 "publicProfileUrl": "https://www.linkedin.com/in/johndoe",
73 }
74 acc = SocialAccount(extra_data=data, provider="linkedin_oauth2")
75 self.assertEqual(acc.get_provider_account().to_str(), "[email protected]")
76
77 def test_get_avatar_url_no_picture_setting(self):
78 extra_data = """
79 {
80 "profilePicture": {
81 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
82 },
83 "id": "1234567",
84 "lastName": {
85 "preferredLocale": {
86 "language": "en",
87 "country": "US"
88 },
89 "localized": {
90 "en_US": "Penners"
91 }
92 },
93 "firstName": {
94 "preferredLocale": {
95 "language": "en",
96 "country": "US"
97 },
98 "localized": {
99 "en_US": "Raymond"
100 }
101 }
102 }
103 """
104 acc = SocialAccount(
105 extra_data=loads(extra_data),
106 provider="linkedin_oauth2",
107 )
108 self.assertIsNone(acc.get_avatar_url())
109
110 @override_settings(
111 SOCIALACCOUNT_PROVIDERS={
112 "linkedin_oauth2": {
113 "PROFILE_FIELDS": [
114 "id",
115 "firstName",
116 "lastName",
117 "profilePicture(displayImage~:playableStreams)",
118 ],
119 "PROFILEPICTURE": {
120 "display_size_w_h": (400, 400.0),
121 },
122 },
123 }
124 )
125 def test_get_avatar_url_with_setting(self):
126 extra_data = """
127 {
128 "profilePicture": {
129 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
130 },
131 "id": "1234567",
132 "lastName": {
133 "preferredLocale": {
134 "language": "en",
135 "country": "US"
136 },
137 "localized": {
138 "en_US": "Penners"
139 }
140 },
141 "firstName": {
142 "preferredLocale": {
143 "language": "en",
144 "country": "US"
145 },
146 "localized": {
147 "en_US": "Raymond"
148 }
149 }
150 }
151 """
152 acc = SocialAccount(
153 extra_data=loads(extra_data),
154 provider="linkedin_oauth2",
155 )
156 self.assertIsNone(acc.get_avatar_url())
157
158 @override_settings(
159 SOCIALACCOUNT_PROVIDERS={
160 "linkedin_oauth2": {
161 "PROFILE_FIELDS": [
162 "id",
163 "firstName",
164 "lastName",
165 "profilePicture(displayImage~:playableStreams)",
166 ],
167 "PROFILEPICTURE": {
168 "display_size_w_h": (100, 100.0),
169 },
170 },
171 }
172 )
173 def test_get_avatar_url_with_picture(self):
174 extra_data = """
175 {
176 "profilePicture": {
177 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
178 },
179 "id": "1234567",
180 "lastName": {
181 "preferredLocale": {
182 "language": "en",
183 "country": "US"
184 },
185 "localized": {
186 "en_US": "Penners"
187 }
188 },
189 "firstName": {
190 "preferredLocale": {
191 "language": "en",
192 "country": "US"
193 },
194 "localized": {
195 "en_US": "Raymond"
196 }
197 },
198 "profilePicture": {
199 "displayImage~": {
200 "elements": [
201 {
202 "authorizationMethod": "PUBLIC",
203 "data": {
204 "com.linkedin.digitalmedia.mediaartifact.StillImage": {
205 "storageSize": {
206 "height": 100,
207 "width": 100
208 },
209 "storageAspectRatio": {
210 "heightAspect": 1.0,
211 "formatted": "1.00:1.00",
212 "widthAspect": 1.0
213 },
214 "displaySize": {
215 "height": 100.0,
216 "width": 100.0,
217 "uom": "PX"
218 },
219 "rawCodecSpec": {
220 "name": "jpeg",
221 "type": "image"
222 },
223 "displayAspectRatio": {
224 "heightAspect": 1.0,
225 "formatted": "1.00:1.00",
226 "widthAspect": 1.0
227 },
228 "mediaType": "image/jpeg"
229 }
230 },
231 "artifact": "urn:li:digitalmediaMediaArtifact:avatar",
232 "identifiers": [
233 {
234 "identifierExpiresInSeconds": 4,
235 "file": "urn:li:digitalmediaFile:this-is-the-link",
236 "index": 0,
237 "identifier": "this-is-the-link",
238 "mediaType": "image/jpeg",
239 "identifierType": "EXTERNAL_URL"
240 }
241 ]
242 }
243 ]
244 }
245 }
246 }
247 """
248 acc = SocialAccount(
249 extra_data=loads(extra_data),
250 provider="linkedin_oauth2",
251 )
252 self.assertEqual("this-is-the-link", acc.get_avatar_url())
253
254 @override_settings(
255 SOCIALACCOUNT_PROVIDERS={
256 "linkedin_oauth2": {
257 "PROFILE_FIELDS": [
258 "id",
259 "firstName",
260 "lastName",
261 "profilePicture(displayImage~:playableStreams)",
262 ],
263 "PROFILEPICTURE": {
264 "display_size_w_h": (400, 400.0),
265 },
266 },
267 }
268 )
269 def test_get_avatar_url_size_mismatch(self):
270 extra_data = """
271 {
272 "profilePicture": {
273 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
274 },
275 "id": "1234567",
276 "lastName": {
277 "preferredLocale": {
278 "language": "en",
279 "country": "US"
280 },
281 "localized": {
282 "en_US": "Penners"
283 }
284 },
285 "firstName": {
286 "preferredLocale": {
287 "language": "en",
288 "country": "US"
289 },
290 "localized": {
291 "en_US": "Raymond"
292 }
293 },
294 "profilePicture": {
295 "displayImage~": {
296 "elements": [
297 {
298 "authorizationMethod": "PUBLIC",
299 "data": {
300 "com.linkedin.digitalmedia.mediaartifact.StillImage": {
301 "storageSize": {
302 "height": 100,
303 "width": 100
304 },
305 "storageAspectRatio": {
306 "heightAspect": 1.0,
307 "formatted": "1.00:1.00",
308 "widthAspect": 1.0
309 },
310 "displaySize": {
311 "height": 100.0,
312 "width": 100.0,
313 "uom": "PX"
314 },
315 "rawCodecSpec": {
316 "name": "jpeg",
317 "type": "image"
318 },
319 "displayAspectRatio": {
320 "heightAspect": 1.0,
321 "formatted": "1.00:1.00",
322 "widthAspect": 1.0
323 },
324 "mediaType": "image/jpeg"
325 }
326 },
327 "artifact": "urn:li:digitalmediaMediaArtifact:avatar",
328 "identifiers": [
329 {
330 "identifierExpiresInSeconds": 4,
331 "file": "urn:li:digitalmediaFile:this-is-the-link",
332 "index": 0,
333 "identifier": "this-is-the-link",
334 "mediaType": "image/jpeg",
335 "identifierType": "EXTERNAL_URL"
336 }
337 ]
338 }
339 ]
340 }
341 }
342 }
343 """
344 acc = SocialAccount(
345 extra_data=loads(extra_data),
346 provider="linkedin_oauth2",
347 )
348 self.assertIsNone(acc.get_avatar_url())
349
350 @override_settings(
351 SOCIALACCOUNT_PROVIDERS={
352 "linkedin_oauth2": {
353 "PROFILE_FIELDS": [
354 "id",
355 "firstName",
356 "lastName",
357 "profilePicture(displayImage~:playableStreams)",
358 ],
359 "PROFILEPICTURE": {
360 "display_size_w_h": (400, 400.0),
361 },
362 },
363 }
364 )
365 def test_get_avatar_url_auth_mismatch(self):
366 extra_data = """
367 {
368 "profilePicture": {
369 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
370 },
371 "id": "1234567",
372 "lastName": {
373 "preferredLocale": {
374 "language": "en",
375 "country": "US"
376 },
377 "localized": {
378 "en_US": "Penners"
379 }
380 },
381 "firstName": {
382 "preferredLocale": {
383 "language": "en",
384 "country": "US"
385 },
386 "localized": {
387 "en_US": "Raymond"
388 }
389 },
390 "profilePicture": {
391 "displayImage~": {
392 "elements": [
393 {
394 "authorizationMethod": "PRIVATE",
395 "data": {
396 "com.linkedin.digitalmedia.mediaartifact.StillImage": {
397 "storageSize": {
398 "height": 100,
399 "width": 100
400 },
401 "storageAspectRatio": {
402 "heightAspect": 1.0,
403 "formatted": "1.00:1.00",
404 "widthAspect": 1.0
405 },
406 "displaySize": {
407 "height": 100.0,
408 "width": 100.0,
409 "uom": "PX"
410 },
411 "rawCodecSpec": {
412 "name": "jpeg",
413 "type": "image"
414 },
415 "displayAspectRatio": {
416 "heightAspect": 1.0,
417 "formatted": "1.00:1.00",
418 "widthAspect": 1.0
419 },
420 "mediaType": "image/jpeg"
421 }
422 },
423 "artifact": "urn:li:digitalmediaMediaArtifact:avatar",
424 "identifiers": [
425 {
426 "identifierExpiresInSeconds": 4,
427 "file": "urn:li:digitalmediaFile:this-is-the-link",
428 "index": 0,
429 "identifier": "this-is-the-link",
430 "mediaType": "image/jpeg",
431 "identifierType": "EXTERNAL_URL"
432 }
433 ]
434 }
435 ]
436 }
437 }
438 }
439 """
440 acc = SocialAccount(
441 extra_data=loads(extra_data),
442 provider="linkedin_oauth2",
443 )
444 self.assertIsNone(acc.get_avatar_url())
445
446 @override_settings(
447 SOCIALACCOUNT_PROVIDERS={
448 "linkedin_oauth2": {
449 "PROFILE_FIELDS": [
450 "id",
451 "firstName",
452 "lastName",
453 "profilePicture(displayImage~:playableStreams)",
454 ],
455 "PROFILEPICTURE": {
456 "display_size_w_h": (100, 100),
457 },
458 },
459 }
460 )
461 def test_get_avatar_url_float_vs_int(self):
462 extra_data = """
463 {
464 "profilePicture": {
465 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
466 },
467 "id": "1234567",
468 "lastName": {
469 "preferredLocale": {
470 "language": "en",
471 "country": "US"
472 },
473 "localized": {
474 "en_US": "Penners"
475 }
476 },
477 "firstName": {
478 "preferredLocale": {
479 "language": "en",
480 "country": "US"
481 },
482 "localized": {
483 "en_US": "Raymond"
484 }
485 },
486 "profilePicture": {
487 "displayImage~": {
488 "elements": [
489 {
490 "authorizationMethod": "PUBLIC",
491 "data": {
492 "com.linkedin.digitalmedia.mediaartifact.StillImage": {
493 "storageSize": {
494 "height": 100,
495 "width": 100
496 },
497 "storageAspectRatio": {
498 "heightAspect": 1.0,
499 "formatted": "1.00:1.00",
500 "widthAspect": 1.0
501 },
502 "displaySize": {
503 "height": 100.0,
504 "width": 100.0,
505 "uom": "PX"
506 },
507 "rawCodecSpec": {
508 "name": "jpeg",
509 "type": "image"
510 },
511 "displayAspectRatio": {
512 "heightAspect": 1.0,
513 "formatted": "1.00:1.00",
514 "widthAspect": 1.0
515 },
516 "mediaType": "image/jpeg"
517 }
518 },
519 "artifact": "urn:li:digitalmediaMediaArtifact:avatar",
520 "identifiers": [
521 {
522 "identifierExpiresInSeconds": 4,
523 "file": "urn:li:digitalmediaFile:this-is-the-link",
524 "index": 0,
525 "identifier": "this-is-the-link",
526 "mediaType": "image/jpeg",
527 "identifierType": "EXTERNAL_URL"
528 }
529 ]
530 }
531 ]
532 }
533 }
534 }
535 """
536 acc = SocialAccount(
537 extra_data=loads(extra_data),
538 provider="linkedin_oauth2",
539 )
540 self.assertEqual("this-is-the-link", acc.get_avatar_url())
541
542 def test_id_missing(self):
543 extra_data = """
544 {
545 "profilePicture": {
546 "displayImage": "urn:li:digitalmediaAsset:12345abcdefgh-12abcd"
547 },
548 "Id": "1234567"
549 }
550 """
551 self.assertRaises(
552 ProviderException, self.provider.extract_uid, loads(extra_data)
553 )