This repository has been archived by the owner on Feb 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
users_test.py
220 lines (191 loc) · 10.4 KB
/
users_test.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
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
#!/usr/bin/python
# Copyright 2013 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at: http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software distrib-
# uted under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
# OR CONDITIONS OF ANY KIND, either express or implied. See the License for
# specific language governing permissions and limitations under the License.
"""Tests for users.py."""
__author__ = '[email protected] (Ka-Ping Yee)'
import test_utils
import users
class UsersTest(test_utils.BaseTest):
"""Tests for the users module."""
# These tests don't use non-numeric uids like other tests, because their
# purpose is to realistically test the underlying login mechanism.
def testIsDeveloper(self):
with test_utils.EnvContext(SERVER_SOFTWARE='Google App Engine/1.7.6'):
self.assertFalse(users.IsDeveloper())
with test_utils.EnvContext(USER_IS_ADMIN='1'):
self.assertTrue(users.IsDeveloper())
with test_utils.EnvContext(SERVER_SOFTWARE='Development/1.0'):
self.assertTrue(users.IsDeveloper())
def testGetNonExistentUser(self):
user = users.Get('77')
self.assertEquals(None, user)
def testGetCurrent_NormalLogin(self):
# Try an effective user determined by the Google Account login.
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]',
USER_ORGANIZATION='alpha.test'):
user = users.GetCurrent() # should allocate the first uid, '1'
self.assertEquals('1', user.id)
user = users.Get('1')
self.assertEquals('alpha.test', user.ga_domain)
self.assertEquals('[email protected]', user.email)
def testGetCurrent_GoogleAccountMapping(self):
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]'):
user = users.GetCurrent() # should allocate the first uid, '1'
self.assertEquals('1', user.id)
with test_utils.EnvContext(USER_ID='666666',
USER_EMAIL='[email protected]'):
user = users.GetCurrent() # should allocate the next uid, '2'
self.assertEquals('2', user.id)
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]'):
user = users.GetCurrent() # should match by USER_ID
self.assertEquals('1', user.id)
def testGetCurrent_UpdateEmailAndDomain(self):
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]',
USER_ORGANIZATION='alpha.test'):
users.GetCurrent() # should allocate the first uid, '1'
user = users.Get('1')
self.assertEquals('alpha.test', user.ga_domain)
self.assertEquals('[email protected]', user.email)
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]',
USER_ORGANIZATION='beta.test'):
users.GetCurrent() # should update the existing UserModel
user = users.Get('1')
self.assertEquals('beta.test', user.ga_domain)
self.assertEquals('[email protected]', user.email)
def testGetCurrent_ImpersonationNotAllowed(self):
# Verify that the crisismap_login cookie doesn't work for ordinary users.
with test_utils.EnvContext(
SERVER_SOFTWARE='Google App Engine/1.7.6', USER_ID='123456789',
USER_EMAIL='[email protected]', USER_ORGANIZATION='alpha.test',
HTTP_COOKIE='crisismap_login=t1000:sky.net:[email protected]'):
user = users.GetCurrent()
self.assertEquals('1', user.id) # cookie should be ignored
self.assertEquals('alpha.test', user.ga_domain)
self.assertEquals('[email protected]', user.email)
def testGetCurrent_ImpersonationInProd(self):
# Verify that the crisismap_login cookie works for admins in prod.
with test_utils.EnvContext(
SERVER_SOFTWARE='Google App Engine/1.7.6',
USER_ID='123456789', USER_EMAIL='[email protected]',
USER_ORGANIZATION='alpha.test', USER_IS_ADMIN='1',
HTTP_COOKIE='crisismap_login=t1000:sky.net:[email protected]'):
user = users.GetCurrent()
self.assertEquals('t1000', user.id) # cookie should be used
self.assertEquals('sky.net', user.ga_domain)
self.assertEquals('[email protected]', user.email)
def testGetCurrent_ImpersonationInDev(self):
# Verify that the crisismap_login cookie works in development.
with test_utils.EnvContext(
SERVER_SOFTWARE='Development/1.0',
USER_ID='123456789', USER_EMAIL='[email protected]',
USER_ORGANIZATION='alpha.test',
HTTP_COOKIE='crisismap_login=t1000:sky.net:[email protected]'):
user = users.GetCurrent()
self.assertEquals('t1000', user.id) # cookie should be used
self.assertEquals('sky.net', user.ga_domain)
self.assertEquals('[email protected]', user.email)
def testGetAll(self):
users._UserModel(id='one', ga_domain='xyz.com', email='[email protected]').put()
users._UserModel(id='two', ga_domain='abc.com', email='[email protected]').put()
[user1, user2] = sorted(users.GetAll(), key=lambda u: u.id)
self.assertEquals(('one', 'xyz.com', '[email protected]'),
(user1.id, user1.ga_domain, user1.email))
self.assertEquals(('two', 'abc.com', '[email protected]'),
(user2.id, user2.ga_domain, user2.email))
[user2] = sorted(users.GetAllWithFilter(lambda u: u.id == 'two'),
key=lambda u: u.id)
self.assertEquals(('two', 'abc.com', '[email protected]'),
(user2.id, user2.ga_domain, user2.email))
def testGetAllGoogleAccounts(self):
users._GoogleAccount(id='111111', uid='100').put()
users._GoogleAccount(id='222222', uid='200').put()
[gau1, gau2] = sorted(users.GetAllGoogleAccounts(),
key=lambda gau: gau.uid)
self.assertEquals(('111111', '100'), (gau1.id, gau1.uid))
self.assertEquals(('222222', '200'), (gau2.id, gau2.uid))
[gau2] = sorted(
users.GetAllGoogleAccountsWithFilter(lambda gau: gau.uid == '200'),
key=lambda gau: gau.uid)
self.assertEquals(('222222', '200'), (gau2.id, gau2.uid))
def testGetForEmail_NonexistentGoogleAccounts(self):
self.mox.stubs.Set(users, '_EmailToGaeUserId', {}.get)
# Normal e-mail addresses should allocate uids sequentially.
self.assertEquals('1', users.GetForEmail('[email protected]').id)
self.assertEquals('2', users.GetForEmail('[email protected]').id)
# For an e-mail address we've seen before, we should get the matching User.
self.assertEquals('1', users.GetForEmail('[email protected]').id)
# User entities should have been stored as a side effect.
self.assertEquals('[email protected]', users.Get('1').email)
self.assertEquals('[email protected]', users.Get('2').email)
def testGetForEmail_TestAccounts(self):
self.mox.stubs.Set(users, '_EmailToGaeUserId', {}.get)
# Accounts under ".test" should get their uids from the e-mail address.
self.assertEquals('test1', users.GetForEmail('[email protected]').id)
self.assertEquals('agablaga', users.GetForEmail('[email protected]').id)
# User entities should have been stored as a side effect.
self.assertEquals('[email protected]', users.Get('test1').email)
self.assertEquals('[email protected]', users.Get('agablaga').email)
def testGetCurrent_AfterGetForEmailWithNonexistentGoogleAccount(self):
self.mox.stubs.Set(users, '_EmailToGaeUserId', {}.get)
# Introduce a previously unseen e-mail address.
self.assertEquals('1', users.GetForEmail('[email protected]').id)
# A sign-in with that e-mail address should associate the Google Account.
with test_utils.EnvContext(USER_ID='123456789', USER_EMAIL='[email protected]'):
self.assertEquals('1', users.GetCurrent().id)
# Subsequent sign-ins with the same Google Account ID should also hook up.
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]'):
self.assertEquals('1', users.GetCurrent().id)
def testGetCurrent_AfterGetForEmailWithExistingGoogleAccount(self):
self.mox.stubs.Set(users, '_EmailToGaeUserId', {
'[email protected]': '123456789',
'[email protected]': '123456789'
}.get)
# Introduce an e-mail address that's new to us but has a Google Account.
# GetForEmail should associate the address with the Google Account.
self.assertEquals('1', users.GetForEmail('[email protected]').id)
# A sign-in with that Google Account ID should get the same UserModel,
# even if the e-mail address is different.
with test_utils.EnvContext(USER_ID='123456789',
USER_EMAIL='[email protected]'):
self.assertEquals('1', users.GetCurrent().id)
# Any other e-mail address associated with the same Google Account should
# yield the same UserModel.
self.assertEquals('1', users.GetForEmail('[email protected]').id)
def testSetWelcomeMessageDismissed(self):
users._UserModel(id='1', ga_domain='xyz.com', email='[email protected]').put()
self.assertEquals(False, users.Get('1').welcome_message_dismissed)
users.SetWelcomeMessageDismissed('1', True)
self.assertEquals(True, users.Get('1').welcome_message_dismissed)
def testSetMarketingConsent(self):
users._UserModel(id='1', ga_domain='xyz.com', email='[email protected]').put()
self.assertEquals(False, users.Get('1').marketing_consent_answered)
self.assertEquals(False, users.Get('1').marketing_consent)
users.SetMarketingConsent('1', False)
self.assertEquals(True, users.Get('1').marketing_consent_answered)
self.assertEquals(False, users.Get('1').marketing_consent)
users.SetMarketingConsent('1', True)
self.assertEquals(True, users.Get('1').marketing_consent_answered)
self.assertEquals(True, users.Get('1').marketing_consent)
def testGetLoginUrl(self):
# We just test dev mode; in production this forwards to create_login_url.
self.assertEquals('/root/.login?redirect=abc', users.GetLoginUrl('abc'))
def testGetLogoutUrl(self):
# We just test dev mode; in production this forwards to create_logout_url.
self.assertEquals('/root/.login?logout=1&redirect=abc',
users.GetLogoutUrl('abc'))
if __name__ == '__main__':
test_utils.main()