def claim_printer(self, claim_code, name): """Claiming can happen before the printer "calls home" for the first time so we need to be able to deal with that.""" # TODO(tom): This method probably belongs to printer, not # user. Move at some point. claim_code = claiming.canonicalize(claim_code) hcc = hardware.ClaimCode.query.filter_by(claim_code=claim_code).first() hardware_xor, _ = claiming.process_claim_code(claim_code) if hcc is not None and hcc.by != self: raise ClaimCodeInUse( "Claim code {} already claimed by {}".format( claim_code, hcc.by)) if hcc is None: hcc = hardware.ClaimCode( by_id=self.id, hardware_xor=hardware_xor, claim_code=claim_code, name=name, ) db.session.add(hcc) else: # we already have a claim code, don't do anything. pass # Check whether we've seen this printer and if so: connect it # to claim code and make it "owned" but *only* if it does not # have an owner yet. printer_query = hardware.Printer.query.filter_by( hardware_xor=hardware_xor) printer = printer_query.first() if printer is None: return if printer.owner is not None and printer.owner != self: raise CannotChangeOwner( "Printer {} already owned by {}. Cannot claim for {}.".format( printer, printer.owner, self)) assert printer_query.count() == 1, \ "hardware xor collision: {}".format(hardware_xor) printer.used_claim_code = claim_code printer.hardware_xor = hardware_xor printer.owner_id = hcc.by_id printer.name = name db.session.add(printer) return printer
def test_decoding(self): claim_code = '6xwh-441j-8115-zyrh' expected_encryption_key = b'F7D9bmztHV32+WJScGZR0g==' _, calculated_encryption_key = claiming.process_claim_code(claim_code) self.assertEqual(expected_encryption_key, calculated_encryption_key)
def test_decoding(self): claim_code = '6xwh-441j-8115-zyrh' expected_encryption_key = b'F7D9bmztHV32+WJScGZR0g==\n' _, calculated_encryption_key = claiming.process_claim_code(claim_code) self.assertEqual(expected_encryption_key, calculated_encryption_key)