def test_dont_add_user_if_user_not_exist(test_client, api_headers_bearer,
                                         insert_tokens):
    password = "******"

    user = User(first_name="Max",
                last_name="Muster",
                email="*****@*****.**",
                password=password)
    user = insert_user(user)
    tokens = insert_tokens(user.email)

    group_member = GroupMember(user=user)
    group = Group(name="Group", group_members=[group_member])
    insert_group(group)

    group_add_user_data = {
        "user_id": 2,
    }

    response = test_client.post("/groups/{}/members".format(group.id),
                                headers=api_headers_bearer(
                                    tokens["access_token"]["token"]),
                                data=json.dumps(group_add_user_data))
    json_response = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_response["message"] == "User does not exist"
    assert len(get_all_users()) == 1
def test_dont_add_existing_user(app, test_client, api_headers):
    user_data = {
        "first_name": "Max",
        "last_name": "Muster",
        "email": "*****@*****.**",
        "password": "******"
    }

    test_client.post("/users", headers=api_headers, data=json.dumps(user_data))

    user_data2 = {
        "first_name": "Maxi",
        "last_name": "Mustermann",
        "email": "*****@*****.**",
        "password": "******"
    }

    response = test_client.post("/users",
                                headers=api_headers,
                                data=json.dumps(user_data2))
    json_respone = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_respone["message"] == "User already exists"
    assert len(get_all_users()) == 1
def test_error_on_empty_request(test_client, api_headers):
    user_data = {}

    response = test_client.post("/users",
                                headers=api_headers,
                                data=json.dumps(user_data))
    json_respone = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_respone["message"] == "No input data provided"
    assert len(get_all_users()) == 0
def test_error_on_last_name_missing(app, test_client, api_headers):
    user_data = {
        "first_name": "Max",
        "email": "*****@*****.**",
        "password": "******"
    }

    response = test_client.post("/users",
                                headers=api_headers,
                                data=json.dumps(user_data))
    json_respone = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_respone["message"] == "Missing attribute last_name"
    assert len(get_all_users()) == 0
def test_error_on_email_wrong_type(app, test_client, api_headers):
    user_data = {
        "first_name": "Max",
        "last_name": "Muster",
        "email": True,
        "password": "******"
    }

    response = test_client.post("/users",
                                headers=api_headers,
                                data=json.dumps(user_data))
    json_respone = json.loads(response.get_data(as_text=True))

    assert response.status_code == 400
    assert json_respone["message"] == "Attribute email needs to be of type str"
    assert len(get_all_users()) == 0