Ejemplo n.º 1
0
    def test_20_new_id(self):
        """HistoriaSession: Generate a new session ID"""
        sess = session.HistoriaSession(self.db)

        session_id = sess.new_id()

        self.assertEqual(sess.sessionid, session_id, "New ID wasn't saved")
Ejemplo n.º 2
0
    def test_10_internals(self):
        """HistoriaSession: __setattr__"""
        sess = session.HistoriaSession(self.db)

        with self.assertRaises(AttributeError):
            sess.bogus_field = "Junk Data"

        attrs = ['sessionid', 'created', 'last_seen', 'ip']

        # All of the listed fields on a User should raise a ValueError when they are fed an integer
        for attr in attrs:
            with self.assertRaises(ValueError):
                setattr(sess, attr, 123243)

        sess._anything = "ok"
        self.assertEqual(
            sess._anything, "ok",
            "Assignment of _ variables works fine...except that they fail all the time"
        )

        current_stamp = datetime.datetime.now()
        sess.sessionid = "12354ABCDEF"
        sess.created = current_stamp
        sess.last_seen = current_stamp
        sess.userid = 123
        self.assertEqual(sess.sessionid, "12354ABCDEF",
                         "Assignment of sessionid failed.")
        self.assertEqual(sess.userid, 123, "Assignment of uid  failed.")
        self.assertEqual(sess.created, current_stamp,
                         "Assignment of created timestamp failed.")
        self.assertEqual(sess.last_seen, current_stamp,
                         "Assignment of  access timestamp failed.")
Ejemplo n.º 3
0
    def test_40_load(self):
        """HistoriaSetting: load()"""
        self.database_setup(withTables=True)
        sess1 = session.HistoriaSession(self.db)
        current_stamp = datetime.datetime.now()
        session_id = sess1.new_id()
        sess1.ip = "127.0.0.1"
        sess1.userid = 123
        sess1.created = current_stamp
        sess1.last_seen = current_stamp
        sess1.save()

        sess2 = session.HistoriaSession(self.db)
        sess2.load(sess1.sessionid)

        self.assertEqual(
            sess1.sessionid, sess2.sessionid,
            "sessionid on original and loaded object don't match")
        self.assertFalse(sess2._dirty, "The dirty bit is wrong after load.")
        self.assertEqual(
            sess2, sess1,
            "The two copies of the record should consider themselves equal.")
        self.assertEqual(
            sess2.userid, sess1.userid,
            "userid in the table should match the userid on the record.")
        self.assertEqual(
            sess2.ip, sess1.ip,
            "ip in the table should match the one on the record.")
        self.assertAlmostEqual(
            sess2.created,
            sess1.created,
            delta=datetime.timedelta(seconds=1),
            msg="created in the table should match the one on the record.")
        self.assertAlmostEqual(
            sess2.last_seen,
            sess1.last_seen,
            delta=datetime.timedelta(seconds=1),
            msg="last_access in the table should match the one on the record.")

        # Try to load with bogus ID
        self.assertRaises(
            exceptions.DataLoadError, sess2.load,
            '0215158c-93d9-2338-ac38-cb45e6096ed2'
        )  # that session id was generated during testing, so shouldn't come around again.
Ejemplo n.º 4
0
    def start_session(self, ip):
        """Return a new session object"""
        sess = session.HistoriaSession(self.database)
        sess_id = sess.new_id()
        sess.ip = ip
        sess.userid = 0
        sess.save()

        self.logger.info("New session started with ID: {0}".format(sess_id))

        return sess
