Switch test_app generation over to use py.test fixtures.
[mediagoblin.git] / mediagoblin / tests / test_edit.py
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 from nose.tools import assert_equal
18
19 from mediagoblin import mg_globals
20 from mediagoblin.db.models import User
21 from mediagoblin.tests.tools import fixture_add_user
22 from mediagoblin.tools import template
23 from mediagoblin.auth.lib import bcrypt_check_password
24
25 class TestUserEdit(object):
26 def setup(self):
27 # set up new user
28 self.user_password = u'toast'
29 self.user = fixture_add_user(password = self.user_password)
30
31 def login(self, test_app):
32 test_app.post(
33 '/auth/login/', {
34 'username': self.user.username,
35 'password': self.user_password})
36
37
38 def test_user_deletion(self, test_app):
39 """Delete user via web interface"""
40 self.login(test_app)
41
42 # Make sure user exists
43 assert User.query.filter_by(username=u'chris').first()
44
45 res = test_app.post('/edit/account/delete/', {'confirmed': 'y'})
46
47 # Make sure user has been deleted
48 assert User.query.filter_by(username=u'chris').first() == None
49
50 #TODO: make sure all corresponding items comments etc have been
51 # deleted too. Perhaps in submission test?
52
53 #Restore user at end of test
54 self.user = fixture_add_user(password = self.user_password)
55 self.login(test_app)
56
57
58 def test_change_password(self, test_app):
59 """Test changing password correctly and incorrectly"""
60 self.login(test_app)
61
62 # test that the password can be changed
63 # template.clear_test_template_context()
64 res = test_app.post(
65 '/edit/account/', {
66 'old_password': 'toast',
67 'new_password': '123456',
68 'wants_comment_notification': 'y'
69 })
70
71 # Check for redirect on success
72 assert_equal(res.status_int, 302)
73 # test_user has to be fetched again in order to have the current values
74 test_user = User.query.filter_by(username=u'chris').first()
75 assert bcrypt_check_password('123456', test_user.pw_hash)
76 # Update current user passwd
77 self.user_password = '123456'
78
79 # test that the password cannot be changed if the given
80 # old_password is wrong template.clear_test_template_context()
81 test_app.post(
82 '/edit/account/', {
83 'old_password': 'toast',
84 'new_password': '098765',
85 })
86
87 test_user = User.query.filter_by(username=u'chris').first()
88 assert not bcrypt_check_password('098765', test_user.pw_hash)
89
90
91 def test_change_bio_url(self, test_app):
92 """Test changing bio and URL"""
93 self.login(test_app)
94
95 # Test if legacy profile editing URL redirects correctly
96 res = test_app.post(
97 '/edit/profile/', {
98 'bio': u'I love toast!',
99 'url': u'http://dustycloud.org/'}, expect_errors=True)
100
101 # Should redirect to /u/chris/edit/
102 assert_equal (res.status_int, 302)
103 assert res.headers['Location'].endswith("/u/chris/edit/")
104
105 res = test_app.post(
106 '/u/chris/edit/', {
107 'bio': u'I love toast!',
108 'url': u'http://dustycloud.org/'})
109
110 test_user = User.query.filter_by(username=u'chris').first()
111 assert_equal(test_user.bio, u'I love toast!')
112 assert_equal(test_user.url, u'http://dustycloud.org/')
113
114 # change a different user than the logged in (should fail with 403)
115 fixture_add_user(username=u"foo")
116 res = test_app.post(
117 '/u/foo/edit/', {
118 'bio': u'I love toast!',
119 'url': u'http://dustycloud.org/'}, expect_errors=True)
120 assert_equal(res.status_int, 403)
121
122 # test changing the bio and the URL inproperly
123 too_long_bio = 150 * 'T' + 150 * 'o' + 150 * 'a' + 150 * 's' + 150* 't'
124
125 test_app.post(
126 '/u/chris/edit/', {
127 # more than 500 characters
128 'bio': too_long_bio,
129 'url': 'this-is-no-url'})
130
131 # Check form errors
132 context = template.TEMPLATE_TEST_CONTEXT['mediagoblin/edit/edit_profile.html']
133 form = context['form']
134
135 assert_equal(form.bio.errors, [u'Field must be between 0 and 500 characters long.'])
136 assert_equal(form.url.errors, [u'This address contains errors'])
137
138 # test changing the url inproperly