Exemplo n.º 1
0
    def test_post_request_creates_user(self, client):
        user = UserFactory.build()
        sent = user_schema.dump(user)
        sent["password"] = "******"
        del sent["id"]

        response = client.post(
            "/users", data=json.dumps(sent), content_type="application/json"
        )

        assert response.status == "201 CREATED"

        recv = response.get_json()

        assert recv is not None
        assert "id" in recv

        user = User.query.get(recv["id"])

        assert user is not None
        assert user.id == recv["id"]
        for key in ["username", "email"]:
            assert sent[key] == recv[key]
            assert sent[key] == getattr(user, key)
        assert "password" not in recv
        assert user.check_password(sent["password"])
Exemplo n.º 2
0
 def test_post_request_fails_if_attribute_is_invalid(self, client, attribute, value):
     user = UserFactory.build()
     data = user_schema.dump(user)
     data[attribute] = value
     data["password"] = "******"
     response = client.post(
         "/users", data=json.dumps(data), content_type="application/json"
     )
     assert response.status == "422 UNPROCESSABLE ENTITY"
     assert attribute in response.get_json()["details"]
Exemplo n.º 3
0
 def test_post_request_fails_if_attribute_exists(self, attribute, user, client):
     user, existing_user = UserFactory.build(), user
     data = user_schema.dump(user)
     data[attribute] = getattr(existing_user, attribute)
     data["password"] = "******"
     del data["id"]
     response = client.post(
         "/users", data=json.dumps(data), content_type="application/json"
     )
     assert response.status == "400 BAD REQUEST"
     assert attribute in response.get_json()["details"]
Exemplo n.º 4
0
def test_user_is_created():
    user = UserFactory.build(username="******", email="*****@*****.**")

    assert user.id is None
    assert user.username == "john"
    assert user.email == "*****@*****.**"
    assert user.check_password("example")
    assert user.token is None
    assert user.token_expiration is None
    assert user.is_admin is None
    assert str(user) == "<User john>"