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)
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)
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))
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))
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)
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)
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'])
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)
def test_check_session_with_nonexisting_session_id_returns_none(self): session = UserSession(self.db) self.assertIsNone(session.check_session('0000001'))
def test_check_session_with_none_returns_none(self): session = UserSession(self.db) self.assertIsNone(session.check_session(None))
def test_get_user_from_session_user_is_none_returns_none(self): session = UserSession(self.db) self.assertIsNone(session.get_user_from_session(None))
def test_get_user_from_session_user_doesnt_exist_returns_none(self): session = UserSession(self.db) self.assertIsNone(session.get_user_from_session('0000001'))