Skip to content

Commit

Permalink
generate uidb64 and token for a test user in a fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentporte committed Nov 13, 2024
1 parent ee4c843 commit c904b31
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions lacommunaute/forum_member/tests/tests_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def mock_respx_post_to_sib_smtp_url_fixture():
yield


@pytest.fixture(name="user_token")
def user_token_fixture():
user = UserFactory(first_name="Samuel", last_name="Jackson", email="[email protected]")
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
token = default_token_generator.make_token(user)
return user, uidb64, token


class ForumProfileUpdateViewTest(TestCase):
def test_success_url(self):
forum_profiles = ForumProfileFactory()
Expand Down Expand Up @@ -177,30 +185,25 @@ def test_user_not_found(self, client, db):
assert response.status_code == 302
assert response.url == reverse("members:login")

def test_invalid_token(self, client, db):
user = UserFactory()
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
response = client.get(reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": "token"}))
def test_invalid_token(self, client, db, user_token):
user, uidb64, token = user_token
response = client.get(reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": "invalid_token"}))
assert response.status_code == 302
assert response.url == reverse("members:login")

def test_expired_token(self, client, db):
user = UserFactory()
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
expired_token = default_token_generator.make_token(user)
def test_expired_token(self, client, db, user_token):
user, uidb64, token = user_token

client.force_login(user)
# force logout
client.session.flush()

response = client.get(reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": expired_token}))
response = client.get(reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": token}))
assert response.status_code == 302
assert response.url == reverse("members:login")

def test_success(self, client, db):
user = UserFactory()
uidb64 = urlsafe_base64_encode(force_bytes(user.pk))
token = default_token_generator.make_token(user)
def test_success(self, client, db, user_token):
user, uidb64, token = user_token
next_url = "/topics/"

response = client.get(
Expand All @@ -210,6 +213,16 @@ def test_success(self, client, db):
assert response.url == next_url
assert client.session.get(IdentityProvider.MAGIC_LINK.name) == 1

def test_unallowed_next_url(self, client, db, user_token):
user, uidb64, token = user_token
next_url = "http://www.unallowed_host.com"

response = client.get(
reverse("members:login_with_link", kwargs={"uidb64": uidb64, "token": token}), data={"next": next_url}
)
assert response.status_code == 302
assert response.url == "/"


class TestLogoutView:
@pytest.mark.parametrize(
Expand Down

0 comments on commit c904b31

Please sign in to comment.