Example #1
0
    def test_cleanup_temporary_query_tables(self, mock_connections, mock_databases_data):
        mock_cursor = Mock()
        mock_connection = Mock()
        mock_cursor_ctx_manager = MagicMock()

        mock_cursor_ctx_manager.__enter__.return_value = mock_cursor
        mock_connection.cursor.return_value = mock_cursor_ctx_manager
        mock_connections.__getitem__.return_value = mock_connection
        mock_databases_data.__getitem__.return_value = {
            "USER": "******",
            "NAME": "my_database" "",
        }

        user = UserFactory()
        user.profile.sso_id = (
            "00000000-0000-0000-0000-000000000000"  # yields a short hexdigest of 12b9377c
        )
        user.profile.save()

        # last run 1 day and 1 hour ago so its materialized view should be deleted
        with freeze_time(datetime.utcnow() - timedelta(days=1, hours=1)):
            query_log_1 = QueryLogFactory.create(run_by_user=user)

        # last run 2 hours ago so its materialized view should be kept
        with freeze_time(datetime.utcnow() - timedelta(hours=2)):
            QueryLogFactory.create(run_by_user=user)

        cleanup_temporary_query_tables()

        expected_calls = [
            call("GRANT _user_12b9377c TO postgres"),
            call(f"DROP TABLE IF EXISTS _user_12b9377c._data_explorer_tmp_query_{query_log_1.id}"),
            call("REVOKE _user_12b9377c FROM postgres"),
        ]
        mock_cursor.execute.assert_has_calls(expected_calls)
Example #2
0
def test_other_users_query_results(user):
    client = Client(raise_request_exception=False, **get_http_sso_data(user))
    ql = QueryLogFactory.create(run_by_user=factories.UserFactory.create())
    response = client.get(reverse("explorer:running_query", args=(ql.id, )))
    assert response.status_code == 403
    assert "You can collaborate on Data Explorer queries" in response.content.decode(
        response.charset)
Example #3
0
    def test_downloading_from_playground(self, staff_user, staff_client):
        querylog = QueryLogFactory.create(sql="select 1, 2", run_by_user=staff_user)
        create_temporary_results_table(querylog)

        resp = staff_client.get(
            reverse("explorer:download_querylog", kwargs=dict(querylog_id=querylog.id))
        )

        assert "attachment" in resp["Content-Disposition"]
        assert "text/csv" in resp["content-type"]
        assert 'filename="Playground_-_select_1_2.csv"' in resp["Content-Disposition"]
Example #4
0
    def test_downloading_from_results_table_redirects_with_error_if_table_is_not_there(
        self, staff_user, staff_client
    ):
        querylog = QueryLogFactory.create(sql="select 1, 2", run_by_user=staff_user)

        # Make sure it's not there
        drop_temporary_results_table(querylog)

        resp = staff_client.get(
            reverse("explorer:download_querylog", kwargs=dict(querylog_id=querylog.id)),
        )

        assert resp.status_code == 302
        assert resp["Location"] == f"/data-explorer/?querylog_id={querylog.id}&error=download"
Example #5
0
 def test_create_chart_not_owner(self, staff_user, client):
     query_log = QueryLogFactory.create(run_by_user=staff_user)
     response = client.get(reverse("explorer:create_chart", args=(query_log.id,)))
     assert response.status_code == 403