def test_init(self): link = Link("keiffster", "ABCDEF123", "PASSWORD123", expired=False, expires="12/12/2012") link.id = "666" self.assertIsNotNone(link) self.assertEqual("666", link.id) self.assertEqual("keiffster", link.primary_user) self.assertEqual("ABCDEF123", link.generated_key) self.assertEqual("PASSWORD123", link.provided_key) self.assertEqual(False, link.expired) self.assertEqual("12/12/2012", link.expires) self.assertEqual( { '_id': '666', 'expired': False, 'expires': "12/12/2012", 'generated_key': 'ABCDEF123', 'primary_user': '******', 'provided_key': 'PASSWORD123' }, link.to_document())
def test_from_document(self): data1 = { "primary_user": "******", "generated_key": "ABCDEFG", "provided_key": "1234567890", "expired": False, "expires": "12/12/2012" } link1 = Link.from_document(data1) self.assertIsNotNone(link1) self.assertEqual("user1", link1.primary_user) self.assertEqual("ABCDEFG", link1.generated_key) self.assertEqual("1234567890", link1.provided_key) self.assertEqual(False, link1.expired) self.assertEqual("12/12/2012", link1.expires) data2 = { "_id": "666", "primary_user": "******", "generated_key": "ABCDEFG", "provided_key": "1234567890", "expired": False, "expires": "12/12/2012" } link2 = Link.from_document(data2) self.assertIsNotNone(link2) self.assertEqual("user1", link2.primary_user) self.assertEqual("ABCDEFG", link2.generated_key) self.assertEqual("1234567890", link2.provided_key) self.assertEqual(False, link2.expired) self.assertEqual("12/12/2012", link2.expires)
def test_init_no_id_no(self): link = Link("keiffster", "PASSWORD123", "ABCDEF123", expired=False, expires="12/12/2012") self.assertIsNotNone(link) self.assertIsNone(link.id) self.assertEqual("keiffster", link.primary_user) self.assertEqual("ABCDEF123", link.generated_key) self.assertEqual("PASSWORD123", link.provided_key) self.assertEqual(False, link.expired) self.assertEqual("12/12/2012", link.expires) self.assertEqual(0, link.retry_count) self.assertEqual( { 'expired': False, 'expires': "12/12/2012", 'generated_key': 'ABCDEF123', 'primary_user': '******', 'provided_key': 'PASSWORD123', 'retry_count': 0 }, link.to_document())
def patch_get_link(self, userid): link = Link("userid1", "abcdefg", "xxxxxxxxxx", expires=None, expired=True, retry_count=0) link.expired = True return link
def test_init_no_id_no_expiration(self): link = Link("keiffster", "PASSWORD123", "ABCDEF123") self.assertIsNotNone(link) self.assertIsNone(link.id) self.assertEqual("keiffster", link.primary_user) self.assertEqual("ABCDEF123", link.generated_key) self.assertEqual("PASSWORD123", link.provided_key) self.assertEqual({'expired': False, 'expires': None, 'generated_key': 'ABCDEF123', 'primary_user': '******', 'provided_key': 'PASSWORD123', 'retry_count': 0}, link.to_document())
def test_from_document_with_no_data(self): data2 = {"_id": "666"} link2 = Link.from_document(data2) self.assertIsNotNone(link2) self.assertIsNone(link2.primary_user) self.assertIsNone(link2.generated_key) self.assertIsNone(link2.provided_key) self.assertFalse(link2.expired) self.assertIsNone(link2.expires) self.assertEquals(0, link2.retry_count)
def test_from_document_with_id(self): data2 = {"_id": "666", "primary_user": "******", "generated_key": "ABCDEFG", "provided_key": "1234567890", "expired": False, "expires": "12/12/2012", 'retry_count': 0} link2 = Link.from_document(data2) self.assertIsNotNone(link2) self.assertEqual("user1", link2.primary_user) self.assertEqual("ABCDEFG", link2.generated_key) self.assertEqual("1234567890", link2.provided_key) self.assertEqual(False, link2.expired) self.assertEqual("12/12/2012", link2.expires) self.assertEqual(0, link2.retry_count)
def create_link(self, primary_userid, provided_key, generated_key, expires, expired=False, retry_count=0): YLogger.info(self, "Creating link in Mongo [%s] [%s] [%s]", primary_userid, generated_key, provided_key) link = Link(primary_userid, provided_key, generated_key, expires, expired, retry_count) return self.add_document(link)
def update_link(self, link): collection = self.collection() collection.update({'_id': link.id}, Link.to_document(link)) return True
def get_link(self, primary_user): collection = self.collection() doc = collection.find_one({MongoLinkStore.PRIMARY_USER: primary_user}) if doc is not None: return Link.from_document(doc) return None
def create_link(self, primary_userid, generated_key, provided_key): YLogger.info(self, "Creating link in Mongo [%s] [%s] [%s]", primary_userid, generated_key, provided_key) link = Link(primary_userid, generated_key, provided_key) self.add_document(link) return True
def update_link(self, link): collection = self.collection() result = collection.replace_one({'_id': link.id}, Link.to_document(link)) return bool(result.modified_count > 0)
def test_repr_wit_id(self): data2 = {"_id": "666", "primary_user": "******", "generated_key": "ABCDEFG", "provided_key": "1234567890", "expired": False, "expires": "12/12/2012", 'retry_count': 0} link2 = Link.from_document(data2) self.assertEquals("<Linked(id='666', primary='user1', generated='ABCDEFG', provided='1234567890', expired='12/12/2012', expires='False')>", str(link2))