web-base/test/apiTest.py

73 lines
2.7 KiB
Python
Raw Normal View History

2020-04-02 15:08:14 +02:00
from phpTest import PhpTest
class ApiTestCase(PhpTest):
def __init__(self):
2020-04-02 16:16:58 +02:00
super().__init__({
"Testing login…": self.test_login,
"Testing already logged in…": self.test_already_logged_in,
2020-04-02 21:19:06 +02:00
# ApiKeys
2020-04-02 16:31:17 +02:00
"Testing get api keys empty…": self.test_get_api_keys_empty,
"Testing create api key…": self.test_create_api_key,
"Testing referesh api key…": self.test_refresh_api_key,
"Testing revoke api key…": self.test_revoke_api_key,
2020-04-02 21:19:06 +02:00
# Notifications
"Testing fetch notifications…": self.test_fetch_notifications,
2020-04-02 16:31:17 +02:00
"Testing logout…": self.test_logout,
2020-04-02 16:16:58 +02:00
})
2020-04-02 15:08:14 +02:00
def api(self, method):
2020-04-02 16:16:58 +02:00
return "/api/%s" % method
2020-04-02 16:31:17 +02:00
def getApiKeys(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("apiKey/fetch"))
2020-04-02 16:31:17 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
return obj
2020-04-02 16:16:58 +02:00
def test_login(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("user/login"), data={ "username": PhpTest.ADMIN_USERNAME, "password": PhpTest.ADMIN_PASSWORD })
2020-04-02 16:16:58 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
return obj
2020-04-02 15:08:14 +02:00
2020-04-02 16:16:58 +02:00
def test_already_logged_in(self):
obj = self.test_login()
self.assertEquals("You are already logged in", obj["msg"])
2020-04-02 15:08:14 +02:00
2020-04-02 16:31:17 +02:00
def test_get_api_keys_empty(self):
obj = self.getApiKeys()
2020-04-02 16:16:58 +02:00
self.assertEquals([], obj["api_keys"])
2020-04-02 16:31:17 +02:00
def test_create_api_key(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("apiKey/create"))
2020-04-02 16:31:17 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
self.assertTrue("api_key" in obj)
self.apiKey = obj["api_key"]
obj = self.getApiKeys()
self.assertEquals(1, len(obj["api_keys"]))
self.assertDictEqual(self.apiKey, obj["api_keys"][0])
def test_refresh_api_key(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("apiKey/refresh"), data={"id": self.apiKey["uid"]})
2020-04-02 16:31:17 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
self.assertTrue("valid_until" in obj)
self.assertTrue(obj["valid_until"] >= self.apiKey["valid_until"])
def test_revoke_api_key(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("apiKey/revoke"), data={"id": self.apiKey["uid"]})
2020-04-02 16:31:17 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
self.test_get_api_keys_empty()
2020-04-02 21:19:06 +02:00
def test_fetch_notifications(self):
obj = self.httpPost(self.api("notifications/fetch"))
self.assertEquals(True, obj["success"], obj["msg"])
2020-04-02 16:31:17 +02:00
def test_logout(self):
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("user/logout"))
2020-04-02 16:31:17 +02:00
self.assertEquals(True, obj["success"], obj["msg"])
2020-04-02 21:19:06 +02:00
obj = self.httpPost(self.api("user/logout"))
2020-04-02 16:31:17 +02:00
self.assertEquals(False, obj["success"])