从DRF中的APIClient()获取用户

huangapple go评论74阅读模式
英文:

Getting the User from an APIClient() in DRF

问题

以下是您要翻译的代码部分:

I'm using a fixture in pytest that returns a client that has been logged in:

    @pytest.fixture 
    def create_client(create_user) -> APIClient:
        data = {
            "email": create_user.email,
            "password": "TestPassword",
        }
        client = APIClient()
        client.post(path="/user/login/", data=data)
        return client

How I get the user that has been logged in in the test?

I understand that I might be able to use a get a request from the client and get it that way:

    def test_get_user(create_client):
        response = create_client.get(path="/some/random/path/")
        user = response.user
        return user

but is there a better way to do this?
英文:

I'm using a fixture in pytest that returns a client that has been logged in:

@pytest.fixture 
def create_client(create_user) -> APIClient:
    data = {
        "email": create_user.email,
        "password": "TestPassword",
    }
    client = APIClient()
    client.post(path="/user/login/", data=data)
    return client

How I get the user that has been logged in in the test?

I understand that I might be able to use a get a request from the client and get it that way:

def test_get_user(create_client):
	response = create_client.get(path="/some/random/path/")
	user = response.user
	return user

but is there a better way to do this?

答案1

得分: 1

I'm sure that the create_user is another fixture and thus you can have that in your test_get_user(...) as

<pre><code>
def test_get_user(create_client, create_user):
print(create_user)
# do some tests here
</code></pre>

英文:

I'm sure that the create_user is another fixture and thus you can have that in your test_get_user(...) as

<pre><code>
def test_get_user(create_client, create_user):
print(create_user)
# do some tests here
</code></pre>

huangapple
  • 本文由 发表于 2023年2月19日 12:32:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75498011.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定