Skip to content
Snippets Groups Projects
Unverified Commit ad0f099d authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Fix sending a test subscription with non-default parameters (#18965)

parent d32a252d
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,13 @@ import React, { Component } from "react";
import PropTypes from "prop-types";
import { t } from "ttag";
import { cleanPulse } from "metabase/lib/pulse";
import ActionButton from "metabase/components/ActionButton";
export default class SendTestPulse extends Component {
static propTypes = {
channel: PropTypes.object.isRequired,
channelSpecs: PropTypes.object.isRequired,
pulse: PropTypes.object.isRequired,
testPulse: PropTypes.func.isRequired,
disabled: PropTypes.bool.isRequired,
......@@ -16,8 +18,11 @@ export default class SendTestPulse extends Component {
static defaultProps = {};
onTestPulseChannel = () => {
const { pulse, channel, testPulse } = this.props;
return testPulse({ ...pulse, channels: [channel] });
const { pulse, channel, channelSpecs, testPulse } = this.props;
const channelPulse = { ...pulse, channels: [channel] };
const cleanedPulse = cleanPulse(channelPulse, channelSpecs);
return testPulse(cleanedPulse);
};
render() {
......
......@@ -4,6 +4,7 @@ import MetabaseUtils from "metabase/lib/utils";
import {
hasDefaultParameterValue,
hasParameterValue,
normalizeParameterValue,
} from "metabase/parameters/utils/parameter-values";
export const NEW_PULSE_TEMPLATE = {
......@@ -119,12 +120,33 @@ export function emailIsEnabled(pulse) {
export function cleanPulse(pulse, channelSpecs) {
return {
...pulse,
channels: pulse.channels.filter(c =>
channelIsValid(c, channelSpecs && channelSpecs[c.channel_type]),
),
channels: cleanPulseChannels(pulse.channels, channelSpecs),
parameters: cleanPulseParameters(getPulseParameters(pulse)),
};
}
function cleanPulseChannels(channels, channelSpecs) {
return channels.filter(c =>
channelIsValid(c, channelSpecs && channelSpecs[c.channel_type]),
);
}
function cleanPulseParameters(parameters) {
return parameters.map(parameter => {
const { default: defaultValue, name, slug, type, value, id } = parameter;
const normalizedValue = normalizeParameterValue(type, value);
return {
default: defaultValue,
id,
name,
slug,
type,
value: normalizedValue,
};
});
}
export function getDefaultChannel(channelSpecs) {
// email is the first choice
if (channelSpecs.email && channelSpecs.email.configured) {
......
......@@ -131,6 +131,7 @@ function _AddEditEmailSidebar({
<div className="pt2 pb1">
<SendTestPulse
channel={channel}
channelSpecs={formInput.channels}
pulse={pulse}
testPulse={testPulse}
normalText={t`Send email now`}
......@@ -329,6 +330,7 @@ function _AddEditSlackSidebar({
<div className="pt2 pb1">
<SendTestPulse
channel={channel}
channelSpecs={formInput.channels}
pulse={pulse}
testPulse={testPulse}
normalText={t`Send to Slack now`}
......
......@@ -13,14 +13,12 @@ import {
import Sidebar from "metabase/dashboard/components/Sidebar";
import Pulses from "metabase/entities/pulses";
import User from "metabase/entities/users";
import { normalizeParameterValue } from "metabase/parameters/utils/parameter-values";
import { connect } from "react-redux";
import {
cleanPulse,
createChannel,
getPulseParameters,
NEW_PULSE_TEMPLATE,
} from "metabase/lib/pulse";
......@@ -207,27 +205,6 @@ class SharingSidebar extends React.Component {
const cleanedPulse = cleanPulse(pulse, formInput.channels);
cleanedPulse.name = dashboard.name;
cleanedPulse.parameters = getPulseParameters(cleanedPulse).map(
parameter => {
const {
default: defaultValue,
name,
slug,
type,
value,
id,
} = parameter;
const normalizedValue = normalizeParameterValue(type, value);
return {
default: defaultValue,
id,
name,
slug,
type,
value: normalizedValue,
};
},
);
try {
this.setState({ isSaving: true });
......
import {
describeWithToken,
popover,
restore,
setupSMTP,
sidebar,
} from "__support__/e2e/cypress";
import { USERS } from "__support__/e2e/cypress_data";
import { SAMPLE_DATASET } from "__support__/e2e/cypress_sample_dataset";
const { admin } = USERS;
const { PRODUCTS_ID, PRODUCTS } = SAMPLE_DATASET;
describeWithToken("issue 18669", () => {
beforeEach(() => {
restore();
cy.signInAsAdmin();
setupSMTP();
cy.createQuestionAndDashboard({ questionDetails, dashboardDetails }).then(
({ body: card }) => {
cy.editDashboardCard(card, getFilterMapping(card));
cy.visit(`/dashboard/${card.dashboard_id}`);
},
);
});
it("should send a test email with non-default parameters (metabase#18669)", () => {
cy.icon("share").click();
cy.findByText("Dashboard subscriptions").click();
cy.findByText("Email it").click();
cy.findByPlaceholderText("Enter user names or email addresses")
.click()
.type(`${admin.first_name} ${admin.last_name}{enter}`)
.blur();
sidebar().within(() => {
cy.findByText("Doohickey").click();
});
popover().within(() => {
cy.findByText("Gizmo").click();
cy.button("Update filter").click();
});
cy.button("Send email now").click();
cy.findByText("Email sent", { timeout: 10000 });
});
});
const questionDetails = {
name: "Product count",
database: 1,
type: "query",
query: {
"source-table": PRODUCTS_ID,
aggregation: [["count"]],
},
};
const filterDetails = {
name: "Category",
slug: "category",
id: "c32a49e1",
type: "category",
default: ["Doohickey"],
};
const dashboardDetails = {
parameters: [filterDetails],
};
const getFilterMapping = card => ({
parameter_mappings: [
{
parameter_id: filterDetails.id,
card_id: card.card_id,
target: ["dimension", ["field", PRODUCTS.CATEGORY, null]],
},
],
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment