Skip to content

Commit 867c19e

Browse files
committed
use SQL's NOW() fct to log creation of a new user account (validated or not).
1 parent 9de3f38 commit 867c19e

File tree

6 files changed

+56
-11
lines changed

6 files changed

+56
-11
lines changed

qiita_db/support_files/populate_test_db.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ INSERT INTO qiita.user_level VALUES (7, 'wet-lab admin', 'Can access the private
5050
-- Data for Name: qiita_user; Type: TABLE DATA; Schema: qiita; Owner: antoniog
5151
--
5252

53-
INSERT INTO qiita.qiita_user VALUES ('[email protected]', 4, '$2a$12$gnUi8Qg.0tvW243v889BhOBhWLIHyIJjjgaG6dxuRJkUM8nXG9Efe', 'Dude', 'Nowhere University', '123 fake st, Apt 0, Faketown, CO 80302', '111-222-3344', NULL, NULL, NULL, false, '0000-0002-0975-9019', 'Rob-Knight', '_e3QL94AAAAJ');
53+
INSERT INTO qiita.qiita_user VALUES ('[email protected]', 4, '$2a$12$gnUi8Qg.0tvW243v889BhOBhWLIHyIJjjgaG6dxuRJkUM8nXG9Efe', 'Dude', 'Nowhere University', '123 fake st, Apt 0, Faketown, CO 80302', '111-222-3344', NULL, NULL, NULL, false, '0000-0002-0975-9019', 'Rob-Knight', '_e3QL94AAAAJ', '2015-12-03 13:52:42.751331-07');
5454
INSERT INTO qiita.qiita_user VALUES ('[email protected]', 4, '$2a$12$gnUi8Qg.0tvW243v889BhOBhWLIHyIJjjgaG6dxuRJkUM8nXG9Efe', 'Shared', 'Nowhere University', '123 fake st, Apt 0, Faketown, CO 80302', '111-222-3344', NULL, NULL, NULL, false);
5555
INSERT INTO qiita.qiita_user VALUES ('[email protected]', 1, '$2a$12$gnUi8Qg.0tvW243v889BhOBhWLIHyIJjjgaG6dxuRJkUM8nXG9Efe', 'Admin', 'Owner University', '312 noname st, Apt K, Nonexistantown, CO 80302', '222-444-6789', NULL, NULL, NULL, false);
5656
INSERT INTO qiita.qiita_user VALUES ('[email protected]', 4, '$2a$12$gnUi8Qg.0tvW243v889BhOBhWLIHyIJjjgaG6dxuRJkUM8nXG9Efe', 'Demo', 'Qiita Dev', '1345 Colorado Avenue', '303-492-1984', NULL, NULL, NULL, false);

qiita_db/support_files/qiita-db-unpatched.sql

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,8 @@ CREATE TABLE qiita.qiita_user (
18911891
receive_processing_job_emails boolean DEFAULT false,
18921892
social_orcid character varying DEFAULT NULL,
18931893
social_researchgate character varying DEFAULT NULL,
1894-
social_googlescholar character varying DEFAULT NULL
1894+
social_googlescholar character varying DEFAULT NULL,
1895+
creation_timestamp timestamp without time zone DEFAULT NULL
18951896
);
18961897

18971898

@@ -1931,6 +1932,13 @@ COMMENT ON COLUMN qiita.qiita_user.pass_reset_code IS 'Randomly generated code f
19311932
COMMENT ON COLUMN qiita.qiita_user.pass_reset_timestamp IS 'Time the reset code was generated';
19321933

19331934

1935+
--
1936+
-- Name: COLUMN qiita_user.creation_timestamp; Type: COMMENT; Schema: qiita
1937+
--
1938+
1939+
COMMENT ON COLUMN qiita.qiita_user.creation_timestamp IS 'The date the user account was created';
1940+
1941+
19341942
--
19351943
-- Name: reference; Type: TABLE; Schema: qiita
19361944
--

qiita_db/test/test_user.py

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ def setUp(self):
7575
'receive_processing_job_emails': True,
7676
'social_orcid': None,
7777
'social_researchgate': None,
78-
'social_googlescholar': None
78+
'social_googlescholar': None,
79+
'creation_timestamp': None
7980
}
8081

8182
def tearDown(self):
@@ -88,7 +89,22 @@ def test_instantiate_unknown_user(self):
8889
with self.assertRaises(qdb.exceptions.QiitaDBUnknownIDError):
8990
qdb.user.User('[email protected]')
9091

