-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_server.py
137 lines (109 loc) · 4.21 KB
/
test_server.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
import pytest
from quart import Quart, jsonify, request
from server import (
check_chatfilter,
check_user_db,
save_data,
load_data
)
badwords = ["badword1", "badword2", "badword3"]
goodwords = ["goodword1", "goodword2"]
ids_list = {
"123": {"name": "Test User", "id": 123, "reason": "spamming"},
"456": {"name": "Another User", "id": 456, "reason": "offensive language"},
}
# Quart Test App
@pytest.fixture
def app():
app = Quart(__name__)
@app.route("/chatfilter", methods=["POST"])
async def check_message():
data = await request.get_json()
message = data["message"]
cid = 123
sid = 123
results = check_chatfilter(input_str=message, badwords=badwords, goodwords=goodwords, cid=cid, gid=sid)
return jsonify(results)
@app.route("/user", methods=["POST"])
async def check_user():
data = await request.get_json()
user_id = data["id"]
result = check_user_db(int(user_id), ids_list)
return jsonify(result)
@app.route("/flagg_user", methods=["POST"])
async def add_flagged_user():
data = await request.get_json()
user_id = data["id"]
user_name = data["name"]
reason = data["reason"]
if str(user_id) in ids_list:
return jsonify({"success": False, "message": "User already in Database!"})
else:
ids_list[str(user_id)] = {
"id": int(user_id),
"name": str(user_name),
"reason": str(reason),
"flagged": True
}
return jsonify({"success": True, "message": "User was flagged"})
@app.route("/deflag_user", methods=["POST"])
async def remove_flagged_user():
data = await request.get_json()
user_id = data["id"]
if str(user_id) in ids_list:
del ids_list[str(user_id)]
return jsonify({"success": True, "message": "User was deflagged"})
else:
return jsonify({"success": False, "message": "User not flagged!"})
yield app
# Chatfilter Test
@pytest.mark.asyncio
async def test_check_chatfilter(app):
client = app.test_client()
response = await client.post("/chatfilter", json={"message": "This is a badword1"})
data = await response.get_json()
assert "input_word" in data
assert data["input_word"] == "badword1"
response = await client.post("/chatfilter", json={"message": "This is a goodword1"})
data = await response.get_json()
assert not data
# User Test
@pytest.mark.asyncio
async def test_check_user(app):
client = app.test_client()
response = await client.post("/user", json={"id": 123})
data = await response.get_json()
assert data["id"] == 123
assert data["name"] == "Test User"
assert data["flagged"] is True
response = await client.post("/user", json={"id": 999})
data = await response.get_json()
assert data["flagged"] is False
assert data["name"] is None
# Admin Endpoints Tests
@pytest.mark.asyncio
async def test_flag_user(app):
client = app.test_client()
# Benutzer flaggen, der nicht in der Datenbank ist
response = await client.post("/flagg_user", json={"id": 789, "name": "New User", "reason": "test reason"})
data = await response.get_json()
assert data["success"] is True
assert data["message"] == "User was flagged"
# Versuchen, denselben Benutzer erneut zu flaggen
response = await client.post("/flagg_user", json={"id": 789, "name": "New User", "reason": "test reason"})
data = await response.get_json()
assert data["success"] is False
assert data["message"] == "User already in Database!"
@pytest.mark.asyncio
async def test_deflag_user(app):
client = app.test_client()
# Benutzer, der bereits in der Datenbank ist, deflaggen
response = await client.post("/deflag_user", json={"id": 123})
data = await response.get_json()
assert data["success"] is True
assert data["message"] == "User was deflagged"
# Versuchen, einen Benutzer zu deflaggen, der nicht in der Datenbank ist
response = await client.post("/deflag_user", json={"id": 999})
data = await response.get_json()
assert data["success"] is False
assert data["message"] == "User not flagged!"