5 Commits

Author SHA1 Message Date
a13feba991 fix: create value var 2023-11-06 17:26:27 +01:00
1f8bb5800b fix: separate csrf_token and token 2023-11-06 17:26:09 +01:00
22ff3d58d3 core: better annimations 2023-11-06 15:53:59 +01:00
617b67df24 split test 2023-11-06 14:36:28 +01:00
a17f09e04b create package and separate code 2023-11-06 14:26:40 +01:00
12 changed files with 110 additions and 83 deletions

3
setup.py Normal file
View File

@ -0,0 +1,3 @@
from setuptools import setup, find_packages
setup(name='transcendence_api', version='1.0', packages=find_packages())

0
src/__init__.py Normal file
View File

View File

@ -1,7 +1,9 @@
import urls
from src import urls
from requests import Response
class Account:
class Accounts:
def __init__(self, client):
self._client = client

View File

@ -1,21 +1,21 @@
import requests
from requests import Response, Request, Session
import Profiles
import Accounts
import urls
from src.profiles import Profiles
from src.accounts import Accounts
from src import urls
class Client:
def __init__(self, url: str):
self.url: str = url
self.token: str = None
self.csrf_token: str = None
self.session: Session = Session()
self.accounts: Account = Accounts.Account(self)
self.profiles = Profiles.Profiles(self)
self.accounts: Accounts = Accounts(self)
self.profiles: Profiles = Profiles(self)
def is_authentificate(self):
return (not self.token is None)
return (self.token is not None)
def login(self, username: str, password: str):
response: Response = self._post(urls.accounts_login, {'username': username, 'password': password})
@ -23,16 +23,16 @@ class Client:
def _post(self, uri: str, data: dict = {}):
url: str = self.url + uri
if self.token is None:
if self.csrf_token is None:
response: Response = self.session.get(url)
self.token = response.cookies.get('csrftoken')
data.update({'csrfmiddlewaretoken': self.token})
self.csrf_token = response.cookies.get('csrftoken')
data.update({'csrfmiddlewaretoken': self.csrf_token})
response: Response = self.session.post(url, data = data, headers = dict(Referer=url))
self.token = response.cookies.get('csrftoken')
self.csrf_token = response.cookies.get('csrftoken')
return response
def _get(self, uri: str):
url: str = self.url + uri
response: Response = self.session.get(url)
self.token = response.cookies.get('csrftoken')
self.csrf_token = response.cookies.get('csrftoken')
return response

View File

@ -1,8 +1,9 @@
import urls
import Client
from src import urls
from src.profile import Profile
from requests import Response
import Profile
class Profiles:
@ -14,4 +15,4 @@ class Profiles:
if response.status_code == 404:
return None
content: dict = eval(response.content)
return Profile.Profile(data = content)
return Profile(data = content)

View File

@ -1,65 +0,0 @@
import Client
import Profile
from urls import *
from uuid import uuid4
import os
def test(value, expected_value, title, description = None):
print(title, end=" ")
if (value == expected_value):
print("[OK]")
return
print ("[ERROR]")
print ("expected", expected_value, ", got", value)
if not description is None:
print (description)
def test_accounts_register(client, username, password):
print ("REGISTER")
test(client.accounts.create(username, password), b'ok: user added', 'normal', None)
print()
def test_accounts_login(client, username, password):
print ("LOGIN")
test(client.login(username, password), b'ok: account valid', "normal", None)
print()
def test_accounts_delete(client):
print ("DELETE")
test(client.accounts.delete(), b'ok: account has been deleted', 'normal')
print()
def test_profile_get(client):
print ("GET")
test(client.profiles.get(1), Profile.Profile(username="997e13f5-474d-4fea-b55a-ad8a27b9534b", title=""), "normal")
print()
def tests():
username = uuid4()
password = uuid4()
client = Client.Client("http://0.0.0.0:8000/")
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
test_accounts_register(client, username, password)
test_accounts_login(client, username, password)
test_accounts_delete(client)
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
test_profile_get(client)
if __name__ == "__main__":
tests()

0
tests/__init__.py Normal file
View File

35
tests/accounts.py Normal file
View File

@ -0,0 +1,35 @@
from uuid import uuid4
from tests.utils import test
def test_accounts_register(client, username, password):
print ("REGISTER")
test(client.accounts.create, (username, password), b'ok: user added', 'normal', None)
print()
def test_accounts_login(client, username, password):
print ("LOGIN")
test(client.login, (username, password), b'ok: account valid', "normal", None)
print()
def test_accounts_delete(client):
print ("DELETE")
test(client.accounts.delete, (), b'ok: account has been deleted', 'normal')
print()
def test_accounts(client):
username = uuid4()
password = uuid4()
test_accounts_register(client, username, password)
test_accounts_login(client, username, password)
test_accounts_delete(client)

19
tests/profiles.py Normal file
View File

@ -0,0 +1,19 @@
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # Add parent directory to system path
from tests.utils import test
from src.profile import Profile
def test_profiles_get(client):
print ("GET")
test(client.profiles.get, (1, ), Profile(username="997e13f5-474d-4fea-b55a-ad8a27b9534b", title=""), "normal")
print()
def test_profiles(client):
test_profiles_get(client)

22
tests/test.py Normal file
View File

@ -0,0 +1,22 @@
import os
import sys
sys.path.append(os.path.join(os.path.dirname(__file__), '..')) # Add parent directory to system path
from src.client import Client
from src import urls
from tests.accounts import test_accounts
from tests.profiles import test_profiles
def tests():
client = Client("http://0.0.0.0:8000/")
print("ACCOUNTS".center(os.get_terminal_size()[0], '-'))
test_accounts(client)
print("PROFILES".center(os.get_terminal_size()[0], '-'))
test_profiles(client)
if __name__ == "__main__":
tests()

10
tests/utils.py Normal file
View File

@ -0,0 +1,10 @@
def test(func: callable, params, expected_value, title: str, description = None):
print(title, end=" ")
value = func(*params)
if (value == expected_value):
print("[OK]")
return
print ("[ERROR]")
print ("expected", expected_value, ", got", value)
if not description is None:
print (description)