aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/tests
diff options
context:
space:
mode:
Diffstat (limited to 'mediagoblin/tests')
-rw-r--r--mediagoblin/tests/test_api.py4
-rw-r--r--mediagoblin/tests/test_auth.py34
-rw-r--r--mediagoblin/tests/test_modelmethods.py43
-rw-r--r--mediagoblin/tests/test_oauth2.py3
-rw-r--r--mediagoblin/tests/tools.py13
5 files changed, 70 insertions, 27 deletions
diff --git a/mediagoblin/tests/test_api.py b/mediagoblin/tests/test_api.py
index 89cf1026..eb9c0fd4 100644
--- a/mediagoblin/tests/test_api.py
+++ b/mediagoblin/tests/test_api.py
@@ -25,6 +25,7 @@ from mediagoblin.tools import template, pluginapi
from mediagoblin.tests.tools import fixture_add_user
from .resources import GOOD_JPG, GOOD_PNG, EVIL_FILE, EVIL_JPG, EVIL_PNG, \
BIG_BLUE
+from mediagoblin.db.models import Privilege
_log = logging.getLogger(__name__)
@@ -35,7 +36,8 @@ class TestAPI(object):
self.db = mg_globals.database
self.user_password = u'4cc355_70k3N'
- self.user = fixture_add_user(u'joapi', self.user_password)
+ self.user = fixture_add_user(u'joapi', self.user_password,
+ privileges=[u'active',u'uploader'])
def login(self, test_app):
test_app.post(
diff --git a/mediagoblin/tests/test_auth.py b/mediagoblin/tests/test_auth.py
index 61503d32..7d7748ac 100644
--- a/mediagoblin/tests/test_auth.py
+++ b/mediagoblin/tests/test_auth.py
@@ -83,18 +83,18 @@ def test_register_views(test_app):
template.clear_test_template_context()
response = test_app.post(
'/auth/register/', {
- 'username': u'happygirl',
- 'password': 'iamsohappy',
- 'email': 'happygrrl@example.org'})
+ 'username': u'angrygirl',
+ 'password': 'iamsoangry',
+ 'email': 'angrygrrl@example.org'})
response.follow()
## Did we redirect to the proper page? Use the right template?
- assert urlparse.urlsplit(response.location)[2] == '/u/happygirl/'
+ assert urlparse.urlsplit(response.location)[2] == '/u/angrygirl/'
assert 'mediagoblin/user_pages/user.html' in template.TEMPLATE_TEST_CONTEXT
## Make sure user is in place
new_user = mg_globals.database.User.query.filter_by(
- username=u'happygirl').first()
+ username=u'angrygirl').first()
assert new_user
assert new_user.status == u'needs_email_verification'
assert new_user.email_verified == False
@@ -107,7 +107,7 @@ def test_register_views(test_app):
## Make sure we get email confirmation, and try verifying
assert len(mail.EMAIL_TEST_INBOX) == 1
message = mail.EMAIL_TEST_INBOX.pop()
- assert message['To'] == 'happygrrl@example.org'
+ assert message['To'] == 'angrygrrl@example.org'
email_context = template.TEMPLATE_TEST_CONTEXT[
'mediagoblin/auth/verification_email.txt']
assert email_context['verification_url'] in message.get_payload(decode=True)
@@ -129,7 +129,7 @@ def test_register_views(test_app):
# assert context['verification_successful'] == True
# TODO: Would be good to test messages here when we can do so...
new_user = mg_globals.database.User.query.filter_by(
- username=u'happygirl').first()
+ username=u'angrygirl').first()
assert new_user
assert new_user.status == u'needs_email_verification'
assert new_user.email_verified == False
@@ -143,7 +143,7 @@ def test_register_views(test_app):
# assert context['verification_successful'] == True
# TODO: Would be good to test messages here when we can do so...
new_user = mg_globals.database.User.query.filter_by(
- username=u'happygirl').first()
+ username=u'angrygirl').first()
assert new_user
assert new_user.status == u'active'
assert new_user.email_verified == True
@@ -154,9 +154,9 @@ def test_register_views(test_app):
template.clear_test_template_context()
response = test_app.post(
'/auth/register/', {
- 'username': u'happygirl',
- 'password': 'iamsohappy2',
- 'email': 'happygrrl2@example.org'})
+ 'username': u'angrygirl',
+ 'password': 'iamsoangry2',
+ 'email': 'angrygrrl2@example.org'})
context = template.TEMPLATE_TEST_CONTEXT[
'mediagoblin/auth/register.html']
@@ -171,7 +171,7 @@ def test_register_views(test_app):
template.clear_test_template_context()
response = test_app.post(
'/auth/forgot_password/',
- {'username': u'happygirl'})
+ {'username': u'angrygirl'})
response.follow()
## Did we redirect to the proper page? Use the right template?
@@ -181,7 +181,7 @@ def test_register_views(test_app):
## Make sure link to change password is sent by email
assert len(mail.EMAIL_TEST_INBOX) == 1
message = mail.EMAIL_TEST_INBOX.pop()
- assert message['To'] == 'happygrrl@example.org'
+ assert message['To'] == 'angrygrrl@example.org'
email_context = template.TEMPLATE_TEST_CONTEXT[
'mediagoblin/auth/fp_verification_email.txt']
#TODO - change the name of verification_url to something forgot-password-ish
@@ -210,7 +210,7 @@ def test_register_views(test_app):
template.clear_test_template_context()
response = test_app.post(
'/auth/forgot_password/verify/', {
- 'password': 'iamveryveryhappy',
+ 'password': 'iamveryveryangry',
'token': parsed_get_params['token']})
response.follow()
assert 'mediagoblin/auth/login.html' in template.TEMPLATE_TEST_CONTEXT
@@ -219,8 +219,8 @@ def test_register_views(test_app):
template.clear_test_template_context()
response = test_app.post(
'/auth/login/', {
- 'username': u'happygirl',
- 'password': 'iamveryveryhappy'})
+ 'username': u'angrygirl',
+ 'password': 'iamveryveryangry'})
# User should be redirected
response.follow()
@@ -233,7 +233,7 @@ def test_authentication_views(test_app):
Test logging in and logging out
"""
# Make a new user
- test_user = fixture_add_user(active_user=False)
+ test_user = fixture_add_user()
# Get login
diff --git a/mediagoblin/tests/test_modelmethods.py b/mediagoblin/tests/test_modelmethods.py
index 427aa47c..77d375b7 100644
--- a/mediagoblin/tests/test_modelmethods.py
+++ b/mediagoblin/tests/test_modelmethods.py
@@ -18,7 +18,7 @@
# methods, and so it makes sense to test them here.
from mediagoblin.db.base import Session
-from mediagoblin.db.models import MediaEntry
+from mediagoblin.db.models import MediaEntry, User, Privilege
from mediagoblin.tests.tools import fixture_add_user
@@ -151,6 +151,46 @@ class TestMediaEntrySlugs(object):
qbert_entry.generate_slug()
assert qbert_entry.slug is None
+class TestUserHasPrivilege:
+ def _setup(self):
+ self.natalie_user = fixture_add_user(u'natalie')
+ self.aeva_user = fixture_add_user(u'aeva')
+ self.natalie_user.all_privileges += [
+ Privilege.query.filter(
+ Privilege.privilege_name == u'admin').one(),
+ Privilege.query.filter(
+ Privilege.privilege_name == u'moderator').one()]
+ self.aeva_user.all_privileges += [
+ Privilege.query.filter(
+ Privilege.privilege_name == u'moderator').one()]
+
+ def test_privilege_added_correctly(self, test_app):
+ self._setup()
+ admin = Privilege.query.filter(
+ Privilege.privilege_name == u'admin').one()
+ # first make sure the privileges were added successfully
+
+ assert admin in self.natalie_user.all_privileges
+ assert admin not in self.aeva_user.all_privileges
+
+ def test_user_has_privilege_one(self, test_app):
+ self._setup()
+
+ # then test out the user.has_privilege method for one privilege
+ assert not natalie_user.has_privilege(u'commenter')
+ assert aeva_user.has_privilege(u'active')
+
+
+ def test_user_has_privileges_multiple(self, test_app):
+ self._setup()
+
+ # when multiple args are passed to has_privilege, the method returns
+ # True if the user has ANY of the privileges
+ assert natalie_user.has_privilege(u'admin',u'commenter')
+ assert aeva_user.has_privilege(u'moderator',u'active')
+ assert not natalie_user.has_privilege(u'commenter',u'uploader')
+
+
def test_media_data_init(test_app):
Session.rollback()
@@ -165,3 +205,4 @@ def test_media_data_init(test_app):
obj_in_session += 1
print repr(obj)
assert obj_in_session == 0
+
diff --git a/mediagoblin/tests/test_oauth2.py b/mediagoblin/tests/test_oauth2.py
index 86f9e8cc..957f4e65 100644
--- a/mediagoblin/tests/test_oauth2.py
+++ b/mediagoblin/tests/test_oauth2.py
@@ -38,7 +38,8 @@ class TestOAuth(object):
self.pman = pluginapi.PluginManager()
self.user_password = u'4cc355_70k3N'
- self.user = fixture_add_user(u'joauth', self.user_password)
+ self.user = fixture_add_user(u'joauth', self.user_password,
+ privileges=[u'active'])
self.login()
diff --git a/mediagoblin/tests/tools.py b/mediagoblin/tests/tools.py
index 98361adc..ec17d791 100644
--- a/mediagoblin/tests/tools.py
+++ b/mediagoblin/tests/tools.py
@@ -25,7 +25,7 @@ from webtest import TestApp
from mediagoblin import mg_globals
from mediagoblin.db.models import User, MediaEntry, Collection, MediaComment, \
- CommentSubscription, CommentNotification
+ CommentSubscription, CommentNotification, Privilege
from mediagoblin.tools import testing
from mediagoblin.init.config import read_mediagoblin_config
from mediagoblin.db.base import Session
@@ -170,7 +170,7 @@ def assert_db_meets_expected(db, expected):
def fixture_add_user(username=u'chris', password=u'toast',
- active_user=True, wants_comment_notification=True):
+ privileges=[], wants_comment_notification=True):
# Reuse existing user or create a new one
test_user = User.query.filter_by(username=username).first()
if test_user is None:
@@ -179,14 +179,13 @@ def fixture_add_user(username=u'chris', password=u'toast',
test_user.email = username + u'@example.com'
if password is not None:
test_user.pw_hash = gen_password_hash(password)
- if active_user:
- test_user.email_verified = True
- test_user.status = u'active'
-
test_user.wants_comment_notification = wants_comment_notification
+ for privilege in privileges:
+ query = Privilege.query.filter(Privilege.privilege_name==privilege)
+ if query.count():
+ test_user.all_privileges.append(query.one())
test_user.save()
-
# Reload
test_user = User.query.filter_by(username=username).first()