Compare commits

..

3 Commits

Author SHA1 Message Date
06e95b9751 add: accounts: create, delete 2023-11-03 21:52:26 +01:00
f14279b956 fix: post method, csrf token and return right valu 2023-11-03 21:51:57 +01:00
2e680d94b6 update: urls var name 2023-11-03 21:50:23 +01:00
4 changed files with 57 additions and 21 deletions

15
src/Accounts.py Normal file
View File

@ -0,0 +1,15 @@
import urls
class Account:
def __init__(self, client):
self._client = client
def create(self, username: str, password: str):
response: Response = self._client._post(urls.accounts_register, {'username': username, 'password': password})
return response.content
def delete(self):
assert self._client.is_authentificate
response: Response = self._client._post(urls.accounts_delete, {})
return response.content

View File

@ -1,6 +1,8 @@
import requests import requests
from requests import Response, Request, Session from requests import Response, Request, Session
import Accounts
import urls import urls
class Client: class Client:
@ -8,23 +10,21 @@ class Client:
self.url: str = url self.url: str = url
self.token: str = None self.token: str = None
self.session: Session = Session() self.session: Session = Session()
self.accounts: Account = Accounts.Account(self)
def is_authentificate(self): def is_authentificate(self):
return (not self.token is None) return (not self.token is None)
def login(self, username: str, password: str): def login(self, username: str, password: str):
response: Response = self._post(urls.accounts_login_url, {'username': username, 'password': password}) response: Response = self._post(urls.accounts_login, {'username': username, 'password': password})
return response.content return response.content
def register(self, username: str, password: str): def _post(self, uri: str, data: dict = {}):
response: Response = self._post(urls.accounts_register_url, {'username': username, 'password': password})
return response
def _post(self, uri: str, data: dict):
url: str = self.url + uri url: str = self.url + uri
if self.token is None: if self.token is None:
response: Response = self.session.get(url) response: Response = self.session.get(url)
self.token = response.cookies['csrftoken'] self.token = response.cookies.get('csrftoken')
data.update({'csrfmiddlewaretoken': self.token}) data.update({'csrfmiddlewaretoken': self.token})
response: Response = self.session.post(url, data = data, headers = dict(Referer=url)) response: Response = self.session.post(url, data = data, headers = dict(Referer=url))
return response.content self.token = response.cookies.get('csrftoken')
return response

View File

@ -3,7 +3,7 @@ from urls import *
from uuid import uuid4 from uuid import uuid4
def test(value, expected_value, title, description): def test(value, expected_value, title, description = None):
if (value == expected_value): if (value == expected_value):
print(title, "[OK]") print(title, "[OK]")
return return
@ -12,20 +12,40 @@ def test(value, expected_value, title, description):
if not description is None: if not description is None:
print (description) print (description)
def test_accounts_register(): 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 tests():
username = uuid4() username = uuid4()
password = uuid4() password = uuid4()
client = Client.Client("http://0.0.0.0:8000/") client = Client.Client("http://0.0.0.0:8000/")
print ("REGISTER")
test_accounts_register(client, username, password)
test(client.register(username, password), b'ok: user added', 'normal register', None) test_accounts_login(client, username, password)
test_accounts_delete(client)
print()
def tests():
test_accounts_register()
if __name__ == "__main__": if __name__ == "__main__":
tests() tests()

View File

@ -1,3 +1,4 @@
accounts_url: str = "api/accounts/" accounts: str = "api/accounts/"
accounts_login_url: str = accounts_url + "login" accounts_login: str = accounts + "login"
accounts_register_url: str = accounts_url + "register" accounts_delete: str = accounts + "delete"
accounts_register: str = accounts + "register"