Пример #1
0
    def test_insert_select_operations(self):

        # Initialize our db instance
        db = Obdb()

        # Create a dictionary of a random review
        review_to_store = {"pubKey":"123", \
                            "subject":"A review", \
                            "signature": "a signature", \
                            "text": "Very happy to be a customer.", \
                            "rating": 10 }

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", "pubkey = '123'")

        # The above statement will return a list with all the retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(review_to_store["pubKey"], retrieved_review["pubKey"],
                         "123")
        self.assertEqual(review_to_store["subject"],
                         retrieved_review["subject"], "A review")
        self.assertEqual(review_to_store["signature"],
                         retrieved_review["signature"], "a signature")
        self.assertEqual(review_to_store["text"], retrieved_review["text"],
                         "Very happy to be a customer.")
        self.assertEqual(review_to_store["rating"], retrieved_review["rating"],
                         10)
Пример #2
0
    def test_insert_select_operations(self):

        # Initialize our db instance
        db = Obdb()

        # Create a dictionary of a random review
        review_to_store = {"pubKey":"123", \
                            "subject":"A review", \
                            "signature": "a signature", \
                            "text": "Very happy to be a customer.", \
                            "rating": 10 }

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", {"pubkey":"123"})

        # The above statement will return a list with all the retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(review_to_store["pubKey"], retrieved_review["pubKey"], "123")
        self.assertEqual(review_to_store["subject"], retrieved_review["subject"], "A review")
        self.assertEqual(review_to_store["signature"], retrieved_review["signature"], "a signature")
        self.assertEqual(review_to_store["text"], retrieved_review["text"], "Very happy to be a customer.")
        self.assertEqual(review_to_store["rating"], retrieved_review["rating"], 10)
Пример #3
0
 def test_get_or_create_record_when_not_exists(self):
     db = Obdb(TEST_DB_PATH)
     record = {"city": "Zurich"}
     table = "settings"
     retrieved_record = db.getOrCreate(table, record)
     self.assertEqual(retrieved_record["city"], record["city"])
     # check that the missing fields were created as empty
     self.assertEqual(retrieved_record["countryCode"], "")
Пример #4
0
 def test_get_or_create_record_when_not_exists(self):
     db = Obdb(TEST_DB_PATH)
     record = {"city": "Zurich"}
     table = "settings"
     retrieved_record = db.getOrCreate(table, record)
     self.assertEqual(retrieved_record["city"], record["city"])
     # check that the missing fields were created as empty
     self.assertEqual(retrieved_record["countryCode"], "")
Пример #5
0
    def test_delete_operation(self):

        # Initialize our db instance
        db = Obdb(TEST_DB_PATH)

        # Delete the entry with pubkey equal to '123'
        db.deleteEntries("reviews", {"pubkey": "123"})

        # Looking for this record with will bring nothing
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})
        self.assertEqual(len(retrieved_review), 0)
Пример #6
0
    def test_delete_operation(self):

        # Initialize our db instance
        db = Obdb(TEST_DB_PATH)

        # Delete the entry with pubkey equal to '123'
        db.deleteEntries("reviews", {"pubkey": "123"})

        # Looking for this record with will bring nothing
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})
        self.assertEqual(len(retrieved_review), 0)
Пример #7
0
    def __init__(self, ob_ctx):
        self.shutdown_mutex = Lock()
        self.ob_ctx = ob_ctx
        self.loop = tornado.ioloop.IOLoop.instance()
        db_connection = Obdb(ob_ctx.db_path, ob_ctx.disable_sqlite_crypt)
        self.transport = CryptoTransportLayer(ob_ctx, db_connection)
        self.market = Market(self.transport, db_connection)
        self.upnp_mapper = None

        Thread(target=reactor.run, args=(False, )).start()

        # Mediator is used to route messages between NAT'd peers
        #self.mediator = Mediator(self.ob_ctx.http_ip, self.ob_ctx.mediator_port)

        peers = ob_ctx.seeds if not ob_ctx.seed_mode else []
        self.transport.join_network(peers)

        handlers = [(r"/", MainHandler), (r"/main", MainHandler),
                    (r"/html/(.*)", OpenBazaarStaticHandler, {
                        'path': './html'
                    }),
                    (r"/ws", WebSocketHandler, {
                        'transport': self.transport,
                        'market_application': self,
                        'db_connection': db_connection
                    })]

        # TODO: Move debug settings to configuration location
        settings = dict(debug=True)
        super(MarketApplication, self).__init__(handlers, **settings)
def create_layers(context, num_layers):
    layers = []
    for i in range(num_layers):
        db_path = get_db_path(i)
        setup_db(db_path)
        # dev_mode is True because otherwise the layer's ip is set to the
        # public ip of the node
        layers.append(CryptoTransportLayer(ip_address(i), port, i,
                                           Obdb(db_path), dev_mode=True))
    context.layers = layers
Пример #9
0
    def test_update_operation(self):

        # Initialize our db instance
        db = Obdb()

        # Retrieve the record with pubkey equal to '123'
        retrieved_review = db.selectEntries("reviews", {"pubkey":"123"})[0]

        # Check that the rating is still '10' as expected
        self.assertEqual(retrieved_review["rating"], 10)

        # Update the record with pubkey equal to '123' and lower its rating to 9
        db.updateEntries("reviews", {"pubkey":"123"}, {"rating": 9})

        # Retrieve the same record again
        retrieved_review = db.selectEntries("reviews", {"pubkey":"123"})[0]

        # Test that the rating has been updated succesfully
        self.assertEqual(retrieved_review["rating"], 9)
