Beispiel #1
0
    def test_delete_user_remove_audit_trail(self):
        """audit trail is deleted together with user it belongs to"""
        user = self.get_authenticated_user()
        create_user_audit_trail(user, USER_IP, self.obj)
        self.assertEqual(AuditTrail.objects.count(), 1)

        user.delete()
        self.assertEqual(AuditTrail.objects.count(), 0)
Beispiel #2
0
    def test_create_user_audit_trail(self):
        """create_user_audit_trail creates new db record"""
        user = self.get_authenticated_user()
        create_user_audit_trail(user, USER_IP, self.obj)
        self.assertEqual(AuditTrail.objects.count(), 1)

        audit_trail = user.audittrail_set.all()[0]
        self.assertEqual(audit_trail.user, user)
        self.assertEqual(audit_trail.ip_address, USER_IP)
        self.assertEqual(audit_trail.content_object, self.obj)
Beispiel #3
0
    def test_delete_audit_trail(self):
        """audit trail deletion leaves other data untouched"""
        user = self.get_authenticated_user()
        create_user_audit_trail(user, USER_IP, self.obj)
        self.assertEqual(AuditTrail.objects.count(), 1)

        audit_trail = user.audittrail_set.all()[0]
        audit_trail.delete()

        UserModel.objects.get(id=user.id)
        UserModel.objects.get(id=self.obj.id)
Beispiel #4
0
    def test_delete_obj_keep_audit_trail(self):
        """audit trail is kept after with obj it points at is deleted"""
        user = self.get_authenticated_user()
        create_user_audit_trail(user, USER_IP, self.obj)
        self.assertEqual(AuditTrail.objects.count(), 1)

        self.obj.delete()
        self.assertEqual(AuditTrail.objects.count(), 1)

        audit_trail = user.audittrail_set.all()[0]
        self.assertEqual(audit_trail.user, user)
        self.assertEqual(audit_trail.ip_address, USER_IP)
        self.assertIsNone(audit_trail.content_object)
Beispiel #5
0
    def test_recent_audit_trail_is_kept(self):
        """remove_old_ips keeps recent audit trails"""
        user = self.get_authenticated_user()
        audit_trail = create_user_audit_trail(user, USER_IP, self.obj)

        remove_old_ips.send(None)

        self.assertEqual(user.audittrail_set.count(), 1)
Beispiel #6
0
    def test_old_audit_trail_is_removed(self):
        """remove_old_ips removes old audit trails"""
        user = self.get_authenticated_user()
        audit_trail = create_user_audit_trail(user, USER_IP, self.obj)

        audit_trail.created_on = timezone.now() - timedelta(days=50)
        audit_trail.save()

        remove_old_ips.send(None)

        self.assertEqual(user.audittrail_set.count(), 0)
Beispiel #7
0
 def test_create_user_audit_trail_anonymous_user(self):
     """create_user_audit_trail doesn't record anonymous users"""
     user = self.get_anonymous_user()
     create_user_audit_trail(user, USER_IP, self.obj)
     self.assertEqual(AuditTrail.objects.count(), 0)
Beispiel #8
0
 def test_create_user_audit_require_model(self):
     """create_user_audit_trail requires model instance"""
     anonymous_user = self.get_anonymous_user()
     with self.assertRaises(ValueError):
         create_user_audit_trail(anonymous_user, USER_IP, anonymous_user)
     self.assertEqual(AuditTrail.objects.count(), 0)
    def create_user(
            self, username, email, password=None, create_audit_trail=False,
            joined_from_ip=None, set_default_avatar=False, **extra_fields):
        from misago.users.validators import validate_email, validate_username

        email = self.normalize_email(email)
        username = self.model.normalize_username(username)

        if not email:
            raise ValueError(_("User must have an email address."))

        WATCH_DICT = {
            'no': self.model.SUBSCRIBE_NONE,
            'watch': self.model.SUBSCRIBE_NOTIFY,
            'watch_email': self.model.SUBSCRIBE_ALL,
        }

        if not 'subscribe_to_started_threads' in extra_fields:
            new_value = WATCH_DICT[settings.subscribe_start]
            extra_fields['subscribe_to_started_threads'] = new_value

        if not 'subscribe_to_replied_threads' in extra_fields:
            new_value = WATCH_DICT[settings.subscribe_reply]
            extra_fields['subscribe_to_replied_threads'] = new_value

        extra_fields.update({'is_staff': False, 'is_superuser': False})

        now = timezone.now()
        user = self.model(
            last_login=now, 
            joined_on=now, 
            joined_from_ip=joined_from_ip,
            **extra_fields
        )

        user.set_username(username)
        user.set_email(email)
        user.set_password(password)

        validate_username(username)
        validate_email(email)

        if not 'rank' in extra_fields:
            user.rank = Rank.objects.get_default()

        user.save(using=self._db)

        if set_default_avatar:
            avatars.set_default_avatar(
                user, settings.default_avatar, settings.default_gravatar_fallback
            )
        else:
            # just for test purposes
            user.avatars = [{'size': 400, 'url': 'http://placekitten.com/400/400'}]

        authenticated_role = Role.objects.get(special_role='authenticated')
        if authenticated_role not in user.roles.all():
            user.roles.add(authenticated_role)
        user.update_acl_key()

        user.save(update_fields=['avatars', 'acl_key'])

        if create_audit_trail:
            create_user_audit_trail(user, user.joined_from_ip, user)

        # populate online tracker with default value
        Online.objects.create(user=user, last_click=now)

        return user
    def test_prepare_download_with_audit_trail(self):
        """function creates data download for user with audit trail"""
        create_user_audit_trail(self.user, '127.0.0.1', self.user)

        self.assert_download_is_valid()