Debian Codegraph @

tests/apps/socialaccount/providers/telegram/tests.py

raw
1 import base64
2 import json
3 from http import HTTPStatus
4
5 from django.urls import reverse
6
7 import pytest
8 from pytest_django.asserts import assertTemplateUsed
9
10 from allauth.socialaccount.models import SocialAccount
11
12
13 @pytest.fixture
14 def telegram_app(settings):
15 settings.SOCIALACCOUNT_PROVIDERS = {
16 "telegram": {
17 "APPS": [
18 {
19 "client_id": "123",
20 }
21 ]
22 }
23 }
24
25
26 def test_login(client, db, telegram_app):
27 resp = client.post(reverse("telegram_login"))
28 assert resp.status_code == HTTPStatus.FOUND
29 assert resp["location"].startswith(
30 "https://oauth.telegram.org/auth?origin=http%3A%2F%2Ftestserver%2F&bot_id=123&request_access=write&embed=0&return_to=http%3A%2F%2Ftestserver%2Faccounts%2Ftelegram%2Flogin%2Fcallback%2F%3Fstate%3D"
31 )
32
33
34 def test_callback_get(client, db, telegram_app):
35 resp = client.get(reverse("telegram_callback"))
36 assert resp.status_code == HTTPStatus.OK
37 assertTemplateUsed(resp, "telegram/callback.html")
38
39
40 def test_callback(client, db, telegram_app, sociallogin_setup_state):
41 state = sociallogin_setup_state(client)
42 auth_result = (
43 base64.b64encode(
44 json.dumps(
45 {
46 "id": "123",
47 "hash": "0744ab643757850e82fa8b4ac35978dca287c81df6a9829032d868c7f90e3b99",
48 "auth_date": 2342342342,
49 }
50 ).encode("utf8")
51 )
52 .decode("ascii")
53 .replace("=", "")
54 )
55 post_data = {
56 "tgAuthResult": auth_result,
57 }
58 resp = client.post(f"{reverse('telegram_callback')}?state={state}", post_data)
59 assert resp.status_code == HTTPStatus.FOUND
60 assert SocialAccount.objects.filter(uid="123").exists()