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

Add test to illustrate how to call actions locally #276

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

PierreFritsch
Copy link
Contributor

Add the code sample from the capire section Unbound Actions / Functions as a test in the code sample repository.

@PierreFritsch
Copy link
Contributor Author

I'd need help on this one - my new test fails with:

TypeError: Cannot read property 'SELECT' of undefined

@PierreFritsch
Copy link
Contributor Author

The test file consuming-services.test.js might be a good place to add this code sample, but it seems to be set up to serve only the AdminService which, in contrast with the CatalogService, doesn't have an action that can be called.

@chgeo
Copy link
Member

chgeo commented Oct 4, 2021

Full error is

    TypeError: Cannot read property 'SELECT' of undefined

       6 |   }
       7 |
    >  8 |   if (query.SELECT) {
         |             ^
       9 |     return read(model, dbc, query, req)
      10 |   }
      11 |

      at SQLiteDatabase._run (../../cds/libx/_runtime/db/query/run.js:8:13)
      at SQLiteDatabase.call (../../cds/libx/_runtime/sqlite/Service.js:74:19)
      at next (../../cds/lib/serve/Service-dispatch.js:74:36)
      at SQLiteDatabase.handle (../../cds/lib/serve/Service-dispatch.js:72:16)

@chgeo
Copy link
Member

chgeo commented Oct 4, 2021

Internal ticket is cap/issues#9798

@PierreFritsch
Copy link
Contributor Author

Updated the code sample to reflect O.T.'s internal comment on October 4. What did I miss?

FAIL test/consuming-actions.test.js
  ● Consuming actions locally › calls unbound actions
      21 |
      22 |     // basic variant using srv.send
    > 23 |     const res1 = await cats.send("submitOrder", {
         |                             ^
      24 |       book: 111,
      25 |       quantity: 1,
      26 |     });

      at Request.reject (node_modules/@sap/cds/lib/req/request.js:65:39)
      at _reject (node_modules/@sap/cds/libx/_runtime/common/generic/auth.js:37:16)
      at CatalogService.handler (node_modules/@sap/cds/libx/_runtime/common/generic/auth.js:638:57)
      at CatalogService.handle (node_modules/@sap/cds/lib/serve/Service-dispatch.js:64:53)
      at CatalogService.handle (node_modules/@sap/cds/libx/_runtime/cds-services/services/Service.js:147:18)
      at CatalogService.dispatch (node_modules/@sap/cds/lib/serve/Service-dispatch.js:47:15)
      at CatalogService.dispatch (node_modules/@sap/cds/lib/serve/Service-dispatch.js:20:17)
      at CatalogService.send (node_modules/@sap/cds/lib/serve/Service-api.js:38:17)
      at Object.<anonymous> (test/consuming-actions.test.js:23:29)

@PierreFritsch
Copy link
Contributor Author

PierreFritsch commented Dec 1, 2021

looks like the action was rejected due to missing authorization.

@requires: 'authenticated-user'
action submitOrder ( book: Books:ID, quantity: Integer ) returns { stock: Integer };

you'll have to use a manual transaction to create a continuation with an authenticated-user.

(Thank you S.!)

Add the code sample from the capire section [Unbound Actions / Functions](https://cap.cloud.sap/docs/node.js/services#-unbound-actions--functions) as a test in the code sample repository.

Signed-off-by: Pierre Fritsch <[email protected]>
@PierreFritsch PierreFritsch force-pushed the CallUnboundAction branch 3 times, most recently from 9b35a96 to 3928600 Compare December 7, 2021 07:37
@PierreFritsch PierreFritsch force-pushed the CallUnboundAction branch 2 times, most recently from 4d5de47 to 7fcf60b Compare December 10, 2021 13:06
Signed-off-by: Pierre Fritsch <[email protected]>
@PierreFritsch PierreFritsch marked this pull request as ready for review December 10, 2021 13:08
Copy link
Member

@danjoa danjoa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks pretty good. Thanks Pierre!
Remark: another valid way to bypass access control in tests is this:

cds.User.default = cds.User.Privileged

e.g. also done here

@gregorwolf
Copy link
Contributor

I like the approach providing the user explicitly as this will actually test the authorizations.

@PierreFritsch
Copy link
Contributor Author

(Note: I currently don't have write access to this repository, so I cannot merge this PR myself.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants