]> git.aero2k.de Git - sirius.git/commitdiff
Correctly handle two different users claiming the same XOR.
authorTom Hunger <tehunger@gmail.com>
Fri, 23 Jan 2015 18:42:21 +0000 (18:42 +0000)
committerTom Hunger <tehunger@gmail.com>
Fri, 23 Jan 2015 18:42:21 +0000 (18:42 +0000)
sirius/models/hardware.py
sirius/web/test_claiming.py

index 2ba0f04a7bfe2b4e9b007aeae0065dee6f64c594..cb42995357fbb4354623a89868aef633b285f540 100644 (file)
@@ -1,7 +1,7 @@
 import datetime
+from sqlalchemy import desc
 
 from sirius.coding import bitshuffle
-
 from sirius.models.db import db
 
 
@@ -64,14 +64,16 @@ class Printer(db.Model):
 
         # 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
index 5b4310316d9b0a8b7a4025ba25dfef57d6b72175..790ffc0913513fc181e0b6f3ad15b4bf65c78152 100644 (file)
@@ -56,6 +56,21 @@ class TestClaiming(Base):
 
         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):