Example icon indicating copy to clipboard operation
Example copied to clipboard

Enable `api.js` test cases that were omitted

Open KLarpen opened this issue 1 year ago • 3 comments

Closes: https://github.com/metarhia/Example/issues/263

The cause appears to be a tiny typo in a first guard line at https://github.com/metarhia/Example/blob/2435b8e643af3fc4a62fea1f3f20bdedc1e1d9c1/application/domain/tests/api.js#L2-L3

Reverse of the check enables assertions and cases from the script. Console result of the npm t now shows its execution

13:14:16  W1   debug   ✅ Passed: Get room with domain.chat.getRoom (0.234334ms)
13:14:16  W1   debug   ✅ Passed: Send message with domain.chat.send (0.09725ms)
13:14:16  W1   debug   ✅ Passed: Chat test (1.097958ms)
13:14:16  W2   debug   127.0.0.1        POST    /api    200
13:14:16  W2   log     127.0.0.1        system/introspect
13:14:16  W2   debug   SELECT * FROM "Account" WHERE "login" = $1       [marcus]
13:14:17  W2   log     Logged user: marcus
13:14:17  W2   log     {
  createSession: {
    token: 'aNzY51hcAKpQVXtoS9xXzpnBGNF83NdMZ9YYEwG4ofbqhbZ2NG4u8MVV3FdW5db7',
    data: '{"accountId":"2"}',
    ip: '127.0.0.1',
    accountId: '2'
  }
}
13:14:17  W2   debug   INSERT INTO "Session" ("token", "data", "ip", "accountId") VALUES ($1, $2, $3, $4) RETURNING *   [aNzY51hcAKpQVXtoS9xXzpnBGNF83NdMZ9YYEwG4ofbqhbZ2NG4u8MVV3FdW5db7,{"accountId":"2"},127.0.0.1,2]
13:14:17  W2   debug   127.0.0.1        POST    /api    200
13:14:17  W2   log     127.0.0.1        auth/signin
13:14:17  W2   debug   127.0.0.1        POST    /api    200
13:14:17  W2   log     127.0.0.1        example/add
13:14:17  W1   debug   ✅ Passed: Call example.add({ a, b }) (2.3935ms)
13:14:17  W1   debug   ✅ Passed: Metacom over HTTP (112.830125ms)
13:14:17  W1   debug   127.0.0.1        GET     /demo.txt       200
13:14:17  W1   debug   ✅ Passed: Test to serve without cache (5.920542ms)
13:14:17  W3   debug   127.0.0.1        GET     /       200
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/ (6.396459ms)
13:14:17  W1   debug   127.0.0.1        GET     /console.js     200
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/console.js (2.886916ms)
13:14:17  W1   error   127.0.0.1        GET     /unknown        404
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/unknown (5.30275ms)
13:14:17  W1   error   127.0.0.1        GET     /unknown.png    404
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/unknown.png (1.873792ms)
13:14:17  W1   error   127.0.0.1        GET     /unknown/unknown        404
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/unknown/unknown (1.914459ms)
13:14:17  W1   error   127.0.0.1        GET     /unknown/unknown.png    404
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/unknown/unknown.png (1.583125ms)
13:14:17  W1   debug   127.0.0.1        GET     /article        200
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/article (1.344333ms)
13:14:17  W1   debug   127.0.0.1        GET     /article/file.txt       200
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/article/file.txt (1.523125ms)
13:14:17  W1   debug   127.0.0.1        GET     /article/name   200
13:14:17  W1   debug   ✅ Passed: Get static resource: http://127.0.0.1:8000/article/name (1.151792ms)
13:14:17  W1   debug   ✅ Passed: Static server test (31.472667ms)
13:14:17  W2   log     127.0.0.1        system/introspect
13:14:17  W2   debug   SELECT * FROM "Account" WHERE "login" = $1       [marcus]
13:14:17  W2   log     Logged user: marcus
13:14:17  W2   log     {
  createSession: {
    token: '1GSI5hJQuIX98Cb6uMVrctQDhpPUDfqLi9s0U2wowKN6t7MUSrm5YDcS69Bpf262',
    data: '{"accountId":"2"}',
    ip: '127.0.0.1',
    accountId: '2'
  }
}
13:14:17  W2   debug   INSERT INTO "Session" ("token", "data", "ip", "accountId") VALUES ($1, $2, $3, $4) RETURNING *   [1GSI5hJQuIX98Cb6uMVrctQDhpPUDfqLi9s0U2wowKN6t7MUSrm5YDcS69Bpf262,{"accountId":"2"},127.0.0.1,2]
13:14:17  W2   log     127.0.0.1        auth/signin
13:14:17  W2   log     127.0.0.1        example/add
13:14:17  W1   debug   ✅ Passed: Call example.add({ a, b }) (0.380292ms)
13:14:17  W1   debug   ✅ Passed: Metacom over Websocket (76.290041ms)
13:14:17  W1   debug   🟢 Passed 18, Failed: 0
  • [x] tests and linter show no problems (npm t)
  • [x] tests are added/updated
  • [x] code is properly formatted (npm run fmt)

