from test_main import *
import pytest

@pytest.mark.order(1)
def test_register_user():
    response = client.post("/register", json={"username": "testuser", "password": "testpassword"})
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert "token_type" in data
    assert "user_id" in data

@pytest.mark.order(2)
def test_register_user_second():
    response = client.post("/register", json={"username": "testuser2", "password": "testpassword2"})
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert "token_type" in data
    assert "user_id" in data

def test_register_same_user():
    response = client.post("/register", json={"username": "testuser", "password": "testpassword"})
    assert response.status_code == 409 # Conflict - Cannot create two times the same user

def test_login_user():
    response = client.post("/login", data={"username": "testuser", "password": "testpassword"})
    assert response.status_code == 200
    data = response.json()
    assert "access_token" in data
    assert "token_type" in data
    assert "user_id" in data


def test_login_user_bad_password():
    response = client.post("/login", data={"username": "testuser", "password": "BADpassword"})
    assert response.status_code == 401 # Unauthorized, bad pwd

def test_search_users(token):
    response = client.get("/users?name=testuser", headers={"Authorization": f"Bearer {token}"})
    assert response.status_code == 200
    data = response.json()
    assert isinstance(data, list)
    assert data[0]["username"] == "testuser"