# Unit tests for API endpoints that need authentication

**URL:** <https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369>\
**Category:** Get Help\
**Tags:** apis, application\
**Created:** [December 19, 2023, 3:04pm UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369 "2023-12-19T15:04:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![luuk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/luuk/32/50584_2.png) [@luuk](https://community.auth0.com/u/luuk)\
**Post date:** [December 19, 2023, 3:04pm UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369/1 "2023-12-19T15:04:45Z")

</div>

Hi everyone! I want to create unit tests for my Nest.js API. In those tests, I want to test the endpoints that need authentication. I have made a test user and I just want to log that user in so I can use it’s token.

I already tried it with just logging in into my client application and copying the code but this is manual and I want it to be automated just in the unit tests itself.

I already looked at the Authentication API because I saw that I needed to follow this: ([https://auth0.com/docs/api/authentication?javascript#resource-owner-password](https://auth0.com/docs/api/authentication?javascript#resource-owner-password)). But when I post the login request I get this error: Authorization server not configured with default connection. I also read that this resource-owner-password way of authenticating is not recommended. If so, how else can I test my API and provide the token of a test user?

If you need more information just let me know! 🙂

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 20, 2023, 9:50pm UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369/3 "2023-12-20T21:50:09Z")

</div>

Hi there @luuk welcome to the community!

> [@luuk](#):
>
> Authorization server not configured with default connection.

This error can be resolved by [setting a default directory](https://auth0.com/docs/get-started/authentication-and-authorization-flow/call-your-api-using-resource-owner-password-flow#configure-tenant) in your tenant settings.

> [@luuk](#):
>
> I also read that this resource-owner-password way of authenticating is not recommended. If so, how else can I test my API and provide the token of a test user?

Using the ROPG flow for testing purposes is totally fine - There are certainly other flows recommended for production:

> **[Authentication and Authorization Flows](https://auth0.com/docs/get-started/authentication-and-authorization-flow)**
>
> Learn about the various flows used for authentication and authorization of applications and APIs.

---

<div class="post-metadata">

**Author:** ![luuk](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/luuk/32/50584_2.png) [@luuk](https://community.auth0.com/u/luuk)\
**Post date:** [December 21, 2023, 8:40am UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369/4 "2023-12-21T08:40:31Z")

</div>

It worked! Thank you very much!

---

<div class="post-metadata">

**Author:** ![ty.frith](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/ty.frith/32/58484_2.png) [@ty.frith](https://community.auth0.com/u/ty.frith)\
**Post date:** [December 21, 2023, 7:41pm UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369/5 "2023-12-21T19:41:37Z")

</div>

Awesome, thanks for confirming! 😄

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [January 4, 2024, 7:42pm UTC](https://community.auth0.com/t/unit-tests-for-api-endpoints-that-need-authentication/123369/6 "2024-01-04T19:42:13Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
