|
| 1 | +from django.urls import reverse |
| 2 | +from rest_framework.exceptions import ErrorDetail |
| 3 | + |
| 4 | +from sentry.models.dashboard import DashboardFavoriteUser |
| 5 | +from sentry.testutils.cases import OrganizationDashboardWidgetTestCase |
| 6 | + |
| 7 | + |
| 8 | +class OrganizationDashboardsStarredOrderTest(OrganizationDashboardWidgetTestCase): |
| 9 | + def setUp(self): |
| 10 | + super().setUp() |
| 11 | + self.login_as(self.user) |
| 12 | + self.url = reverse( |
| 13 | + "sentry-api-0-organization-dashboard-starred-order", |
| 14 | + kwargs={"organization_id_or_slug": self.organization.slug}, |
| 15 | + ) |
| 16 | + self.dashboard_1 = self.create_dashboard(title="Dashboard 1") |
| 17 | + self.dashboard_2 = self.create_dashboard(title="Dashboard 2") |
| 18 | + self.dashboard_3 = self.create_dashboard(title="Dashboard 3") |
| 19 | + |
| 20 | + def create_dashboard_favorite(self, dashboard, user, organization, position): |
| 21 | + DashboardFavoriteUser.objects.create( |
| 22 | + dashboard=dashboard, user_id=user.id, organization=organization, position=position |
| 23 | + ) |
| 24 | + |
| 25 | + def test_reorder_dashboards(self): |
| 26 | + self.create_dashboard_favorite(self.dashboard_1, self.user, self.organization, 0) |
| 27 | + self.create_dashboard_favorite(self.dashboard_2, self.user, self.organization, 1) |
| 28 | + self.create_dashboard_favorite(self.dashboard_3, self.user, self.organization, 2) |
| 29 | + |
| 30 | + assert list( |
| 31 | + DashboardFavoriteUser.objects.filter( |
| 32 | + organization=self.organization, user_id=self.user.id |
| 33 | + ) |
| 34 | + .order_by("position") |
| 35 | + .values_list("dashboard_id", flat=True) |
| 36 | + ) == [ |
| 37 | + self.dashboard_1.id, |
| 38 | + self.dashboard_2.id, |
| 39 | + self.dashboard_3.id, |
| 40 | + ] |
| 41 | + |
| 42 | + # Reorder the favorited dashboards |
| 43 | + with self.feature("organizations:dashboards-starred-reordering"): |
| 44 | + response = self.do_request( |
| 45 | + "put", |
| 46 | + self.url, |
| 47 | + data={ |
| 48 | + "dashboard_ids": [self.dashboard_3.id, self.dashboard_1.id, self.dashboard_2.id] |
| 49 | + }, |
| 50 | + ) |
| 51 | + assert response.status_code == 204 |
| 52 | + |
| 53 | + assert list( |
| 54 | + DashboardFavoriteUser.objects.filter( |
| 55 | + organization=self.organization, user_id=self.user.id |
| 56 | + ) |
| 57 | + .order_by("position") |
| 58 | + .values_list("dashboard_id", flat=True) |
| 59 | + ) == [ |
| 60 | + self.dashboard_3.id, |
| 61 | + self.dashboard_1.id, |
| 62 | + self.dashboard_2.id, |
| 63 | + ] |
| 64 | + |
| 65 | + def test_throws_an_error_if_dashboard_ids_are_not_unique(self): |
| 66 | + self.create_dashboard_favorite(self.dashboard_1, self.user, self.organization, 0) |
| 67 | + self.create_dashboard_favorite(self.dashboard_2, self.user, self.organization, 1) |
| 68 | + self.create_dashboard_favorite(self.dashboard_3, self.user, self.organization, 2) |
| 69 | + |
| 70 | + with self.feature("organizations:dashboards-starred-reordering"): |
| 71 | + response = self.do_request( |
| 72 | + "put", |
| 73 | + self.url, |
| 74 | + data={ |
| 75 | + "dashboard_ids": [self.dashboard_1.id, self.dashboard_1.id, self.dashboard_2.id] |
| 76 | + }, |
| 77 | + ) |
| 78 | + assert response.status_code == 400 |
| 79 | + assert response.data == { |
| 80 | + "dashboard_ids": ["Single dashboard cannot take up multiple positions"] |
| 81 | + } |
| 82 | + |
| 83 | + def test_throws_an_error_if_reordered_dashboard_ids_are_not_complete(self): |
| 84 | + self.create_dashboard_favorite(self.dashboard_1, self.user, self.organization, 0) |
| 85 | + self.create_dashboard_favorite(self.dashboard_2, self.user, self.organization, 1) |
| 86 | + self.create_dashboard_favorite(self.dashboard_3, self.user, self.organization, 2) |
| 87 | + |
| 88 | + with self.feature("organizations:dashboards-starred-reordering"): |
| 89 | + response = self.do_request( |
| 90 | + "put", |
| 91 | + self.url, |
| 92 | + data={"dashboard_ids": [self.dashboard_1.id, self.dashboard_2.id]}, |
| 93 | + ) |
| 94 | + assert response.status_code == 400 |
| 95 | + assert response.data == { |
| 96 | + "detail": ErrorDetail( |
| 97 | + string="Mismatch between existing and provided starred dashboards.", |
| 98 | + code="parse_error", |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + def test_allows_reordering_even_if_no_initial_positions(self): |
| 103 | + self.create_dashboard_favorite(self.dashboard_1, self.user, self.organization, 0) |
| 104 | + self.create_dashboard_favorite(self.dashboard_2, self.user, self.organization, 1) |
| 105 | + self.create_dashboard_favorite(self.dashboard_3, self.user, self.organization, 2) |
| 106 | + |
| 107 | + with self.feature("organizations:dashboards-starred-reordering"): |
| 108 | + response = self.do_request( |
| 109 | + "put", |
| 110 | + self.url, |
| 111 | + data={ |
| 112 | + "dashboard_ids": [self.dashboard_3.id, self.dashboard_1.id, self.dashboard_2.id] |
| 113 | + }, |
| 114 | + ) |
| 115 | + assert response.status_code == 204 |
| 116 | + |
| 117 | + assert list( |
| 118 | + DashboardFavoriteUser.objects.filter( |
| 119 | + organization=self.organization, user_id=self.user.id |
| 120 | + ) |
| 121 | + .order_by("position") |
| 122 | + .values_list("dashboard_id", flat=True) |
| 123 | + ) == [self.dashboard_3.id, self.dashboard_1.id, self.dashboard_2.id] |
0 commit comments