Ejemplo n.º 5
0
    def test_33_save(self):
        """HistoriaSession: save() with only the minimum required settings"""
        sess = session.HistoriaSession(self.db)

        self.assertRaises(exceptions.DataConnectionError, sess.save)

        # Setup the database and try again.
        self.database_setup(withTables=True)

        self.assertRaises(exceptions.DataSaveError, sess.save)

        current_stamp = datetime.datetime.now()
        session_id = sess.new_id()
        sess.ip = "127.0.0.1"
        sess.userid = 123
        sess.save()

        self.assertFalse(sess._dirty, "Dirty bit active after save")

        # Now let's go see if it's really there
        select = ("SELECT * FROM `{0}`".format(
            session.HistoriaSession.machine_type), {})
        result = self.db.execute_select(select)

        self.assertEqual(len(result), 1,
                         "There should be 1 and only 1 entry in the table.")
        self.assertEqual(
            result[0]['sessionid'], sess.sessionid,
            "sessionid in the table should match the sessionid on the record.")
        self.assertEqual(
            result[0]['ip'], sess.ip,
            "ip in the table should match the one on the record.")
        self.assertEqual(
            result[0]['userid'], sess.userid,
            "userid in the table should match the one on the record.")

        # Resave and make sure we still only have one in the database (and force the update sql to run)
        sess.save()
        # Now let's go see if it's really there
        select = ("SELECT * FROM `{0}`".format(
            session.HistoriaSession.machine_type), {})
        result = self.db.execute_select(select)

        self.assertEqual(len(result), 1,
                         "There should be 1 and only 1 entry in the table.")
        self.assertEqual(
            result[0]['sessionid'], sess.sessionid,
            "sessionid in the table should match the sessionid on the record.")
        self.assertEqual(
            result[0]['ip'], sess.ip,
            "ip in the table should match the one on the record.")
        self.assertEqual(
            result[0]['userid'], sess.userid,
            "userid in the table should match the one on the record.")
Ejemplo n.º 6
0
    def test_15_internals(self):
        """HistoriaSession: __eq__ and __ne__"""

        # Two HistoriaRecords are equal when they have the same ID and same type.
        s1 = session.HistoriaSession(self.db)
        s2 = session.HistoriaSession(self.db)
        hr = core_data_objects.HistoriaRecord(self.db)

        self.assertNotEqual(
            s1, s2, "By default a blank record is equal to nothing else")
        self.assertNotEqual(
            s1, s1,
            "By default a blank record is equal to nothing else...even itself")

        s1.sessionid = "123"

        self.assertNotEqual(
            s1, s2, "Coming up as equal when they should have different IDs")

        s2.sessionid = "123"

        self.assertEqual(s1, s2, "With matched ID they report Not equal")
        self.assertNotEqual(hr, s2, "A generic is matching a setting")
Ejemplo n.º 7
0
    def reload_session(self, session_id, ip):
        """Return a new session object"""
        sess = session.HistoriaSession(self.database)
        try:
            sess.load(session_id)
            if sess.ip != ip:
                sess.ip = ip
            sess.save()  # reset the last_seen value in the database

            self.logger.info("Loaded session with ID: {0}".format(session_id))

            if sess.userid > 0:
                try:
                    active_user = user.HistoriaUser(self.database)
                    active_user.load(sess.userid)
                    sess._user = active_user
                except database.exceptions.DataLoadError as err:
                    # If there is an error loading the user for this session
                    # then the session is corrupt and should be destoryed and a
                    # new one created.
                    self.logger.notice(
                        'Invalid user associated with session. Destorying session: {0}'
                        .format(session_id))
                    self.end_session(session_id)
                    sess = session.HistoriaSession(self.database)
        except database.exceptions.DataLoadError as err:
            self.logger.error('Unable to load session: {0}'.format(session_id))
            raise InvalidSessionError(
                "Invalid Session ID: {0}".format(session_id))
        except database.exceptions.DataConnectionError as err:
            self.logger.error(
                'Unable to connect to database to load session: {0}'.format(
                    session_id))
            raise err
        else:
            return sess
Ejemplo n.º 8
0
    def test_45_save_and_load(self):
        """HistoriaSetting: test save() then load() then save()."""
        self.database_setup(withTables=True)
        sess1 = session.HistoriaSession(self.db)
        current_stamp = datetime.datetime.now()
        session_id = sess1.new_id()
        sess1.ip = "127.0.0.1"
        sess1.userid = 123
        sess1.created = current_stamp
        sess1.last_seen = current_stamp
        sess1.save()

        sess2 = session.HistoriaSession(self.db)
        sess2.load(sess1.sessionid)

        sess2.save()

        # Now let's go see if it's really there
        select = ("SELECT * FROM `{0}`".format(
            session.HistoriaSession.machine_type), {})
        result = self.db.execute_select(select)

        self.assertEqual(
            sess1.sessionid, sess2.sessionid,
            "sessionid on original and loaded object don't match")
        self.assertEqual(len(result), 1,
                         "There should be 1 and only 1 entry in the table.")
        self.assertEqual(
            result[0]['sessionid'], sess2.sessionid,
            "sessionid in the table should match the sessionid on the record.")
        self.assertEqual(
            result[0]['ip'], sess2.ip,
            "ip in the table should match the one on the record.")
        self.assertEqual(
            result[0]['userid'], sess2.userid,
            "userid in the table should match the one on the record.")
