Skip to content

Commit 2f65938

Browse files
Merge pull request #84 from cap-js/DINC0367584
DINC0367584 :Issue with repository Id caching in multitenacy SDM CAP-JS
2 parents 5182681 + df3b4bb commit 2f65938

File tree

4 files changed

+83
-11
lines changed

4 files changed

+83
-11
lines changed

lib/sdm.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ module.exports = class SDMAttachmentsService extends (
3939

4040
async checkRepositoryType(req) {
4141
const { repositoryId } = getConfigurations();
42+
let subdomain = cds.context.user?.tokenInfo?.getPayload()?.ext_attr?.zdn;
4243
//check if repository is versionable
43-
let repotype = cache.get(repositoryId)
44+
let repotype = cache.get(repositoryId+"_"+subdomain);
4445
let isVersioned;
4546
if (repotype == undefined) {
4647
const token = await getClientCredentialsToken(this.creds);

lib/util/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,10 @@ function isRepositoryVersioned(repoInfo, repositoryId) {
7070
}
7171

7272
function saveRepoToCache(repositoryId, type) {
73-
const repoType = cache.get(repositoryId);
73+
let subdomain = cds.context.user?.tokenInfo?.getPayload()?.ext_attr?.zdn;
74+
const repoType = cache.get(repositoryId+"_"+subdomain);
7475
if (repoType === undefined) {
75-
cache.set(repositoryId, type, 60 * 60 * 24 * 60);
76+
cache.set((repositoryId+"_"+subdomain), type, 60 * 60 * 24 * 60);
7677
}
7778
}
7879

test/lib/sdm.test.js

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,18 @@ describe("SDMAttachmentsService", () => {
101101
},
102102
},
103103
};
104-
104+
cds = require("@sap/cds/lib");
105+
cds.context = {
106+
user: {
107+
tokenInfo: {
108+
getPayload: jest.fn(() => ({
109+
ext_attr: {
110+
zdn: 'subdomain' // simulate the subdomain extraction
111+
}
112+
})),
113+
},
114+
},
115+
};
105116
const attachments = ["attachment1", "attachment2"];
106117
const keys = ["key1", "key2"];
107118
const response = { url: "mockUrl" };
@@ -132,6 +143,18 @@ describe("SDMAttachmentsService", () => {
132143
},
133144
},
134145
};
146+
cds = require("@sap/cds/lib");
147+
cds.context = {
148+
user: {
149+
tokenInfo: {
150+
getPayload: jest.fn(() => ({
151+
ext_attr: {
152+
zdn: 'subdomain' // simulate the subdomain extraction
153+
}
154+
})),
155+
},
156+
},
157+
};
135158
const attachments = ["attachment1", "attachment2"];
136159
const keys = ["key1", "key2"];
137160
const response = { url: "mockUrl" };
@@ -165,6 +188,18 @@ describe("SDMAttachmentsService", () => {
165188
},
166189
reject: jest.fn(),
167190
};
191+
cds = require("@sap/cds/lib");
192+
cds.context = {
193+
user: {
194+
tokenInfo: {
195+
getPayload: jest.fn(() => ({
196+
ext_attr: {
197+
zdn: 'subdomain' // simulate the subdomain extraction
198+
}
199+
})),
200+
},
201+
},
202+
};
168203
const attachments = ["attachment1", "attachment2"];
169204
const keys = ["key1", "key2"];
170205
isRepositoryVersioned.mockResolvedValue(true);
@@ -185,6 +220,18 @@ describe("SDMAttachmentsService", () => {
185220
},
186221
reject: jest.fn(),
187222
};
223+
cds = require("@sap/cds/lib");
224+
cds.context = {
225+
user: {
226+
tokenInfo: {
227+
getPayload: jest.fn(() => ({
228+
ext_attr: {
229+
zdn: 'subdomain' // simulate the subdomain extraction
230+
}
231+
})),
232+
},
233+
},
234+
};
188235
const attachments = ["attachment1", "attachment2"];
189236
const keys = ["key1", "key2"];
190237
isRepositoryVersioned.mockResolvedValue(true);
@@ -204,7 +251,18 @@ describe("SDMAttachmentsService", () => {
204251
},
205252
},
206253
};
207-
254+
cds = require("@sap/cds/lib");
255+
cds.context = {
256+
user: {
257+
tokenInfo: {
258+
getPayload: jest.fn(() => ({
259+
ext_attr: {
260+
zdn: 'subdomain' // simulate the subdomain extraction
261+
}
262+
})),
263+
},
264+
},
265+
};
208266
const attachments = ["attachment1", "attachment2"];
209267
const keys = ["key1", "key2"];
210268
const response = { url: "mockUrl" };
@@ -263,14 +321,25 @@ describe("SDMAttachmentsService", () => {
263321
info: jest.fn(),
264322
warn: jest.fn()
265323
};
266-
324+
cds = require("@sap/cds/lib");
267325
cds.model.definitions[mockReq.query.target.name + ".attachments"] = {
268326
keys: {
269327
up_: {
270328
keys: [{ ref: ["attachment"] }],
271329
},
272330
},
273331
};
332+
cds.context = {
333+
user: {
334+
tokenInfo: {
335+
getPayload: jest.fn(() => ({
336+
ext_attr: {
337+
zdn: 'subdomain' // simulate the subdomain extraction
338+
}
339+
})),
340+
},
341+
},
342+
};
274343
NodeCache.prototype.get.mockImplementation(() => undefined);
275344
getConfigurations.mockResolvedValueOnce({repositoryId: "123"});
276345
getRepositoryInfo.mockResolvedValueOnce(repoInfo);

test/lib/util/index.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ describe("util", () => {
276276
}
277277
const isVersioned = isRepositoryVersioned(mockRepoInfo, "mockedRepoId");
278278
expect(isVersioned).toBe(true);
279-
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId");
280-
expect(NodeCache.prototype.set).toBeCalledWith("mockedRepoId", "versioned", 60 * 60 * 24 * 60);
279+
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId_subdomain");
280+
expect(NodeCache.prototype.set).toBeCalledWith("mockedRepoId_subdomain", "versioned", 60 * 60 * 24 * 60);
281281
});
282282

283283
it("should not set cache and return true when repotype is pwconly", () => {
@@ -293,7 +293,7 @@ describe("util", () => {
293293
}
294294
const isVersioned = isRepositoryVersioned(mockRepoInfo, "mockedRepoId");
295295
expect(isVersioned).toBe(true);
296-
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId");
296+
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId_subdomain");
297297
expect(NodeCache.prototype.set).not.toHaveBeenCalled();
298298
});
299299

@@ -308,10 +308,11 @@ describe("util", () => {
308308
}
309309
}
310310
}
311+
311312
const isVersioned = isRepositoryVersioned(mockRepoInfo, "mockedRepoId");
312313
expect(isVersioned).toBe(false);
313-
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId");
314-
expect(NodeCache.prototype.set).toBeCalledWith("mockedRepoId", "non-versioned", 60 * 60 * 24 * 60);
314+
expect(NodeCache.prototype.get).toBeCalledWith("mockedRepoId_subdomain");
315+
expect(NodeCache.prototype.set).toBeCalledWith("mockedRepoId_subdomain", "non-versioned", 60 * 60 * 24 * 60);
315316
});
316317
})
317318

0 commit comments

Comments
 (0)