Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import { combineReducers } from "@reduxjs/toolkit";
import { waitForElementToBeRemoved } from "@testing-library/react";
import { Route } from "react-router";
import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
import { ImpersonationModal } from "metabase-enterprise/advanced_permissions/components/ImpersonationModal/ImpersonationModal";
import { shared } from "metabase-enterprise/shared/reducer";
import { advancedPermissionsSlice } from "metabase-enterprise/advanced_permissions/reducer";
import {
setupDatabaseEndpoints,
setupUserAttributesEndpoint,
} from "__support__/server-mocks";
import { createMockDatabase, createMockTable } from "metabase-types/api/mocks";
import {
setupExistingImpersonationEndpoint,
setupMissingImpersonationEndpoint,
} from "__support__/server-mocks/impersonation";
import { createMockImpersonation } from "metabase-types/api/mocks/permissions";
import { getImpersonations } from "metabase-enterprise/advanced_permissions/selectors";
import { AdvancedPermissionsStoreState } from "metabase-enterprise/advanced_permissions/types";
const groupId = 2;
const databaseId = 1;
const selectedAttribute = "foo";
const defaultUserAttributes = ["foo", "bar"];
const setup = async ({
userAttributes = defaultUserAttributes,
hasImpersonation = true,
} = {}) => {
const database = createMockDatabase({
id: databaseId,
tables: [createMockTable()],
});
setupDatabaseEndpoints(database);
fetchMock.get(
{
url: `path:/api/database/${databaseId}/metadata`,
query: { include_hidden: true },
},
database,
);
setupUserAttributesEndpoint(userAttributes);
if (hasImpersonation) {
setupExistingImpersonationEndpoint(
createMockImpersonation({
db_id: databaseId,
group_id: groupId,
attribute: selectedAttribute,
}),
);
} else {
setupMissingImpersonationEndpoint(databaseId, groupId);
}
const { store } = renderWithProviders(
<>
<Route path="/" />
<Route
path="database/:databaseId/impersonated/group/:groupId"
component={ImpersonationModal}
/>
</>,
{
initialRoute: `database/${databaseId}/impersonated/group/${groupId}`,
withRouter: true,
customReducers: {
plugins: combineReducers({
shared: shared.reducer,
advancedPermissionsPlugin: advancedPermissionsSlice.reducer,
}),
},
},
);
await waitForElementToBeRemoved(() => screen.queryByText("Loading..."));
return store;
};
describe("impersonation modal", () => {
it("should render the content", async () => {
await setup();
expect(
await screen.findByText("Map a user attribute to database roles"),
).toBeInTheDocument();
expect(
await screen.findByText(
"When the person runs a query (including native queries), Metabase will impersonate the privileges of the database role you associate with the user attribute.",
),
).toBeInTheDocument();
expect(
await screen.findByRole("link", { name: /learn more/i }),
).toHaveAttribute(
"href",
"https://www.metabase.com/docs/latest/permissions/data.html",
);
expect(
await screen.findByText(
"Make sure the main database credential has access to everything different user groups may need access to. It's what Metabase uses to sync table information.",
),
).toBeInTheDocument();
expect(
await screen.findByRole("link", { name: /edit settings/i }),
).toHaveAttribute("href", "/admin/databases/1");
});
it("should not update impersonation if it has not changed", async () => {
const store = await setup({ userAttributes: ["foo"] });
userEvent.click(screen.getByText(/save/i));
expect(
getImpersonations(store.getState() as AdvancedPermissionsStoreState),
).toHaveLength(0);
});
it("should create impersonation", async () => {
const store = await setup({ hasImpersonation: false });
userEvent.click(await screen.findByText(/pick a user attribute/i));
userEvent.click(await screen.findByText("foo"));
expect(await screen.findByRole("button", { name: /save/i })).toBeEnabled();
userEvent.click(await screen.findByRole("button", { name: /save/i }));
await waitFor(() => {
expect(
getImpersonations(store.getState() as AdvancedPermissionsStoreState),
).toStrictEqual([
{
attribute: "foo",
db_id: 1,
group_id: 2,
},
]);
});
});
it("should update impersonation", async () => {
const store = await setup();
userEvent.click(await screen.findByText(selectedAttribute));
userEvent.click(await screen.findByText("bar"));
expect(await screen.findByRole("button", { name: /save/i })).toBeEnabled();
userEvent.click(await screen.findByRole("button", { name: /save/i }));
await waitFor(() => {
expect(
getImpersonations(store.getState() as AdvancedPermissionsStoreState),
).toStrictEqual([
{
attribute: "bar",
db_id: 1,
group_id: 2,
},
]);
});
});
it("should show only already selected attribute if attributes array is empty", async () => {
await setup({ hasImpersonation: true, userAttributes: [] });
await screen.findByText(selectedAttribute);
expect(await screen.findByRole("button", { name: /save/i })).toBeEnabled();
});
it("should show the link to people settings if there is no impersonation and no attributes", async () => {
await setup({ hasImpersonation: false, userAttributes: [] });
expect(
await screen.findByText(
"To associate a user with a database role, you'll need to give that user at least one user attribute.",
),
).toBeInTheDocument();
expect(
await screen.findByRole("link", { name: /edit user settings/i }),
).toHaveAttribute("href", "/admin/people");
expect(await screen.findByRole("button", { name: /close/i })).toBeEnabled();
});
});