-
Notifications
You must be signed in to change notification settings - Fork 2k
/
test_update.py
146 lines (114 loc) · 5.91 KB
/
test_update.py
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
'''Unit tests for ckan/logic/action/update.py.
'''
import nose.tools
import ckan.logic as logic
import ckan.new_tests.helpers as helpers
import ckan.new_tests.data as data
class TestClass(object):
@classmethod
def setup_class(cls):
# Initialize the test db (if it isn't already) and clean out any data
# left in it.
helpers.reset_db()
def setup(self):
import ckan.model as model
# Reset the db before each test method.
model.repo.rebuild_db()
def test_update_user_name(self):
'''Test that updating a user's name works successfully.'''
# The canonical form of a test has four steps:
# 1. Setup anything preconditions needed for the test.
# 2. Call the function that's being tested, once only.
# 3. Make assertions about the return value and/or side-effects of
# of the function that's being tested.
# 4. Do absolutely nothing else!
# 1. Setup.
user = helpers.call_action('user_create', **data.TYPICAL_USER)
# 2. Call the function that is being tested, once only.
# Note we have to pass the email address and password (in plain text!)
# to user_update even though we're not updating those fields, otherwise
# validation fails.
helpers.call_action('user_update', id=user['name'],
email=user['email'],
password=data.TYPICAL_USER['password'],
name='updated',
)
# 3. Make assertions about the return value and/or side-effects.
updated_user = helpers.call_action('user_show', id=user['id'])
# Note that we check just the field we were trying to update, not the
# entire dict, only assert what we're actually testing.
assert updated_user['name'] == 'updated'
# 4. Do absolutely nothing else!
def test_user_update_with_id_that_does_not_exist(self):
# FIXME: Does this need to be more realistic?
# - Actually create a user before trying to update it with the wrong
# id?
# - Actually pass other params (name, about..) to user_update?
with nose.tools.assert_raises(logic.NotFound) as context:
helpers.call_action('user_update',
id="there's no user with this id")
# TODO: Could assert the actual error message, not just the exception?
# (Could also do this with many of the tests below.)
def test_user_update_with_no_id(self):
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update')
def test_user_update_with_invalid_name(self):
user = helpers.call_action('user_create', **data.TYPICAL_USER)
# FIXME: This actually breaks the canonical test form rule by calling
# the function-under-test multiple times.
# Breaking this up into multiple tests would actually be clearer in
# terms of each test testing one thing and the names of the test
# methods documenting what the test does and how the function is
# supposed to behave.
# BUT the test code would be very repetitive.
invalid_names = ('', 'a', False, 0, -1, 23, 'new', 'edit', 'search',
'a'*200, 'Hi!', )
for name in invalid_names:
user['name'] = name
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
def test_user_update_to_name_that_already_exists(self):
fred = helpers.call_action('user_create', **data.TYPICAL_USER)
bob = helpers.call_action('user_create', name='bob',
email='bob@bob.com', password='pass')
# Try to update fred and change his user name to bob, which is already
# bob's user name
fred['name'] = bob['name']
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **fred)
def test_update_user_password(self):
'''Test that updating a user's password works successfully.'''
user = helpers.call_action('user_create', **data.TYPICAL_USER)
# Note we have to pass the email address to user_update even though
# we're not updating it, otherwise validation fails.
helpers.call_action('user_update', id=user['name'],
email=user['email'],
password='new password',
)
# user_show() never returns the user's password, so we have to access
# the model directly to test it.
import ckan.model as model
updated_user = model.User.get(user['id'])
assert updated_user.validate_password('new password')
def test_user_update_with_short_password(self):
user = helpers.call_action('user_create', **data.TYPICAL_USER)
user['password'] = 'xxx' # This password is too short.
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
def test_user_update_with_empty_password(self):
user = helpers.call_action('user_create', **data.TYPICAL_USER)
user['password'] = ''
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
def test_user_update_with_null_password(self):
user = helpers.call_action('user_create', **data.TYPICAL_USER)
user['password'] = None
with nose.tools.assert_raises(logic.ValidationError) as context:
helpers.call_action('user_update', **user)
# TODO: Valid and invalid values for the rest of the user model's fields.
def test_user_update_activity_stream(self):
pass
def test_user_update_with_custom_schema(self):
pass
def test_user_update_with_deferred_commit(self):
pass