0
votes

I am writing a REST API for authentication, which return a token if the endpoint /user/login is called with mail and password inside the body. I can cover all the code, where an error occus, for example the password or mail is missing inside the body, or the mail does not exist in the database. Can i test this endpoint for successful authentication without relying on the corrosponding POST Endpoint to correctly registering a user inside the database.

In my case I have an endpoint /user/register, which takes mail, name, password and creates a user inside the database.

Is there a way to test these two dependent endpoints independently?

Edit: Only way I could think of is posting the user in the before call and then test for it and seperately test the post endpoint

1

1 Answers

0
votes

Recommended solution

I think you should not overengineer this one. Write 2 test cases in the same file called something like authentication.test.js.

  1. register a user
  2. login with the registered user
  3. *remove user (test if account deletion works maybe? )

alternatively to point 3 cleanup the user in afterAll/afterEach

For the application it's necessary for both to succeed so it's ok if they depend on each other aslong as you put them in the same test file. If one fails, you have to check/fix anyways.

Answer to question

You can store a test user within your database which is a requirement for the test to be executed. Either have it statically there OR create it in beforeAll/beforeEach. But if you have a test, which register's a user, why should you do the work twice?