Skip to content

Commit 41d7722

Browse files
authored
Merge pull request #2190 from headlamp-k8s/testauth-fix
frontend: apiProxy: Pass in namespace to testAuth
2 parents 7ae51f6 + 983d868 commit 41d7722

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

frontend/src/lib/k8s/apiProxy.test.ts

+27-10
Original file line numberDiff line numberDiff line change
@@ -791,37 +791,54 @@ describe('apiProxy', () => {
791791

792792
describe('testAuth', () => {
793793
const apiPath = '/apis/authorization.k8s.io/v1/selfsubjectrulesreviews';
794-
const spec = { namespace: namespace };
794+
const customNamespace = 'custom-namespace';
795+
const specDefault = { namespace: namespace };
796+
const specCustom = { namespace: customNamespace };
795797

796798
beforeEach(() => {
797799
nock(baseApiUrl)
798-
.persist()
799-
.post(`/clusters/${clusterName}${apiPath}`, { spec })
800+
.post(`/clusters/${clusterName}${apiPath}`, { spec: specDefault })
801+
.reply(200, mockResponse);
802+
803+
nock(baseApiUrl)
804+
.post(`/clusters/${clusterName}${apiPath}`, { spec: specCustom })
800805
.reply(200, mockResponse);
801806
});
802807

803808
afterEach(() => {
804809
nock.cleanAll();
805810
});
806811

807-
it('Successfully handles authentication', async () => {
812+
it('Successfully handles authentication with default namespace', async () => {
808813
const response = await apiProxy.testAuth(clusterName);
809814
expect(response).toEqual(mockResponse);
810815
});
811816

817+
it('Successfully handles authentication with custom namespace', async () => {
818+
const response = await apiProxy.testAuth(clusterName, customNamespace);
819+
expect(response).toEqual(mockResponse);
820+
});
821+
812822
it.each([
813-
[401, errorResponse401],
814-
[500, errorResponse500],
823+
[401, errorResponse401, namespace],
824+
[500, errorResponse500, namespace],
825+
[401, errorResponse401, customNamespace],
826+
[500, errorResponse500, customNamespace],
815827
])(
816828
'Successfully handles authentication with error status %d',
817-
async (statusCode, errorResponse) => {
829+
async (statusCode, errorResponse, namespace) => {
818830
nock.cleanAll();
819831
nock(baseApiUrl)
820-
.persist()
821-
.post(`/clusters/${clusterName}${apiPath}`, { spec })
832+
.post(`/clusters/${clusterName}${apiPath}`, { spec: specDefault })
822833
.reply(statusCode, { message: errorResponse.error });
823834

824-
await expect(apiProxy.testAuth(clusterName)).rejects.toThrow(errorResponse.error);
835+
nock(baseApiUrl)
836+
.post(`/clusters/${clusterName}${apiPath}`, { spec: specCustom })
837+
.reply(statusCode, { message: errorResponse.error });
838+
839+
await expect(apiProxy.testAuth(clusterName, namespace)).rejects.toThrow(
840+
errorResponse.error
841+
);
825842
}
826843
);
827844
});

frontend/src/lib/k8s/apiProxy.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1641,8 +1641,8 @@ export async function metrics(
16411641
//@todo: these need documenting.
16421642
//@todo: these need return types.
16431643

1644-
export async function testAuth(cluster = '') {
1645-
const spec = { namespace: 'default' };
1644+
export async function testAuth(cluster = '', namespace = 'default') {
1645+
const spec = { namespace };
16461646
const clusterName = cluster || getCluster();
16471647

16481648
return post('/apis/authorization.k8s.io/v1/selfsubjectrulesreviews', { spec }, false, {

0 commit comments

Comments
 (0)