Skip to content

Commit

Permalink
add user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marceloarocha committed Nov 8, 2023
1 parent c3f4226 commit dd04db0
Showing 1 changed file with 168 additions and 5 deletions.
173 changes: 168 additions & 5 deletions tests/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ def delete_user(email):
session_commit()


def create_user(client, data, access_token):
delete_user(data["email"])

return client.put(
"/editUser", data=json.dumps(data), headers=make_headers(access_token)
)


def test_get_reports(client):
"""Teste get /reports/ - Valida status_code 200"""
access_token = get_access(client)
Expand Down Expand Up @@ -66,9 +74,7 @@ def test_put_user(client):
"active": "true",
}

response = client.put(
"/editUser", data=json.dumps(data), headers=make_headers(access_token)
)
response = create_user(client, data, access_token)
responseObject = json.loads(response.data)
userId = responseObject["data"]
user = session.query(User).filter(User.id == userId).first()
Expand All @@ -81,7 +87,19 @@ def test_put_editUser(client):
"""Teste put /editUser/<int:idUser> - Compara o response.data e edita o usuário"""
access_token = get_access(client, roles=["userAdmin", "staging"])

user = session.query(User).filter(User.email == "[email protected]").first()
# first insert user
create_data = {
"id": "",
"email": "[email protected]",
"name": "test3",
"external": "test",
"active": "true",
}

response = create_user(client, create_data, access_token)
assert response.status_code == 200

user = session.query(User).filter(User.email == create_data["email"]).first()
assert user != None

data = {
Expand All @@ -107,4 +125,149 @@ def test_put_editUser(client):
assert data["external"] == userEdited.external
assert data["active"] == userEdited.active

delete_user("[email protected]")

def test_create_user_invalid_role(client):
"""Teste put /editUser - Verifica roles criadas"""
access_token = get_access(client, roles=["userAdmin", "staging"])
email = "[email protected]"

delete_user(email)

data = {
"id": "",
"email": email,
"name": "test3",
"external": "test",
"active": "true",
"roles": ["admin"],
}

response = client.put(
"/editUser", data=json.dumps(data), headers=make_headers(access_token)
)
responseObject = json.loads(response.data)
userId = responseObject["data"]
user = session.query(User).filter(User.id == userId).first()

assert response.status_code == 200
assert "admin" not in user.config["roles"]
assert "userAdmin" not in user.config["roles"]


def test_update_user_invalid_role(client):
"""Teste put /editUser/<int:idUser> - Compara o response.data e edita o usuário"""
access_token = get_access(client, roles=["userAdmin", "staging"])

# first insert user
create_data = {
"id": "",
"email": "[email protected]",
"name": "test3",
"external": "test",
"active": "true",
}

response = create_user(client, create_data, access_token)
assert response.status_code == 200

user = session.query(User).filter(User.email == create_data["email"]).first()
assert user != None

data = {
"id": user.id,
"email": user.email,
"name": "updateTest",
"external": "updateTest",
"active": False,
"roles": ["admin"],
}

response = client.put(
"/editUser/" + str(user.id),
data=json.dumps(data),
headers=make_headers(access_token),
)
assert response.status_code == 200

session_commit()

userEdited = session.query(User).filter(User.id == user.id).first()

assert data["name"] == userEdited.name
assert data["external"] == userEdited.external
assert "admin" not in user.config["roles"]
assert "userAdmin" not in user.config["roles"]


def test_create_user_invalid_role_perimission(client):
"""Teste put /editUser - Roles inválidas"""

email = "[email protected]"

delete_user(email)

data = {
"id": "",
"email": email,
"name": "test3",
"external": "test",
"active": "true",
"roles": ["admin"],
}

access_token = get_access(client, roles=["userAdmin", "staging", "admin"])
response = client.put(
"/editUser", data=json.dumps(data), headers=make_headers(access_token)
)
assert response.status_code == 401

access_token = get_access(client, roles=["userAdmin", "staging", "training"])
response = client.put(
"/editUser", data=json.dumps(data), headers=make_headers(access_token)
)
assert response.status_code == 401


def test_update_user_invalid_role_permission(client):
"""Teste put /editUser/<int:idUser> - Edição com roles inválidas"""
access_token = get_access(client, roles=["userAdmin", "staging"])

# first insert user
create_data = {
"id": "",
"email": "[email protected]",
"name": "test3",
"external": "test",
"active": "true",
}

response = create_user(client, create_data, access_token)
assert response.status_code == 200

user = session.query(User).filter(User.email == create_data["email"]).first()
assert user != None

data = {
"id": user.id,
"email": user.email,
"name": "updateTest",
"external": "updateTest",
"active": False,
"roles": ["admin"],
}

access_token = get_access(client, roles=["userAdmin", "staging", "admin"])
response = client.put(
"/editUser/" + str(user.id),
data=json.dumps(data),
headers=make_headers(access_token),
)
assert response.status_code == 401

access_token = get_access(client, roles=["userAdmin", "staging", "training"])
response = client.put(
"/editUser/" + str(user.id),
data=json.dumps(data),
headers=make_headers(access_token),
)
assert response.status_code == 401

0 comments on commit dd04db0

Please sign in to comment.