-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgwire_checks.py
executable file
·240 lines (211 loc) · 6.79 KB
/
cgwire_checks.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
import os
import sys
import requests
class CheckURL:
def __init__(self, base_url):
self.base_url = base_url
self.status = 0
self.request = None
self.kitsu_version = None
self.zou_version = None
def check_url(self, url, message_ok, message_ko, data=None, error_code=200):
try:
if data:
self.request = requests.post(f"{self.base_url}{url}", json=data)
else:
self.request = requests.get(f"{self.base_url}{url}")
if self.request.status_code == error_code:
return message_ok
else:
self.status = 1
return message_ko + "\n" + self.request.text
except requests.exceptions.ConnectionError:
self.status = 1
return message_ko
def check_if_last_request_is_a_kitsu(self, message_ok, message_ko):
if self.request and "Kitsu" in self.request.text:
return message_ok
else:
return message_ko
def check_if_last_request_is_a_zou(self, message_ok, message_ko):
api = ""
if self.request:
api = self.request.json().get("api", "")
if api == "Zou":
return message_ok
else:
return message_ko
def check_if_error(self, message_ok, message_ko):
if self.request is None:
return message_ko
try:
error = self.request.json().get("error", False)
except requests.exceptions.JSONDecodeError:
return message_ko + "\n" + self.request.text
if error:
return message_ko + "\n" + self.request.text
else:
return message_ok
def check_login(self, message_ok, message_ko):
if self.request is None:
return message_ko
try:
error = self.request.json().get("login", False)
except requests.exceptions.JSONDecodeError:
return message_ko + "\n" + self.request.text
if error:
return message_ok
else:
return message_ko + "\n" + self.request.text
def check_bad_login(self, message_ok, message_ko):
if self.request is None:
return message_ko
try:
error = self.request.json().get("login", False)
except requests.exceptions.JSONDecodeError:
return message_ko + "\n" + self.request.text
if error:
return message_ko
else:
return message_ok
def check_kitsu_version(self, message_ok, message_ko):
if self.request is None:
return message_ko
if self.request.text.rstrip() == self.kitsu_version:
return message_ok + self.kitsu_version
else:
return message_ko + "\n" + self.request.text
def check_zou_version(self, message_ok, message_ko):
if self.request is None:
return message_ko
try:
version = self.request.json().get("version")
except requests.exceptions.JSONDecodeError:
return message_ko + "\n" + self.request.text
if version == self.zou_version:
return message_ok + self.zou_version
else:
return message_ko + "\n" + self.request.json().get("version")
if __name__ == "__main__": # pragma: nocover
print(80 * "#")
t = CheckURL(os.getenv("KITSU_URL", "http://127.0.0.1"))
t.kitsu_version = os.getenv("KITSU_VERSION", None)
t.zou_version = os.getenv("ZOU_VERSION", None)
print(f"Kitsu URL: {t.base_url}")
print(f"Kitsu version: {t.kitsu_version}")
print(f"Zou version: {t.zou_version}")
# Check Kitsu
print(
t.check_url(
"/",
"✅ 01a Check Kitsu /",
"🔥 01a Check Kitsu /",
)
)
print(
t.check_if_last_request_is_a_kitsu(
"✅ 01b Check if it's really a Kitsu",
"🔥 01b Check if it's really a Kitsu",
)
)
# Check Kitsu API (Zou)
print(
t.check_url(
"/api",
"✅ 02a Check Kitsu API /api",
"🔥 02a Check Kitsu API /api",
)
)
print(
t.check_if_last_request_is_a_zou(
"✅ 02b Check if it's really a Kitsu API",
"🔥 02b Check if it's really a Kitsu API",
)
)
# Check good login
print(
t.check_url(
"/api/auth/login",
"✅ 03a Check login /api/auth/login",
"🔥 03a Check login /api/auth/login",
{"email": "[email protected]", "password": "mysecretpassword"},
)
)
print(
t.check_if_error(
"✅ 03b Check login /api/auth/login (check error status)",
"🔥 03b Check login /api/auth/login (check error status)",
)
)
print(
t.check_login(
"✅ 03c Check login /api/auth/login (check login status)",
"🔥 03c Check login /api/auth/login (check login status)",
)
)
# Check bad pass
print(
t.check_url(
"/api/auth/login",
"✅ 04a Check login /api/auth/login",
"🔥 04a Check login /api/auth/login",
{"email": "[email protected]", "password": "badpass"},
400,
)
)
print(
t.check_bad_login(
"✅ 04b Check login /api/auth/login (check login status)",
"🔥 04b Check login /api/auth/login (check login status)",
)
)
# Check bad account
print(
t.check_url(
"/api/auth/login",
"✅ 05a Check login /api/auth/login",
"🔥 05a Check login /api/auth/login",
{"email": "[email protected]", "password": "badpass"},
400,
)
)
print(
t.check_bad_login(
"✅ 05b Check login /api/auth/login (check login status)",
"🔥 05b Check login /api/auth/login (check login status)",
)
)
# Check Kistu version
print(
t.check_url(
"/.version.txt",
"✅ 06a Kitsu version",
"🔥 06a Kitsu version",
)
)
if t.kitsu_version:
print(
t.check_kitsu_version(
f"✅ 06b Kitsu version {t.kitsu_version} == ",
f"🔥 06b Kitsu {t.kitsu_version} != ",
)
)
# Check Zou version
print(
t.check_url(
"/api",
"✅ 07a Zou version",
"🔥 07a Zou version",
)
)
if t.zou_version:
print(
t.check_zou_version(
f"✅ 07b Zou version {t.zou_version} == ",
f"🔥 07b Zou {t.zou_version} != ",
)
)
# Show status and exit with error code
print(f"Error code: {t.status}")
sys.exit(t.status)