log_api_tests.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # Licensed to the Apache Software Foundation (ASF) under one
  2. # or more contributor license agreements. See the NOTICE file
  3. # distributed with this work for additional information
  4. # regarding copyright ownership. The ASF licenses this file
  5. # to you under the Apache License, Version 2.0 (the
  6. # "License"); you may not use this file except in compliance
  7. # with the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing,
  12. # software distributed under the License is distributed on an
  13. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. # KIND, either express or implied. See the License for the
  15. # specific language governing permissions and limitations
  16. # under the License.
  17. # isort:skip_file
  18. """Unit tests for Superset"""
  19. import json
  20. from typing import Optional
  21. import prison
  22. from flask_appbuilder.security.sqla.models import User
  23. import tests.test_app
  24. from superset import db
  25. from superset.models.core import Log
  26. from .base_tests import SupersetTestCase
  27. class LogApiTests(SupersetTestCase):
  28. def insert_log(
  29. self,
  30. action: str,
  31. user: "User",
  32. dashboard_id: Optional[int] = 0,
  33. slice_id: Optional[int] = 0,
  34. json: Optional[str] = "",
  35. duration_ms: Optional[int] = 0,
  36. ):
  37. log = Log(
  38. action=action,
  39. user=user,
  40. dashboard_id=dashboard_id,
  41. slice_id=slice_id,
  42. json=json,
  43. duration_ms=duration_ms,
  44. )
  45. db.session.add(log)
  46. db.session.commit()
  47. return log
  48. def test_get_list(self):
  49. """
  50. Log API: Test get list
  51. """
  52. admin_user = self.get_user("admin")
  53. log = self.insert_log("some_action", admin_user)
  54. self.login(username="admin")
  55. arguments = {"filters": [{"col": "action", "opr": "sw", "value": "some_"}]}
  56. uri = f"api/v1/log/?q={prison.dumps(arguments)}"
  57. rv = self.client.get(uri)
  58. self.assertEqual(rv.status_code, 200)
  59. response = json.loads(rv.data.decode("utf-8"))
  60. expected_columns = ["action", "dttm", "user"]
  61. self.assertEqual(list(response["result"][0].keys()), expected_columns)
  62. self.assertEqual(response["result"][0]["action"], "some_action")
  63. self.assertEqual(response["result"][0]["user"], {"username": "admin"})
  64. db.session.delete(log)
  65. db.session.commit()
  66. def test_get_list_not_allowed(self):
  67. """
  68. Log API: Test get list
  69. """
  70. admin_user = self.get_user("admin")
  71. log = self.insert_log("action", admin_user)
  72. self.login(username="gamma")
  73. uri = "api/v1/log/"
  74. rv = self.client.get(uri)
  75. self.assertEqual(rv.status_code, 401)
  76. self.login(username="alpha")
  77. rv = self.client.get(uri)
  78. self.assertEqual(rv.status_code, 401)
  79. def test_get_item(self):
  80. """
  81. Log API: Test get item
  82. """
  83. admin_user = self.get_user("admin")
  84. log = self.insert_log("some_action", admin_user)
  85. self.login(username="admin")
  86. uri = f"api/v1/log/{log.id}"
  87. rv = self.client.get(uri)
  88. self.assertEqual(rv.status_code, 200)
  89. response = json.loads(rv.data.decode("utf-8"))
  90. expected_columns = ["action", "dttm", "user"]
  91. self.assertEqual(list(response["result"].keys()), expected_columns)
  92. self.assertEqual(response["result"]["action"], "some_action")
  93. self.assertEqual(response["result"]["user"], {"username": "admin"})
  94. db.session.delete(log)
  95. db.session.commit()
  96. def test_delete_log(self):
  97. """
  98. Log API: Test delete (does not exist)
  99. """
  100. admin_user = self.get_user("admin")
  101. log = self.insert_log("action", admin_user)
  102. self.login(username="admin")
  103. uri = f"api/v1/log/{log.id}"
  104. rv = self.client.delete(uri)
  105. self.assertEqual(rv.status_code, 405)
  106. db.session.delete(log)
  107. db.session.commit()
  108. def test_update_log(self):
  109. """
  110. Log API: Test update (does not exist)
  111. """
  112. admin_user = self.get_user("admin")
  113. log = self.insert_log("action", admin_user)
  114. self.login(username="admin")
  115. log_data = {"action": "some_action"}
  116. uri = f"api/v1/log/{log.id}"
  117. rv = self.client.put(uri, json=log_data)
  118. self.assertEqual(rv.status_code, 405)
  119. db.session.delete(log)
  120. db.session.commit()