1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-22 21:14:18 +00:00
pds-2021-g2-nest/nest_backend/test/test_1_user.py

72 lines
2.8 KiB
Python
Raw Normal View History

2021-05-07 22:38:17 +00:00
from flask.testing import Client
'''A file that contains tests of classes and methods for all the requests concerning an user.'''
2021-05-07 22:38:17 +00:00
class TestUserGet:
2021-05-10 22:16:02 +00:00
def test_admin_user(self, flask_client: Client, admin_headers):
2021-05-08 15:02:53 +00:00
r = flask_client.get(f'/api/v1/users/admin@admin.com', headers=admin_headers)
2021-05-10 22:16:02 +00:00
assert r.json["result"] == "success"
assert r.json["data"]["email"] == "admin@admin.com"
assert r.json["data"]["isAdmin"] is True
assert r.json["data"]["username"] == "admin"
2021-05-07 22:38:17 +00:00
2021-05-10 22:16:02 +00:00
def test_non_existing_user(self, flask_client: Client, admin_headers):
r = flask_client.get(f'/api/v1/users/ciccio@dev.com', headers=admin_headers)
assert r.json["result"] == "failure"
assert r.json["msg"] == "Could not locate the user."
2021-05-07 22:38:17 +00:00
# ritorna i dati di tutti gli utenti registrati
class TestUserGetAll:
2021-05-10 22:16:02 +00:00
def test_for_success(self, flask_client: Client, admin_headers):
r = flask_client.get(f'/api/v1/users/', headers=admin_headers)
assert r.json["result"] == "success"
def test_for_failure(self, flask_client: Client, user_headers):
2021-05-28 16:22:50 +00:00
r = flask_client.patch(f'/api/v1/users/', headers=user_headers)
assert r.json["result"] == "failure"
2021-05-07 22:38:17 +00:00
class TestUserAdd:
def test_valid_user(self, flask_client: Client, admin_headers):
2021-05-08 16:00:53 +00:00
r = flask_client.post(f'/api/v1/users/', headers=admin_headers, json={
'email': 'utente1_test@nest.com',
2021-05-08 16:00:53 +00:00
'password': 'password',
'username': 'utente_test'
})
assert r.json["result"] == "success"
2021-05-07 22:38:17 +00:00
def test_existing_user(self, flask_client: Client, user_headers):
2021-05-08 16:00:53 +00:00
r = flask_client.post(f'/api/v1/users/', headers=user_headers, json={
'email': 'utente_test@nest.com',
'password': 'password',
'username': 'utente_test'
})
assert r.json["result"] == "failure"
2021-05-08 16:00:53 +00:00
2021-05-07 22:38:17 +00:00
class TestUserDelete:
def test_valid_user(self, flask_client: Client, admin_headers):
r = flask_client.delete(f'/api/v1/users/utente1_test@nest.com', headers=admin_headers)
assert r.status_code == 204
2021-05-08 16:00:53 +00:00
# the admin tries to commit suicide
def test_himself(self, flask_client: Client, admin_headers):
2021-05-08 16:00:53 +00:00
r = flask_client.delete(f'/api/v1/users/admin@admin.com', headers=admin_headers)
assert r.json["result"] == "failure"
2021-05-08 16:00:53 +00:00
class TestUserPatch:
2021-05-10 22:16:02 +00:00
def test_for_success(self, flask_client: Client, admin_headers):
r = flask_client.patch(f'/api/v1/users/admin@admin.com', headers=admin_headers, json={
'username': 'admin_patched'
})
assert r.json["result"] == "success"
def test_not_authorized(self, flask_client: Client, user_headers):
r = flask_client.patch(f'/api/v1/users/admin@admin.com', headers=user_headers, json={
'username': 'admin_patched'
})
assert r.json["result"] == "failure"