def test_pop_fifo(self):
        """Pop should work in FIFO order.

        Uses random UUID as the point of comparison.
        """
        nr1 = get_dummy_email_notif_req()
        nr2 = get_dummy_email_notif_req()
        nr3 = get_dummy_email_notif_req()

        if ENABLE_DEBUG_PRINT:
            print("request #1: {0}".format(nr1.uuid))
            print("request #2: {0}".format(nr2.uuid))
            print("request #3: {0}".format(nr3.uuid))

        with datastore.DatabaseConnection(
            filename=self.temp_file.name, file_path_abs=True) as db_con:

            db_con.add_notification(nr1)
            nr1_expected = db_con.pop_notif()
            self.assertEqual(nr1.uuid, nr1_expected.uuid)
            self.assertNotEqual(nr2.uuid, nr1_expected.uuid)
            self.assertNotEqual(nr3.uuid, nr1_expected.uuid)
            db_con.add_notification(nr2)
            db_con.add_notification(nr3)
            nr2_expected = db_con.pop_notif()
            self.assertEqual(nr2.uuid, nr2_expected.uuid)
            self.assertNotEqual(nr1.uuid, nr2_expected.uuid)
            self.assertNotEqual(nr3.uuid, nr2_expected.uuid)
    def test_store_key_val(self):
        """Store a key/value pair in the db"""
        with datastore.DatabaseConnection(filename=self.temp_file.name,
                                          file_path_abs=True) as db_con:

            key = 'myKey'
            val = 'myVal'
            db_con.store_key_val(app_id=self.app_id, app_secret=self.app_secret,
                                 key=key, val=val)
    def test_get_key_val(self):
        """Gey the value stored for 'key'"""
        with datastore.DatabaseConnection(filename=self.temp_file.name,
                                          file_path_abs=True) as db_con:

            key = 'myKey'
            val = 'myVal'
            db_con.store_key_val(app_id=self.app_id, app_secret=self.app_secret,
                                 key=key, val=val)

            value = db_con.get_key_val(app_id=self.app_id,
                                       app_secret=self.app_secret, key='myKey')
            self.assertEqual(val, value)
    def test_get_random_iv(self):
        """Retrieve random iv used to encrypt value in key-value store"""
        with datastore.DatabaseConnection(filename=self.temp_file.name,
                                          file_path_abs=True) as db_con:

            key = 'myKey'
            val = 'myVal'
            db_con.store_key_val(app_id=self.app_id, app_secret=self.app_secret,
                                 key=key, val=val)
            hashed_key = blake2.blake2(data=key, hashSize=64, key=self.app_secret)
            iv = db_con.get_iv(app_id=self.app_id, hashed_key=hashed_key)
            self.assertTrue(isinstance(iv, str))
            common.b64decode(iv) #raises TypeError
    def test_pop_removes_one_notif(self):
        """Popping a notification should cause it to be removed."""
        with datastore.DatabaseConnection(
            filename=self.temp_file.name, file_path_abs=True) as db_con:

            self.assertEqual(db_con.get_queue_size(), 0)
            nr1 = get_dummy_email_notif_req()
            nr2 = get_dummy_email_notif_req()
            db_con.add_notification(nr1)
            self.assertEqual(db_con.get_queue_size(), 1)
            db_con.add_notification(nr2)
            self.assertEqual(db_con.get_queue_size(), 2)
            db_con.pop_notif()
            self.assertEqual(db_con.get_queue_size(), 1)
    def test_val_unretrievable_bad_app_secret(self):
        """If the app secret is not correct, the value should be unretrievable."""
        with datastore.DatabaseConnection(filename=self.temp_file.name,
                                          file_path_abs=True) as db_con:

            key = 'myKey'
            val = 'myVal'
            bad_app_id = datastore.generate_app_id()
            db_con.store_key_val(app_id=self.app_id, app_secret=self.app_secret,
                                 key=key, val=val)

            with self.assertRaises(datastore.DecryptionFailError):
                db_con.get_key_val(
                    app_id=bad_app_id, app_secret=self.app_secret, key=key)
 def test_db_file_empty(self):
     """Initializes db if db file is initially empty."""
     self.assertEqual(os.stat(self.temp_file.name).st_size, 0)
     datastore.DatabaseConnection(filename=self.temp_file.name,
                                  file_path_abs=True)
     self.assertNotEqual(os.stat(self.temp_file.name).st_size, 0)