Files
@ 01dbd21d206c
Branch filter:
Location: kallithea/kallithea/tests/functional/test_my_account.py
01dbd21d206c
13.8 KiB
text/x-python
i18n: updated translation for Russian
Currently translated at 100.0% (691 of 691 strings)
Currently translated at 100.0% (691 of 691 strings)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | # -*- coding: utf-8 -*-
from tg.util.webtest import test_context
from kallithea.lib import helpers as h
from kallithea.model.db import Repository, User, UserApiKeys, UserFollowing, UserSshKeys
from kallithea.model.meta import Session
from kallithea.model.user import UserModel
from kallithea.tests.base import *
from kallithea.tests.fixture import Fixture
fixture = Fixture()
class TestMyAccountController(TestController):
test_user_1 = 'testme'
@classmethod
def teardown_class(cls):
if User.get_by_username(cls.test_user_1):
UserModel().delete(cls.test_user_1)
Session().commit()
def test_my_account(self):
self.log_user()
response = self.app.get(url('my_account'))
response.mustcontain('value="%s' % TEST_USER_ADMIN_LOGIN)
def test_my_account_my_repos(self):
self.log_user()
response = self.app.get(url('my_account_repos'))
cnt = Repository.query().filter(Repository.owner ==
User.get_by_username(TEST_USER_ADMIN_LOGIN)).count()
response.mustcontain('"raw_name": "%s"' % HG_REPO)
response.mustcontain('"just_name": "%s"' % GIT_REPO)
def test_my_account_my_watched(self):
self.log_user()
response = self.app.get(url('my_account_watched'))
cnt = UserFollowing.query().filter(UserFollowing.user ==
User.get_by_username(TEST_USER_ADMIN_LOGIN)).count()
response.mustcontain('"raw_name": "%s"' % HG_REPO)
response.mustcontain('"just_name": "%s"' % GIT_REPO)
def test_my_account_my_emails(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
def test_my_account_my_emails_add_existing_email(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'new_email': TEST_USER_REGULAR_EMAIL, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'This email address is already in use')
def test_my_account_my_emails_add_missing_email_in_form(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'Please enter an email address')
def test_my_account_my_emails_add_remove(self):
self.log_user()
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
response = self.app.post(url('my_account_emails'),
{'new_email': 'barz@example.com', '_session_csrf_secret_token': self.session_csrf_secret_token()})
response = self.app.get(url('my_account_emails'))
from kallithea.model.db import UserEmailMap
email_id = UserEmailMap.query() \
.filter(UserEmailMap.user == User.get_by_username(TEST_USER_ADMIN_LOGIN)) \
.filter(UserEmailMap.email == 'barz@example.com').one().email_id
response.mustcontain('barz@example.com')
response.mustcontain('<input id="del_email_id" name="del_email_id" type="hidden" value="%s" />' % email_id)
response = self.app.post(url('my_account_emails_delete'),
{'del_email_id': email_id, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'Removed email from user')
response = self.app.get(url('my_account_emails'))
response.mustcontain('No additional emails specified')
@parametrize('name,attrs',
[('firstname', {'firstname': 'new_username'}),
('lastname', {'lastname': 'new_username'}),
('admin', {'admin': True}),
('admin', {'admin': False}),
('extern_type', {'extern_type': 'ldap'}),
('extern_type', {'extern_type': None}),
#('extern_name', {'extern_name': 'test'}),
#('extern_name', {'extern_name': None}),
('active', {'active': False}),
('active', {'active': True}),
('email', {'email': 'someemail@example.com'}),
# ('new_password', {'new_password': 'foobar123',
# 'password_confirmation': 'foobar123'})
])
def test_my_account_update(self, name, attrs):
usr = fixture.create_user(self.test_user_1, password='qweqwe',
email='testme@example.com',
extern_type='internal',
extern_name=self.test_user_1,
skip_if_exists=True)
params = usr.get_api_data(True) # current user data
user_id = usr.user_id
self.log_user(username=self.test_user_1, password='qweqwe')
params.update({'password_confirmation': ''})
params.update({'new_password': ''})
params.update({'extern_type': 'internal'})
params.update({'extern_name': self.test_user_1})
params.update({'_session_csrf_secret_token': self.session_csrf_secret_token()})
params.update(attrs)
response = self.app.post(url('my_account'), params)
self.checkSessionFlash(response,
'Your account was updated successfully')
updated_user = User.get_by_username(self.test_user_1)
updated_params = updated_user.get_api_data(True)
updated_params.update({'password_confirmation': ''})
updated_params.update({'new_password': ''})
params['last_login'] = updated_params['last_login']
if name == 'email':
params['emails'] = [attrs['email']]
if name == 'extern_type':
# cannot update this via form, expected value is original one
params['extern_type'] = "internal"
if name == 'extern_name':
# cannot update this via form, expected value is original one
params['extern_name'] = str(user_id)
if name == 'active':
# my account cannot deactivate account
params['active'] = True
if name == 'admin':
# my account cannot make you an admin !
params['admin'] = False
params.pop('_session_csrf_secret_token')
assert params == updated_params
def test_my_account_update_err_email_exists(self):
self.log_user()
new_email = TEST_USER_REGULAR_EMAIL # already existing email
response = self.app.post(url('my_account'),
params=dict(
username=TEST_USER_ADMIN_LOGIN,
new_password=TEST_USER_ADMIN_PASS,
password_confirmation='test122',
firstname=u'NewName',
lastname=u'NewLastname',
email=new_email,
_session_csrf_secret_token=self.session_csrf_secret_token())
)
response.mustcontain('This email address is already in use')
def test_my_account_update_err(self):
self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
new_email = 'newmail.pl'
response = self.app.post(url('my_account'),
params=dict(
username=TEST_USER_ADMIN_LOGIN,
new_password=TEST_USER_ADMIN_PASS,
password_confirmation='test122',
firstname=u'NewName',
lastname=u'NewLastname',
email=new_email,
_session_csrf_secret_token=self.session_csrf_secret_token()))
response.mustcontain('An email address must contain a single @')
from kallithea.model import validators
with test_context(self.app):
msg = validators.ValidUsername(edit=False, old_data={}) \
._messages['username_exists']
msg = h.html_escape(msg % {'username': TEST_USER_ADMIN_LOGIN})
response.mustcontain(msg)
def test_my_account_api_keys(self):
usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
user = User.get(usr['user_id'])
response = self.app.get(url('my_account_api_keys'))
response.mustcontain(user.api_key)
response.mustcontain('Expires: Never')
@parametrize('desc,lifetime', [
('forever', -1),
('5mins', 60*5),
('30days', 60*60*24*30),
])
def test_my_account_add_api_keys(self, desc, lifetime):
usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
user = User.get(usr['user_id'])
response = self.app.post(url('my_account_api_keys'),
{'description': desc, 'lifetime': lifetime, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'API key successfully created')
try:
response = response.follow()
user = User.get(usr['user_id'])
for api_key in user.api_keys:
response.mustcontain(api_key)
finally:
for api_key in UserApiKeys.query().all():
Session().delete(api_key)
Session().commit()
def test_my_account_remove_api_key(self):
usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
user = User.get(usr['user_id'])
response = self.app.post(url('my_account_api_keys'),
{'description': 'desc', 'lifetime': -1, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'API key successfully created')
response = response.follow()
# now delete our key
keys = UserApiKeys.query().all()
assert 1 == len(keys)
response = self.app.post(url('my_account_api_keys_delete'),
{'del_api_key': keys[0].api_key, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'API key successfully deleted')
keys = UserApiKeys.query().all()
assert 0 == len(keys)
def test_my_account_reset_main_api_key(self):
usr = self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
user = User.get(usr['user_id'])
api_key = user.api_key
response = self.app.get(url('my_account_api_keys'))
response.mustcontain(api_key)
response.mustcontain('Expires: Never')
response = self.app.post(url('my_account_api_keys_delete'),
{'del_api_key_builtin': api_key, '_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'API key successfully reset')
response = response.follow()
response.mustcontain(no=[api_key])
def test_my_account_add_ssh_key(self):
description = u'something'
public_key = u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC6Ycnc2oUZHQnQwuqgZqTTdMDZD7ataf3JM7oG2Fw8JR6cdmz4QZLe5mfDwaFwG2pWHLRpVqzfrD/Pn3rIO++bgCJH5ydczrl1WScfryV1hYMJ/4EzLGM657J1/q5EI+b9SntKjf4ax+KP322L0TNQGbZUHLbfG2MwHMrYBQpHUQ== me@localhost'
fingerprint = u'Ke3oUCNJM87P0jJTb3D+e3shjceP2CqMpQKVd75E9I8'
self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
response = self.app.post(url('my_account_ssh_keys'),
{'description': description,
'public_key': public_key,
'_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'SSH key %s successfully added' % fingerprint)
response = response.follow()
response.mustcontain(fingerprint)
user_id = response.session['authuser']['user_id']
ssh_key = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).one()
assert ssh_key.fingerprint == fingerprint
assert ssh_key.description == description
Session().delete(ssh_key)
Session().commit()
def test_my_account_remove_ssh_key(self):
description = u''
public_key = u'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAgQC6Ycnc2oUZHQnQwuqgZqTTdMDZD7ataf3JM7oG2Fw8JR6cdmz4QZLe5mfDwaFwG2pWHLRpVqzfrD/Pn3rIO++bgCJH5ydczrl1WScfryV1hYMJ/4EzLGM657J1/q5EI+b9SntKjf4ax+KP322L0TNQGbZUHLbfG2MwHMrYBQpHUQ== me@localhost'
fingerprint = u'Ke3oUCNJM87P0jJTb3D+e3shjceP2CqMpQKVd75E9I8'
self.log_user(TEST_USER_REGULAR2_LOGIN, TEST_USER_REGULAR2_PASS)
response = self.app.post(url('my_account_ssh_keys'),
{'description': description,
'public_key': public_key,
'_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'SSH key %s successfully added' % fingerprint)
response.follow()
user_id = response.session['authuser']['user_id']
ssh_key = UserSshKeys.query().filter(UserSshKeys.user_id == user_id).one()
assert ssh_key.description == u'me@localhost'
response = self.app.post(url('my_account_ssh_keys_delete'),
{'del_public_key': ssh_key.public_key,
'_session_csrf_secret_token': self.session_csrf_secret_token()})
self.checkSessionFlash(response, 'SSH key successfully deleted')
keys = UserSshKeys.query().all()
assert 0 == len(keys)
|