def test_media_entry_change_and_delete(test_app): """ Test that media entry additions/modification/deletes automatically show up in the index. """ media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='processed') media_b = fixture_media_entry(title='mediaB', save=False, expunge=False, fake_upload=False, state='processed') media_a.description = 'DescriptionA' media_b.description = 'DescriptionB' Session.add(media_a) Session.add(media_b) Session.commit() # Check that the media entries are in the index engine = get_engine() assert engine.search('mediaA') == [media_a.id] assert engine.search('mediaB') == [media_b.id] # Modify one, and delete the other media_a.title = 'new' media_b.delete() # Check that the changes are present in the index assert engine.search('new') == [media_a.id] assert engine.search('mediaA') == [] assert engine.search('mediaB') == []
def test_user_deletes_other_comments(test_app): user_a = fixture_add_user(u"chris_a") user_b = fixture_add_user(u"chris_b") media_a = fixture_media_entry(uploader=user_a.id, save=False, expunge=False, fake_upload=False) media_b = fixture_media_entry(uploader=user_b.id, save=False, expunge=False, fake_upload=False) Session.add(media_a) Session.add(media_b) Session.flush() # Create all 4 possible comments: for u in (user_a, user_b): for m in (media_a, media_b): cmt = TextComment() cmt.actor = u.id cmt.content = u"Some Comment" Session.add(cmt) # think i need this to get the command ID Session.flush() link = Comment() link.target = m link.comment = cmt Session.add(link) Session.flush() usr_cnt1 = User.query.count() med_cnt1 = MediaEntry.query.count() cmt_cnt1 = Comment.query.count() User.query.get(user_a.id).delete(commit=False) usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = Comment.query.count() # One user deleted assert usr_cnt2 == usr_cnt1 - 1 # One media gone assert med_cnt2 == med_cnt1 - 1 # Three of four comments gone. assert cmt_cnt2 == cmt_cnt1 - 3 User.query.get(user_b.id).delete() usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = Comment.query.count() # All users gone assert usr_cnt2 == usr_cnt1 - 2 # All media gone assert med_cnt2 == med_cnt1 - 2 # All comments gone assert cmt_cnt2 == cmt_cnt1 - 4
def test_user_deletes_other_comments(test_app): user_a = fixture_add_user(u"chris_a") user_b = fixture_add_user(u"chris_b") media_a = fixture_media_entry(uploader=user_a.id, save=False, expunge=False, fake_upload=False) media_b = fixture_media_entry(uploader=user_b.id, save=False, expunge=False, fake_upload=False) Session.add(media_a) Session.add(media_b) Session.flush() # Create all 4 possible comments: for u_id in (user_a.id, user_b.id): for m_id in (media_a.id, media_b.id): cmt = MediaComment() cmt.media_entry = m_id cmt.author = u_id cmt.content = u"Some Comment" Session.add(cmt) Session.flush() usr_cnt1 = User.query.count() med_cnt1 = MediaEntry.query.count() cmt_cnt1 = MediaComment.query.count() User.query.get(user_a.id).delete(commit=False) usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = MediaComment.query.count() # One user deleted assert usr_cnt2 == usr_cnt1 - 1 # One media gone assert med_cnt2 == med_cnt1 - 1 # Three of four comments gone. assert cmt_cnt2 == cmt_cnt1 - 3 User.query.get(user_b.id).delete() usr_cnt2 = User.query.count() med_cnt2 = MediaEntry.query.count() cmt_cnt2 = MediaComment.query.count() # All users gone assert usr_cnt2 == usr_cnt1 - 2 # All media gone assert med_cnt2 == med_cnt1 - 2 # All comments gone assert cmt_cnt2 == cmt_cnt1 - 4
def testMediaReports(self): self.login(u'allie') allie_user, natalie_user = self.query_for_users() allie_id = allie_user.id media_entry = fixture_media_entry(uploader=natalie_user.id, state=u'processed') mid = media_entry.id media_uri_slug = '/u/{0}/m/{1}/'.format(natalie_user.username, media_entry.slug) response = self.test_app.get(media_uri_slug + "report/") assert response.status == "200 OK" response, context = self.do_post( {'report_reason':u'Testing Media Report', 'reporter_id':six.text_type(allie_id)},url= media_uri_slug + "report/") assert response.status == "302 FOUND" media_report = MediaReport.query.first() allie_user, natalie_user = self.query_for_users() assert media_report is not None assert media_report.report_content == u'Testing Media Report' assert media_report.reporter_id == allie_id assert media_report.reported_user_id == natalie_user.id assert media_report.created is not None assert media_report.discriminator == 'media_report'
def test_comments_removed_when_graveyarded(test_app): """ Checks comments which are tombstones are removed from collection """ user = fixture_add_user() media = fixture_media_entry( uploader=user.id, expunge=False, fake_upload=False ) # Add the TextComment comment = TextComment() comment.actor = user.id comment.content = u"This is a comment that will be deleted." comment.save() # Add a link for the comment link = Comment() link.target = media link.comment = comment link.save() # First double check it's there and all is well... assert Comment.query.filter_by(target_id=link.target_id).first() is not None # Now delete the comment. comment.delete() # Verify this also deleted the Comment link, ergo there is no comment left. assert Comment.query.filter_by(target_id=link.target_id).first() is None
def testCommentReports(self): self.login('allie') allie_user, natalie_user = self.query_for_users() allie_id = allie_user.id media_entry = fixture_media_entry(uploader=natalie_user.id, state='processed') mid = media_entry.id fixture_add_comment(media_entry=media_entry, author=natalie_user.id) comment = TextComment.query.first() comment_uri_slug = '/u/{}/m/{}/c/{}/'.format(natalie_user.username, media_entry.slug, comment.id) response = self.test_app.get(comment_uri_slug + "report/") assert response.status == "200 OK" response, context = self.do_post( { 'report_reason': 'Testing Comment Report', 'reporter_id': str(allie_id) }, url=comment_uri_slug + "report/") assert response.status == "302 FOUND" comment_report = Report.query.first() allie_user, natalie_user = self.query_for_users() assert comment_report is not None assert comment_report.report_content == 'Testing Comment Report' assert comment_report.reporter_id == allie_id assert comment_report.reported_user_id == natalie_user.id assert comment_report.created is not None
def testCommentReports(self): self.login(u'allie') allie_user, natalie_user = self.query_for_users() allie_id = allie_user.id media_entry = fixture_media_entry(uploader=natalie_user.id, state=u'processed') mid = media_entry.id fixture_add_comment(media_entry=mid, author=natalie_user.id) comment = MediaComment.query.first() comment_uri_slug = '/u/{0}/m/{1}/c/{2}/'.format(natalie_user.username, media_entry.slug, comment.id) response = self.test_app.get(comment_uri_slug + "report/") assert response.status == "200 OK" response, context = self.do_post({ 'report_reason':u'Testing Comment Report', 'reporter_id':unicode(allie_id)},url= comment_uri_slug + "report/") assert response.status == "302 FOUND" comment_report = CommentReport.query.first() allie_user, natalie_user = self.query_for_users() assert comment_report is not None assert comment_report.report_content == u'Testing Comment Report' assert comment_report.reporter_id == allie_id assert comment_report.reported_user_id == natalie_user.id assert comment_report.created is not None assert comment_report.discriminator == 'comment_report'
def test_comments_removed_when_graveyarded(test_app): """ Checks comments which are tombstones are removed from collection """ user = fixture_add_user() media = fixture_media_entry(uploader=user.id, expunge=False, fake_upload=False) # Add the TextComment comment = TextComment() comment.actor = user.id comment.content = u"This is a comment that will be deleted." comment.save() # Add a link for the comment link = Comment() link.target = media link.comment = comment link.save() # First double check it's there and all is well... assert Comment.query.filter_by( target_id=link.target_id).first() is not None # Now delete the comment. comment.delete() # Verify this also deleted the Comment link, ergo there is no comment left. assert Comment.query.filter_by(target_id=link.target_id).first() is None
def test_edit_metadata(self, test_app): media_entry = fixture_media_entry(uploader=self.user.id, state=u'processed') media_slug = "/u/{username}/m/{media_id}/metadata/".format( username=str(self.user.username), media_id=str(media_entry.id)) self.login(test_app) response = test_app.get(media_slug) assert response.status == '200 OK' assert media_entry.media_metadata == {} # First test adding in metadata ################################ response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title", "media_metadata-1-identifier": "dc:creator", "media_metadata-1-value": "Me" }, url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata != {} assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") == "Me" # Now test removing the metadata ################################ response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title" }, url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") is None # Now test adding bad metadata ############################### response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title", "media_metadata-1-identifier": "dc:creator", "media_metadata-1-value": "Me", "media_metadata-2-identifier": "dc:created", "media_metadata-2-value": "On the worst day" }, url=media_slug) media_entry = MediaEntry.query.first() old_metadata = new_metadata new_metadata = media_entry.media_metadata assert new_metadata == old_metadata assert ("u'On the worst day' is not a 'date-time'" in response.body)
def test_edit_metadata(self, test_app): media_entry = fixture_media_entry(uploader=self.user.id, state=u'processed') media_slug = "/u/{username}/m/{media_id}/metadata/".format( username = str(self.user.username), media_id = str(media_entry.id)) self.login(test_app) response = test_app.get(media_slug) assert response.status == '200 OK' assert media_entry.media_metadata == {} # First test adding in metadata ################################ response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title", "media_metadata-1-identifier":"dc:creator", "media_metadata-1-value":"Me"},url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata != {} assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") == "Me" # Now test removing the metadata ################################ response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title"},url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") is None # Now test adding bad metadata ############################### response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title", "media_metadata-1-identifier":"dc:creator", "media_metadata-1-value":"Me", "media_metadata-2-identifier":"dc:created", "media_metadata-2-value":"On the worst day"},url=media_slug) media_entry = MediaEntry.query.first() old_metadata = new_metadata new_metadata = media_entry.media_metadata assert new_metadata == old_metadata context = template.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/edit/metadata.html'] if six.PY2: expected = "u'On the worst day' is not a 'date-time'" else: expected = "'On the worst day' is not a 'date-time'" assert context['form'].errors[ 'media_metadata'][0]['identifier'][0] == expected
def test_mark_all_comment_notifications_seen(self): """ Test that mark_all_comments_seen works""" user = fixture_add_user('otherperson', password='******', privileges=[u'active']) media_entry = fixture_media_entry(uploader=user.id, state=u'processed') fixture_comment_subscription(media_entry) media_uri_id = '/u/{0}/m/{1}/'.format(user.username, media_entry.id) # add 2 comments self.test_app.post( media_uri_id + 'comment/add/', { 'comment_content': u'Test comment #43' } ) self.test_app.post( media_uri_id + 'comment/add/', { 'comment_content': u'Test comment #44' } ) notifications = Notification.query.filter_by( user_id=user.id).all() assert len(notifications) == 2 # both comments should not be marked seen assert notifications[0].seen == False assert notifications[1].seen == False # login with other user to mark notifications seen self.logout() self.login('otherperson', 'nosreprehto') # mark all comment notifications seen res = self.test_app.get('/notifications/comments/mark_all_seen/') res.follow() assert urlparse.urlsplit(res.location)[2] == '/' notifications = Notification.query.filter_by( user_id=user.id).all() # both notifications should be marked seen assert notifications[0].seen == True assert notifications[1].seen == True
def test_media_deletes_broken_attachment(test_app): user_a = fixture_add_user(u"chris_a") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.attachment_files.append(dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_media_deletes_broken_attachment(test_app): user_a = fixture_add_user(u"chris_a") media = fixture_media_entry(uploader=user_a.id, save=False) media.attachment_files.append(dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_customize_subtitle(test_app): user_a = fixture_add_user(u"test_user") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.subtitle_files.append( dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() for subtitle in media.subtitle_files: assert '' == open_subtitle(subtitle['filepath'])[0]
def test_edit_metadata(self, test_app): media_entry = fixture_media_entry(uploader=self.user.id, state=u'processed') media_slug = "/u/{username}/m/{media_id}/metadata/".format( username = str(self.user.username), media_id = str(media_entry.id)) self.login(test_app) response = test_app.get(media_slug) assert response.status == '200 OK' assert media_entry.media_metadata == {} # First test adding in metadata ################################ response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title", "media_metadata-1-identifier":"dc:creator", "media_metadata-1-value":"Me"},url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata != {} assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") == "Me" # Now test removing the metadata ################################ response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title"},url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") is None # Now test adding bad metadata ############################### response, context = self.do_post({ "media_metadata-0-identifier":"dc:title", "media_metadata-0-value":"Some title", "media_metadata-1-identifier":"dc:creator", "media_metadata-1-value":"Me", "media_metadata-2-identifier":"dc:created", "media_metadata-2-value":"On the worst day"},url=media_slug) media_entry = MediaEntry.query.first() old_metadata = new_metadata new_metadata = media_entry.media_metadata assert new_metadata == old_metadata assert ("u'On the worst day' is not a 'date-time'" in response.body)
def test_add_subtitle_entry(test_app): user_a = fixture_add_user(u"test_user") media = fixture_media_entry(uploader=user_a.id, save=False, expunge=False) media.subtitle_files.append( dict( name=u"some name", filepath=[u"does", u"not", u"exist"], )) Session.add(media) Session.flush() MediaEntry.query.get(media.id).delete() User.query.get(user_a.id).delete()
def test_mark_all_comment_notifications_seen(self): """ Test that mark_all_comments_seen works""" user = fixture_add_user('otherperson', password='******', privileges=[u'active']) media_entry = fixture_media_entry(uploader=user.id, state=u'processed') fixture_comment_subscription(media_entry) media_uri_id = '/u/{0}/m/{1}/'.format(user.username, media_entry.id) # add 2 comments self.test_app.post(media_uri_id + 'comment/add/', {'comment_content': u'Test comment #43'}) self.test_app.post(media_uri_id + 'comment/add/', {'comment_content': u'Test comment #44'}) notifications = Notification.query.filter_by(user_id=user.id).all() assert len(notifications) == 2 # both comments should not be marked seen assert notifications[0].seen == False assert notifications[1].seen == False # login with other user to mark notifications seen self.logout() self.login('otherperson', 'nosreprehto') # mark all comment notifications seen res = self.test_app.get('/notifications/comments/mark_all_seen/') res.follow() assert urlparse.urlsplit(res.location)[2] == '/' notifications = Notification.query.filter_by(user_id=user.id).all() # both notifications should be marked seen assert notifications[0].seen == True assert notifications[1].seen == True
def test_unprocess_media_entry(test_app): """ Test that media entries that aren't marked as processed are not added to the index. """ dirname = pluginapi.get_config('indexedsearch').get('INDEX_DIR') media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='unprocessed') media_a.description = 'DescriptionA' Session.add(media_a) Session.commit() # Check that the media entry is not in the index ix = whoosh.index.open_dir(dirname, indexname=INDEX_NAME) with ix.searcher() as searcher: qp = whoosh.qparser.QueryParser('title', schema=ix.schema) query = qp.parse('mediaA') assert len(searcher.search(query)) == 0
def test_mark_all_comment_notifications_seen(self): """ Test that mark_all_comments_seen works""" user = fixture_add_user("otherperson", password="******", privileges=[u"active"]) media_entry = fixture_media_entry(uploader=user.id, state=u"processed") fixture_comment_subscription(media_entry) media_uri_id = "/u/{0}/m/{1}/".format(user.username, media_entry.id) # add 2 comments self.test_app.post(media_uri_id + "comment/add/", {"comment_content": u"Test comment #43"}) self.test_app.post(media_uri_id + "comment/add/", {"comment_content": u"Test comment #44"}) notifications = Notification.query.filter_by(user_id=user.id).all() assert len(notifications) == 2 # both comments should not be marked seen assert notifications[0].seen == False assert notifications[1].seen == False # login with other user to mark notifications seen self.logout() self.login("otherperson", "nosreprehto") # mark all comment notifications seen res = self.test_app.get("/notifications/comments/mark_all_seen/") res.follow() assert urlparse.urlsplit(res.location)[2] == "/" notifications = Notification.query.filter_by(user_id=user.id).all() # both notifications should be marked seen assert notifications[0].seen == True assert notifications[1].seen == True
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/') 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/') 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 #---------------------------------------------------------------------- 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 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'}) 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/") 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/")) 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/") 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/") 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/") 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/") 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/") excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii') assert b'Bad response: 403 FORBIDDEN' in 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() excinfo = str(excinfo) if six.PY2 else str(excinfo).encode('ascii') assert b'Bad response: 403 FORBIDDEN' in excinfo
def test_update_index(test_app): """ Test that the update_index method: - updates any media entries whose time in the index is prior to the updated attribute of the media entry itself - ignores any media_entry whose time in the index matches the updated attribute of the media entry itself - deletes entries from the index that don't exist in the database. - adds entries that are missing from the index """ dirname = pluginapi.get_config('indexedsearch').get('INDEX_DIR') fake_time = datetime.datetime.utcnow() media_a = fixture_media_entry(title='mediaA', save=False, expunge=False, fake_upload=False, state='processed') media_b = fixture_media_entry(title='mediaB', save=False, expunge=False, fake_upload=False, state='processed') media_c = fixture_media_entry(title='mediaC', save=False, expunge=False, fake_upload=False, state='processed') media_a.description = 'DescriptionA' media_b.description = 'DescriptionB' media_c.description = 'DescriptionC' Session.add(media_a) Session.add(media_b) Session.add(media_c) Session.commit() ix = whoosh.index.open_dir(dirname, indexname=INDEX_NAME) with whoosh.writing.AsyncWriter(ix) as writer: # Mess up the index by: # - changing the time of media_a to a fake time before it was created # and changing the description # - changing the description of media_b # - adding a fake entry # - deleting an entry writer.update_document(title='{0}'.format(media_a.title), description='fake_description_a', media_id=media_a.id, time=fake_time) writer.update_document(title='{0}'.format(media_b.title), description='fake_description_b', media_id=media_b.id, time=media_b.updated) writer.update_document(title='fake document', description='fake_description_d', media_id=29, time=fake_time) writer.delete_by_term('media_id', media_c.id) engine = get_engine() engine.update_index() with engine.index.searcher() as searcher: # We changed the time in the index for media_a, so it should have # been audited. qp = whoosh.qparser.QueryParser('description', schema=engine.index.schema) query = qp.parse('fake_description_a') assert len(searcher.search(query)) == 0 query = qp.parse('DescriptionA') fields = searcher.search(query)[0] assert fields['media_id'] == media_a.id # media_b shouldn't have been audited, because we didn't change the # time, so should still have a fake description. query = qp.parse('fake_description_b') fields = searcher.search(query)[0] assert fields['media_id'] == media_b.id # media_c should have been re-added to the index query = qp.parse('DescriptionC') fields = searcher.search(query)[0] assert fields['media_id'] == media_c.id # The fake entry, media_d, should have been deleted query = qp.parse('fake_description_d') assert len(searcher.search(query)) == 0
def test_comment_notification(self, wants_email): ''' Test - if a notification is created when posting a comment on another users media entry. - that the comment data is consistent and exists. ''' user = fixture_add_user('otherperson', password='******', wants_comment_notification=wants_email) user_id = user.id media_entry = fixture_media_entry(uploader=user.id, state=u'processed') media_entry_id = media_entry.id subscription = fixture_comment_subscription(media_entry) subscription_id = subscription.id media_uri_id = '/u/{0}/m/{1}/'.format(user.username, media_entry.id) media_uri_slug = '/u/{0}/m/{1}/'.format(user.username, media_entry.slug) self.test_app.post( media_uri_id + 'comment/add/', { 'comment_content': u'Test comment #42' } ) notifications = Notification.query.filter_by( user_id=user.id).all() assert len(notifications) == 1 notification = notifications[0] assert type(notification) == CommentNotification assert notification.seen == False assert notification.user_id == user.id assert notification.subject.get_author.id == self.test_user.id assert notification.subject.content == u'Test comment #42' if wants_email == True: assert mail.EMAIL_TEST_MBOX_INBOX == [ {'from': '*****@*****.**', 'message': 'Content-Type: text/plain; \ charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: \ base64\nSubject: GNU MediaGoblin - chris commented on your \ post\nFrom: [email protected]\nTo: \ [email protected]\n\nSGkgb3RoZXJwZXJzb24sCmNocmlzIGNvbW1lbnRlZCBvbiB5b3VyIHBvc3QgKGh0dHA6Ly9sb2Nh\nbGhvc3Q6ODAvdS9vdGhlcnBlcnNvbi9tL3NvbWUtdGl0bGUvYy8xLyNjb21tZW50KSBhdCBHTlUg\nTWVkaWFHb2JsaW4KClRlc3QgY29tbWVudCAjNDIKCkdOVSBNZWRpYUdvYmxpbg==\n', 'to': [u'*****@*****.**']}] else: assert mail.EMAIL_TEST_MBOX_INBOX == [] # Save the ids temporarily because of DetachedInstanceError notification_id = notification.id comment_id = notification.subject.id self.logout() self.login('otherperson', 'nosreprehto') self.test_app.get(media_uri_slug + '/c/{0}/'.format(comment_id)) notification = Notification.query.filter_by(id=notification_id).first() assert notification.seen == True self.test_app.get(media_uri_slug + '/notifications/silence/') subscription = CommentSubscription.query.filter_by(id=subscription_id)\ .first() assert subscription.notify == False notifications = Notification.query.filter_by( user_id=user_id).all() # User should not have been notified assert len(notifications) == 1
def test_edit_metadata(self, test_app): media_entry = fixture_media_entry(uploader=self.user.id, state=u'processed') media_slug = "/u/{username}/m/{media_id}/metadata/".format( username=str(self.user.username), media_id=str(media_entry.id)) self.login(test_app) response = test_app.get(media_slug) assert response.status == '200 OK' assert media_entry.media_metadata == {} # First test adding in metadata ################################ response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title", "media_metadata-1-identifier": "dc:creator", "media_metadata-1-value": "Me" }, url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata != {} assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") == "Me" # Now test removing the metadata ################################ response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title" }, url=media_slug) media_entry = MediaEntry.query.first() new_metadata = media_entry.media_metadata assert new_metadata.get("dc:title") == "Some title" assert new_metadata.get("dc:creator") is None # Now test adding bad metadata ############################### response, context = self.do_post( { "media_metadata-0-identifier": "dc:title", "media_metadata-0-value": "Some title", "media_metadata-1-identifier": "dc:creator", "media_metadata-1-value": "Me", "media_metadata-2-identifier": "dc:created", "media_metadata-2-value": "On the worst day" }, url=media_slug) media_entry = MediaEntry.query.first() old_metadata = new_metadata new_metadata = media_entry.media_metadata assert new_metadata == old_metadata context = template.TEMPLATE_TEST_CONTEXT[ 'mediagoblin/edit/metadata.html'] if six.PY2: expected = "u'On the worst day' is not a 'date-time'" else: expected = "'On the worst day' is not a 'date-time'" assert context['form'].errors['media_metadata'][0]['identifier'][ 0] == expected
def setup(self): self.user = fixture_add_user(username='******', privileges=['active']) self.media_entry = fixture_media_entry( uploader=self.user.id, state='processed')
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/") 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/") 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 # ---------------------------------------------------------------------- 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 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"}) 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/") 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/") ) 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/") 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/") 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/") 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/") 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/") excinfo = str(excinfo) if six.PY2 else str(excinfo).encode("ascii") assert b"Bad response: 403 FORBIDDEN" in 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() excinfo = str(excinfo) if six.PY2 else str(excinfo).encode("ascii") assert b"Bad response: 403 FORBIDDEN" in excinfo
def setup(self, test_app): self.app = test_app self.user = fixture_add_user() self.obj = fixture_media_entry() self.target = fixture_media_entry()
def test_comment_notification(self, wants_email): ''' Test - if a notification is created when posting a comment on another users media entry. - that the comment data is consistent and exists. ''' user = fixture_add_user('otherperson', password='******', wants_comment_notification=wants_email, privileges=[u'active', u'commenter']) assert user.wants_comment_notification == wants_email user_id = user.id media_entry = fixture_media_entry(uploader=user.id, state=u'processed') media_entry_id = media_entry.id subscription = fixture_comment_subscription(media_entry) subscription_id = subscription.id media_uri_id = '/u/{0}/m/{1}/'.format(user.username, media_entry.id) media_uri_slug = '/u/{0}/m/{1}/'.format(user.username, media_entry.slug) self.test_app.post(media_uri_id + 'comment/add/', {'comment_content': u'Test comment #42'}) notifications = Notification.query.filter_by(user_id=user.id).all() assert len(notifications) == 1 notification = notifications[0] assert notification.seen == False assert notification.user_id == user.id assert notification.obj().get_actor.id == self.test_user.id assert notification.obj().content == u'Test comment #42' if wants_email == True: assert mail.EMAIL_TEST_MBOX_INBOX == [{ 'from': '*****@*****.**', 'message': 'Content-Type: text/plain; \ charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: \ base64\nSubject: GNU MediaGoblin - chris commented on your \ post\nFrom: [email protected]\nTo: \ [email protected]\n\nSGkgb3RoZXJwZXJzb24sCmNocmlzIGNvbW1lbnRlZCBvbiB5b3VyIHBvc3QgKGh0dHA6Ly9sb2Nh\nbGhvc3Q6ODAvdS9vdGhlcnBlcnNvbi9tL3NvbWUtdGl0bGUvYy8xLyNjb21tZW50KSBhdCBHTlUg\nTWVkaWFHb2JsaW4KClRlc3QgY29tbWVudCAjNDIKCkdOVSBNZWRpYUdvYmxpbg==\n', 'to': [u'*****@*****.**'] }] else: assert mail.EMAIL_TEST_MBOX_INBOX == [] # Save the ids temporarily because of DetachedInstanceError notification_id = notification.id comment_id = notification.obj().get_comment_link().id self.logout() self.login('otherperson', 'nosreprehto') self.test_app.get(media_uri_slug + 'c/{0}/'.format(comment_id)) notification = Notification.query.filter_by(id=notification_id).first() assert notification.seen == True self.test_app.get(media_uri_slug + 'notifications/silence/') subscription = CommentSubscription.query.filter_by(id=subscription_id)\ .first() assert subscription.notify == False notifications = Notification.query.filter_by(user_id=user_id).all() # User should not have been notified assert len(notifications) == 1