91-
def _check_correct_info(self, obs, exp):
92+
def _check_correct_info(self, obs, exp, ts_before=None):
93+
"""Compares info dict of user with special handling of specific keys.
94+
95+
Parameters
96+
----------
97+
obs : dict
98+
Observed user info dictionary.
99+
exp : dict
100+
Expected user info dictionary.
101+
ts_before : datetime.datetime or None
102+
User.create records the creation timestamp through SQL's NOW().
103+
Since it is set by the database to the microsecond, we can't
104+
predict it a priori and therefore simply record timestamp before
105+
execution of user.create() and compare the relation.
106+
The DB creation_timestamp column is optional, i.e. can be None.
107+
"""
92108
self.assertEqual(set(exp.keys()), set(obs.keys()))
93109
for key in exp:
94110
# user_verify_code and password seed randomly generated so just
@@ -97,10 +113,14 @@ def _check_correct_info(self, obs, exp):
97113
self.assertEqual(len(obs[key]), 20)
98114
elif key == "password":
99115
self.assertEqual(len(obs[key]), 60)
116+
elif key == "creation_timestamp":
117+
self.assertTrue(((exp[key] is None) and (obs[key] is None))
118+
or (ts_before <= exp[key]))
100119
else:
101120
self.assertEqual(obs[key], exp[key])
102121

103122
def test_create_user(self):
123+
before = datetime.now()
104124
user = qdb.user.User.create('[email protected]', 'password')
105125

106126
# adding a couple of messages
@@ -131,8 +151,9 @@ def test_create_user(self):
131151
'email': '[email protected]',
132152
'social_orcid': None,
133153
'social_researchgate': None,
134-
'social_googlescholar': None}
135-
self._check_correct_info(obs, exp)
154+
'social_googlescholar': None,
155+
'creation_timestamp': datetime.now()}
156+
self._check_correct_info(obs, exp, before)
136157

137158
# Make sure new system messages are linked to user
138159
sql = """SELECT message_id FROM qiita.message_user
@@ -146,6 +167,7 @@ def test_create_user(self):
146167
qdb.util.clear_system_messages()
147168

148169
def test_create_user_info(self):
170+
before = datetime.now()
149171
user = qdb.user.User.create('[email protected]', 'password',
150172
self.userinfo)
151173
self.assertEqual(user.id, '[email protected]')
@@ -171,8 +193,9 @@ def test_create_user_info(self):
171193
'email': '[email protected]',
172194
'social_orcid': None,
173195
'social_researchgate': None,
174-
'social_googlescholar': None}
175-
self._check_correct_info(obs, exp)
196+
'social_googlescholar': None,
197+
'creation_timestamp': datetime.now()}
198+
self._check_correct_info(obs, exp, before)
176199

177200
def test_create_user_column_not_allowed(self):
178201
self.userinfo["email"] = "FAIL"
@@ -241,7 +264,8 @@ def test_get_info(self):
241264
'phone': '222-444-6789',
242265
'social_orcid': None,
243266
'social_researchgate': None,
244-
'social_googlescholar': None
267+
'social_googlescholar': None,
268+
'creation_timestamp': None
245269
}
246270
self.assertEqual(self.user.info, expinfo)
247271

qiita_db/user.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,12 @@ def create(cls, email, password, info=None):
234234
cls._table, ','.join(columns), ','.join(['%s'] * len(values)))
235235
qdb.sql_connection.TRN.add(sql, values)
236236

237+
# log timestamp of user creation
238+
sql = """UPDATE qiita.{0}
239+
SET creation_timestamp = NOW()
240+
WHERE email = %s""".format(cls._table)
241+
qdb.sql_connection.perform_as_transaction(sql, [email])
242+
237243
# Add system messages to user
238244
sql = """INSERT INTO qiita.message_user (email, message_id)
239245
SELECT %s, message_id FROM qiita.message

qiita_pet/handlers/user_handlers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ class UserProfileHandler(BaseHandler):
196196
def get(self):
197197
profile = UserProfile()
198198
profile.process(data=self.current_user.info)
199-
self.render("user_profile.html", profile=profile, msg="", passmsg="")
199+
self.render("user_profile.html", profile=profile, msg="", passmsg="",
200+
creation_timestamp=self.current_user.info[
201+
'creation_timestamp'])
200202

201203
@authenticated
202204
@execute_as_transaction
@@ -248,7 +250,9 @@ def post(self):
248250
else:
249251
passmsg = "Incorrect old password"
250252
self.render("user_profile.html", user=user.id, profile=form_data,
251-
msg=msg, passmsg=passmsg)
253+
msg=msg, passmsg=passmsg,
254+
creation_timestamp=self.current_user.info[
255+
'creation_timestamp'])
252256

253257

254258
class ForgotPasswordHandler(BaseHandler):

qiita_pet/templates/user_profile.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ <h3>User Information</h3>
2727
{% end %}
2828
</div>
2929
{% end %}
30+
{%if creation_timestamp is not None %}
31+
<div style="padding-left: 1em; padding-bottom: 1em; color: grey;">account created at {{creation_timestamp}}</div>
32+
{% end %}
3033
<div style="color:{% if msg.startswith('ERROR:') %}red{% else %}darkgreen{% end %};">{{msg}}</div>
3134
<button type="submit" class="btn btn-success">Save Edits</button>
3235
</form>

0 commit comments

Comments
 (0)