import datetime
+from sqlalchemy import desc
from sirius.coding import bitshuffle
-
from sirius.models.db import db
# Connect hardware xor and printer if there is a claim code
# waiting.
- claim_code_query = ClaimCode.query.filter_by(hardware_xor=hardware_xor)
+ #
+ # Printers always generate the same XOR. I.e. we can have more
+ # than one claim code with the same XOR. We always pick the
+ # newest claim code.
+ claim_code_query = ClaimCode.query.filter_by(
+ hardware_xor=hardware_xor).order_by(desc('created'), desc('id'))
claim_code = claim_code_query.first()
if claim_code is None:
return
- assert claim_code_query.count() == 1, \
- "claim code hardware xor collision: {}".format(hardware_xor)
-
printer.owner_id = claim_code.by_id
printer.name = claim_code.name
printer.used_claim_code = claim_code.claim_code
self.assertEqual(printer.owner, self.testuser)
+ def test_two_claims(self):
+ "We expect the newer claim to beat the older claim."
+ self.testuser2 = user.User(username="testuser 2")
+ db.session.add(self.testuser2)
+ db.session.commit()
+
+ self.testuser.claim_printer('n5ry-p6x6-kth7-7hc4', 'my test printer')
+ self.testuser2.claim_printer('n5ry-p6x6-kth7-7hc4', 'my test printer 2')
+
+ hardware.Printer.phone_home('000d6f000273ce0b')
+ db.session.commit()
+ printer = hardware.Printer.query.first()
+
+ self.assertEqual(printer.owner, self.testuser2)
+
class TestClaimingWeb(Base):