1
0
Fork 0
mirror of https://gitlab.com/bramw/baserow.git synced 2025-04-11 07:51:20 +00:00

call user_signed_in hook when a user signs in

This commit is contained in:
Bram Wiepjes 2021-04-12 19:05:34 +02:00
parent d06eb01de1
commit bff1b632ac
3 changed files with 61 additions and 23 deletions
backend
src/baserow
tests/baserow/api/users

View file

@ -80,6 +80,12 @@ class NormalizedEmailWebTokenSerializer(JSONWebTokenSerializer):
validated_data = super().validate(attrs)
update_last_login(None, validated_data["user"])
# Call the user_signed_in method for each plugin that is un the registry to
# notify all plugins that a user has signed in.
from baserow.core.registries import plugin_registry
for plugin in plugin_registry.registry.values():
plugin.user_signed_in(validated_data["user"])
return validated_data

View file

@ -86,6 +86,14 @@ class Plugin(APIUrlsInstanceMixin, Instance):
:type group_invitation: GroupInvitation
"""
def user_signed_in(self, user):
"""
A hook that is called after an existing user has signed in.
:param user: The user that just signed in.
:type user: User
"""
class PluginRegistry(APIUrlsRegistryMixin, Registry):
"""

View file

@ -11,6 +11,8 @@ from django.contrib.auth import get_user_model
from rest_framework_jwt.settings import api_settings
from baserow.core.registries import plugin_registry, Plugin
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
@ -21,6 +23,15 @@ User = get_user_model()
@pytest.mark.django_db
def test_token_auth(api_client, data_fixture):
class TmpPlugin(Plugin):
type = "tmp_plugin"
called = False
def user_signed_in(self, user):
self.called = True
plugin_mock = TmpPlugin()
user = data_fixture.create_user(
email="test@test.nl", password="password", first_name="Test1"
)
@ -45,19 +56,21 @@ def test_token_auth(api_client, data_fixture):
assert response.status_code == HTTP_400_BAD_REQUEST
assert len(json["non_field_errors"]) > 0
with freeze_time("2020-01-01 12:00"):
response = api_client.post(
reverse("api:user:token_auth"),
{"username": "test@test.nl", "password": "password"},
format="json",
)
json = response.json()
assert response.status_code == HTTP_200_OK
assert "token" in json
assert "user" in json
assert json["user"]["username"] == "test@test.nl"
assert json["user"]["first_name"] == "Test1"
assert json["user"]["is_staff"] is False
with patch.dict(plugin_registry.registry, {"tmp": plugin_mock}):
with freeze_time("2020-01-01 12:00"):
response = api_client.post(
reverse("api:user:token_auth"),
{"username": "test@test.nl", "password": "password"},
format="json",
)
json = response.json()
assert response.status_code == HTTP_200_OK
assert "token" in json
assert "user" in json
assert json["user"]["username"] == "test@test.nl"
assert json["user"]["first_name"] == "Test1"
assert json["user"]["is_staff"] is False
assert plugin_mock.called
user.refresh_from_db()
assert user.last_login == datetime(2020, 1, 1, 12, 00, tzinfo=timezone("UTC"))
@ -82,6 +95,15 @@ def test_token_auth(api_client, data_fixture):
@pytest.mark.django_db
def test_token_refresh(api_client, data_fixture):
class TmpPlugin(Plugin):
type = "tmp_plugin"
called = False
def user_signed_in(self, user):
self.called = True
plugin_mock = TmpPlugin()
user, token = data_fixture.create_user_and_token(
email="test@test.nl", password="password", first_name="Test1"
)
@ -91,16 +113,18 @@ def test_token_refresh(api_client, data_fixture):
)
assert response.status_code == HTTP_400_BAD_REQUEST
response = api_client.post(
reverse("api:user:token_refresh"), {"token": token}, format="json"
)
assert response.status_code == HTTP_200_OK
json = response.json()
assert "token" in json
assert "user" in json
assert json["user"]["username"] == "test@test.nl"
assert json["user"]["first_name"] == "Test1"
assert json["user"]["is_staff"] is False
with patch.dict(plugin_registry.registry, {"tmp": plugin_mock}):
response = api_client.post(
reverse("api:user:token_refresh"), {"token": token}, format="json"
)
assert response.status_code == HTTP_200_OK
json = response.json()
assert "token" in json
assert "user" in json
assert json["user"]["username"] == "test@test.nl"
assert json["user"]["first_name"] == "Test1"
assert json["user"]["is_staff"] is False
assert not plugin_mock.called
with patch("rest_framework_jwt.utils.datetime") as mock_datetime:
mock_datetime.utcnow.return_value = datetime(2019, 1, 1, 1, 1, 1, 0)