def apply_actions(self, user, options): if options.get('admin'): user.is_superuser = True elif options.get('noadmin'): user.is_superuser = False if options.get('reject'): reject_reason = options.get('reject_reason', None) res = user_action.reject(user, reject_reason) if res.is_error(): self.stderr.write("Failed to reject: %s\n" % res.message) else: self.stderr.write("Account rejected\n") if options.get('verify'): res = user_action.verify(user, user.verification_code) if res.is_error(): self.stderr.write("Failed to verify: %s\n" % res.message) else: self.stderr.write("Account verified (%s)\n" % res.status_display()) if options.get('accept'): res = user_action.accept(user) if res.is_error(): self.stderr.write("Failed to accept: %s\n" % res.message) else: self.stderr.write("Account accepted and activated\n") if options.get('active'): res = user_action.activate(user) if res.is_error(): self.stderr.write("Failed to activate: %s\n" % res.message) else: self.stderr.write("Account %s activated\n" % user.username) elif options.get('inactive'): inactive_reason = options.get('inactive_reason', None) res = user_action.deactivate(user, inactive_reason) if res.is_error(): self.stderr.write("Failed to deactivate: %s\n" % res.message) else: self.stderr.write("Account %s deactivated\n" % user.username) invitations = options.get('invitations') if invitations is not None: user.invitations = int(invitations) groupname = options.get('add-group') if groupname is not None: try: group = Group.objects.get(name=groupname) user.groups.add(group) except Group.DoesNotExist, e: self.stderr.write( "Group named %s does not exist\n" % groupname)
def apply_actions(self, user, options): if options.get('admin'): user.is_superuser = True elif options.get('noadmin'): user.is_superuser = False if options.get('reject'): reject_reason = options.get('reject_reason', None) res = user_action.reject(user, reject_reason) if res.is_error(): self.stderr.write("Failed to reject: %s\n" % res.message) else: self.stderr.write("Account rejected\n") if options.get('verify'): res = user_action.verify(user, user.verification_code) if res.is_error(): self.stderr.write("Failed to verify: %s\n" % res.message) else: self.stderr.write("Account verified (%s)\n" % res.status_display()) if options.get('accept'): res = user_action.accept(user) if res.is_error(): self.stderr.write("Failed to accept: %s\n" % res.message) else: self.stderr.write("Account accepted and activated\n") if options.get('active'): res = user_action.activate(user) if res.is_error(): self.stderr.write("Failed to activate: %s\n" % res.message) else: self.stderr.write("Account %s activated\n" % user.username) elif options.get('inactive'): inactive_reason = options.get('inactive_reason', None) res = user_action.deactivate(user, inactive_reason) if res.is_error(): self.stderr.write("Failed to deactivate: %s\n" % res.message) else: self.stderr.write("Account %s deactivated\n" % user.username) invitations = options.get('invitations') if invitations is not None: user.invitations = int(invitations) groupname = options.get('add-group') if groupname is not None: try: group = Group.objects.get(name=groupname) user.groups.add(group) except Group.DoesNotExist, e: self.stderr.write("Group named %s does not exist\n" % groupname)
def test_rejection(self): """Test if rejections are handled properly.""" # Verify the user first. res = verify(self.user1, self.user1.verification_code) self.assertFalse(res.is_error()) # Check rejection. ok, _ = validate_user_action(self.user1, "REJECT") self.assertTrue(ok) res = reject(self.user1, reason="Because") self.assertFalse(res.is_error()) self.assertEqual(len(mail.outbox), 0) # Check if reason has been registered. self.assertEqual(self.user1.rejected_reason, "Because") # We cannot reject twice. ok, _ = validate_user_action(self.user1, "REJECT") self.assertFalse(ok) res = reject(self.user1, reason="Because") self.assertTrue(res.is_error()) self.assertEqual(len(mail.outbox), 0) # We cannot deactivate a rejected user. ok, _ = validate_user_action(self.user1, "DEACTIVATE") self.assertFalse(ok) res = deactivate(self.user1) self.assertTrue(res.is_error()) # We can, however, accept a rejected user. ok, msg = validate_user_action(self.user1, "ACCEPT") self.assertTrue(ok) # Test if accept action works on rejected users. res = accept(self.user1) self.assertFalse(res.is_error()) self.assertEqual(len(mail.outbox), 1)
def test_accept(self): """Test acceptance logic.""" # Verify the user first. res = verify(self.user1, self.user1.verification_code) self.assertFalse(res.is_error()) # Test if check function works properly for unmoderated user. ok, _ = validate_user_action(self.user1, "ACCEPT") self.assertTrue(ok) # Test if accept action works properly for unmoderated user. res = accept(self.user1) self.assertFalse(res.is_error()) self.assertEqual(len(mail.outbox), 1) # Test if check function fails properly for moderated user. ok, _ = validate_user_action(self.user1, "ACCEPT") self.assertFalse(ok) # Test if accept action fails properly for moderated user. res = accept(self.user1) self.assertTrue(res.is_error()) self.assertEqual(len(mail.outbox), 1) # Test if the rest of the actions can apply on a moderated user. # User cannot be rejected. ok, _ = validate_user_action(self.user1, "REJECT") self.assertFalse(ok) res = reject(self.user1, 'Too late') self.assertTrue(res.is_error()) # User cannot be reactivated. ok, _ = validate_user_action(self.user1, "ACTIVATE") self.assertFalse(ok) res = activate(self.user1) self.assertTrue(res.is_error())