-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Embedding QS into Control Panel (#1350)
* Inital commit * Added code from UI to control panel * Code added to enable embedding of quicksight * Removed call to check group as not required * Bumped dependencies. Added new env vars to test settings * Added page name to template * made QS superuser only for now * Removed bucket message on quicksight page. Added tests
- Loading branch information
1 parent
37e7056
commit 661095c
Showing
14 changed files
with
214 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
{% extends "base.html" %} | ||
|
||
{% set page_name = "quicksight" %} | ||
{% set page_title = "Quicksight" %} | ||
|
||
{% block container_class_names %}govuk-grid-column-full{% endblock container_class_names %} | ||
|
||
{% block content %} | ||
{% if embed_url %} | ||
<body onload="embedConsole()"> | ||
<div id="experience-container"></div> | ||
</body> | ||
{% else %} | ||
<div class="govuk-width-container"> | ||
<h2 class="govuk-heading-m">Something went wrong, try refreshing the page</h2> | ||
<p class="govuk-body">If the problem persists, please contact the AP support team.</p> | ||
</div> | ||
{% endif %} | ||
{% endblock content %} | ||
|
||
{% block body_end %} | ||
<script src="https://unpkg.com/[email protected]/dist/quicksight-embedding-js-sdk.min.js"></script> | ||
<script type="text/javascript"> | ||
const embedConsole = async() => { | ||
const { | ||
createEmbeddingContext, | ||
} = QuickSightEmbedding; | ||
|
||
const embeddingContext = await createEmbeddingContext({ | ||
onChange: (changeEvent, metadata) => { | ||
console.log('Context received a change', changeEvent, metadata); | ||
}, | ||
}); | ||
|
||
const frameOptions = { | ||
url: "{{ embed_url|safe }}", // replace this value with the url generated via embedding API | ||
container: '#experience-container', | ||
height: "700px", | ||
width: "100%", | ||
onChange: (changeEvent, metadata) => { | ||
switch (changeEvent.eventName) { | ||
case 'FRAME_MOUNTED': { | ||
console.log("Do something when the experience frame is mounted."); | ||
break; | ||
} | ||
case 'FRAME_LOADED': { | ||
console.log("Do something when the experience frame is loaded."); | ||
break; | ||
} | ||
} | ||
}, | ||
}; | ||
|
||
const contentOptions = { | ||
onMessage: async (messageEvent, experienceMetadata) => { | ||
switch (messageEvent.eventName) { | ||
case 'ERROR_OCCURRED': { | ||
console.log("Do something when the embedded experience fails loading."); | ||
break; | ||
} | ||
} | ||
} | ||
}; | ||
const embeddedConsoleExperience = await embeddingContext.embedConsole(frameOptions, contentOptions); | ||
}; | ||
</script> | ||
{% endblock body_end %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Standard library | ||
from typing import Any | ||
|
||
# Third-party | ||
from django.conf import settings | ||
from django.views.generic import TemplateView | ||
from rules.contrib.views import PermissionRequiredMixin | ||
|
||
# First-party/Local | ||
from controlpanel.api.aws import AWSQuicksight | ||
from controlpanel.oidc import OIDCLoginRequiredMixin | ||
|
||
|
||
class QuicksightView(OIDCLoginRequiredMixin, PermissionRequiredMixin, TemplateView): | ||
template_name = "quicksight.html" | ||
permission_required = "api.quicksight_embed_access" | ||
|
||
def get_context_data(self, **kwargs: Any) -> dict[str, Any]: | ||
context = super().get_context_data(**kwargs) | ||
profile_name = f"quicksight_user_{self.request.user.justice_email}" | ||
context["broadcast_messages"] = None | ||
context["embed_url"] = AWSQuicksight( | ||
assume_role_name=settings.QUICKSIGHT_ASSUMED_ROLE, | ||
profile_name=profile_name, | ||
region_name=settings.QUICKSIGHT_ACCOUNT_REGION, | ||
).get_embed_url(user=self.request.user) | ||
return context |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
import hashlib | ||
import json | ||
import uuid | ||
from unittest.mock import MagicMock, call, patch | ||
from unittest.mock import MagicMock, Mock, call, patch | ||
|
||
# Third-party | ||
import pytest | ||
|
@@ -1134,3 +1134,24 @@ def test_list_attached_policies_returns_list_of_policies(iam, roles, test_policy | |
|
||
assert len(policies) == 1 | ||
assert policies[0].arn == test_policy["Arn"] | ||
|
||
|
||
@pytest.fixture | ||
def quicksight_service(): | ||
yield aws.AWSQuicksight() | ||
|
||
|
||
def test_get_embed_url(quicksight_service): | ||
""" | ||
Patching client as no way to get url from moto. | ||
Should return some URL anyway | ||
""" | ||
|
||
embedded_url = "https://embedded-url.com" | ||
client = Mock() | ||
client.generate_embed_url_for_registered_user.return_value = {"EmbedUrl": embedded_url} | ||
|
||
with patch.object(quicksight_service, "client", client): | ||
mock_user = Mock(email="[email protected]") | ||
url = quicksight_service.get_embed_url(mock_user) | ||
assert url == embedded_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Standard library | ||
from unittest.mock import patch | ||
|
||
# Third-party | ||
import botocore | ||
import pytest | ||
from django.conf import settings | ||
from django.urls import reverse | ||
from rest_framework import status | ||
|
||
# Original botocore _make_api_call function | ||
orig = botocore.client.BaseClient._make_api_call | ||
|
||
|
||
def quicksight(client): | ||
return client.get(reverse("quicksight")) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"view,user,expected_status", | ||
[ | ||
(quicksight, "superuser", status.HTTP_200_OK), | ||
(quicksight, "database_user", status.HTTP_403_FORBIDDEN), | ||
(quicksight, "normal_user", status.HTTP_403_FORBIDDEN), | ||
], | ||
) | ||
def test_permission(client, users, view, user, expected_status): | ||
for key, val in users.items(): | ||
client.force_login(val) | ||
client.force_login(users[user]) | ||
response = view(client) | ||
assert response.status_code == expected_status |