Ticket #465: 465-unit-tests-v1.patch

File 465-unit-tests-v1.patch, 5.0 KB (added by digital-dreamer, 10 years ago)
  • new file mediagoblin/tests/test_attachments.py

    diff --git a/mediagoblin/tests/test_attachments.py b/mediagoblin/tests/test_attachments.py
    new file mode 100644
    index 0000000..bdd79c8
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17import pytest
     18from mediagoblin import mg_globals, auth, storage
     19from mediagoblin.db.base import Session
     20from mediagoblin.db.models import User, MediaEntry, MediaAttachmentFile
     21from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry
     22from datetime import datetime
     23
     24class TestAttachments:
     25    @pytest.fixture(autouse=True)
     26    def setup(self, test_app):
     27        self.test_app = test_app
     28        self.user_owner = fixture_add_user(u"owner",
     29            privileges=[u"active"])
     30        fixture_add_user(u"otheruser",
     31            privileges=[u"active"])
     32        fixture_add_user(u"admin",
     33            privileges=[u"admin",u"active"])
     34   
     35    def login(self, username):
     36        self.test_app.post(
     37            "/auth/login/", {
     38                "username": username,
     39                "password": "toast"})
     40           
     41    def logout(self):
     42        self.test_app.get("/auth/logout/")
     43       
     44    def test_attachment_file_delete(self, test_app):
     45        media_entry = fixture_media_entry(uploader=self.user_owner.id,
     46            state=u"processed")
     47        Session.add(media_entry)
     48       
     49        this_storage = storage.storage_system_from_config(
     50        {"base_url": "http://example.org/moodia/",
     51         "base_dir": "/tmp/"})
     52        mg_globals.public_store = this_storage
     53       
     54        attachment_path = ["attachment_to_delete.txt"]
     55        attachment_file = this_storage.get_file(
     56                attachment_path, "wb")
     57        attachment_file.write("Testing MediaAttachmentFile.delete()");
     58        media_entry.attachment_files.append(dict(
     59                    name="Attachment to delete",
     60                    filepath=attachment_path,
     61                    created=datetime.utcnow(),
     62                    ))
     63               
     64        assert MediaAttachmentFile.query.filter_by(name="Attachment to delete").count() == 1
     65        assert this_storage.file_exists(attachment_path) == True
     66               
     67        attachment = MediaAttachmentFile.query.filter_by(name="Attachment to delete").first()
     68        attachment.delete();
     69       
     70        assert MediaAttachmentFile.query.filter_by(name="Attachment to delete").count() == 0
     71        assert this_storage.file_exists(attachment_path) == False
     72       
     73    def test_confirm_delete_attachment(self, test_app):
     74        self.login(u"owner")
     75        media_entry = fixture_media_entry(uploader=self.user_owner.id,
     76            state=u"processed")
     77        Session.add(media_entry)
     78       
     79        media_entry.attachment_files.append(dict(
     80            name=u"some name",
     81            filepath=[u"does", u"not", u"exist"],
     82            ))
     83        cda_slug1 = "/u/{username}/m/{media_id}/confirm-delete-attachment/1/".format(
     84            username = str(self.user_owner.username),
     85            media_id = str(media_entry.id))
     86        cda_slug2 = "/u/{username}/m/{media_id}/confirm-delete-attachment/2/".format(
     87            username = str(self.user_owner.username),
     88            media_id = str(media_entry.id))
     89
     90        response = test_app.get(cda_slug1)
     91        assert response.status == "200 OK"
     92                     
     93        response = test_app.get(cda_slug2, expect_errors=True)
     94        assert response.status == "404 NOT FOUND"
     95
     96        self.logout()
     97       
     98        response = test_app.get(cda_slug1)
     99        assert response.status == "302 FOUND"
     100       
     101        self.login(u"otheruser")
     102       
     103        response = test_app.get(cda_slug1, expect_errors=True)
     104        assert response.status == "403 FORBIDDEN"
     105       
     106        self.logout()
     107        self.login(u"admin")
     108       
     109        Session.add(media_entry)
     110        media_entry.attachment_files.append(dict(
     111            name=u"some name",
     112            filepath=[u"does", u"not", u"exist"],
     113            ))
     114        cda_slug1 = "/u/{username}/m/{media_id}/confirm-delete-attachment/1/".format(
     115            username = str(self.user_owner.username),
     116            media_id = str(media_entry.id))
     117       
     118        response = test_app.get(cda_slug1)
     119        assert response.status == "200 OK"