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

feat: Embedded SDK #18250

Merged
merged 23 commits into from
Feb 3, 2022
Merged

feat: Embedded SDK #18250

merged 23 commits into from
Feb 3, 2022

Conversation

suddjian
Copy link
Member

@suddjian suddjian commented Feb 2, 2022

SUMMARY

Part of #17187.

Implements the frontend sdk that will be used by host apps to embed dashboards.

See the included documentation files for more info on how this package works.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

TESTING INSTRUCTIONS

This will be easier to test once we add a demo app that uses it. For the time being if you want to test this you'd have to try the feature out in your own app. The added README.md file explains how to do this.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@pull-request-size pull-request-size bot added size/XL and removed size/L labels Feb 2, 2022

## Testing

You may notice a lack of tests in this directory. The functions used in the sdk so far are very closely tied to browser behavior,
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure about this. It would be really cool if we could spend more time trying to use Jest/JSDOM to write tests that check for specific conditions like:

  • Failure when invoking fetchGuestToken
  • An unknown id
  • An invalid supersetDomain
  • Test the unmount behavior

You could use "testEnvironmentOptions": { "resources": "usable" } in jest.config.js to enable sub-resouces like iframes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm, I think you're right that it would be good to unit test the fetchGuestToken handling. And it would be nice to at least have one test to ensure that the code doesn't blow up. As for the others though, in my experience, unit testing helps most when you have branching logic in your code, and this code currently doesn't contain even one if statement. It just executes a series of instructions, all of which are interactions with external APIs. So, unit testing it beyond the "it doesn't blow up" case will essentially be asserting that the code exists as written. Those kinds of tests tend to just get in the way of changing the code instead of really helping much.

I'll also add an "on pull request" github action that makes sure the build can still run.

Copy link
Member

Choose a reason for hiding this comment

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

I think there are valid test cases for this code and some of the test cases can even introduce if or try/catch blocks in the logic. Some examples:

If a user passes an unknown dashboard id like one from a deleted dashboard, what happens when this piece of code executes?

iframe.src = `${supersetDomain}/dashboard/${id}/embedded`;

If the user passes an invalid supersetDomain what happens when this piece of code executes?

iframe.contentWindow!.postMessage(
  { type: IFRAME_COMMS_MESSAGE_TYPE, handshake: "port transfer" },
  supersetDomain,
  [theirPort],
)

For both scenarios, are we going to display the default browser error or a custom message inside the iframe? if we chose to display a custom message, the test cases could account for that.

Copy link
Member Author

@suddjian suddjian Feb 2, 2022

Choose a reason for hiding this comment

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

For case 1, the SDK does not have any clue that the id doesn't exist. All it does is create an iframe and send a guest token to it. If we want to display some special message in that case, that would happen on the superset side, not in the SDK.

In the second case, what happens is entirely dependent on browser implementation. Some browsers could throw an error, which would be fine imo since the caller passed in the wrong thing it should probably throw anyway. But some browsers silently ignore the message, I believe this is to prevent a malicious page from gathering info about other pages.

Copy link
Member

Choose a reason for hiding this comment

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

In the case a browser silently ignores the message, won't this line fail later?

ourPort.postMessage({ guestToken });

Copy link
Member Author

Choose a reason for hiding this comment

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

It will fail to actually convey the guest token to its intended recipient since no one is listening on the other side, but it won't throw an error. The message channel and port do exist, they're just not connected to anything.

Copy link
Member Author

@suddjian suddjian Feb 2, 2022

Choose a reason for hiding this comment

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

I wrote a test for "it doesn't blow up", but actually jsdom doesn't seem to support the iframe's sandbox property. It's just not present on the iframe element. The jsdom changelog says that it was removed many versions ago.

Unless someone knows of a good way around that, I think I'll forego unit testing in favor of thorough cypress testing using the demo host app, which should be added soon. That way we can make actual assertions about the behaviors and security characteristics of this system. For now, this code has been manually tested pretty thoroughly in a couple of applications, and we're focused on getting it usable asap.

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
@@ -0,0 +1,40 @@
name: embedded-sdk-release-workflow

on:
Copy link
Member Author

Choose a reason for hiding this comment

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

Hopefully this workflow doesn't have any issues, or I'll need a follow-up PR to fix it. Reviewers please help me check this! 🙏

Copy link
Member Author

Choose a reason for hiding this comment

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

I tested and got it working on my fork, so I can be pretty confident that it will work here.

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

A few comments relating to GHA workflows

Comment on lines +4 to +6
pull_request:
paths:
- "superset-embedded-sdk/**"
Copy link
Member

Choose a reason for hiding this comment

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

In other similar flows we have slightly more specific ons to avoid CI running on Draft PRs. Something like this perhaps?

on:
  push:
    branches:
      - 'master'
  pull_request:
    paths:
      - "superset-embedded-sdk/**"
    types: [synchronize, opened, reopened, ready_for_review]

Copy link
Member Author

Choose a reason for hiding this comment

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

nice, thanks!

Comment on lines +1 to +3
bundle
dist
lib
Copy link
Member

Choose a reason for hiding this comment

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

Any need to add node_modules or other typical cruft here?

Copy link
Member Author

Choose a reason for hiding this comment

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

nah, the root .gitignore file also applies here

@suddjian suddjian merged commit 1c2936b into apache:master Feb 3, 2022
shcoderAlex pushed a commit to casual-precision/superset that referenced this pull request Feb 7, 2022
* feat: embedded sdk

* correct values

* better version

* readme stuff

* release script

* doc

* oops

* better package description

* license

* that was invalid json

* Apply suggestions from code review

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>

* Update superset-embedded-sdk/README.md

* a github workflow to make sure the build succeeds

* fix github workflows

* writing

* try a different trigger

* no point in a single unit matrix

* Revert "no point in a single unit matrix"

This reverts commit 90f78bf.

* workflow changes

* fix some scripts

* pull request types

* slight rename

* test list

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
ofekisr pushed a commit to nielsen-oss/superset that referenced this pull request Feb 8, 2022
* feat: embedded sdk

* correct values

* better version

* readme stuff

* release script

* doc

* oops

* better package description

* license

* that was invalid json

* Apply suggestions from code review

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>

* Update superset-embedded-sdk/README.md

* a github workflow to make sure the build succeeds

* fix github workflows

* writing

* try a different trigger

* no point in a single unit matrix

* Revert "no point in a single unit matrix"

This reverts commit 90f78bf.

* workflow changes

* fix some scripts

* pull request types

* slight rename

* test list

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
bwang221 pushed a commit to casual-precision/superset that referenced this pull request Feb 10, 2022
* feat: embedded sdk

* correct values

* better version

* readme stuff

* release script

* doc

* oops

* better package description

* license

* that was invalid json

* Apply suggestions from code review

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>

* Update superset-embedded-sdk/README.md

* a github workflow to make sure the build succeeds

* fix github workflows

* writing

* try a different trigger

* no point in a single unit matrix

* Revert "no point in a single unit matrix"

This reverts commit 90f78bf.

* workflow changes

* fix some scripts

* pull request types

* slight rename

* test list

Co-authored-by: Michael S. Molina <70410625+michael-s-molina@users.noreply.github.com>
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 1.5.0 labels Mar 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels preset-io size/XL 🚢 1.5.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants