Ejemplo n.º 1
0
def test_create_user_bulk(client):
    user1 = StubFactory.create_user({"cpf": "71188941097"}, save_db=False)
    user2 = StubFactory.create_user(
        {
            "first_name": "Jane",
            "last_name": "Dunninghan",
            "cpf": "37600750880"
        },
        save_db=False,
    )

    schema = UserSchema(many=True)
    payload = {"data": schema.dump([user1, user2]).data}

    resp = client.post(
        url_for("user.create"),
        data=json.dumps(payload),
        content_type="application/json",
    )

    user1 = resp.json[0]
    user2 = resp.json[1]

    User.objects.get(**user1)
    User.objects.get(**user2)

    assert resp.status_code == 201
Ejemplo n.º 2
0
def test_list_users_pagination_filtered(client):
    StubFactory.create_user({
        "first_name": "Jane",
        "last_name": "Dunninghan",
        "cpf": "52203351039"
    })
    StubFactory.create_user({
        "first_name": "Jane",
        "last_name": "Captain",
        "cpf": "61755397020"
    })

    resp = client.get(
        url_for("user.list"),
        query_string={
            "page": 2,
            "per_page": 1,
            "first_name": "Jane"
        },
    )

    assert resp.json.get("next") is None
    assert resp.json["previous"] == url_for("user.list",
                                            page=1,
                                            per_page=1,
                                            first_name="Jane")
    assert resp.status_code == 200
    assert len(resp.json["result"]) == 1
Ejemplo n.º 3
0
def test_update_user_put(client):
    user1 = StubFactory.create_user(save_db=True)
    payload = {
        "data": {
            "first_name": "Tony",
            "last_name": "Soprano",
            "cpf": "03709557062",
            "birthdate": "22/10/1956",
            "telephones": ["5516996101220", "551133351122"],
            "address": {
                "city": "New Jersey",
                "state_province": "New York",
                "country": "United States",
                "zip_code": "66888",
                "public_area_desc": "Dover Beaches South",
                "number": "877",
            },
        }
    }

    resp = client.put(
        url_for("user.update", user_id=user1.id),
        data=json.dumps(payload),
        content_type="application/json",
    )

    assert User.objects.get(**payload["data"]).id == user1.id
    assert resp.status_code == 204
Ejemplo n.º 4
0
def test_can_serialize_address(app):
    address = StubFactory.create_address()
    schema = AddressSchema()
    result = schema.dump(address)

    assert result.errors == {}
    assert json.dumps(result.data)
Ejemplo n.º 5
0
def test_remove_user(client):
    user = StubFactory.create_user(save_db=True)
    assert User.objects.get(id=user.id).active is True

    resp = client.post(url_for("user.remove", user_id=user.id))

    assert User.objects.get(id=user.id).active is False
    assert resp.status_code == 201
Ejemplo n.º 6
0
def test_can_serialize_user(app):
    user = StubFactory.create_user(save_db=True)
    schema = UserSchema()

    result = schema.dump(user)

    assert result.errors == {}
    assert json.dumps(result.data)
Ejemplo n.º 7
0
def test_list_users(client):
    StubFactory.create_user()
    StubFactory.create_user({
        "first_name": "Jane",
        "last_name": "Dunninghan",
        "cpf": "52203351039"
    })

    resp = client.get(url_for("user.list"))

    user_1 = resp.json["result"][0]
    user_2 = resp.json["result"][1]

    User.objects.get(**user_1).id == user_1["id"]
    User.objects.get(**user_2).id == user_2["id"]

    assert len(resp.json["result"]) == 2
    assert resp.status_code == 200
Ejemplo n.º 8
0
def test_list_filtered_user(client):
    user = StubFactory.create_user({
        "first_name": "Jane",
        "last_name": "Dunninghan",
        "cpf": "52203351039"
    })

    resp = client.get(url_for("user.list"),
                      query_string={"first_name": "JaNe"})

    assert User.objects.get(id=user.id).first_name == "Jane"

    assert len(resp.json["result"]) == 1
    assert resp.status_code == 200

    resp = client.get(url_for("user.list"),
                      query_string={"cpf": "52203351039"})

    assert User.objects.get(id=user.id).cpf == "52203351039"

    assert len(resp.json["result"]) == 1
    assert resp.status_code == 200

    resp = client.get(url_for("user.list"),
                      query_string={"last_name": "DunninGHan"})

    assert User.objects.get(id=user.id).last_name == "Dunninghan"

    assert len(resp.json["result"]) == 1
    assert resp.status_code == 200

    resp = client.get(
        url_for("user.list"),
        query_string={
            "cpf": "52203351039",
            "first_name": "Jane",
            "last_name": "Dunninghan",
        },
    )

    user = User.objects.get(id=user.id)
    assert user.last_name == "Dunninghan"
    assert user.first_name == "Jane"
    assert user.cpf == "52203351039"

    assert len(resp.json["result"]) == 1
    assert resp.status_code == 200

    resp = client.get(url_for("user.list"),
                      query_string={"last_name": "Not Found"})

    assert len(resp.json["result"]) == 0
    assert resp.status_code == 200
Ejemplo n.º 9
0
def test_update_user_patch(client):
    user = StubFactory.create_user(save_db=True)
    payload = {"data": {"first_name": "Tony", "last_name": "Soprano"}}

    resp = client.patch(
        url_for("user.update", user_id=user.id),
        data=json.dumps(payload),
        content_type="application/json",
    )

    assert User.objects.get(**payload["data"]).id == user.id
    assert resp.status_code == 204
Ejemplo n.º 10
0
def test_create_user(client):
    user = StubFactory.create_user({"cpf": "37600750880"}, save_db=False)
    schema = UserSchema()
    payload = {"data": schema.dump(user).data}

    resp = client.post(
        url_for("user.create"),
        data=json.dumps(payload),
        content_type="application/json",
    )

    User.objects.get(**resp.json)

    assert resp.status_code == 201
Ejemplo n.º 11
0
def test_can_create_user_without_emails(client):
    address = StubFactory.create_address()

    user_data = {
        "first_name": "John",
        "last_name": "Doe",
        "cpf": "03709557062",
        "birthdate": "2010-10-28",
        "telephones": ["551622338877", "551633448977"],
        "address": address,
    }

    user = User(**user_data)
    user.save()

    assert user_data["last_name"] == User.objects.first().last_name
    assert user_data["telephones"] == User.objects.first().telephones
Ejemplo n.º 12
0
def test_get_user(client):
    user = StubFactory.create_user(save_db=True)
    resp = client.get(url_for("user.get", user_id=user.id))

    assert User.objects.get(**resp.json).id == user.id
    assert resp.status_code == 200