[879b32]: / qiita_pet / test / test_user_handlers.py

Download this file

160 lines (126 with data), 5.8 kB

  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
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from unittest import main
from wtforms.validators import ValidationError
from wtforms import StringField
from mock import Mock
from json import loads
from qiita_pet.test.tornado_test_base import TestHandlerBase
from qiita_pet.handlers.user_handlers import UserProfile
from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_db.user import User
class TestUserProfile(TestHandlerBase):
# TODO: add proper test for this once figure out how. Issue 567
pass
class TestUserProfileHandler(TestHandlerBase):
def test_get(self):
response = self.get('/profile/')
self.assertEqual(response.code, 200)
def test_post_password(self):
post_args = {
'action': 'password',
'oldpass': 'password',
'newpass': 'newpass'
}
response = self.post('/profile/', post_args)
self.assertEqual(response.code, 200)
def test_post_profile(self):
post_args = {
'action': ['profile'],
'affiliation': ['NEWNAME'],
'address': ['ADDRESS'],
'name': ['TESTDUDE'],
'phone': ['111-222-3333'],
'social_orcid': [''],
'social_googlescholar': [''],
'social_researchgate': ['']}
response = self.post('/profile/', post_args)
self.assertEqual(response.code, 200)
def test_validators_social(self):
# None or empty should be valid
obs = UserProfile.validate_general(None, "", "")
self.assertEqual(obs, None)
obs = UserProfile.validate_general("", "", "")
self.assertEqual(obs, None)
# having white spaces should raise errors
with self.assertRaises(ValidationError):
obs = UserProfile.validate_general(" infix", "", "")
with self.assertRaises(ValidationError):
obs = UserProfile.validate_general("infix ", "", "")
with self.assertRaises(ValidationError):
obs = UserProfile.validate_general(" infix ", "", "")
obs = UserProfile.validate_general("infix", "", "")
self.assertEqual(obs, 'infix')
with self.assertRaises(ValidationError):
obs = UserProfile.validate_general(
"http://kurt.com/id1234", "msg", r"http://kurt.\w{1,3}/")
def test_validator_orcid_id(self):
field = StringField("testfield")
field.data = "0000-0002-0975-9019"
obs = UserProfile.validator_orcid_id(None, field)
self.assertEqual(obs, None)
field.data = "https://orcid.org/0000-0002-0975-9019"
with self.assertRaises(ValidationError):
obs = UserProfile.validator_orcid_id(None, field)
field.data = "wrong"
with self.assertRaises(ValidationError):
obs = UserProfile.validator_orcid_id(None, field)
def test_validator_gscholar_id(self):
field = StringField("testfield")
field.data = "_e3QL94AAAAJ"
obs = UserProfile.validator_gscholar_id(None, field)
self.assertEqual(obs, None)
field.data = ('https://scholar.google.com/citations?user=_e3QL94AAAAJ&'
'hl=en')
with self.assertRaises(ValidationError):
obs = UserProfile.validator_gscholar_id(None, field)
field.data = 'user=_e3QL94AAAAJ&hl=en'
with self.assertRaises(ValidationError):
obs = UserProfile.validator_gscholar_id(None, field)
field.data = 'user=_e3QL94AAAAJ'
with self.assertRaises(ValidationError):
obs = UserProfile.validator_gscholar_id(None, field)
field.data = '=_e3QL94AAAAJ'
with self.assertRaises(ValidationError):
obs = UserProfile.validator_gscholar_id(None, field)
def test_validator_rgate_id(self):
field = StringField("testfield")
field.data = "Rob-Knight"
obs = UserProfile.validator_rgate_id(None, field)
self.assertEqual(obs, None)
field.data = 'https://www.researchgate.net/profile/Rob-Knight'
with self.assertRaises(ValidationError):
obs = UserProfile.validator_rgate_id(None, field)
class TestUserJobsHandler(TestHandlerBase):
def test_get(self):
response = self.get('/user/jobs/')
self.assertEqual(response.code, 200)
class TestPurgeUsersAJAXHandler(TestHandlerBase):
def setUp(self):
super().setUp()
BaseHandler.get_current_user = Mock(return_value=User("admin@foo.bar"))
def test_get(self):
response = self.get('/admin/purge_usersAjax/?_=1718805487494')
obs_users_table = loads(response.body.decode('ascii'))
obs_users = {user['email'] for user in obs_users_table}
self.assertIn('ayearago@nonvalidat.ed', obs_users)
self.assertIn('3Xdays@nonvalidat.ed', obs_users)
self.assertNotIn('justnow@nonvalidat.ed', obs_users)
def test_post_removeBoth(self):
# remove both users
response = self.post('/admin/purge_users/',
{'action': 'Remove',
'selected': ['ayearago@nonvalidat.ed',
'3Xdays@nonvalidat.ed']})
self.assertEqual(response.code, 200)
# test that zero users are listed now
response = self.get('/admin/purge_usersAjax/?_=1718805487495')
obs_users_table = loads(response.body.decode('ascii'))
self.assertEqual(obs_users_table, [])
if __name__ == "__main__":
main()