Ejemplo n.º 9
0
    def test_30_save(self):
        """HistoriaSession: save()"""
        sess = session.HistoriaSession(self.db)

        self.assertRaises(exceptions.DataConnectionError, sess.save)

        # Setup the database and try again.
        self.database_setup(withTables=True)

        self.assertRaises(exceptions.DataSaveError, sess.save)

        current_stamp = datetime.datetime.now()
        test_id = sess.new_id()
        sess.created = current_stamp
        sess.last_seen = current_stamp
        sess.ip = "127.0.0.1"
        sess.userid = 123
        self.assertTrue(sess._dirty, "Dirty bit not active but data changed")
        sess.save()

        self.assertFalse(sess._dirty, "Dirty bit active after save")

        # Now let's go see if it's really there
        select = ("SELECT * FROM `{0}`".format(
            session.HistoriaSession.machine_type), {})
        result = self.db.execute_select(select)

        self.assertEqual(len(result), 1,
                         "There should be 1 and only 1 entry in the table.")
        self.assertEqual(
            result[0]['sessionid'], sess.sessionid,
            "sessionid in the table should match the sessionid on the record.")
        self.assertEqual(
            result[0]['userid'], sess.userid,
            "userid in the table should match the one on the record.")
        self.assertEqual(
            result[0]['ip'], sess.ip,
            "ip in the table should match the one on the record.")
        self.assertAlmostEqual(
            result[0]['created'],
            sess.created,
            delta=datetime.timedelta(seconds=1),
            msg="created in the table should match the one on the record.")
        self.assertAlmostEqual(
            result[0]['last_seen'],
            sess.last_seen,
            delta=datetime.timedelta(seconds=1),
            msg="last_seen in the table should match the one on the record.")
Ejemplo n.º 10
0
    def test_50_delete(self):
        """HistoriaSetting: delete()"""
        self.database_setup(withTables=True)
        sess1 = session.HistoriaSession(self.db)
        current_stamp = datetime.datetime.now()
        session_id = sess1.new_id()
        sess1.ip = "127.0.0.1"
        sess1.userid = 123
        sess1.created = current_stamp
        sess1.last_seen = current_stamp
        sess1.save()

        sess1.delete()

        # Now let's go see if it's really there
        select = ("SELECT * FROM `{0}`".format(
            session.HistoriaSession.machine_type), {})
        result = self.db.execute_select(select)

        self.assertEqual(len(result), 0,
                         "There should nothing in the table now.")
        self.assertEqual(-1, sess1.id, "The ID should reset to -1")
Ejemplo n.º 11
0
    def test_10_construct(self):
        """HistoriaSession: Constructor"""

        sess = session.HistoriaSession(self.db)

        self.assertIsInstance(sess, session.HistoriaSession,
                              "Umm, yeah, that's a problem...")
        self.assertIsNone(sess.sessionid,
                          "sessionid attribute missing from new object")
        self.assertIsNone(sess.userid,
                          "userid attribute missing from new object")
        self.assertIsNone(sess.created,
                          "created attribute missing from new object")
        self.assertIsNone(sess.last_seen,
                          "last_seen attribute missing from new object")
        self.assertIsNone(sess.ip, "ip attribute missing from new object")
        self.assertIsInstance(sess._logger, logging.Logger,
                              "Default logger isn't a logger")
        self.assertFalse(sess._dirty, "Dirty bit is be clean to start")
        self.assertIs(sess.database, self.db,
                      "Database isn't the one we sent over")
        self.assertIsInstance(sess.database,
                              core_data_objects.HistoriaDatabase,
                              "Database object isn't the right type (oops)")