File: test_allauth.py

package info (click to toggle)
django-invitations 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 456 kB
  • sloc: python: 1,484; makefile: 27; sh: 6
file content (177 lines) | stat: -rw-r--r-- 5,588 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from invitations.app_settings import app_settings

try:
    from django.urls import reverse
except ImportError:
    from django.core.urlresolvers import reverse

import pytest
from allauth.account.models import EmailAddress
from django.test import Client
from django.test.client import RequestFactory

from invitations.adapters import get_invitations_adapter
from invitations.models import InvitationsAdapter
from invitations.utils import get_invitation_model

Invitation = get_invitation_model()


class TestAllAuthIntegrationAcceptAfterSignup:
    client = Client()
    adapter = get_invitations_adapter()

    @pytest.mark.parametrize(
        "method",
        [
            ("get"),
            ("post"),
        ],
    )
    def test_accept_invite_accepted_invitation_after_signup(
        self,
        settings,
        method,
        sent_invitation_by_user_a,
        user_a,
    ):
        settings.INVITATIONS_ACCEPT_INVITE_AFTER_SIGNUP = True
        client_with_method = getattr(self.client, method)
        resp = client_with_method(
            reverse(
                app_settings.CONFIRMATION_URL_NAME,
                kwargs={"key": sent_invitation_by_user_a.key},
            ),
            follow=True,
        )
        assert resp.status_code == 200

        invite = Invitation.objects.get(email="email@example.com")
        assert invite.inviter == user_a
        assert invite.accepted is False
        assert resp.request["PATH_INFO"] == reverse("account_signup")
        form = resp.context_data["form"]
        assert "email@example.com" == form.fields["email"].initial

        resp = self.client.post(
            reverse("account_signup"),
            {
                "email": "email@example.com",
                "username": "username",
                "password1": "password",
                "password2": "password",
            },
        )
        invite = Invitation.objects.get(email="email@example.com")
        assert invite.accepted is True

    @pytest.mark.parametrize(
        "method",
        [
            ("get"),
            ("post"),
        ],
    )
    def test_invite_accepted_after_signup_with_altered_case_email(
        self,
        settings,
        method,
        sent_invitation_by_user_a,
        user_a,
    ):
        settings.INVITATIONS_ACCEPT_INVITE_AFTER_SIGNUP = True
        client_with_method = getattr(self.client, method)
        resp = client_with_method(
            reverse(
                app_settings.CONFIRMATION_URL_NAME,
                kwargs={"key": sent_invitation_by_user_a.key},
            ),
            follow=True,
        )

        invite = Invitation.objects.get(email="email@example.com")
        assert invite.accepted is False
        form = resp.context_data["form"]
        assert "email@example.com" == form.fields["email"].initial

        resp = self.client.post(
            reverse("account_signup"),
            {
                "email": "EMAIL@EXAMPLE.COM",
                "username": "username",
                "password1": "password",
                "password2": "password",
            },
        )
        invite = Invitation.objects.get(email="email@example.com")
        assert invite.accepted is True


class TestAllAuthIntegration:
    client = Client()
    adapter = get_invitations_adapter()

    @pytest.mark.parametrize(
        "method",
        [
            ("get"),
            ("post"),
        ],
    )
    def test_accept_invite_allauth(
        self,
        method,
        settings,
        user_a,
        sent_invitation_by_user_a,
    ):
        client_with_method = getattr(self.client, method)
        resp = client_with_method(
            reverse(
                app_settings.CONFIRMATION_URL_NAME,
                kwargs={"key": sent_invitation_by_user_a.key},
            ),
            follow=True,
        )
        invite = Invitation.objects.get(email="email@example.com")
        assert invite.accepted
        assert invite.inviter == user_a
        assert resp.request["PATH_INFO"] == reverse("account_signup")

        form = resp.context_data["form"]
        assert "email@example.com" == form.fields["email"].initial
        messages = resp.context["messages"]
        message_text = [message.message for message in messages]
        assert "Invitation to - email@example.com - has been accepted" in message_text

        resp = self.client.post(
            reverse("account_signup"),
            {
                "email": "email@example.com",
                "username": "username",
                "password1": "password",
                "password2": "password",
            },
        )

        allauth_email_obj = EmailAddress.objects.get(email="email@example.com")
        assert allauth_email_obj.verified is True

    def test_fetch_adapter(self):
        assert isinstance(self.adapter, InvitationsAdapter)

    def test_allauth_signup_open(self):
        signup_request = RequestFactory().get(
            reverse("account_signup", urlconf="allauth.account.urls"),
        )
        assert self.adapter.is_open_for_signup(signup_request) is True

    @pytest.mark.django_db
    def test_allauth_adapter_invitations_only(self, settings):
        settings.INVITATIONS_INVITATION_ONLY = True
        signup_request = RequestFactory().get(
            reverse("account_signup", urlconf="allauth.account.urls"),
        )
        assert self.adapter.is_open_for_signup(signup_request) is False
        response = self.client.get(reverse("account_signup"))
        assert "Sign Up Closed" in response.content.decode("utf8")