test_edit.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. import six
  17. import six.moves.urllib.parse as urlparse
  18. import pytest
  19. from mediagoblin import mg_globals
  20. from mediagoblin.db.models import User, MediaEntry
  21. from mediagoblin.tests.tools import fixture_add_user, fixture_media_entry
  22. from mediagoblin import auth
  23. from mediagoblin.tools import template, mail
  24. class TestUserEdit(object):
  25. def setup(self):
  26. # set up new user
  27. self.user_password = u'toast'
  28. self.user = fixture_add_user(password = self.user_password,
  29. privileges=[u'active'])
  30. def login(self, test_app):
  31. test_app.post(
  32. '/auth/login/', {
  33. 'username': self.user.username,
  34. 'password': self.user_password})
  35. def test_user_deletion(self, test_app):
  36. """Delete user via web interface"""
  37. self.login(test_app)
  38. # Make sure user exists
  39. assert User.query.filter_by(username=u'chris').first()
  40. res = test_app.post('/edit/account/delete/', {'confirmed': 'y'})
  41. # Make sure user has been deleted
  42. assert User.query.filter_by(username=u'chris').first() == None
  43. #TODO: make sure all corresponding items comments etc have been
  44. # deleted too. Perhaps in submission test?
  45. #Restore user at end of test
  46. self.user = fixture_add_user(password = self.user_password,
  47. privileges=[u'active'])
  48. self.login(test_app)
  49. def test_change_bio_url(self, test_app):
  50. """Test changing bio and URL"""
  51. self.login(test_app)
  52. # Test if legacy profile editing URL redirects correctly
  53. res = test_app.post(
  54. '/edit/profile/', {
  55. 'bio': u'I love toast!',
  56. 'url': u'http://dustycloud.org/'}, expect_errors=True)
  57. # Should redirect to /u/chris/edit/
  58. assert res.status_int == 302
  59. assert res.headers['Location'].endswith("/u/chris/edit/")
  60. res = test_app.post(
  61. '/u/chris/edit/', {
  62. 'bio': u'I love toast!',
  63. 'url': u'http://dustycloud.org/'})
  64. test_user = User.query.filter_by(username=u'chris').first()
  65. assert test_user.bio == u'I love toast!'
  66. assert test_user.url == u'http://dustycloud.org/'
  67. # change a different user than the logged in (should fail with 403)
  68. fixture_add_user(username=u"foo",
  69. privileges=[u'active'])
  70. res = test_app.post(
  71. '/u/foo/edit/', {
  72. 'bio': u'I love toast!',
  73. 'url': u'http://dustycloud.org/'}, expect_errors=True)
  74. assert res.status_int == 403
  75. # test changing the bio and the URL inproperly
  76. too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
  77. test_app.post(
  78. '/u/chris/edit/', {
  79. # more than 500 characters
  80. 'bio': too_long_bio,
  81. 'url': 'this-is-no-url'})
  82. # Check form errors
  83. context = template.TEMPLATE_TEST_CONTEXT[
  84. 'mediagoblin/edit/edit_profile.html']
  85. form = context['form']
  86. assert form.bio.errors == [
  87. u'Field must be between 0 and 500 characters long.']
  88. assert form.url.errors == [
  89. u'This address contains errors']
  90. def test_email_change(self, test_app):
  91. self.login(test_app)
  92. # Test email already in db
  93. template.clear_test_template_context()
  94. test_app.post(
  95. '/edit/email/', {
  96. 'new_email': 'chris@example.com',
  97. 'password': 'toast'})
  98. # Check form errors
  99. context = template.TEMPLATE_TEST_CONTEXT[
  100. 'mediagoblin/edit/change_email.html']
  101. assert context['form'].new_email.errors == [
  102. u'Sorry, a user with that email address already exists.']
  103. # Test successful email change
  104. template.clear_test_template_context()
  105. res = test_app.post(
  106. '/edit/email/', {
  107. 'new_email': 'new@example.com',
  108. 'password': 'toast'})
  109. res.follow()
  110. # Correct redirect?
  111. assert urlparse.urlsplit(res.location)[2] == '/edit/account/'
  112. # Make sure we get email verification and try verifying
  113. assert len(mail.EMAIL_TEST_INBOX) == 1
  114. message = mail.EMAIL_TEST_INBOX.pop()
  115. assert message['To'] == 'new@example.com'
  116. email_context = template.TEMPLATE_TEST_CONTEXT[
  117. 'mediagoblin/edit/verification.txt']
  118. assert email_context['verification_url'].encode('ascii') in message.get_payload(decode=True)
  119. path = urlparse.urlsplit(email_context['verification_url'])[2]
  120. assert path == u'/edit/verify_email/'
  121. ## Try verifying with bs verification key, shouldn't work
  122. template.clear_test_template_context()
  123. res = test_app.get(
  124. "/edit/verify_email/?token=total_bs")
  125. res.follow()
  126. # Correct redirect?
  127. assert urlparse.urlsplit(res.location)[2] == '/'
  128. # Email shouldn't be saved
  129. email_in_db = mg_globals.database.User.query.filter_by(
  130. email='new@example.com').first()
  131. email = User.query.filter_by(username='chris').first().email
  132. assert email_in_db is None
  133. assert email == 'chris@example.com'
  134. # Verify email activation works
  135. template.clear_test_template_context()
  136. get_params = urlparse.urlsplit(email_context['verification_url'])[3]
  137. res = test_app.get('%s?%s' % (path, get_params))
  138. res.follow()
  139. # New email saved?
  140. email = User.query.filter_by(username='chris').first().email
  141. assert email == 'new@example.com'
  142. # test changing the url inproperly
  143. class TestMetaDataEdit:
  144. @pytest.fixture(autouse=True)
  145. def setup(self, test_app):
  146. # set up new user
  147. self.user_password = u'toast'
  148. self.user = fixture_add_user(password = self.user_password,
  149. privileges=[u'active',u'admin'])
  150. self.test_app = test_app
  151. def login(self, test_app):
  152. test_app.post(
  153. '/auth/login/', {
  154. 'username': self.user.username,
  155. 'password': self.user_password})
  156. def do_post(self, data, *context_keys, **kwargs):
  157. url = kwargs.pop('url', '/submit/')
  158. do_follow = kwargs.pop('do_follow', False)
  159. template.clear_test_template_context()
  160. response = self.test_app.post(url, data, **kwargs)
  161. if do_follow:
  162. response.follow()
  163. context_data = template.TEMPLATE_TEST_CONTEXT
  164. for key in context_keys:
  165. context_data = context_data[key]
  166. return response, context_data
  167. def test_edit_metadata(self, test_app):
  168. media_entry = fixture_media_entry(uploader=self.user.id,
  169. state=u'processed')
  170. media_slug = "/u/{username}/m/{media_id}/metadata/".format(
  171. username = str(self.user.username),
  172. media_id = str(media_entry.id))
  173. self.login(test_app)
  174. response = test_app.get(media_slug)
  175. assert response.status == '200 OK'
  176. assert media_entry.media_metadata == {}
  177. # First test adding in metadata
  178. ################################
  179. response, context = self.do_post({
  180. "media_metadata-0-identifier":"dc:title",
  181. "media_metadata-0-value":"Some title",
  182. "media_metadata-1-identifier":"dc:creator",
  183. "media_metadata-1-value":"Me"},url=media_slug)
  184. media_entry = MediaEntry.query.first()
  185. new_metadata = media_entry.media_metadata
  186. assert new_metadata != {}
  187. assert new_metadata.get("dc:title") == "Some title"
  188. assert new_metadata.get("dc:creator") == "Me"
  189. # Now test removing the metadata
  190. ################################
  191. response, context = self.do_post({
  192. "media_metadata-0-identifier":"dc:title",
  193. "media_metadata-0-value":"Some title"},url=media_slug)
  194. media_entry = MediaEntry.query.first()
  195. new_metadata = media_entry.media_metadata
  196. assert new_metadata.get("dc:title") == "Some title"
  197. assert new_metadata.get("dc:creator") is None
  198. # Now test adding bad metadata
  199. ###############################
  200. response, context = self.do_post({
  201. "media_metadata-0-identifier":"dc:title",
  202. "media_metadata-0-value":"Some title",
  203. "media_metadata-1-identifier":"dc:creator",
  204. "media_metadata-1-value":"Me",
  205. "media_metadata-2-identifier":"dc:created",
  206. "media_metadata-2-value":"On the worst day"},url=media_slug)
  207. media_entry = MediaEntry.query.first()
  208. old_metadata = new_metadata
  209. new_metadata = media_entry.media_metadata
  210. assert new_metadata == old_metadata
  211. context = template.TEMPLATE_TEST_CONTEXT[
  212. 'mediagoblin/edit/metadata.html']
  213. if six.PY2:
  214. expected = "u'On the worst day' is not a 'date-time'"
  215. else:
  216. expected = "'On the worst day' is not a 'date-time'"
  217. assert context['form'].errors[
  218. 'media_metadata'][0]['identifier'][0] == expected