Fix #5415 - Deleted comments get removed properly when tombstones
[mediagoblin.git] / mediagoblin / tests / test_privileges.py
index ced87b7f59745815c2136feee83fa6a349e2532a..2e0b73477f3d54d30ea7b03346c8f062b825458b 100644 (file)
 # 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 six
 import pytest
-from datetime import datetime, timedelta
+from datetime import date, 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.db.models import User, LocalUser, UserBan
 from mediagoblin.tools import template
 
 from .resources import GOOD_JPG
@@ -34,7 +34,7 @@ class TestPrivilegeFunctionality:
 
         fixture_add_user(u'alex',
             privileges=[u'admin',u'active'])
-        fixture_add_user(u'raven',
+        fixture_add_user(u'meow',
             privileges=[u'moderator',u'active',u'reporter'])
         fixture_add_user(u'natalie',
             privileges=[u'active'])
@@ -64,9 +64,9 @@ class TestPrivilegeFunctionality:
         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()
+        self.admin_user = LocalUser.query.filter(LocalUser.username==u'alex').first()
+        self.mod_user = LocalUser.query.filter(LocalUser.username==u'meow').first()
+        self.user = LocalUser.query.filter(LocalUser.username==u'natalie').first()
 
     def testUserBanned(self):
         self.login(u'natalie')
@@ -80,7 +80,7 @@ class TestPrivilegeFunctionality:
 
         response = self.test_app.get('/')
         assert response.status == "200 OK"
-        assert "You are Banned" in response.body
+        assert b"You are Banned" in response.body
         # Then test what happens when that ban has an expiration date which
         # hasn't happened yet
         #----------------------------------------------------------------------
@@ -88,19 +88,19 @@ class TestPrivilegeFunctionality:
         user_ban.delete()
         user_ban = UserBan(user_id=uid,
             reason=u'Testing whether user is banned',
-            expiration_date= datetime.now() + timedelta(days=20))
+            expiration_date= date.today() + timedelta(days=20))
         user_ban.save()
 
         response = self.test_app.get('/')
         assert response.status == "200 OK"
-        assert "You are Banned" in response.body
+        assert b"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)
+        exp_date = date.today() - timedelta(days=20)
         user_ban = UserBan(user_id=uid,
             reason=u'Testing whether user is banned',
             expiration_date= exp_date)
@@ -108,7 +108,7 @@ class TestPrivilegeFunctionality:
 
         response = self.test_app.get('/')
         assert response.status == "302 FOUND"
-        assert not "You are Banned" in response.body
+        assert not b"You are Banned" in response.body
 
     def testVariousPrivileges(self):
         # The various actions that require privileges (ex. reporting,
@@ -128,14 +128,16 @@ class TestPrivilegeFunctionality:
         #----------------------------------------------------------------------
         with pytest.raises(AppError) as excinfo:
             response = self.test_app.get('/submit/')
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in 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)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         # Test that a user cannot comment without the commenter privilege
         #----------------------------------------------------------------------
@@ -150,50 +152,58 @@ class TestPrivilegeFunctionality:
         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
+        assert not b"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)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in 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)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in 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)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in 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)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         with pytest.raises(AppError) as excinfo:
             response = self.test_app.get("/mod/reports/")
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         with pytest.raises(AppError) as excinfo:
             response = self.test_app.get("/mod/media/")
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         with pytest.raises(AppError) as excinfo:
             response = self.test_app.get("/mod/users/1/")
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         with pytest.raises(AppError) as excinfo:
             response = self.test_app.get("/mod/reports/1/")
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo
 
         self.query_for_users()
 
@@ -203,4 +213,5 @@ class TestPrivilegeFunctionality:
                 'targeted_user':self.admin_user.id},
                 url='/mod/reports/1/')
             self.query_for_users()
-        assert 'Bad response: 403 FORBIDDEN' in str(excinfo)
+        excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii')
+        assert b'Bad response: 403 FORBIDDEN' in excinfo