/test_info.py

https://github.com/mpessas/pimme · Python · 134 lines · 104 code · 29 blank · 1 comment · 4 complexity · 611aa497cd50520a342bf6c6ca0cde93 MD5 · raw file

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import unittest
  4. import tempfile
  5. import json
  6. from Crypto.Cipher import AES, Blowfish
  7. import secret_key
  8. import settings
  9. settings.read_settings(None)
  10. settings.get_key = secret_key.get_key_dummy
  11. settings.data_file = '/tmp/rm.txt'
  12. import crypto
  13. import info
  14. class TestEncryption(unittest.TestCase):
  15. def test_blowfish_encryption(self):
  16. settings.CipherAlgorithm = Blowfish
  17. settings.IV = '01234567'
  18. class Test(object):
  19. enc = crypto.EncryptedDescriptor()
  20. t = Test()
  21. data = u'1234567890'
  22. t.enc = data
  23. self.assertEqual(t.enc, data)
  24. def test_aes_encryption(self):
  25. settings.CipherAlgorithm = AES
  26. settings.IV = '0123456789abcdef'
  27. class Test(object):
  28. enc = crypto.EncryptedDescriptor()
  29. t = Test()
  30. data = u'1234567890'
  31. t.enc = data
  32. self.assertEqual(t.enc, data)
  33. class TestInfoItem(unittest.TestCase):
  34. def setUp(self):
  35. self.value = u'value'
  36. def test_enc_getter(self):
  37. c = crypto.Cipher(secret_key.get_key_dummy())
  38. e = c.encrypt(self.value)
  39. i = info.InfoItem(u'name', e)
  40. self.assertEqual(self.value, i.value)
  41. def test_enc_setter(self):
  42. c = crypto.Cipher(secret_key.get_key_dummy())
  43. e = c.encrypt(u'dummy')
  44. i = info.InfoItem(u'name', e)
  45. i.value = self.value
  46. self.assertEqual(self.value, i.value)
  47. class TestJSON(unittest.TestCase):
  48. def setUp(self):
  49. value = u'value'
  50. c = crypto.Cipher(secret_key.get_key_dummy())
  51. e = c.encrypt(value)
  52. self.item = info.InfoItem(u'name', e)
  53. def test_encoding(self):
  54. json_msg = json.dumps(self.item, cls=info.InfoItemEncoder)
  55. msg = json.loads(json_msg, object_hook=info.infoitem_decoder)
  56. self.assertEqual(msg, self.item)
  57. class TestInfoCollection(unittest.TestCase):
  58. def setUp(self):
  59. self.col = info.InfoCollection()
  60. self.item = info.InfoItem('name', None)
  61. self.item.value = 'value'
  62. self.item_with_tags = info.InfoItem('tagged')
  63. self.item_with_tags.value = '12345667dsds'
  64. self.item_with_tags.tags = set(['bank', 'password', 'travel'])
  65. def tearDown(self):
  66. if os.path.exists(settings.data_file):
  67. os.unlink(settings.data_file)
  68. def test_add(self):
  69. self.col.load()
  70. self.col.add(self.item)
  71. self.assertEqual(len(self.col), 1)
  72. def test_getitem(self):
  73. self.col.load()
  74. self.col.add(self.item)
  75. self.assertEqual(self.col[self.item.name], self.item)
  76. def test_load(self):
  77. item_in_json = json.dumps(self.item, cls=info.InfoItemEncoder)
  78. with open(settings.data_file, 'w') as f:
  79. f.write(item_in_json + '\n')
  80. self.col.load()
  81. self.assertEqual(self.col[self.item.name], self.item)
  82. def test_save(self):
  83. self.col.load()
  84. self.col.add(self.item)
  85. self.col.save()
  86. item_in_json = json.dumps(self.item, cls=info.InfoItemEncoder)
  87. with open(settings.data_file) as f:
  88. line = f.readline()
  89. line = line[:-1]
  90. self.assertEqual(line, item_in_json)
  91. def test_search(self):
  92. self.col.load()
  93. item = info.InfoItem('account')
  94. item.value = '0x1234 09909 213243'
  95. item.tags = set(['bank'])
  96. self.col.add(item)
  97. self.col.add(self.item)
  98. self.col.add(self.item_with_tags)
  99. banked = list(self.col.search('bank'))
  100. self.assertEqual(len(banked), 2)
  101. self.assertTrue(banked[0].has_tag('bank'))
  102. self.assertTrue(banked[1].has_tag('bank'))
  103. self.assertNotEqual(banked[0], banked[1])
  104. if __name__ == '__main__':
  105. unittest.main()