Python 3 library and CLI application for SporeStack
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.3 KiB

  1. import string
  2. import pytest
  3. from . import validate
  4. valid_id = "01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
  5. invalid_id = "z1ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b"
  6. def test_machine_id():
  7. assert validate.machine_id(valid_id) is True
  8. with pytest.raises(TypeError):
  9. validate.machine_id(1337)
  10. with pytest.raises(ValueError):
  11. validate.machine_id(valid_id + "b")
  12. with pytest.raises(ValueError):
  13. validate.machine_id(invalid_id)
  14. def test_settlement_token():
  15. assert validate.settlement_token(valid_id) is True
  16. with pytest.raises(TypeError):
  17. validate.settlement_token(1337)
  18. with pytest.raises(ValueError):
  19. validate.settlement_token(valid_id + "b")
  20. with pytest.raises(ValueError):
  21. validate.settlement_token(invalid_id)
  22. def test_days():
  23. with pytest.raises(ValueError):
  24. validate.days(0)
  25. assert validate.days(1) is True
  26. assert validate.days(28) is True
  27. assert validate.days(0, zero_allowed=True) is True
  28. assert validate.days(1, zero_allowed=True) is True
  29. assert validate.days(28, zero_allowed=True) is True
  30. with pytest.raises(ValueError):
  31. validate.days(29)
  32. with pytest.raises(ValueError):
  33. validate.days(29, zero_allowed=True)
  34. with pytest.raises(ValueError):
  35. validate.days(-1)
  36. with pytest.raises(ValueError):
  37. validate.days(-1, zero_allowed=True)
  38. with pytest.raises(TypeError):
  39. validate.days("one")
  40. def test_memory():
  41. assert validate.memory(1) is True
  42. assert validate.memory(2) is True
  43. with pytest.raises(TypeError):
  44. validate.memory(-1)
  45. with pytest.raises(ValueError):
  46. validate.memory(0)
  47. def test_disk():
  48. assert validate.disk(10) is True
  49. assert validate.disk(1) is True
  50. assert validate.disk(0) is True
  51. with pytest.raises(TypeError):
  52. validate.disk(-10)
  53. with pytest.raises(TypeError):
  54. validate.disk("10")
  55. def test_unsigned_int():
  56. assert validate.unsigned_int(0) is True
  57. assert validate.unsigned_int(1) is True
  58. assert validate.unsigned_int(1000) is True
  59. assert validate.unsigned_int(-1) is False
  60. assert validate.unsigned_int(1.0) is False
  61. assert validate.unsigned_int("a") is False
  62. assert validate.unsigned_int(None) is False
  63. def test_bandwidth():
  64. assert validate.bandwidth(-1) is True
  65. assert validate.bandwidth(1) is True
  66. assert validate.bandwidth(0) is True
  67. assert validate.bandwidth(10) is True
  68. assert validate.bandwidth(1000000) is True
  69. with pytest.raises(ValueError):
  70. validate.bandwidth(-2)
  71. with pytest.raises(TypeError):
  72. validate.bandwidth(1.0)
  73. def test_cents():
  74. assert validate.cents(1) is True
  75. assert validate.cents(0) is True
  76. assert validate.cents(10) is True
  77. assert validate.cents(1000000) is True
  78. with pytest.raises(TypeError):
  79. validate.cents(-1)
  80. with pytest.raises(TypeError):
  81. validate.cents("a")
  82. with pytest.raises(TypeError):
  83. validate.cents(None)
  84. def test_further_ipv4_ipv6():
  85. assert validate.further_ipv4_ipv6("tor", "tor") is True
  86. assert validate.further_ipv4_ipv6("nat", "nat") is True
  87. with pytest.raises(ValueError):
  88. validate.further_ipv4_ipv6("tor", "nat")
  89. with pytest.raises(ValueError):
  90. validate.further_ipv4_ipv6(False, "nat")
  91. with pytest.raises(ValueError):
  92. validate.further_ipv4_ipv6("tor", False)
  93. with pytest.raises(ValueError):
  94. validate.further_ipv4_ipv6("tor", "/128")
  95. with pytest.raises(ValueError):
  96. validate.further_ipv4_ipv6("/32", "tor")
  97. def test_further_dollars_cents():
  98. assert validate._further_dollars_cents(10, None) is True
  99. assert validate._further_dollars_cents(None, 1000) is True
  100. with pytest.raises(ValueError):
  101. validate._further_dollars_cents(10, 10)
  102. with pytest.raises(ValueError):
  103. validate._further_dollars_cents(None, None)
  104. def test_organization():
  105. assert validate.organization(None) is True
  106. assert validate.organization("Corporation") is True
  107. assert validate.organization("google") is True
  108. assert validate.organization("Yahoo") is True
  109. # Shortest valid.
  110. assert validate.organization("A") is True
  111. # Longest valid
  112. valid = validate.organization(string.ascii_lowercase[:16])
  113. assert valid is True
  114. with pytest.raises(TypeError):
  115. validate.organization(False)
  116. with pytest.raises(TypeError):
  117. validate.organization(1234)
  118. # Too long
  119. with pytest.raises(ValueError):
  120. validate.organization(string.ascii_lowercase[:17])
  121. # Too short
  122. with pytest.raises(ValueError):
  123. validate.organization("")
  124. # Bad character
  125. with pytest.raises(ValueError):
  126. validate.organization("Google5")
  127. def test_operating_system():
  128. assert validate.operating_system("debian-9") is True
  129. assert validate.operating_system("debian-10") is True
  130. assert validate.operating_system("ubuntu-16-04") is True
  131. assert validate.operating_system("something-else") is True
  132. assert validate.operating_system(None) is True
  133. # Shortest valid.
  134. assert validate.operating_system("a") is True
  135. # Longest valid
  136. valid = validate.operating_system(string.ascii_lowercase[:16])
  137. assert valid is True
  138. with pytest.raises(TypeError):
  139. validate.operating_system(1)
  140. with pytest.raises(TypeError):
  141. validate.operating_system(0)
  142. # Too short.
  143. with pytest.raises(ValueError):
  144. validate.operating_system("")
  145. # One too long.
  146. with pytest.raises(ValueError):
  147. validate.operating_system(string.ascii_lowercase[:17])
  148. # Bad character.
  149. with pytest.raises(ValueError):
  150. validate.operating_system("_")
  151. def test_flavor():
  152. assert validate.flavor("debian-9") is True
  153. assert validate.flavor("debian-10") is True
  154. assert validate.flavor("ubuntu-16-04") is True
  155. assert validate.flavor("something-else") is True
  156. assert validate.flavor(None) is True
  157. # Shortest valid.
  158. assert validate.flavor("a") is True
  159. # Longest valid
  160. valid = validate.flavor(string.ascii_lowercase[:16])
  161. assert valid is True
  162. with pytest.raises(TypeError):
  163. validate.flavor(1)
  164. with pytest.raises(TypeError):
  165. validate.flavor(0)
  166. # Too short.
  167. with pytest.raises(ValueError):
  168. validate.flavor("")
  169. # One too long.
  170. with pytest.raises(ValueError):
  171. validate.flavor(string.ascii_lowercase[:17])
  172. # Bad character.
  173. with pytest.raises(ValueError):
  174. validate.flavor("_")
  175. valid_ssh_key = (
  176. "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDLuFASoTgo5r/bBGcawcN"
  177. "7B/NTyjmEi3cdgl8r3ldZXVXl6N/7vfF/O6ggkU1iJCHUgxOqGHzJMyJ3ZL"
  178. "6osyhgMF0htWYH7LhS4lJkzayUpCizelvW6FS//00smjxPnvOicEQNuuP0i"
  179. "XYZVZIzbubZn8fJi0ZoJ5LkTpqNdAx1M420cplFGlcMww/jk0gGvlQZnEV4"
  180. "Qra0Wh88s3xzPeryAoi+CwVAkBqHfntgkAVMComCfah8D+7nCS4F+Wi70hp"
  181. "wrUhKulm3r5sOsyU1fGduvyL8XBYH8+yHwe5H+5TK5kJ6gAmyjNQ8fw+6Wk"
  182. "wcB/heHAAJSUysYyfatIqKeWsj"
  183. )
  184. valid_ssh_key_with_comment = valid_ssh_key + " root@localhost"
  185. def test_ssh_key():
  186. assert validate.ssh_key(valid_ssh_key) is True
  187. assert validate.ssh_key(valid_ssh_key_with_comment) is True
  188. assert validate.ssh_key(None) is True
  189. with pytest.raises(TypeError):
  190. validate.ssh_key(1)
  191. with pytest.raises(ValueError):
  192. validate.ssh_key("")
  193. with pytest.raises(ValueError):
  194. validate.ssh_key("ssh-rsa")
  195. def test_ipxescript():
  196. assert validate.ipxescript(None) is True
  197. assert validate.ipxescript("#!ipxe") is True
  198. with pytest.raises(TypeError):
  199. validate.ipxescript(1)
  200. with pytest.raises(TypeError):
  201. validate.ipxescript(True)
  202. with pytest.raises(ValueError):
  203. validate.ipxescript("")
  204. def test_region():
  205. assert validate.region(None) is True
  206. assert validate.region("#!ipxe") is True
  207. with pytest.raises(TypeError):
  208. validate.region(1)
  209. with pytest.raises(TypeError):
  210. validate.region(True)
  211. with pytest.raises(ValueError):
  212. validate.region("")
  213. def test_affiliate_amount():
  214. assert validate.affiliate_amount(None) is True
  215. assert validate.affiliate_amount(1) is True
  216. assert validate.affiliate_amount(1000) is True
  217. with pytest.raises(TypeError):
  218. validate.affiliate_amount(0)
  219. with pytest.raises(TypeError):
  220. validate.affiliate_amount(-1)
  221. with pytest.raises(TypeError):
  222. validate.affiliate_amount("string")