test_bucketstore.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. """
  2. pytest the bucketstore functionality
  3. """
  4. import bucketstore
  5. import json
  6. import os
  7. import pytest
  8. import sys
  9. import tempfile
  10. from moto import mock_s3
  11. def dbg(text: str) -> None:
  12. """debug printer for tests"""
  13. if isinstance(text, dict):
  14. text = json.dumps(text, sort_keys=True, indent=2)
  15. caller = sys._getframe(1)
  16. print("")
  17. print("----- {} line {} ------".format(caller.f_code.co_name, caller.f_lineno))
  18. print(text)
  19. print("-----")
  20. print("")
  21. def test_login() -> None:
  22. """
  23. Ensure that login sets the correct environment variables.
  24. The ``login`` fixture sets these automatically.
  25. """
  26. assert os.environ["AWS_ACCESS_KEY_ID"] == "access_key"
  27. assert os.environ["AWS_SECRET_ACCESS_KEY"] == "secret_key"
  28. assert os.environ["AWS_DEFAULT_REGION"] == "us-east-1"
  29. @mock_s3
  30. def test_buckets_can_be_created() -> None:
  31. bucket = bucketstore.get("test-bucket", create=True)
  32. assert bucket.name == "test-bucket"
  33. assert not bucket.is_public # Buckets are private, by default.
  34. assert not bucket.all() # Buckets are empty, by default.
  35. assert "<S3Bucket" in repr(bucket)
  36. @mock_s3
  37. def test_buckets_are_not_created_automatically() -> None:
  38. with pytest.raises(ValueError):
  39. bucketstore.get("non-existent-bucket")
  40. def test_buckets_can_be_listed(bucket: bucketstore.S3Bucket) -> None:
  41. assert bucket.name in bucketstore.list()
  42. def test_buckets_can_be_deleted(bucket: bucketstore.S3Bucket) -> None:
  43. bucket["foo"] = "bar"
  44. bucket.delete()
  45. # Catching an overly generic exception because boto uses factories to
  46. # create the exception raised here and thus, isn't importable.
  47. with pytest.raises(Exception):
  48. bucket.all()
  49. def test_buckets_can_be_made_public(bucket: bucketstore.S3Bucket) -> None:
  50. assert not bucket.is_public
  51. bucket.make_public()
  52. assert bucket.is_public
  53. def test_buckets_can_set_keys(bucket: bucketstore.S3Bucket) -> None:
  54. # Buckets can set keys with a function
  55. bucket.set("foo", "bar")
  56. assert bucket.get("foo") == b"bar"
  57. # Keys can also be set via index
  58. bucket["foo2"] = "bar2"
  59. assert bucket["foo2"] == b"bar2"
  60. def test_keys_can_be_renamed(bucket: bucketstore.S3Bucket) -> None:
  61. """test renaming keys"""
  62. bucket.set("original_name", "value")
  63. bucket.key("original_name").rename("new_name")
  64. assert bucket["new_name"] == b"value"
  65. # check that the original was removed
  66. assert "original_name" not in bucket
  67. def test_keys_can_be_deleted(bucket: bucketstore.S3Bucket) -> None:
  68. """test deleting a key"""
  69. bucket["foo"] = "bar"
  70. bucket.delete("foo")
  71. assert not bucket.all()
  72. def test_keys_can_be_made_public(key: bucketstore.S3Key) -> None:
  73. """make sure keys are private by default, but able to be public"""
  74. # Keys are private by default.
  75. assert not key.is_public
  76. # But they can be made public.
  77. key.make_public()
  78. assert key.is_public
  79. def test_keys_can_be_linked_to(key: bucketstore.S3Key) -> None:
  80. """test that we can link to keys using temp_url and url on public keys"""
  81. # A public link is going to fail because it's private.
  82. with pytest.raises(ValueError):
  83. assert key.url
  84. # A temp link can be generated for private keys.
  85. temp_url = key.temp_url()
  86. assert "http" in temp_url
  87. assert "Expires" in temp_url
  88. assert "Signature" in temp_url
  89. assert key.name in temp_url
  90. # Once it is made public, a URL can be derived from it's elements.
  91. key.make_public()
  92. assert "http" in key.url
  93. assert key.name in key.url
  94. # empty response for trying to make a public key public
  95. assert key.make_public() == {}
  96. def test_keys_have_metadata(key: bucketstore.S3Key) -> None:
  97. """Metadata is empty by default"""
  98. assert key.meta == {}
  99. metadata = {"foo": "bar"}
  100. key.meta = metadata
  101. assert key.meta == metadata
  102. def test_keys_have_a_cool_repr(key: bucketstore.S3Key) -> None:
  103. """The textual representation of the class is nifty, so test it."""
  104. rep = repr(key)
  105. assert "S3Key" in rep
  106. assert key.name in rep
  107. assert key.bucket.name in rep
  108. def test_private_methods(key: bucketstore.S3Key) -> None:
  109. """This method contains boto internals, so as long as it returns a truthy value, it is good."""
  110. assert key._boto_object
  111. def test_bucket_keys_can_be_iterated_upon(bucket: bucketstore.S3Bucket) -> None:
  112. """Create 10 keys"""
  113. for index in range(10):
  114. bucket[str(index)] = str(index)
  115. keys = bucket.all()
  116. assert len(keys) == 10
  117. for index, key in enumerate(keys):
  118. assert key.name == str(index)
  119. def test_bucket_in_operator(bucket: bucketstore.S3Bucket) -> None:
  120. """test that we can use the `in` operator on a bucket"""
  121. for index in range(10):
  122. bucket[str(index)] = str(index)
  123. assert "0" in bucket
  124. assert "10" not in bucket
  125. def test_bucket_del_operator(bucket: bucketstore.S3Bucket) -> None:
  126. """test that we can use the `del` operator on a bucket + key"""
  127. for index in range(10):
  128. bucket[str(index)] = str(index)
  129. assert "0" in bucket
  130. del bucket["0"]
  131. assert "0" not in bucket
  132. def test_key_upload(bucket: bucketstore.S3Bucket) -> None:
  133. """test that we can use the key upload function"""
  134. path = "test_upload_file_path"
  135. # test not a file
  136. with pytest.raises(Exception):
  137. bucket.key(path).upload("/this/is/not/a/file")
  138. # test uploading from a file
  139. bucket.key(path).upload("LICENSE")
  140. assert path in bucket
  141. assert "MIT License" in str(bucket[path])
  142. # test uploading a file object
  143. path = "test_upload_file_obj"
  144. with open("LICENSE", "rb") as temp:
  145. bucket.key(path).upload(temp)
  146. assert path in bucket
  147. assert "MIT License" in str(bucket[path])
  148. def test_key_download(bucket: bucketstore.S3Bucket, key: bucketstore.S3Key) -> None:
  149. """test that we can use the key download function"""
  150. # test key doesn't exist
  151. with pytest.raises(Exception):
  152. bucket.key("this/is/not/a/key").download("/this/is/not/a/file")
  153. # test downloading to a file
  154. _, path = tempfile.mkstemp()
  155. key.download(path)
  156. with open(path, "r") as file_0:
  157. data = file_0.read()
  158. assert isinstance(data, str)
  159. assert "a testing value" in data
  160. os.remove(path)
  161. # test downloading to a file object
  162. _, path = tempfile.mkstemp()
  163. with open(path, "wb") as file_1:
  164. key.download(file_1)
  165. with open(path, "r") as file_2:
  166. data = file_2.read()
  167. assert isinstance(data, str)
  168. assert "a testing value" in data
  169. os.remove(path)
  170. def test_key_size(key: bucketstore.S3Key) -> None:
  171. """test getting the size of a key"""
  172. size = key.size()
  173. assert size
  174. assert size == 15
  175. assert size == len(key)