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