Ejemplo n.º 1
0
    def validate_password_change(self, instance: User,
                                 current_password: Optional[str],
                                 password: Optional[str]) -> Optional[str]:
        if password:
            if instance.password and instance.has_usable_password():
                # If user has a password set, we check it's provided to allow updating it. We need to check that is both
                # usable (properly hashed) and that a password actually exists.
                if not current_password:
                    raise serializers.ValidationError(
                        {
                            "current_password": [
                                "This field is required when updating your password."
                            ]
                        },
                        code="required")

                if not instance.check_password(current_password):
                    raise serializers.ValidationError(
                        {
                            "current_password":
                            ["Your current password is incorrect."]
                        },
                        code="incorrect_password")
            try:
                validate_password(password, instance)
            except ValidationError as e:
                raise serializers.ValidationError({"password": e.messages})

        return password
Ejemplo n.º 2
0
    def test_does_not_throw_if_cannot_log_activity(self):
        with self.assertLogs(level="WARN") as log:
            try:
                log_activity(
                    organization_id=UUIDT(),
                    team_id=1,
                    # will cause logging to raise exception because user is unsaved
                    # avoids needing to mock anything to force the exception
                    user=User(first_name="testy", email="*****@*****.**"),
                    item_id="12345",
                    scope="testing throwing exceptions on create",
                    activity="does not explode",
                    detail=Detail(),
                )
            except Exception as e:
                raise pytest.fail(f"Should not have raised exception: {e}")

            logged_warning = log.records[0].__dict__
            self.assertEqual(logged_warning["levelname"], "WARNING")
            self.assertEqual(logged_warning["msg"]["event"], "failed to write activity log")
            self.assertEqual(logged_warning["msg"]["scope"], "testing throwing exceptions on create")
            self.assertEqual(logged_warning["msg"]["team"], 1)
            self.assertEqual(logged_warning["msg"]["activity"], "does not explode")
            self.assertIsInstance(logged_warning["msg"]["exception"], ValueError)
Ejemplo n.º 3
0
 def get_has_password(self, instance: User) -> bool:
     return instance.has_usable_password()