TypeError: client.post(...).loginVia is not a function


#1

I have a RESTful AdonisJs API where I use JWT authentication. I want to test a POST request which requires authentication:

test('User can add a post when authenticated',  async( { client} ) => {                                                                        
  const user = User.find(1)   // I have only one user for the moment                                                                                                                   
  const response = await client                                                                                                                  
    .post('posts')                                                                                                          
    .loginVia(user, 'jwt')                                                                                                                   
    .send({foo: 'bar'})                                                                                                                      
    .end()                                                                                                                                   
  response.assertStatus(200)                                                                                                                     
})

I am getting this error which clearly says I can not use loginVia() as it is the case for get().

TypeError: client.post(…).loginVia is not a function

The Test/ApiClient trait provides an HTTP client to make requests, but I do not see how to set theAuthorization Bearer <token> header here.

I checked how they test authenticated POST requests on the blog demo, but they use sessions there, not JWT tokens.


#2

Hope this will help someone in the future:

Read again what is the purpose of theTest/ApiClient trait and you will guess what to do (that is at least what happened to me): I can use it to send a POST request. How do we do that?

The documentation says you have to specify the header with Bearer authorization and inject the JWT token there. So I just defined the header in a natural way and it worked. I mean, I removed that non existent (POST.loginVia() function ) and replaced it by:

.header('Authorization', 'Bearer mySignedJwtToken')

Of course, this supposes you have generated mySignedJwtToken somewhere in your application.

Billal Begueradj


#3

Hey @begueradj! :wave:

The real solution to this issue is to add the correct trait. Which is trait('Auth/Client').

More on that in the documentation.


#4

Yes I I mentioned I used trait in my solution :slight_smile: But thank you for the feedback :slight_smile:


#5

There’s multiple trait. You mentioned that you used the Test/ApiClient trait. You also need to add the trait Auth/Client to be able to use the loginVia method.


#6

Wow ! That is new to me, I will document more about it, thank you very much Romain Lanz.


#7

I tested your suggestion, it works.
It is much better than my approach because for a test to be always successful, my approach requires to inject a new JWT token whenever the former one is dead.
So I accept your answer instead of mine, that is really a very good point, thank you very much.