Skip to content
This repository was archived by the owner on Jul 19, 2023. It is now read-only.

Commit e965112

Browse files
authored
fix: only pass X-Scope-OrgID header if multi tenant is enabled (#708)
* make jest reuse alias from tsconfig.json * only set X-Scope-OrgID header if multi_tenant
1 parent ef392e8 commit e965112

File tree

7 files changed

+119
-18
lines changed

7 files changed

+119
-18
lines changed

jest.config.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
const path = require('path');
2+
const { pathsToModuleNameMapper } = require('ts-jest');
3+
const { compilerOptions } = require('./tsconfig');
24

35
module.exports = {
46
// TypeScript files (.ts, .tsx) will be transformed by ts-jest to CommonJS syntax, and JavaScript files (.js, jsx) will be transformed by babel-jest.
57
testEnvironment: 'jsdom',
6-
// setupFilesAfterEnv: [path.join(__dirname, 'setupAfterEnv.ts')],
78
testMatch: ['**/?(*.)+(spec|test).+(ts|tsx|js)'],
89
transform: {
910
'^.+\\.(t|j)sx?$': ['@swc/jest'],
@@ -12,9 +13,11 @@ module.exports = {
1213
transformIgnorePatterns: [
1314
// force us to transpile these dependencies
1415
// https://stackoverflow.com/a/69150188
15-
'node_modules/(?!(true-myth|d3|d3-array|internmap|d3-scale|react-notifications-component|graphviz-react))',
16+
'node_modules/(?!(true-myth|d3|d3-array|internmap|d3-scale|react-notifications-component|graphviz-react|pyroscope-oss))',
1617
],
17-
moduleNameMapper: {
18-
'@webapp/util/baseurl': ['<rootDir>/public/app/overrides/util/baseurl'],
19-
},
18+
19+
// Reuse the same modules from typescript
20+
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, {
21+
prefix: '<rootDir>',
22+
}),
2023
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
"@types/lodash.groupby": "^4.6.7",
3434
"@types/lodash.map": "^4.6.13",
3535
"@types/prismjs": "^1.26.0",
36+
"@types/react": "^18.0.0",
3637
"@types/react-datepicker": "^4.3.4",
3738
"@types/react-dom": "^18.0.11",
3839
"@types/react-helmet": "^6.1.5",
3940
"@types/react-outside-click-handler": "^1.3.1",
4041
"@types/react-router-dom": "5.3.0",
41-
"@types/react": "^18.0.0",
4242
"@typescript-eslint/eslint-plugin": "^5.59.5",
4343
"@typescript-eslint/parser": "^5.59.5",
4444
"copy-webpack-plugin": "^11.0.0",
@@ -59,6 +59,7 @@
5959
"prettier": "2.8.8",
6060
"react-svg-loader": "^3.0.3",
6161
"sass": "^1.60.0",
62+
"ts-jest": "^29.1.0",
6263
"tsconfig-paths-webpack-plugin": "^4.0.1",
6364
"webpack": "^5.77.0",
6465
"webpack-cli": "^5.0.1",
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as ogBase from '@pyroscope/webapp/javascript/services/base';
2+
import { requestWithOrgID } from '@webapp/services/base';
3+
import * as tenantSvc from '@phlare/services/tenant';
4+
5+
jest.mock('@pyroscope/webapp/javascript/services/base', () => {
6+
return {
7+
__esModule: true,
8+
...jest.requireActual('@pyroscope/webapp/javascript/services/base'),
9+
};
10+
});
11+
12+
jest.mock('@phlare/services/tenant', () => {
13+
return {
14+
__esModule: true,
15+
tenantIDFromStorage: jest.fn(),
16+
};
17+
});
18+
19+
describe('base', () => {
20+
afterEach(() => {
21+
// restore the spy created with spyOn
22+
jest.restoreAllMocks();
23+
});
24+
25+
it('uses X-Scope-OrgID if set manually', () => {
26+
const spy = jest.spyOn(ogBase, 'request');
27+
28+
requestWithOrgID('/', {
29+
headers: {
30+
'X-Scope-OrgID': 'myID',
31+
},
32+
});
33+
34+
expect(spy).toHaveBeenCalledWith('/', {
35+
headers: {
36+
'X-Scope-OrgID': 'myID',
37+
},
38+
});
39+
});
40+
41+
it('does not set X-Scope-OrgID if tenantID is not available', () => {
42+
const spy = jest.spyOn(ogBase, 'request');
43+
const tenantIdSpy = jest.spyOn(tenantSvc, 'tenantIDFromStorage');
44+
45+
tenantIdSpy.mockReturnValueOnce('');
46+
47+
requestWithOrgID('/');
48+
49+
expect(spy).toHaveBeenCalledWith('/', {
50+
headers: {},
51+
});
52+
});
53+
54+
it('sets X-Scope-OrgID if tenantID is available', () => {
55+
const spy = jest.spyOn(ogBase, 'request');
56+
const tenantIdSpy = jest.spyOn(tenantSvc, 'tenantIDFromStorage');
57+
58+
tenantIdSpy.mockReturnValueOnce('myid');
59+
60+
requestWithOrgID('/');
61+
62+
expect(spy).toHaveBeenCalledWith('/', {
63+
headers: {
64+
'X-Scope-OrgID': 'myid',
65+
},
66+
});
67+
});
68+
});

public/app/overrides/services/base.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ export async function requestWithOrgID(
1919

2020
// Reuse headers if they were passed
2121
if (!config?.headers?.hasOwnProperty('X-Scope-OrgID')) {
22+
const tenantID = tenantIDFromStorage();
2223
headers = {
2324
...config?.headers,
24-
'X-Scope-OrgID': tenantIDFromStorage(),
25+
...(tenantID && { 'X-Scope-OrgID': tenantID }),
2526
};
2627
}
2728

public/app/redux/reducers/tenant.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export const checkTenancyIsRequired = createAsyncThunk<
5151
return Promise.resolve({ tenancy: 'multi_tenant', tenantID });
5252
}
5353

54-
return Promise.resolve({ tenancy: 'single_tenant', tenantID });
54+
return Promise.resolve({ tenancy: 'single_tenant', tenantID: undefined });
5555
},
5656
{
5757
// This check is only valid if we don't know what's the tenancy status yet
@@ -78,9 +78,6 @@ const tenantSlice = createSlice({
7878
setWantsToChange(state) {
7979
state.tenancy = 'wants_to_change';
8080
},
81-
setTenancy(state, action: PayloadAction<TenantState['tenancy']>) {
82-
state.tenancy = action.payload;
83-
},
8481
},
8582
extraReducers: (builder) => {
8683
// This thunk will never reject

tsconfig.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"baseUrl": ".",
33
"compilerOptions": {
44
"plugins": [{ "name": "typescript-plugin-css-modules" }],
5-
// TODO: unify with webpack.common.js
65
"paths": {
76
"@phlare/*": ["./public/app/*"],
87

@@ -37,7 +36,6 @@
3736
"@webapp/services/tags": ["./public/app/overrides/services/tags"],
3837
"@webapp/services/base": ["./public/app/overrides/services/base"],
3938
"@webapp/*": ["./node_modules/pyroscope-oss/webapp/javascript/*"],
40-
// Without this files under pyroscope-oss would resolve to ./node_modules/pyroscope-oss/@mui/base
4139
"@mui/base/*": ["./node_modules/@mui/base/*"]
4240
},
4341
"target": "es5",

yarn.lock

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5145,6 +5145,13 @@ browserslist@^4.12.0, browserslist@^4.14.5, browserslist@^4.18.1, browserslist@^
51455145
node-releases "^2.0.8"
51465146
update-browserslist-db "^1.0.10"
51475147

5148+
bs-logger@0.x:
5149+
version "0.2.6"
5150+
resolved "https://registry.yarnpkg.com/bs-logger/-/bs-logger-0.2.6.tgz#eb7d365307a72cf974cc6cda76b68354ad336bd8"
5151+
integrity sha512-pd8DCoxmbgc7hyPKOvxtqNcjYoOsABPQdcCUjGp3d42VR2CX1ORhk2A87oqqu5R1kk+76nsxZupkmyd+MVtCog==
5152+
dependencies:
5153+
fast-json-stable-stringify "2.x"
5154+
51485155
bser@2.1.1:
51495156
version "2.1.1"
51505157
resolved "https://registry.yarnpkg.com/bser/-/bser-2.1.1.tgz#e6787da20ece9d07998533cfd9de6f5c38f4bc05"
@@ -7704,7 +7711,7 @@ fast-glob@^3.2.11, fast-glob@^3.2.12, fast-glob@^3.2.4, fast-glob@^3.2.9:
77047711
merge2 "^1.3.0"
77057712
micromatch "^4.0.4"
77067713

7707-
fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
7714+
fast-json-stable-stringify@2.x, fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
77087715
version "2.1.0"
77097716
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
77107717
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==
@@ -9623,7 +9630,7 @@ jest-snapshot@^29.5.0:
96239630
pretty-format "^29.5.0"
96249631
semver "^7.3.5"
96259632

9626-
jest-util@^29.5.0:
9633+
jest-util@^29.0.0, jest-util@^29.5.0:
96279634
version "29.5.0"
96289635
resolved "https://registry.yarnpkg.com/jest-util/-/jest-util-29.5.0.tgz#24a4d3d92fc39ce90425311b23c27a6e0ef16b8f"
96299636
integrity sha512-RYMgG/MTadOr5t8KdhejfvUU82MxsCu5MF6KuDUHl+NuwzUt+Sm6jJWxTJVrDR1j5M/gJVCPKQEpWXY+yIQ6lQ==
@@ -9844,7 +9851,7 @@ json5@^1.0.1, json5@^1.0.2:
98449851
dependencies:
98459852
minimist "^1.2.0"
98469853

9847-
json5@^2.1.2, json5@^2.2.0, json5@^2.2.2:
9854+
json5@^2.1.2, json5@^2.2.0, json5@^2.2.2, json5@^2.2.3:
98489855
version "2.2.3"
98499856
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
98509857
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==
@@ -10132,6 +10139,11 @@ lodash.map@^4.6.0:
1013210139
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
1013310140
integrity sha512-worNHGKLDetmcEYDvh2stPCrrQRkP20E4l0iIS7F8EvzMqBBi7ltvFN5m1HvTf1P7Jk1txKhvFcmYsCr8O2F1Q==
1013410141

10142+
lodash.memoize@4.x:
10143+
version "4.1.2"
10144+
resolved "https://registry.yarnpkg.com/lodash.memoize/-/lodash.memoize-4.1.2.tgz#bcc6c49a42a2840ed997f323eada5ecd182e0bfe"
10145+
integrity sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==
10146+
1013510147
lodash.merge@^4.6.2:
1013610148
version "4.6.2"
1013710149
resolved "https://registry.yarnpkg.com/lodash.merge/-/lodash.merge-4.6.2.tgz#558aa53b43b661e1925a0afdfa36a9a1085fe57a"
@@ -10240,7 +10252,7 @@ make-dir@^3.0.0, make-dir@^3.0.2, make-dir@^3.1.0:
1024010252
dependencies:
1024110253
semver "^6.0.0"
1024210254

10243-
make-error@^1.1.1:
10255+
make-error@1.x, make-error@^1.1.1:
1024410256
version "1.3.6"
1024510257
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
1024610258
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==
@@ -13153,6 +13165,13 @@ selfsigned@^2.1.1:
1315313165
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
1315413166
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
1315513167

13168+
semver@7.x:
13169+
version "7.5.1"
13170+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.5.1.tgz#c90c4d631cf74720e46b21c1d37ea07edfab91ec"
13171+
integrity sha512-Wvss5ivl8TMRZXXESstBA4uR5iXgEN/VC5/sOcuXdVLzcdkz4HWetIoRfG5gb5X+ij/G9rw9YoGn3QoQ8OCSpw==
13172+
dependencies:
13173+
lru-cache "^6.0.0"
13174+
1315613175
semver@^6.0.0, semver@^6.1.1, semver@^6.1.2, semver@^6.3.0:
1315713176
version "6.3.0"
1315813177
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
@@ -14071,6 +14090,20 @@ ts-essentials@^9.0.0:
1407114090
resolved "https://registry.yarnpkg.com/ts-essentials/-/ts-essentials-9.3.1.tgz#f2d1e1584ef53c0251012258338421d7cf78a4d0"
1407214091
integrity sha512-9CChSvQMyVRo29Vb1A2jbs+LKo3d/bAf+ndSaX0T8cEiy/HChVaRN/HY5DqUryZ8hZ6uol9bEgCnGmnDbwBR9Q==
1407314092

14093+
ts-jest@^29.1.0:
14094+
version "29.1.0"
14095+
resolved "https://registry.yarnpkg.com/ts-jest/-/ts-jest-29.1.0.tgz#4a9db4104a49b76d2b368ea775b6c9535c603891"
14096+
integrity sha512-ZhNr7Z4PcYa+JjMl62ir+zPiNJfXJN6E8hSLnaUKhOgqcn8vb3e537cpkd0FuAfRK3sR1LSqM1MOhliXNgOFPA==
14097+
dependencies:
14098+
bs-logger "0.x"
14099+
fast-json-stable-stringify "2.x"
14100+
jest-util "^29.0.0"
14101+
json5 "^2.2.3"
14102+
lodash.memoize "4.x"
14103+
make-error "1.x"
14104+
semver "7.x"
14105+
yargs-parser "^21.0.1"
14106+
1407414107
ts-node@^10.4.0:
1407514108
version "10.9.1"
1407614109
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b"
@@ -14932,7 +14965,7 @@ yargs-parser@^20.2.2, yargs-parser@^20.2.3:
1493214965
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-20.2.9.tgz#2eb7dc3b0289718fc295f362753845c41a0c94ee"
1493314966
integrity sha512-y11nGElTIV+CT3Zv9t7VKl+Q3hTQoT9a1Qzezhhl6Rp21gJ/IVTW7Z3y9EWXhuUBC2Shnf+DX0antecpAwSP8w==
1493414967

14935-
yargs-parser@^21.1.1:
14968+
yargs-parser@^21.0.1, yargs-parser@^21.1.1:
1493614969
version "21.1.1"
1493714970
resolved "https://registry.yarnpkg.com/yargs-parser/-/yargs-parser-21.1.1.tgz#9096bceebf990d21bb31fa9516e0ede294a77d35"
1493814971
integrity sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==

0 commit comments

Comments
 (0)