stackvana / hook.io

Open-Source Microservice Hosting Platform
https://hook.io
Other
1.27k stars 119 forks source link

Test Code behavior different from Run Hook #171

Closed monkbroc closed 8 years ago

monkbroc commented 8 years ago

I've seen several difference in behavior between the Test Code button and the running the actual hook.

Here's one:

https://hook.io/admin?owner=monkbroc&name=test-test

module['exports'] = function echoHttp (hook) {
  var res = hook.res, 
      store = hook.datastore;
  // 'counter' is an integer
  store.get('counter', function (err, counter) {
    if (err) { return res.end(err.message); }
    res.end(counter);
  });
};

Running the hook outputs 45

Clicking Test Code outputs [object Object]1111

Marak commented 8 years ago

Good catch! That's good to know.

First off, all "test hook" code is run through the live code execution gateway. This is a slightly separate code path than normal deployment.

see: https://github.com/bigcompany/hooks/blob/master/gateway-javascript/index.js see: https://hook.io/Marak/gateway-javascript/source

Secondly, in this case, I believe that the datastore is going to be scoped to the owner of the gateway-javascript hook, which is "anonymous". This means currently you aren't actually able to interact with your own data through the "test code" button.

It's possible datastore is not working correctly for the testing gateway, but it's also likely that the counter variable, which is scoped to "anonymous", has been set to a weird value.

I think it would be best to have all test code gateways be scoped to a general anonymous user when no session, and if a session is present, scoped to that user's data.

monkbroc commented 8 years ago

OK. Thanks. I suppose it means the env is from the anonymous user too. That explains the inconsistencies of the same hook not working in test mode, but working in deployment.

Marak commented 8 years ago

@monkbroc - For sure. That makes a lot of sense.

I've added a new issue tracking at #172

This is medium priority right now.

Marak commented 8 years ago

Leaving open for now. It's possible we might find other discrepancies and can track them to this issue.

Once we know they both act the same, we can close.

Marak commented 8 years ago

This should be resolved locally.

datastore and env properties are now scoped to the currently logged in user for the hot-code / test-code gateways.

The reason there is a scoping issue for the hot-code gateways is that every gateway is owned by user "marak". We could mitigate this issue by having every user fork the existing hot-code gateways to their own account ( since they are just hooks ), and that would ensure the correct scope always exists for the gateways.

Since it's not very convenient for every user to create a copy of the hot-code gateway, I added some special logic for binding the "marak" owned hot-code gateways to the currently logged in session. This should resolve any current issues with testing code.

test code logs are still not scoped to the logged in user, because the hot-code logging out will go the log entry point for the "marak" owned gateway.

@monkbroc I'll let you know when this is deployed and ready for testing.

monkbroc commented 8 years ago

Thanks @Marak!

Marak commented 8 years ago

@monkbroc -

I've pushed and deployed this now. Still haven't fully tested it in production, but I assume it's working.

All hot-code gateways should be respect the scope of the currently logged in user.

This should result in no more cross contamination of Datastore or Env data to the anoymous user account.

Thanks again for your feedback!