Compare commits
No commits in common. "06e95b9751afb42f40fdbd7b4b207467b85c8e63" and "21ed36fd99f2ef0a771c6745e9bb527ba6a18447" have entirely different histories.
06e95b9751
...
21ed36fd99
@ -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
|
@ -1,8 +1,6 @@
|
||||
import requests
|
||||
from requests import Response, Request, Session
|
||||
|
||||
import Accounts
|
||||
|
||||
import urls
|
||||
|
||||
class Client:
|
||||
@ -10,21 +8,23 @@ class Client:
|
||||
self.url: str = url
|
||||
self.token: str = None
|
||||
self.session: Session = Session()
|
||||
self.accounts: Account = Accounts.Account(self)
|
||||
|
||||
def is_authentificate(self):
|
||||
return (not self.token is None)
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
if self.token is None:
|
||||
response: Response = self.session.get(url)
|
||||
self.token = response.cookies.get('csrftoken')
|
||||
self.token = response.cookies['csrftoken']
|
||||
data.update({'csrfmiddlewaretoken': self.token})
|
||||
response: Response = self.session.post(url, data = data, headers = dict(Referer=url))
|
||||
self.token = response.cookies.get('csrftoken')
|
||||
return response
|
||||
return response.content
|
40
src/tests.py
40
src/tests.py
@ -3,7 +3,7 @@ from urls import *
|
||||
|
||||
from uuid import uuid4
|
||||
|
||||
def test(value, expected_value, title, description = None):
|
||||
def test(value, expected_value, title, description):
|
||||
if (value == expected_value):
|
||||
print(title, "[OK]")
|
||||
return
|
||||
@ -12,40 +12,20 @@ def test(value, expected_value, title, description = None):
|
||||
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 tests():
|
||||
|
||||
def test_accounts_register():
|
||||
username = uuid4()
|
||||
password = uuid4()
|
||||
|
||||
client = Client.Client("http://0.0.0.0:8000/")
|
||||
|
||||
|
||||
test_accounts_register(client, username, password)
|
||||
test_accounts_login(client, username, password)
|
||||
test_accounts_delete(client)
|
||||
print ("REGISTER")
|
||||
|
||||
test(client.register(username, password), b'ok: user added', 'normal register', None)
|
||||
|
||||
print()
|
||||
|
||||
def tests():
|
||||
test_accounts_register()
|
||||
|
||||
if __name__ == "__main__":
|
||||
tests()
|
@ -1,4 +1,3 @@
|
||||
accounts: str = "api/accounts/"
|
||||
accounts_login: str = accounts + "login"
|
||||
accounts_delete: str = accounts + "delete"
|
||||
accounts_register: str = accounts + "register"
|
||||
accounts_url: str = "api/accounts/"
|
||||
accounts_login_url: str = accounts_url + "login"
|
||||
accounts_register_url: str = accounts_url + "register"
|
Loading…
Reference in New Issue
Block a user