lundi 31 août 2015

loopback-testing: what is the correct way to test with a user and role

I am trying to write tests with loopback-testing.

I am a bit confused, and there's barely any documentation at all.

I'd like to test a model for which only a user with role "admin" has WRITE rights.

Now, if I do:

lt.describe.whenCalledByUserWithRole(test_config.adminUserCredentials, test_config.adminRole, ....)

loopback will actually create a user with test_config.adminUserCredentials prior to login! Why is it doing that?

The correct behavior, IMHO, should be:

  • Create the user in test setup
  • Create the role in test setup
  • Associate the role to the user
  • When running the test, only check that the user has rights on the requested operation

But it looks to be quite tricky to do with loopback-testing. If I create a user in setup, the test will crash because whenCalledByUserWithRole will in the process try to create the user again, which loopback will deny saying the user already exists. If I don't create a user and call whenCalledByUserWithRole, this user won't be associated to the "admin" role for some reason (even if the name suggests so), and the test fails.

How do I do this correctly?

Aucun commentaire:

Enregistrer un commentaire