Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad loginAs method args type when chained to a guard #200

Closed
Foohx opened this issue May 23, 2022 · 13 comments
Closed

Bad loginAs method args type when chained to a guard #200

Foohx opened this issue May 23, 2022 · 13 comments
Assignees
Labels
Type: Bug The issue has indentified a bug

Comments

@Foohx
Copy link

Foohx commented May 23, 2022

In tests, only when chaining loginAs to guard methods, arguments of loginAs method are of type never.

(method) loginAs(...args: never): ApiRequest

Package version

@adonisjs/auth: 8.2.1

Node.js and npm version

Node.js: 16.13.0
npm: 8.10.0

Sample Code (to reproduce the issue)

Just create a new project with at least @adonisjs/auth package, then fill the existing test with the following code :

const user = await User.create({ email: 'ex@amp.le', password: '123456' })

const response = await client.get('/').loginAs(user) // Everything good

const responseApi = await client.get('/').guard('api').loginAs(user) // <-- Argument of type 'User' is not assignable to parameter of type 'never'


response.assertStatus(200)
responseApi.assertStatus(200)
@Foohx Foohx changed the title Bad loginAs method args when chained to a guard Bad loginAs method args type when chained to a guard May 23, 2022
@thetutlage
Copy link
Member

Can you share the contracts/auth.ts file?

@thetutlage thetutlage self-assigned this May 24, 2022
@Foohx
Copy link
Author

Foohx commented May 24, 2022

Here is the file:

import User from 'App/Models/User'

declare module '@ioc:Adonis/Addons/Auth' {
  interface ProvidersList {
    user: {
      implementation: LucidProviderContract<typeof User>
      config: LucidProviderConfig<typeof User>
    }
  }

  interface GuardsList {
    api: {
      implementation: OATGuardContract<'user', 'api'>
      config: OATGuardConfig<'user'>
    }
  }
}

@thetutlage thetutlage added the Type: Bug The issue has indentified a bug label May 24, 2022
@thetutlage
Copy link
Member

Its a bug with the default auth contract template. Lemme push a fix

@Foohx
Copy link
Author

Foohx commented May 24, 2022

No problem, thank you for your reactivity.

@sekistner
Copy link

Its a bug with the default auth contract template. Lemme push a fix

Any hint what the problem is?

@thetutlage
Copy link
Member

You will have to make the changes made in this commit manually. 946e730

The changes will be inside the contracts/auth.ts file. New projects will not have this issue

@MANTENN
Copy link

MANTENN commented Aug 17, 2023

@Foohx was guard or loginAs undefined for you when you run the tests?

@Foohx
Copy link
Author

Foohx commented Aug 17, 2023

@Foohx was guard or loginAs undefined for you when you run the tests?

Don't remember but it was fixed by updating manually the auth contract.
Check replies on top :)

@MANTENN
Copy link

MANTENN commented Aug 18, 2023

@Foohx got it, I fixed the type issue—I also had to use the ! operator for the error to go away. only issue I have is during runtime, the authjs test functions are undefined.

@Foohx
Copy link
Author

Foohx commented Aug 18, 2023

@MANTENN Weird that you have to use the ! operator.

I'm using it without problem like this:

    const user = await UserFactory.apply('verified').with('roles').create()

    const mock = sinon.mock(ConversationPolicy.prototype)
    mock.expects('before').once().resolves(undefined)
    mock.expects('viewList').once().resolves(true)

    await client.get('/conversations').guard('api').loginAs(user).json({})

    mock.verify()
    mock.restore()

But I am not in the latest version for the moment. I'm going to update all deps in the next few days and I'll let you know if I encounter the same problem. :)

@MANTENN
Copy link

MANTENN commented Aug 18, 2023

@Foohx I used the User.find(1) which either returns a user or null hence why I had to use the ! operator.

@MANTENN
Copy link

MANTENN commented Aug 21, 2023

@Foohx turns out I had NODE_ENV set to local so the bindings never were loaded when I ran my tests. I will need to open a PR to create a warning in the CLI.

#220

@Foohx
Copy link
Author

Foohx commented Aug 28, 2023

@Foohx I used the User.find(1) which either returns a user or null hence why I had to use the ! operator.

Okay, so everything is normal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug The issue has indentified a bug
Projects
None yet
Development

No branches or pull requests

4 participants