aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests/test_privileges.py
diff options
context:
space:
mode:
authortilly-Q <nattilypigeonfowl@gmail.com>2013-08-29 13:47:50 -0400
committertilly-Q <nattilypigeonfowl@gmail.com>2013-08-29 13:47:50 -0400
commitdfd66b789cd6cc9470c2a98bcbda9ee5e0f3ad0f (patch)
tree799396874b47b86da32a8deb613164c034adcd6c /mediagoblin/tests/test_privileges.py
parente46fb71c1d7067253f30cb7212c676b066a61432 (diff)
downloadmediagoblin-dfd66b789cd6cc9470c2a98bcbda9ee5e0f3ad0f.tar.lz
mediagoblin-dfd66b789cd6cc9470c2a98bcbda9ee5e0f3ad0f.tar.xz
mediagoblin-dfd66b789cd6cc9470c2a98bcbda9ee5e0f3ad0f.zip
This was a big commit! I included lots of documentation below, but generally I
did a few things. I wrote many many many new tests, either in old test files or in the three new test files I made. I also did some code-keeping work, deleting trailing whitespace and deleting vestigial code. Lastly, I fixed the parts of the code which I realized were broken thru the process of running tests. =============================================================================== Deleted trailing whitespace: =============================================================================== --\ mediagoblin/decorators.py --\ mediagoblin/auth/tools.py --\ mediagoblin/db/migrations.py --\ mediagoblin/db/models.py --\ mediagoblin/gmg_commands/users.py --\ mediagoblin/moderation/forms.py --\ mediagoblin/moderation/tools.py --\ mediagoblin/moderation/views.py --\ mediagoblin/templates/mediagoblin/moderation/media_panel.html --\ mediagoblin/templates/mediagoblin/moderation/report.html --\ mediagoblin/templates/mediagoblin/moderation/report_panel.html --\ mediagoblin/templates/mediagoblin/moderation/user.html --\ mediagoblin/templates/mediagoblin/moderation/user_panel.html --\ mediagoblin/templates/mediagoblin/user_pages/report.html --\ mediagoblin/templates/mediagoblin/utils/report.html --\ mediagoblin/user_pages/lib.py --\ mediagoblin/user_pages/views.py =============================================================================== Deleted Vestigial Code =============================================================================== --\ mediagoblin/db/util.py --\ mediagoblin/tests/test_notifications.py =============================================================================== Modified the Code: =============================================================================== --\ mediagoblin/moderation/tools.py --| Encapsulated the code around giving/taking away privileges into two | funtions. --\ mediagoblin/moderation/views.py --| Imported and used the give/take away privilege functions --| Replaced 'require_admin_or_moderator_login' with |'user_has_privilege(u"admin")' for adding/taking away privileges, only | admins are allowed to do this. --\ mediagoblin/templates/mediagoblin/banned.html --| Added relevant translation tags --| Added ability to display indefinite banning --\ mediagoblin/templates/mediagoblin/user_pages/media.html --| Made sure the add comments button was only visible for users with the | `commenter` privilege --\ mediagoblin/tests/test_submission.py --| Paroneayea fixed a DetachedInstanceError I was having with the our_user | function --\ mediagoblin/tests/tools.py --| Added a fixture_add_comment_report function for testing. --\ mediagoblin/tools/response.py --| Fixed a minor error where a necessary return statement was missing --| Fit the code within 80 columns --\ mediagoblin/user_pages/views.py --| Added a necessary decorator to ensure that only users with the 'commenter' | privilege can post comments =============================================================================== Wrote new tests for an old test file: =============================================================================== --\ mediagoblin/tests/test_auth.py --| Added a new test to make sure privilege granting on registration happens | correctly --\ mediagoblin/tests/test_modelmethods.py* --| Added a test to ensure the User method has_privilege works properly =============================================================================== Wrote entirely new files full of tests: =============================================================================== --\ mediagoblin/tests/test_moderation.py --\ mediagoblin/tests/test_privileges.py --\ mediagoblin/tests/test_reporting.py =============================================================================== =============================================================================== NOTE: Any files I've marked with a * in this commit report, were actually subm- itted in my last commit. I made that committ to fix an error I was having, so they weren't properly documented in that report. =============================================================================== ===============================================================================
Diffstat (limited to 'mediagoblin/tests/test_privileges.py')
-rw-r--r--mediagoblin/tests/test_privileges.py206
1 files changed, 206 insertions, 0 deletions
diff --git a/mediagoblin/tests/test_privileges.py b/mediagoblin/tests/test_privileges.py
new file mode 100644
index 00000000..ced87b7f
--- /dev/null
+++ b/mediagoblin/tests/test_privileges.py
@@ -0,0 +1,206 @@
+# GNU MediaGoblin -- federated, autonomous media hosting
+# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+import pytest
+from datetime import datetime, timedelta
+from webtest import AppError
+
+from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry
+
+from mediagoblin.db.models import User, Privilege, UserBan
+from mediagoblin.db.base import Session
+from mediagoblin.tools import template
+
+from .resources import GOOD_JPG
+
+class TestPrivilegeFunctionality:
+
+ @pytest.fixture(autouse=True)
+ def _setup(self, test_app):
+ self.test_app = test_app
+
+ fixture_add_user(u'alex',
+ privileges=[u'admin',u'active'])
+ fixture_add_user(u'raven',
+ privileges=[u'moderator',u'active',u'reporter'])
+ fixture_add_user(u'natalie',
+ privileges=[u'active'])
+ self.query_for_users()
+
+ def login(self, username):
+ self.test_app.post(
+ '/auth/login/', {
+ 'username': username,
+ 'password': 'toast'})
+ self.query_for_users()
+
+ def logout(self):
+ self.test_app.get('/auth/logout/')
+ self.query_for_users()
+
+ def do_post(self, data, *context_keys, **kwargs):
+ url = kwargs.pop('url', '/submit/')
+ do_follow = kwargs.pop('do_follow', False)
+ template.clear_test_template_context()
+ response = self.test_app.post(url, data, **kwargs)
+ if do_follow:
+ response.follow()
+ context_data = template.TEMPLATE_TEST_CONTEXT
+ for key in context_keys:
+ context_data = context_data[key]
+ return response, context_data
+
+ def query_for_users(self):
+ self.admin_user = User.query.filter(User.username==u'alex').first()
+ self.mod_user = User.query.filter(User.username==u'raven').first()
+ self.user = User.query.filter(User.username==u'natalie').first()
+
+ def testUserBanned(self):
+ self.login(u'natalie')
+ uid = self.user.id
+ # First, test what happens when a user is banned indefinitely
+ #----------------------------------------------------------------------
+ user_ban = UserBan(user_id=uid,
+ reason=u'Testing whether user is banned',
+ expiration_date=None)
+ user_ban.save()
+
+ response = self.test_app.get('/')
+ assert response.status == "200 OK"
+ assert "You are Banned" in response.body
+ # Then test what happens when that ban has an expiration date which
+ # hasn't happened yet
+ #----------------------------------------------------------------------
+ user_ban = UserBan.query.get(uid)
+ user_ban.delete()
+ user_ban = UserBan(user_id=uid,
+ reason=u'Testing whether user is banned',
+ expiration_date= datetime.now() + timedelta(days=20))
+ user_ban.save()
+
+ response = self.test_app.get('/')
+ assert response.status == "200 OK"
+ assert "You are Banned" in response.body
+
+ # Then test what happens when that ban has an expiration date which
+ # has already happened
+ #----------------------------------------------------------------------
+ user_ban = UserBan.query.get(uid)
+ user_ban.delete()
+ exp_date = datetime.now() - timedelta(days=20)
+ user_ban = UserBan(user_id=uid,
+ reason=u'Testing whether user is banned',
+ expiration_date= exp_date)
+ user_ban.save()
+
+ response = self.test_app.get('/')
+ assert response.status == "302 FOUND"
+ assert not "You are Banned" in response.body
+
+ def testVariousPrivileges(self):
+ # The various actions that require privileges (ex. reporting,
+ # commenting, moderating...) are tested in other tests. This method
+ # will be used to ensure that those actions are impossible for someone
+ # without the proper privileges.
+ # For other tests that show what happens when a user has the proper
+ # privileges, check out:
+ # tests/test_moderation.py moderator
+ # tests/test_notifications.py commenter
+ # tests/test_reporting.py reporter
+ # tests/test_submission.py uploader
+ #----------------------------------------------------------------------
+ self.login(u'natalie')
+
+ # First test the get and post requests of submission/uploading
+ #----------------------------------------------------------------------
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get('/submit/')
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.do_post({'upload_files':[('file',GOOD_JPG)],
+ 'title':u'Normal Upload 1'},
+ url='/submit/')
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ # Test that a user cannot comment without the commenter privilege
+ #----------------------------------------------------------------------
+ self.query_for_users()
+
+ media_entry = fixture_media_entry(uploader=self.admin_user.id,
+ state=u'processed')
+
+ media_entry_id = media_entry.id
+ media_uri_id = '/u/{0}/m/{1}/'.format(self.admin_user.username,
+ media_entry.id)
+ media_uri_slug = '/u/{0}/m/{1}/'.format(self.admin_user.username,
+ media_entry.slug)
+ response = self.test_app.get(media_uri_slug)
+ assert not "Add a comment" in response.body
+
+ self.query_for_users()
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.post(
+ media_uri_id + 'comment/add/',
+ {'comment_content': u'Test comment #42'})
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ # Test that a user cannot report without the reporter privilege
+ #----------------------------------------------------------------------
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get(media_uri_slug+"report/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.do_post(
+ {'report_reason':u'Testing Reports #1',
+ 'reporter_id':u'3'},
+ url=(media_uri_slug+"report/"))
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ # Test that a user cannot access the moderation pages w/o moderator
+ # or admin privileges
+ #----------------------------------------------------------------------
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get("/mod/users/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get("/mod/reports/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get("/mod/media/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get("/mod/users/1/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ with pytest.raises(AppError) as excinfo:
+ response = self.test_app.get("/mod/reports/1/")
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+
+ self.query_for_users()
+
+ with pytest.raises(AppError) as excinfo:
+ response, context = self.do_post({'action_to_resolve':[u'takeaway'],
+ 'take_away_privileges':[u'active'],
+ 'targeted_user':self.admin_user.id},
+ url='/mod/reports/1/')
+ self.query_for_users()
+ assert 'Bad response: 403 FORBIDDEN' in str(excinfo)