| 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 | |
| 17 | # Maybe not every model needs a test, but some models have special |
| 18 | # methods, and so it makes sense to test them here. |
| 19 | |
| 20 | from mediagoblin.db.base import Session |
| 21 | from mediagoblin.db.models import MediaEntry |
| 22 | |
| 23 | from mediagoblin.tests.tools import fixture_add_user |
| 24 | |
| 25 | import mock |
| 26 | |
| 27 | |
| 28 | class FakeUUID(object): |
| 29 | hex = 'testtest-test-test-test-testtesttest' |
| 30 | |
| 31 | UUID_MOCK = mock.Mock(return_value=FakeUUID()) |
| 32 | |
| 33 | |
| 34 | class TestMediaEntrySlugs(object): |
| 35 | def _setup(self): |
| 36 | self.chris_user = fixture_add_user(u'chris') |
| 37 | self.emily_user = fixture_add_user(u'emily') |
| 38 | self.existing_entry = self._insert_media_entry_fixture( |
| 39 | title=u"Beware, I exist!", |
| 40 | slug=u"beware-i-exist") |
| 41 | |
| 42 | def _insert_media_entry_fixture(self, title=None, slug=None, this_id=None, |
| 43 | uploader=None, save=True): |
| 44 | entry = MediaEntry() |
| 45 | entry.title = title or u"Some title" |
| 46 | entry.slug = slug |
| 47 | entry.id = this_id |
| 48 | entry.uploader = uploader or self.chris_user.id |
| 49 | entry.media_type = u'image' |
| 50 | |
| 51 | if save: |
| 52 | entry.save() |
| 53 | |
| 54 | return entry |
| 55 | |
| 56 | def test_unique_slug_from_title(self, test_app): |
| 57 | self._setup() |
| 58 | |
| 59 | entry = self._insert_media_entry_fixture(u"Totally unique slug!", save=False) |
| 60 | entry.generate_slug() |
| 61 | assert entry.slug == u'totally-unique-slug' |
| 62 | |
| 63 | def test_old_good_unique_slug(self, test_app): |
| 64 | self._setup() |
| 65 | |
| 66 | entry = self._insert_media_entry_fixture( |
| 67 | u"A title here", u"a-different-slug-there", save=False) |
| 68 | entry.generate_slug() |
| 69 | assert entry.slug == u"a-different-slug-there" |
| 70 | |
| 71 | def test_old_weird_slug(self, test_app): |
| 72 | self._setup() |
| 73 | |
| 74 | entry = self._insert_media_entry_fixture( |
| 75 | slug=u"wowee!!!!!", save=False) |
| 76 | entry.generate_slug() |
| 77 | assert entry.slug == u"wowee" |
| 78 | |
| 79 | def test_existing_slug_use_id(self, test_app): |
| 80 | self._setup() |
| 81 | |
| 82 | entry = self._insert_media_entry_fixture( |
| 83 | u"Beware, I exist!!", this_id=9000, save=False) |
| 84 | entry.generate_slug() |
| 85 | assert entry.slug == u"beware-i-exist-9000" |
| 86 | |
| 87 | def test_existing_slug_cant_use_id(self, test_app): |
| 88 | self._setup() |
| 89 | |
| 90 | # Getting tired of dealing with test_app and this mock.patch |
| 91 | # thing conflicting, getting lazy. |
| 92 | @mock.patch('uuid.uuid4', UUID_MOCK) |
| 93 | def _real_test(): |
| 94 | # This one grabs the nine thousand slug |
| 95 | self._insert_media_entry_fixture( |
| 96 | slug=u"beware-i-exist-9000") |
| 97 | |
| 98 | entry = self._insert_media_entry_fixture( |
| 99 | u"Beware, I exist!!", this_id=9000, save=False) |
| 100 | entry.generate_slug() |
| 101 | assert entry.slug == u"beware-i-exist-test" |
| 102 | |
| 103 | _real_test() |
| 104 | |
| 105 | def test_existing_slug_cant_use_id_extra_junk(self, test_app): |
| 106 | self._setup() |
| 107 | |
| 108 | # Getting tired of dealing with test_app and this mock.patch |
| 109 | # thing conflicting, getting lazy. |
| 110 | @mock.patch('uuid.uuid4', UUID_MOCK) |
| 111 | def _real_test(): |
| 112 | # This one grabs the nine thousand slug |
| 113 | self._insert_media_entry_fixture( |
| 114 | slug=u"beware-i-exist-9000") |
| 115 | |
| 116 | # This one grabs makes sure the annoyance doesn't stop |
| 117 | self._insert_media_entry_fixture( |
| 118 | slug=u"beware-i-exist-test") |
| 119 | |
| 120 | entry = self._insert_media_entry_fixture( |
| 121 | u"Beware, I exist!!", this_id=9000, save=False) |
| 122 | entry.generate_slug() |
| 123 | assert entry.slug == u"beware-i-exist-testtest" |
| 124 | |
| 125 | _real_test() |
| 126 | |
| 127 | def test_garbage_slug(self, test_app): |
| 128 | """ |
| 129 | Titles that sound totally like Q*Bert shouldn't have slugs at |
| 130 | all. We'll just reference them by id. |
| 131 | |
| 132 | , |
| 133 | / \ (@!#?@!) |
| 134 | |\,/| ,-, / |
| 135 | | |#| ( ")~ |
| 136 | / \|/ \ L L |
| 137 | |\,/|\,/| |
| 138 | | |#, |#| |
| 139 | / \|/ \|/ \ |
| 140 | |\,/|\,/|\,/| |
| 141 | | |#| |#| |#| |
| 142 | / \|/ \|/ \|/ \ |
| 143 | |\,/|\,/|\,/|\,/| |
| 144 | | |#| |#| |#| |#| |
| 145 | \|/ \|/ \|/ \|/ |
| 146 | """ |
| 147 | self._setup() |
| 148 | |
| 149 | qbert_entry = self._insert_media_entry_fixture( |
| 150 | u"@!#?@!", save=False) |
| 151 | qbert_entry.generate_slug() |
| 152 | assert qbert_entry.slug is None |
| 153 | |
| 154 | |
| 155 | def test_media_data_init(test_app): |
| 156 | Session.rollback() |
| 157 | Session.remove() |
| 158 | media = MediaEntry() |
| 159 | media.media_type = u"mediagoblin.media_types.image" |
| 160 | assert media.media_data is None |
| 161 | media.media_data_init() |
| 162 | assert media.media_data is not None |
| 163 | obj_in_session = 0 |
| 164 | for obj in Session(): |
| 165 | obj_in_session += 1 |
| 166 | print repr(obj) |
| 167 | assert obj_in_session == 0 |