test_from_file.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. """
  2. Unit test for radicale.rights.from_file.
  3. Tests reading the file. The processing is untested, yet.
  4. """
  5. from radicale.rights import from_file
  6. import unittest
  7. class Test1(unittest.TestCase):
  8. def testProcessEmptyLine(self):
  9. """ Line with a comment """
  10. # Input values
  11. line = " "
  12. read = {}
  13. write = {}
  14. try:
  15. # Call SUT
  16. from_file._process(line, read, write)
  17. except from_file.ParsingError:
  18. self.assertTrue(False)
  19. self.assertTrue(len(read.keys()) == 0)
  20. self.assertTrue(len(write.keys()) == 0)
  21. def testProcessComment(self):
  22. """ Line with a comment """
  23. # Input values
  24. line = "# some comment"
  25. read = {}
  26. write = {}
  27. try:
  28. # Call SUT
  29. from_file._process(line, read, write)
  30. except from_file.ParsingError:
  31. self.assertTrue(False)
  32. self.assertTrue(len(read.keys()) == 0)
  33. self.assertTrue(len(write.keys()) == 0)
  34. def testProcess0a(self):
  35. """ Pointless line: no rights definitions """
  36. # Input values
  37. line = "/user1/collection1 : "
  38. read = {}
  39. write = {}
  40. try:
  41. # Call SUT
  42. from_file._process(line, read, write)
  43. except from_file.ParsingError:
  44. self.fail("Unexpected exception")
  45. self.assertTrue(len(read.keys()) == 0)
  46. self.assertTrue(len(write.keys()) == 0)
  47. def testProcess1a(self):
  48. """ Malformed line: no collection definitions """
  49. # Input values
  50. line = " : a b"
  51. read = {}
  52. write = {}
  53. try:
  54. # Call SUT
  55. from_file._process(line, read, write)
  56. except from_file.ParsingError:
  57. """Exception expected"""
  58. else:
  59. self.fail("Expected exception not raised")
  60. def testProcess1b(self):
  61. """ Malformed line: right "b" unknown """
  62. # Input values
  63. line = "/user1/collection1 : a b"
  64. read = {}
  65. write = {}
  66. try:
  67. # Call SUT
  68. from_file._process(line, read, write)
  69. except from_file.ParsingError:
  70. """Exception expected"""
  71. else:
  72. self.fail("Expected exception not raised")
  73. def testProcess1c(self):
  74. """ Malformed line: user/right empty """
  75. # Input values
  76. line = "/user1/collection1 : a"
  77. read = {}
  78. write = {}
  79. try:
  80. # Call SUT
  81. from_file._process(line, read, write)
  82. except from_file.ParsingError:
  83. """Exception expected"""
  84. else:
  85. self.fail("Expected exception not raised")
  86. def testProcess2(self):
  87. """Actual sensible input all of which means the same"""
  88. lines = [
  89. "/user1/collection1 : other1 r, other2 w, other6 rw",
  90. "/user1/collection1/ : other1 r, other2 w, other6 rw",
  91. "/user1/collection1: other1 r, other2 w, other6 rw",
  92. "/user1/collection1/: other1 r, other2 w, other6 rw",
  93. "/user1/collection1: other1 r, other2 w,other6 rw",
  94. "/user1/collection1 :other1 r,other2 w, other6 rw",
  95. "/user1/collection1\t:other1 r,\tother2 w,\tother6 rw",
  96. ]
  97. for line in lines:
  98. # Input values
  99. read = {}
  100. write = {}
  101. try:
  102. # Call SUT
  103. from_file._process(line, read, write)
  104. except:
  105. self.fail("unexpected exception for input %s" % line)
  106. # Check
  107. self.assertEquals(len(read.keys()), 1, "keys in %s" % line)
  108. self.assertEquals(len(read.get("/user1/collection1")), 2, "rights in %s" % line)
  109. self.assertTrue(read.get("/user1/collection1").count("other1"), "other1 read in %s" % line)
  110. self.assertTrue(read.get("/user1/collection1").count("other6"), "other6 read in %s" % line)
  111. self.assertEquals(len(write.keys()), 1, "keys in %s" % line)
  112. self.assertEquals(len(write.get("/user1/collection1")), 2, "rights in %s" % line)
  113. self.assertTrue(write.get("/user1/collection1").count("other2"), "other2 write in %s" % line)
  114. self.assertTrue(write.get("/user1/collection1").count("other6"), "other6 write in %s" % line)
  115. if __name__ == "__main__":
  116. unittest.main()