|
| 1 | +/* |
| 2 | + * Copyright 2025 The Backstage Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { mockCredentials, mockServices } from '@backstage/backend-test-utils'; |
| 17 | +import { NotAllowedError } from '@backstage/errors'; |
| 18 | +import { AuthorizeResult } from '@backstage/plugin-permission-common'; |
| 19 | +import { createFindApplicationsAction } from './createFindApplicationsAction'; |
| 20 | +import { ArgoCDService } from '@backstage-community/plugin-argocd-node'; |
| 21 | + |
| 22 | +const mockInstance = { |
| 23 | + name: 'prod', |
| 24 | + url: 'https://argocd.example.com', |
| 25 | + appName: ['my-app'], |
| 26 | + applications: [ |
| 27 | + { |
| 28 | + metadata: { |
| 29 | + name: 'my-app', |
| 30 | + namespace: 'production', |
| 31 | + }, |
| 32 | + spec: { |
| 33 | + project: 'default', |
| 34 | + destination: { |
| 35 | + server: 'https://kubernetes.default.svc', |
| 36 | + namespace: 'production', |
| 37 | + }, |
| 38 | + }, |
| 39 | + status: { |
| 40 | + sync: { status: 'Synced', revision: 'abc123' }, |
| 41 | + health: { status: 'Healthy' }, |
| 42 | + resources: [], |
| 43 | + history: [], |
| 44 | + operationState: { phase: 'Succeeded', message: '' }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + ], |
| 48 | +}; |
| 49 | + |
| 50 | +describe('createFindApplicationsAction', () => { |
| 51 | + let mockActionsRegistry: { register: jest.Mock }; |
| 52 | + let mockArgoCDService: jest.Mocked<ArgoCDService>; |
| 53 | + let mockPermissions: { |
| 54 | + authorize: jest.Mock; |
| 55 | + authorizeConditional: jest.Mock; |
| 56 | + }; |
| 57 | + |
| 58 | + beforeEach(() => { |
| 59 | + mockActionsRegistry = { register: jest.fn() }; |
| 60 | + |
| 61 | + mockArgoCDService = { |
| 62 | + findApplications: jest.fn().mockResolvedValue([mockInstance]), |
| 63 | + } as any; |
| 64 | + |
| 65 | + mockPermissions = { |
| 66 | + authorize: jest |
| 67 | + .fn() |
| 68 | + .mockResolvedValue([{ result: AuthorizeResult.ALLOW }]), |
| 69 | + authorizeConditional: jest.fn(), |
| 70 | + }; |
| 71 | + |
| 72 | + createFindApplicationsAction({ |
| 73 | + actionsRegistry: mockActionsRegistry as any, |
| 74 | + argoCDService: mockArgoCDService, |
| 75 | + permissions: mockPermissions as any, |
| 76 | + }); |
| 77 | + }); |
| 78 | + |
| 79 | + it('registers the argocd:find-applications action', () => { |
| 80 | + expect(mockActionsRegistry.register).toHaveBeenCalledTimes(1); |
| 81 | + const reg = mockActionsRegistry.register.mock.calls[0][0]; |
| 82 | + expect(reg.name).toBe('argocd:find-applications'); |
| 83 | + expect(reg.attributes.readOnly).toBe(true); |
| 84 | + expect(reg.attributes.destructive).toBe(false); |
| 85 | + expect(reg.attributes.idempotent).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('returns applications across all instances', async () => { |
| 89 | + const reg = mockActionsRegistry.register.mock.calls[0][0]; |
| 90 | + const credentials = mockCredentials.user(); |
| 91 | + |
| 92 | + const result = await reg.action({ |
| 93 | + input: { appName: 'my-app' }, |
| 94 | + credentials, |
| 95 | + logger: mockServices.logger.mock(), |
| 96 | + }); |
| 97 | + |
| 98 | + expect(mockArgoCDService.findApplications).toHaveBeenCalledWith({ |
| 99 | + appName: 'my-app', |
| 100 | + project: undefined, |
| 101 | + appNamespace: undefined, |
| 102 | + }); |
| 103 | + expect(result.output.instances).toHaveLength(1); |
| 104 | + expect(result.output.instances[0].instanceName).toBe('prod'); |
| 105 | + expect(result.output.instances[0].instanceUrl).toBe( |
| 106 | + 'https://argocd.example.com', |
| 107 | + ); |
| 108 | + expect(result.output.instances[0].applications).toHaveLength(1); |
| 109 | + const app = result.output.instances[0].applications[0]; |
| 110 | + expect(app.name).toBe('my-app'); |
| 111 | + expect(app.syncStatus).toBe('Synced'); |
| 112 | + expect(app.healthStatus).toBe('Healthy'); |
| 113 | + expect(app.revision).toBe('abc123'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('passes optional filters to findApplications', async () => { |
| 117 | + const reg = mockActionsRegistry.register.mock.calls[0][0]; |
| 118 | + const credentials = mockCredentials.user(); |
| 119 | + |
| 120 | + await reg.action({ |
| 121 | + input: { appName: 'my-app', project: 'my-project', appNamespace: 'ns' }, |
| 122 | + credentials, |
| 123 | + logger: mockServices.logger.mock(), |
| 124 | + }); |
| 125 | + |
| 126 | + expect(mockArgoCDService.findApplications).toHaveBeenCalledWith({ |
| 127 | + appName: 'my-app', |
| 128 | + project: 'my-project', |
| 129 | + appNamespace: 'ns', |
| 130 | + }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('returns empty applications array when instance has no apps', async () => { |
| 134 | + mockArgoCDService.findApplications.mockResolvedValue([ |
| 135 | + { name: 'prod', url: 'https://argocd.example.com', appName: [] }, |
| 136 | + ]); |
| 137 | + const reg = mockActionsRegistry.register.mock.calls[0][0]; |
| 138 | + const credentials = mockCredentials.user(); |
| 139 | + |
| 140 | + const result = await reg.action({ |
| 141 | + input: { appName: 'missing-app' }, |
| 142 | + credentials, |
| 143 | + logger: mockServices.logger.mock(), |
| 144 | + }); |
| 145 | + |
| 146 | + expect(result.output.instances[0].applications).toHaveLength(0); |
| 147 | + }); |
| 148 | + |
| 149 | + it('throws NotAllowedError when permission is denied', async () => { |
| 150 | + mockPermissions.authorize.mockResolvedValue([ |
| 151 | + { result: AuthorizeResult.DENY }, |
| 152 | + ]); |
| 153 | + const reg = mockActionsRegistry.register.mock.calls[0][0]; |
| 154 | + const credentials = mockCredentials.user(); |
| 155 | + |
| 156 | + await expect( |
| 157 | + reg.action({ |
| 158 | + input: { appName: 'my-app' }, |
| 159 | + credentials, |
| 160 | + logger: mockServices.logger.mock(), |
| 161 | + }), |
| 162 | + ).rejects.toThrow(NotAllowedError); |
| 163 | + }); |
| 164 | +}); |
0 commit comments