KLarpen avatar Dec 18 '23 13:12 KLarpen

As an extra enhancement, I had wrapped signin test result assertions with named test case. That gives ability for test runner to measure execution time of the metacom.api.auth.signin operation.

13:31:09  W1   debug   ✅ Passed: Sign into session (91.059375ms)
13:31:09  W1   debug   ✅ Passed: Metacom over HTTP (110.810125ms)
...
13:31:09  W1   debug   ✅ Passed: Sign into session (65.835917ms)
13:31:09  W1   debug   ✅ Passed: Metacom over Websocket (71.862083ms)
13:31:09  W1   debug   🟢 Passed 20, Failed: 0

KLarpen avatar Dec 18 '23 13:12 KLarpen

@tshemsedinov, beside fix and enhancement I'm looking to add another test case that calls API endpoint with logged access. Because Call example.add({ a, b }) currently public, so it doesn't test actual access after sign into session.

Created test case

await t.test(`Call logged endpoint`, async () => {
  const res = await metacom.api.example.wait({ delay: 1 });
  node.assert.strictEqual(res, 'done');
});

However I didn't place it into commit because it reveals that something not work with access to the logged endpoint through the HTTP transport at least during test run. WS transport at the same time passed this test case successfully.

13:42:05  W1   debug   ✅ Passed: Sign into session (82.932666ms)
13:42:05  W2   debug   127.0.0.1        POST    /api    200
13:42:05  W2   log     127.0.0.1        example/add
13:42:05  W1   debug   ✅ Passed: Call example.add({ a, b }) (2.259083ms)
13:42:05  W2   error   127.0.0.1        POST    /api    403     403     Forbidden
13:42:05  W2   error   127.0.0.1        POST    /api    403
13:42:05  W1   error   ❌ Failed: Call logged endpoint (2.017292ms)
Error: Forbidden
  HttpTransport.message (/node_modules/metacom/lib/client.js:114:28)
  /node_modules/metacom/lib/client.js:277:14
  process.processTicksAndRejections (node:internal/process/task_queues:95:5)
...
13:42:05  W1   debug   ✅ Passed: Sign into session (66.233208ms)
13:42:05  W2   log     127.0.0.1        example/add
13:42:05  W1   debug   ✅ Passed: Call example.add({ a, b }) (0.3925ms)
13:42:05  W2   log     127.0.0.1        example/wait
13:42:05  W1   debug   ✅ Passed: Call logged endpoint (1.755042ms)
13:42:05  W1   debug   ✅ Passed: Metacom over 

KLarpen avatar Dec 18 '23 13:12 KLarpen

GitHub Workflow testing shows Error: connect ECONNREFUSED 127.0.0.1:5432 so the problem is that https://github.com/metarhia/Example/blob/master/.github/workflows/test.yml doesn't contain PostgreSQL container, but test for auth requests require the DB. I guess the if (t) return; guard was place specifically to switch off this test cases from execution during GH workflow.

@MarhiievHE already done great job of enhancing workflow environment in the branch client-test of his fork but it still not merged and PR https://github.com/metarhia/Example/pull/225 is open now (there is a lot of other stuff done). Part of that PR includes, for example:

  • add redis config https://github.com/MarhiievHE/Example/commit/0842251ce00d475e4a61020add39009700759abd
  • Add redis instance (into docker-compose) https://github.com/MarhiievHE/Example/commit/954dbb17f6cc9202c941186024a961104058ebdc
  • GitHub workflow testing with pg and Redis containers https://github.com/MarhiievHE/Example/commit/67317daa83aa825d1c9be49e0c79fa6819c4f9c3

Please give an advice @tshemsedinov . If the PR https://github.com/metarhia/Example/pull/225 is considered too big to land it now as a whole... maybe it's worth to extract some useful part of it into separate PR that you may be confident for apply?

@MarhiievHE thank's for your contribution! What is yours opinion?

KLarpen avatar Dec 18 '23 19:12 KLarpen