|
| 1 | +// |
| 2 | +// SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company and Gardener contributors |
| 3 | +// |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | +// |
| 6 | +'use strict' |
| 7 | +const { mockRequest } = require('@gardener-dashboard/request') |
| 8 | +describe('api', function () { |
| 9 | + let agent |
| 10 | + beforeAll(() => { |
| 11 | + agent = createAgent() |
| 12 | + }) |
| 13 | + afterAll(() => { |
| 14 | + return agent.close() |
| 15 | + }) |
| 16 | + beforeEach(() => { |
| 17 | + mockRequest.mockReset() |
| 18 | + }) |
| 19 | + describe('all namespaced cloudprofiles', function () { |
| 20 | + const user = fixtures.user.create({ id: 'john.doe@example.org' }) |
| 21 | + it('should return all namespaced cloudprofiles from authorized namespaces', async function () { |
| 22 | + mockRequest |
| 23 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: true })) |
| 24 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: false })) |
| 25 | + const res = await agent |
| 26 | + .get('/api/namespacedcloudprofiles') |
| 27 | + .set('cookie', await user.cookie) |
| 28 | + .expect('content-type', /json/) |
| 29 | + .expect(200) |
| 30 | + expect(mockRequest).toHaveBeenCalledTimes(2) |
| 31 | + expect(res.body).toBeInstanceOf(Array) |
| 32 | + expect(res.body.length).toBe(2) |
| 33 | + expect(res.body.every(profile => profile.metadata.namespace === 'garden-local')).toBe(true) |
| 34 | + }) |
| 35 | + it('should return all cloudprofiles when authorized for all namespaces', async function () { |
| 36 | + mockRequest |
| 37 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: true })) |
| 38 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: true })) |
| 39 | + const res = await agent |
| 40 | + .get('/api/namespacedcloudprofiles') |
| 41 | + .set('cookie', await user.cookie) |
| 42 | + .expect('content-type', /json/) |
| 43 | + .expect(200) |
| 44 | + expect(mockRequest).toHaveBeenCalledTimes(2) |
| 45 | + expect(res.body).toBeInstanceOf(Array) |
| 46 | + expect(res.body.length).toBe(3) |
| 47 | + }) |
| 48 | + it('should return empty array when not authorized for any namespace', async function () { |
| 49 | + mockRequest |
| 50 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: false })) |
| 51 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: false })) |
| 52 | + const res = await agent |
| 53 | + .get('/api/namespacedcloudprofiles') |
| 54 | + .set('cookie', await user.cookie) |
| 55 | + .expect('content-type', /json/) |
| 56 | + .expect(200) |
| 57 | + expect(mockRequest).toHaveBeenCalledTimes(2) |
| 58 | + expect(res.body).toBeInstanceOf(Array) |
| 59 | + expect(res.body.length).toBe(0) |
| 60 | + }) |
| 61 | + it('should verify authorization checks with snapshot', async function () { |
| 62 | + mockRequest |
| 63 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: true })) |
| 64 | + .mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess({ allowed: true })) |
| 65 | + await agent |
| 66 | + .get('/api/namespacedcloudprofiles') |
| 67 | + .set('cookie', await user.cookie) |
| 68 | + .expect(200) |
| 69 | + expect(mockRequest).toHaveBeenCalledTimes(2) |
| 70 | + expect(mockRequest.mock.calls).toMatchSnapshot() |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments