Example #1
0
 def test_check_session_with_valid_user_returns_username(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '979393992'
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': datetime.datetime.utcnow() + datetime.timedelta(hours=1)})
     self.assertEqual(session.check_session(session_id), user)
Example #2
0
 def test_get_user_from_session_returns_correct_user(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '23423534'
     expected_date = datetime.datetime.utcnow()
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': expected_date})
     self.assertEqual(session.get_user_from_session(session_id), user)
Example #3
0
 def test_check_session_expired_session_is_not_returned(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '23423425'
     expected_date = datetime.datetime.utcnow() + datetime.timedelta(days=-8)
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': expected_date})
     self.assertIsNone(session.check_session(session_id, extend=False))
Example #4
0
 def test_create_session(self):
     session = UserSession(self.db)
     user = '******'
     session_id = session.create_session(user)
     result = self.db.session.find_one({'session_id': session_id,
                                        'user': user, })
     self.assertIsNotNone(result)
     expiry = result[u'expiry'].replace(microsecond=0)
     self.assertEqual(expiry, (datetime.datetime.utcnow() + datetime.timedelta(days=2)).replace(microsecond=0))
Example #5
0
 def test_end_session_deletes_session(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '23423534'
     expected_date = datetime.datetime.utcnow()
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': expected_date})
     session.end_session(session_id)
     result = self.db.session.find_one({'session_id': session_id})
     self.assertIsNone(result)
Example #6
0
 def test_check_session_expired_session_is_cleaned(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '636345345234'
     expected_date = datetime.datetime.utcnow() + datetime.timedelta(days=-8)
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': expected_date})
     self.assertIsNone(session.check_session(session_id, extend=False))
     result = self.db.session.find_one({'session_id': session_id})
     self.assertIsNone(result)
Example #7
0
 def test_check_session_when_extend_is_false_expiry_is_unchanged(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '553434234'
     expected_date = (datetime.datetime.utcnow() + datetime.timedelta(hours=-6)).replace(microsecond=0)
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': expected_date})
     self.assertEqual(session.check_session(session_id, extend=False), user)
     result = self.db.session.find_one({'session_id': session_id})
     self.assertEqual(expected_date, result[u'expiry'])
Example #8
0
 def test_check_session_with_extend_will_extend_two_days_from_now(self):
     session = UserSession(self.db)
     user = '******'
     session_id = '2434392343'
     self.db.session.insert_one({'user': user,
                                 'session_id': session_id,
                                 'expiry': datetime.datetime.utcnow() + datetime.timedelta(hours=-6)})
     assert session.check_session(session_id, extend=True) == user
     result = self.db.session.find_one({'session_id': session_id})
     expected_date = (datetime.datetime.utcnow() + datetime.timedelta(days=2)).replace(second=0, microsecond=0)
     actual_date = result[u'expiry'].replace(second=0, microsecond=0)
     self.assertEqual(expected_date, actual_date)
Example #9
0
 def test_check_session_with_nonexisting_session_id_returns_none(self):
     session = UserSession(self.db)
     self.assertIsNone(session.check_session('0000001'))
Example #10
0
 def test_check_session_with_none_returns_none(self):
     session = UserSession(self.db)
     self.assertIsNone(session.check_session(None))
Example #11
0
 def test_get_user_from_session_user_is_none_returns_none(self):
     session = UserSession(self.db)
     self.assertIsNone(session.get_user_from_session(None))
Example #12
0
 def test_get_user_from_session_user_doesnt_exist_returns_none(self):
     session = UserSession(self.db)
     self.assertIsNone(session.get_user_from_session('0000001'))