Skip to content

Commit

Permalink
SDMEXT-630: [Nodejs] Support multi-tenancy
Browse files Browse the repository at this point in the history
UT's
  • Loading branch information
rashmiangadi11 committed Aug 22, 2024
1 parent 8a65c19 commit 1addfc0
Showing 1 changed file with 75 additions and 13 deletions.
88 changes: 75 additions & 13 deletions test/lib/util/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,17 @@ describe("util", () => {
xssec.requests.requestUserToken.mockImplementation(
(a, b, c, d, e, f, callback) => callback(null, dummyToken)
);

cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
const credentials = { uaa: "uaa" };
const req = {
user: {
Expand All @@ -57,11 +67,11 @@ describe("util", () => {
},
},
};
const accessToken = await fetchAccessToken(credentials, req.user.tokenInfo.getTokenValue);
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com");
const accessToken = await fetchAccessToken(credentials, req.user.tokenInfo.getTokenValue);
const expectedCacheKey = "example@example.com_subdomain";
expect(xssec.requests.requestUserToken).toBeCalled();
expect(NodeCache.prototype.set).toBeCalledWith(
"[email protected]",
expectedCacheKey,
dummyToken,
11 * 3600
);
Expand All @@ -77,9 +87,20 @@ describe("util", () => {
},
},
};
cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
const credentials = { uaa: "uaa" };
const accessToken = await fetchAccessToken(credentials, req.user.tokenInfo.getTokenValue);
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com");
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com_subdomain");
expect(xssec.requests.requestUserToken).toBeCalled();
expect(accessToken).toBe(dummyToken);
});
Expand All @@ -90,7 +111,17 @@ describe("util", () => {
"email": "[email protected]",
"exp": 2537353178
};

cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
// Please replace 'your_secret_key' with your own secret key
const secretKey = 'your_secret_key';

Expand All @@ -106,7 +137,7 @@ describe("util", () => {
};
const credentials = { uaa: "uaa" };
const accessToken = await fetchAccessToken(credentials, req.user.tokenInfo.getTokenValue);
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com");
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com_subdomain");
expect(xssec.requests.requestUserToken).not.toBeCalled();
expect(accessToken).toBe(dummyToken);
});
Expand All @@ -120,6 +151,17 @@ describe("util", () => {
(a, b, c, d, e, f, callback) =>
callback(new Error("test error"), { statusCode: 500 })
);
cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
const req = {
user: {
tokenInfo: {
Expand All @@ -131,7 +173,7 @@ describe("util", () => {
try {
await fetchAccessToken(credentials, req.user.tokenInfo.getTokenValue);
} catch (err) {
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com");
expect(NodeCache.prototype.get).toBeCalledWith("example@example.com_subdomain");
expect(xssec.requests.requestUserToken).toBeCalled();
expect(consoleErrorSpy).toBeCalledWith(
"Response error while fetching access token 500"
Expand All @@ -153,11 +195,21 @@ describe("util", () => {
it('returns cached token if available', async () => {
const cachedToken = 'mockedAccessToken';
NodeCache.prototype.get.mockImplementation(() => cachedToken);

cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
const token = await getClientCredentialsToken({ uaa: 'mockedUaa' });

expect(token).toBe(cachedToken);
expect(NodeCache.prototype.get).toHaveBeenCalledWith('SDM_ACCESS_TOKEN');
expect(NodeCache.prototype.get).toHaveBeenCalledWith('SDM_ACCESS_TOKEN_subdomain');
expect(xssec.requests.requestClientCredentialsToken).not.toHaveBeenCalled();
});

Expand All @@ -168,13 +220,23 @@ describe("util", () => {
xssec.requests.requestClientCredentialsToken.mockImplementation((_, __, ___, callback) => {
callback(null, mockResponse);
});

cds.context = {
user: {
tokenInfo: {
getPayload: jest.fn(() => ({
ext_attr: {
zdn: 'subdomain' // simulate the subdomain extraction
}
})),
},
},
};
const token = await getClientCredentialsToken(credentials);

expect(token).toBe(mockResponse);
expect(NodeCache.prototype.set).toHaveBeenCalledWith('SDM_ACCESS_TOKEN', mockResponse, expect.any(Number));
expect(NodeCache.prototype.set).toHaveBeenCalledWith('SDM_ACCESS_TOKEN_subdomain', mockResponse, expect.any(Number));
expect(xssec.requests.requestClientCredentialsToken).toHaveBeenCalledWith(
null,
"subdomain",
credentials.uaa,
null,
expect.any(Function)
Expand Down

0 comments on commit 1addfc0

Please sign in to comment.