Пример #10
0
def create_layers(context, num_layers):
    layers = []

    for i in range(num_layers):
        # dev_mode is True because otherwise the layer's ip is set to the
        # public ip of the node
        ob_ctx = OpenBazaarContext.create_default_instance()
        ob_ctx.dev_mode = True
        ob_ctx.server_ip = ip_address(i)
        ob_ctx.server_port = PORT
        db_path = get_db_path(i)
        setup_db(db_path, ob_ctx.disable_sqlite_crypt)
        layers.append(CryptoTransportLayer(ob_ctx, Obdb(db_path)))
    context.layers = layers
Пример #11
0
    def test_update_operation(self):

        # Initialize our db instance
        db = Obdb()

        # Retrieve the record with pubkey equal to '123'
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})[0]

        # Check that the rating is still '10' as expected
        self.assertEqual(retrieved_review["rating"], 10)

        # Update the record with pubkey equal to '123' and lower its rating to 9
        db.updateEntries("reviews", {"pubkey": "123"}, {"rating": 9})

        # Retrieve the same record again
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})[0]

        # Test that the rating has been updated succesfully
        self.assertEqual(retrieved_review["rating"], 9)
Пример #12
0
    def test_insert_select_operations(self):

        # Initialize our db instance
        db = Obdb(TEST_DB_PATH)

        # Create a dictionary of a random review
        review_to_store = {"pubKey": "123",
                           "subject": "A review",
                           "signature": "a signature",
                           "text": "Very happy to be a customer.",
                           "rating": 10}

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})

        # The above statement will return a list with all the
        # retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(
            review_to_store["pubKey"],
            retrieved_review["pubKey"],
        )
        self.assertEqual(
            review_to_store["subject"],
            retrieved_review["subject"],
        )
        self.assertEqual(
            review_to_store["signature"],
            retrieved_review["signature"],
        )
        self.assertEqual(
            review_to_store["text"],
            retrieved_review["text"],
        )
        self.assertEqual(
            review_to_store["rating"],
            retrieved_review["rating"],
        )

        # Let's do it again with a malicious review.
        review_to_store = {"pubKey": "321",
                           "subject": "Devil''''s review",
                           "signature": "quotes\"\"\"\'\'\'",
                           "text": 'Very """"happy"""""" to be a customer.',
                           "rating": 10}

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", {"pubkey": "321"})

        # The above statement will return a list with all the
        # retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(
            review_to_store["pubKey"],
            retrieved_review["pubKey"],
        )
        self.assertEqual(
            review_to_store["subject"],
            retrieved_review["subject"],
        )
        self.assertEqual(
            review_to_store["signature"],
            retrieved_review["signature"],
        )
        self.assertEqual(
            review_to_store["text"],
            retrieved_review["text"],
        )
        self.assertEqual(
            review_to_store["rating"],
            retrieved_review["rating"],
        )

        # By ommiting the second parameter, we are retrieving all reviews
        all_reviews = db.selectEntries("reviews")
        self.assertEqual(len(all_reviews), 2)
Пример #13
0
    def test_insert_select_operations(self):

        # Initialize our db instance
        db = Obdb(TEST_DB_PATH)

        # Create a dictionary of a random review
        review_to_store = {
            "pubKey": "123",
            "subject": "A review",
            "signature": "a signature",
            "text": "Very happy to be a customer.",
            "rating": 10
        }

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", {"pubkey": "123"})

        # The above statement will return a list with all the
        # retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(
            review_to_store["pubKey"],
            retrieved_review["pubKey"],
        )
        self.assertEqual(
            review_to_store["subject"],
            retrieved_review["subject"],
        )
        self.assertEqual(
            review_to_store["signature"],
            retrieved_review["signature"],
        )
        self.assertEqual(
            review_to_store["text"],
            retrieved_review["text"],
        )
        self.assertEqual(
            review_to_store["rating"],
            retrieved_review["rating"],
        )

        # Let's do it again with a malicious review.
        review_to_store = {
            "pubKey": "321",
            "subject": "Devil''''s review",
            "signature": "quotes\"\"\"\'\'\'",
            "text": 'Very """"happy"""""" to be a customer.',
            "rating": 10
        }

        # Use the insert operation to add it to the db
        db.insertEntry("reviews", review_to_store)

        # Try to retrieve the record we just added based on the pubkey
        retrieved_review = db.selectEntries("reviews", {"pubkey": "321"})

        # The above statement will return a list with all the
        # retrieved records as dictionaries
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]

        # Is the retrieved record the same as the one we added before?
        self.assertEqual(
            review_to_store["pubKey"],
            retrieved_review["pubKey"],
        )
        self.assertEqual(
            review_to_store["subject"],
            retrieved_review["subject"],
        )
        self.assertEqual(
            review_to_store["signature"],
            retrieved_review["signature"],
        )
        self.assertEqual(
            review_to_store["text"],
            retrieved_review["text"],
        )
        self.assertEqual(
            review_to_store["rating"],
            retrieved_review["rating"],
        )

        # By ommiting the second parameter, we are retrieving all reviews
        all_reviews = db.selectEntries("reviews")
        self.assertEqual(len(all_reviews), 2)

        # Use the <> operator. This should return the review with pubKey 123.
        retrieved_review = db.selectEntries(
            "reviews", {"pubkey": {
                "value": "321",
                "sign": "<>"
            }})
        self.assertEqual(len(retrieved_review), 1)
        retrieved_review = retrieved_review[0]
        self.assertEqual(retrieved_review["pubKey"], "123")
Пример #14
0
def remove_peers_from_db(i):
    Obdb(get_db_path(i)).deleteEntries('peers')