Browse Source

fix/redirect-users-to-discover-v2 (#19945)

Currently, if an user without access to discover v1 tries to visits
`/organizations/:orgId/discover/queries/`, they will be presented with an error.
This change will redirect these users to the new discover v2.
Tony 4 years ago
parent
commit
7ce52d8fb7

+ 16 - 9
src/sentry/static/sentry/app/views/discover/index.tsx

@@ -1,5 +1,5 @@
 import React from 'react';
-import {browserHistory} from 'react-router';
+import {browserHistory, WithRouterProps} from 'react-router';
 import DocumentTitle from 'react-document-title';
 
 import {getUserTimezone, getUtcToLocalDateObject} from 'app/utils/dates';
@@ -10,6 +10,8 @@ import withOrganization from 'app/utils/withOrganization';
 import Feature from 'app/components/acl/feature';
 import Alert from 'app/components/alert';
 import {GlobalSelection, Organization} from 'app/types';
+import {getDiscoverLandingUrl} from 'app/utils/discover/urls';
+import Redirect from 'app/utils/redirect';
 
 import Discover from './discover';
 import createQueryBuilder from './queryBuilder';
@@ -22,14 +24,12 @@ import {
 import {DiscoverWrapper} from './styles';
 import {SavedQuery} from './types';
 
-const AlertAsAny: any = Alert;
-
 type Props = {
   organization: Organization;
   selection: GlobalSelection;
   params: any;
   location: any;
-};
+} & Pick<WithRouterProps, 'router'>;
 
 type State = {
   isLoading: boolean;
@@ -204,11 +204,18 @@ class DiscoverContainer extends React.Component<Props, State> {
     });
   };
 
-  renderNoAccess() {
-    return (
-      <AlertAsAny type="warning">{t("You don't have access to this feature")}</AlertAsAny>
-    );
-  }
+  renderNoAccess = () => {
+    const {router, organization} = this.props;
+
+    if (
+      organization.features.includes('discover-query') ||
+      organization.features.includes('discover-basic')
+    ) {
+      return <Redirect router={router} to={getDiscoverLandingUrl(organization)} />;
+    } else {
+      return <Alert type="warning">{t("You don't have access to this feature")}</Alert>;
+    }
+  };
 
   render() {
     const {isLoading, savedQuery, view} = this.state;

+ 6 - 3
tests/acceptance/test_organization_discover.py

@@ -53,9 +53,12 @@ class OrganizationDiscoverTest(AcceptanceTestCase, SnubaTestCase):
         self.path = u"/organizations/{}/discover/".format(self.org.slug)
 
     def test_no_access(self):
-        self.browser.get(self.path)
-        self.browser.wait_until_not(".loading")
-        self.browser.snapshot("discover - no access")
+        with self.feature(
+            {"organizations:discover-basic": False, "organizations:discover-query": False}
+        ):
+            self.browser.get(self.path)
+            self.browser.wait_until_not(".loading")
+            self.browser.snapshot("discover - no access")
 
     def test_query_builder(self):
         with self.feature("organizations:discover"):

+ 43 - 1
tests/js/spec/views/discover/index.spec.jsx

@@ -265,7 +265,49 @@ describe('DiscoverContainer', function() {
   });
 
   describe('no access', function() {
-    it('display no access message', async function() {
+    it('redirects to discover query if they have access to discover-query', function() {
+      const organization = TestStubs.Organization({
+        projects: [TestStubs.Project()],
+        features: ['discover-query'],
+      });
+      const router = TestStubs.router();
+      mountWithTheme(
+        <DiscoverContainer
+          location={{query: {}, search: ''}}
+          params={{}}
+          selection={{datetime: {}}}
+          organization={organization}
+          router={router}
+        />,
+        TestStubs.routerContext()
+      );
+      expect(router.replace).toHaveBeenCalledWith(
+        `/organizations/${organization.slug}/discover/queries/`
+      );
+    });
+
+    it('redirects to discover results if they have access to discover-basic', function() {
+      const organization = TestStubs.Organization({
+        projects: [TestStubs.Project()],
+        features: ['discover-basic'],
+      });
+      const router = TestStubs.router();
+      mountWithTheme(
+        <DiscoverContainer
+          location={{query: {}, search: ''}}
+          params={{}}
+          selection={{datetime: {}}}
+          organization={organization}
+          router={router}
+        />,
+        TestStubs.routerContext()
+      );
+      expect(router.replace).toHaveBeenCalledWith(
+        `/organizations/${organization.slug}/discover/results/`
+      );
+    });
+
+    it('shows no feature alert if they have no access', function() {
       const organization = TestStubs.Organization({projects: [TestStubs.Project()]});
       const wrapper = mountWithTheme(
         <DiscoverContainer