Compare commits

..

No commits in common. "06e95b9751afb42f40fdbd7b4b207467b85c8e63" and "21ed36fd99f2ef0a771c6745e9bb527ba6a18447" have entirely different histories.

4 changed files with 21 additions and 57 deletions

View File

@ -1,15 +0,0 @@
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,8 +1,6 @@
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:
@ -10,21 +8,23 @@ 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, {'username': username, 'password': password}) response: Response = self._post(urls.accounts_login_url, {'username': username, 'password': password})
return response.content return response.content
def _post(self, uri: str, data: dict = {}): def register(self, username: str, password: str):
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.get('csrftoken') self.token = response.cookies['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))
self.token = response.cookies.get('csrftoken') return response.content
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 = None): def test(value, expected_value, title, description):
if (value == expected_value): if (value == expected_value):
print(title, "[OK]") print(title, "[OK]")
return return
@ -12,40 +12,20 @@ def test(value, expected_value, title, description = None):
if not description is None: if not description is None:
print (description) print (description)
def test_accounts_register(client, username, password): def test_accounts_register():
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,4 +1,3 @@
accounts: str = "api/accounts/" accounts_url: str = "api/accounts/"
accounts_login: str = accounts + "login" accounts_login_url: str = accounts_url + "login"
accounts_delete: str = accounts + "delete" accounts_register_url: str = accounts_url + "register"
accounts_register: str = accounts + "register"