|
1 | 1 | import express from 'express'; |
2 | 2 | import {get} from 'http'; |
| 3 | +import {Request, Response, NextFunction} from 'express'; |
3 | 4 |
|
4 | 5 | import {Api} from './api'; |
5 | 6 | import {DefaultApi} from './clients/profile_controller'; |
6 | 7 | import {KubernetesService} from './k8s_service'; |
7 | 8 | import {Interval, MetricsService} from './metrics_service'; |
| 9 | +import {WorkgroupApi, WorkgroupInfo, SimpleBinding} from './api_workgroup'; |
8 | 10 |
|
9 | 11 | describe('Main API', () => { |
10 | 12 | let mockK8sService: jasmine.SpyObj<KubernetesService>; |
@@ -116,4 +118,176 @@ describe('Main API', () => { |
116 | 118 | }); |
117 | 119 | }); |
118 | 120 | }); |
| 121 | + |
| 122 | + describe('checkNamespaceAccess middleware', () => { |
| 123 | + let mockWorkgroupApi: jasmine.SpyObj<WorkgroupApi>; |
| 124 | + let api: Api; |
| 125 | + let mockReq: Partial<Request>; |
| 126 | + let mockRes: Partial<Response>; |
| 127 | + let mockNext: jasmine.Spy<NextFunction>; |
| 128 | + let jsonSpy: jasmine.Spy; |
| 129 | + let statusSpy: jasmine.Spy; |
| 130 | + |
| 131 | + beforeEach(() => { |
| 132 | + mockK8sService = jasmine.createSpyObj<KubernetesService>(['']); |
| 133 | + mockWorkgroupApi = jasmine.createSpyObj<WorkgroupApi>(['getWorkgroupInfo']); |
| 134 | + |
| 135 | + jsonSpy = jasmine.createSpy('json'); |
| 136 | + statusSpy = jasmine.createSpy('status').and.returnValue({json: jsonSpy}); |
| 137 | + |
| 138 | + mockRes = { |
| 139 | + status: statusSpy, |
| 140 | + json: jsonSpy, |
| 141 | + }; |
| 142 | + |
| 143 | + mockNext = jasmine.createSpy('next'); |
| 144 | + |
| 145 | + api = new Api(mockK8sService, undefined, mockWorkgroupApi); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return 400 if namespace parameter is missing', async () => { |
| 149 | + mockReq = { |
| 150 | + params: {}, |
| 151 | + }; |
| 152 | + |
| 153 | + // Access the private method via reflection for testing |
| 154 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 155 | + |
| 156 | + expect(statusSpy).toHaveBeenCalledWith(400); |
| 157 | + expect(jsonSpy).toHaveBeenCalledWith({ |
| 158 | + error: 'Namespace parameter is required', |
| 159 | + }); |
| 160 | + expect(mockNext).not.toHaveBeenCalled(); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should allow access if no workgroup API is configured', async () => { |
| 164 | + const apiWithoutWorkgroup = new Api(mockK8sService, undefined, undefined); |
| 165 | + mockReq = { |
| 166 | + params: {namespace: 'test-namespace'}, |
| 167 | + }; |
| 168 | + |
| 169 | + await (apiWithoutWorkgroup as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 170 | + |
| 171 | + expect(mockNext).toHaveBeenCalled(); |
| 172 | + expect(statusSpy).not.toHaveBeenCalled(); |
| 173 | + }); |
| 174 | + |
| 175 | + it('should return 401 if no user is attached to request', async () => { |
| 176 | + mockReq = { |
| 177 | + params: {namespace: 'test-namespace'}, |
| 178 | + user: undefined, |
| 179 | + }; |
| 180 | + |
| 181 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 182 | + |
| 183 | + expect(statusSpy).toHaveBeenCalledWith(401); |
| 184 | + expect(jsonSpy).toHaveBeenCalledWith({ |
| 185 | + error: 'Authentication required to access namespace activities', |
| 186 | + }); |
| 187 | + expect(mockNext).not.toHaveBeenCalled(); |
| 188 | + }); |
| 189 | + |
| 190 | + it('should allow access for non-authenticated users in basic auth mode', async () => { |
| 191 | + mockReq = { |
| 192 | + params: {namespace: 'test-namespace'}, |
| 193 | + user: {hasAuth: false}, |
| 194 | + }; |
| 195 | + |
| 196 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 197 | + |
| 198 | + expect(mockNext).toHaveBeenCalled(); |
| 199 | + expect(statusSpy).not.toHaveBeenCalled(); |
| 200 | + }); |
| 201 | + |
| 202 | + it('should allow access for cluster admins', async () => { |
| 203 | + const workgroupInfo: WorkgroupInfo = { |
| 204 | + isClusterAdmin: true, |
| 205 | + namespaces: [], |
| 206 | + }; |
| 207 | + |
| 208 | + mockWorkgroupApi.getWorkgroupInfo.and.returnValue(Promise.resolve(workgroupInfo)); |
| 209 | + |
| 210 | + mockReq = { |
| 211 | + params: {namespace: 'test-namespace'}, |
| 212 | + user: {hasAuth: true, email: '[email protected]'}, |
| 213 | + }; |
| 214 | + |
| 215 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 216 | + |
| 217 | + expect(mockWorkgroupApi.getWorkgroupInfo).toHaveBeenCalledWith(mockReq.user); |
| 218 | + expect(mockNext).toHaveBeenCalled(); |
| 219 | + expect(statusSpy).not.toHaveBeenCalled(); |
| 220 | + }); |
| 221 | + |
| 222 | + it('should allow access for users with any binding to the namespace', async () => { |
| 223 | + const namespaces: SimpleBinding[] = [ |
| 224 | + {namespace: 'test-namespace', role: 'viewer', user: '[email protected]'}, |
| 225 | + ]; |
| 226 | + const workgroupInfo: WorkgroupInfo = { |
| 227 | + isClusterAdmin: false, |
| 228 | + namespaces, |
| 229 | + }; |
| 230 | + |
| 231 | + mockWorkgroupApi.getWorkgroupInfo.and.returnValue(Promise.resolve(workgroupInfo)); |
| 232 | + |
| 233 | + mockReq = { |
| 234 | + params: {namespace: 'test-namespace'}, |
| 235 | + user: {hasAuth: true, email: '[email protected]'}, |
| 236 | + }; |
| 237 | + |
| 238 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 239 | + |
| 240 | + expect(mockWorkgroupApi.getWorkgroupInfo).toHaveBeenCalledWith(mockReq.user); |
| 241 | + expect(mockNext).toHaveBeenCalled(); |
| 242 | + expect(statusSpy).not.toHaveBeenCalled(); |
| 243 | + }); |
| 244 | + |
| 245 | + it('should deny access for users without any binding to the namespace', async () => { |
| 246 | + const namespaces: SimpleBinding[] = [ |
| 247 | + {namespace: 'other-namespace', role: 'owner', user: '[email protected]'}, |
| 248 | + ]; |
| 249 | + const workgroupInfo: WorkgroupInfo = { |
| 250 | + isClusterAdmin: false, |
| 251 | + namespaces, |
| 252 | + }; |
| 253 | + |
| 254 | + mockWorkgroupApi.getWorkgroupInfo.and.returnValue(Promise.resolve(workgroupInfo)); |
| 255 | + |
| 256 | + mockReq = { |
| 257 | + params: {namespace: 'test-namespace'}, |
| 258 | + user: {hasAuth: true, email: '[email protected]'}, |
| 259 | + }; |
| 260 | + |
| 261 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 262 | + |
| 263 | + expect(mockWorkgroupApi.getWorkgroupInfo).toHaveBeenCalledWith(mockReq.user); |
| 264 | + expect(statusSpy).toHaveBeenCalledWith(403); |
| 265 | + expect(jsonSpy).toHaveBeenCalledWith({ |
| 266 | + error: `Access denied. You do not have permission to view activities for namespace 'test-namespace'.`, |
| 267 | + }); |
| 268 | + expect(mockNext).not.toHaveBeenCalled(); |
| 269 | + }); |
| 270 | + |
| 271 | + it('should return 500 if getWorkgroupInfo throws an error', async () => { |
| 272 | + const error = new Error('Service unavailable'); |
| 273 | + mockWorkgroupApi.getWorkgroupInfo.and.returnValue(Promise.reject(error)); |
| 274 | + |
| 275 | + spyOn(console, 'error'); |
| 276 | + |
| 277 | + mockReq = { |
| 278 | + params: {namespace: 'test-namespace'}, |
| 279 | + user: {hasAuth: true, email: '[email protected]'}, |
| 280 | + }; |
| 281 | + |
| 282 | + await (api as any).checkNamespaceAccess(mockReq, mockRes, mockNext); |
| 283 | + |
| 284 | + expect(mockWorkgroupApi.getWorkgroupInfo).toHaveBeenCalledWith(mockReq.user); |
| 285 | + expect(console.error).toHaveBeenCalledWith('Error checking namespace access:', error); |
| 286 | + expect(statusSpy).toHaveBeenCalledWith(500); |
| 287 | + expect(jsonSpy).toHaveBeenCalledWith({ |
| 288 | + error: 'Unable to verify namespace access permissions', |
| 289 | + }); |
| 290 | + expect(mockNext).not.toHaveBeenCalled(); |
| 291 | + }); |
| 292 | + }); |
119 | 293 | }); |
0 commit comments