|
| 1 | +/* |
| 2 | + * Copyright Red Hat, Inc. |
| 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 { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils'; |
| 17 | +import { createProjectAction } from './createProject'; |
| 18 | + |
| 19 | +describe('x2a:project:create', () => { |
| 20 | + const mockDiscoveryApi = { |
| 21 | + getBaseUrl: jest.fn().mockResolvedValue('http://backstage.example.com'), |
| 22 | + getExternalBaseUrl: jest |
| 23 | + .fn() |
| 24 | + .mockResolvedValue('http://backstage.example.com'), |
| 25 | + }; |
| 26 | + |
| 27 | + const mockFetch = jest.fn(); |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should create a project and output projectId and nextUrl', async () => { |
| 34 | + const createdProject = { |
| 35 | + id: 'project-uuid-123', |
| 36 | + abbreviation: 'PRJ', |
| 37 | + name: 'My Project', |
| 38 | + description: 'A test project', |
| 39 | + createdAt: '2025-01-01T00:00:00.000Z', |
| 40 | + createdBy: 'user:default/jane', |
| 41 | + }; |
| 42 | + |
| 43 | + mockFetch.mockResolvedValueOnce({ |
| 44 | + ok: true, |
| 45 | + json: () => Promise.resolve(createdProject), |
| 46 | + }); |
| 47 | + |
| 48 | + const action = createProjectAction(mockDiscoveryApi); |
| 49 | + |
| 50 | + // The action uses global fetch inside the custom fetchApi; we need to inject our mock. |
| 51 | + // createProjectAction builds DefaultApiClient with fetchApi: { fetch: (url, opts) => fetch(url, opts) }. |
| 52 | + // So the handler uses the real fetch. We mock global fetch so the client's request is intercepted. |
| 53 | + const originalFetch = globalThis.fetch; |
| 54 | + globalThis.fetch = mockFetch; |
| 55 | + |
| 56 | + const mockContext = createMockActionContext({ |
| 57 | + input: { |
| 58 | + name: 'My Project', |
| 59 | + description: 'A test project', |
| 60 | + abbreviation: 'PRJ', |
| 61 | + sourceRepoUrl: 'https://github.com/org/repo', |
| 62 | + areTargeAndSourceRepoShared: false, |
| 63 | + targetRepoBranch: 'main', |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + await action.handler(mockContext); |
| 68 | + |
| 69 | + globalThis.fetch = originalFetch; |
| 70 | + |
| 71 | + expect(mockContext.output).toHaveBeenCalledWith( |
| 72 | + 'projectId', |
| 73 | + 'project-uuid-123', |
| 74 | + ); |
| 75 | + expect(mockContext.output).toHaveBeenCalledWith( |
| 76 | + 'nextUrl', |
| 77 | + '/x2a/projects/project-uuid-123', |
| 78 | + ); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should send name, description (or empty string), and abbreviation in the request body', async () => { |
| 82 | + const createdProject = { |
| 83 | + id: 'project-uuid-456', |
| 84 | + abbreviation: 'ABBR', |
| 85 | + name: 'Another Project', |
| 86 | + description: '', |
| 87 | + createdAt: '2025-01-01T00:00:00.000Z', |
| 88 | + createdBy: 'user:default/john', |
| 89 | + }; |
| 90 | + |
| 91 | + mockFetch.mockImplementation((_url: string, options?: RequestInit) => { |
| 92 | + const body = options?.body ? JSON.parse(options.body as string) : {}; |
| 93 | + expect(body).toEqual({ |
| 94 | + name: 'Another Project', |
| 95 | + description: '', |
| 96 | + abbreviation: 'ABBR', |
| 97 | + }); |
| 98 | + return Promise.resolve({ |
| 99 | + ok: true, |
| 100 | + json: () => Promise.resolve(createdProject), |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + const originalFetch = globalThis.fetch; |
| 105 | + globalThis.fetch = mockFetch; |
| 106 | + |
| 107 | + const action = createProjectAction(mockDiscoveryApi); |
| 108 | + const mockContext = createMockActionContext({ |
| 109 | + input: { |
| 110 | + name: 'Another Project', |
| 111 | + abbreviation: 'ABBR', |
| 112 | + sourceRepoUrl: 'https://github.com/org/repo2', |
| 113 | + areTargeAndSourceRepoShared: true, |
| 114 | + targetRepoBranch: 'main', |
| 115 | + }, |
| 116 | + }); |
| 117 | + |
| 118 | + await action.handler(mockContext); |
| 119 | + |
| 120 | + globalThis.fetch = originalFetch; |
| 121 | + |
| 122 | + expect(mockContext.output).toHaveBeenCalledWith( |
| 123 | + 'projectId', |
| 124 | + 'project-uuid-456', |
| 125 | + ); |
| 126 | + expect(mockContext.output).toHaveBeenCalledWith( |
| 127 | + 'nextUrl', |
| 128 | + '/x2a/projects/project-uuid-456', |
| 129 | + ); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should include Authorization header when backstageToken is provided', async () => { |
| 133 | + const createdProject = { |
| 134 | + id: 'project-uuid-789', |
| 135 | + abbreviation: 'TKN', |
| 136 | + name: 'Token Project', |
| 137 | + description: 'With token', |
| 138 | + createdAt: '2025-01-01T00:00:00.000Z', |
| 139 | + createdBy: 'user:default/alice', |
| 140 | + }; |
| 141 | + |
| 142 | + mockFetch.mockImplementation((_url: string, options?: RequestInit) => { |
| 143 | + const headers = (options?.headers || {}) as Record<string, string>; |
| 144 | + expect(headers.Authorization).toBe('Bearer my-backstage-token'); |
| 145 | + return Promise.resolve({ |
| 146 | + ok: true, |
| 147 | + json: () => Promise.resolve(createdProject), |
| 148 | + }); |
| 149 | + }); |
| 150 | + |
| 151 | + const originalFetch = globalThis.fetch; |
| 152 | + globalThis.fetch = mockFetch; |
| 153 | + |
| 154 | + const action = createProjectAction(mockDiscoveryApi); |
| 155 | + const mockContext = createMockActionContext({ |
| 156 | + input: { |
| 157 | + name: 'Token Project', |
| 158 | + description: 'With token', |
| 159 | + abbreviation: 'TKN', |
| 160 | + sourceRepoUrl: 'https://github.com/org/repo', |
| 161 | + areTargeAndSourceRepoShared: false, |
| 162 | + targetRepoBranch: 'main', |
| 163 | + }, |
| 164 | + secrets: { |
| 165 | + backstageToken: 'my-backstage-token', |
| 166 | + }, |
| 167 | + }); |
| 168 | + |
| 169 | + await action.handler(mockContext); |
| 170 | + |
| 171 | + globalThis.fetch = originalFetch; |
| 172 | + |
| 173 | + expect(mockContext.output).toHaveBeenCalledWith( |
| 174 | + 'projectId', |
| 175 | + 'project-uuid-789', |
| 176 | + ); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should log that the action is running', async () => { |
| 180 | + const loggerInfo = jest.fn(); |
| 181 | + mockFetch.mockResolvedValueOnce({ |
| 182 | + ok: true, |
| 183 | + json: () => |
| 184 | + Promise.resolve({ |
| 185 | + id: 'log-test-id', |
| 186 | + abbreviation: 'LOG', |
| 187 | + name: 'Log Test', |
| 188 | + description: '', |
| 189 | + createdAt: '2025-01-01T00:00:00.000Z', |
| 190 | + createdBy: 'user:default/bob', |
| 191 | + }), |
| 192 | + }); |
| 193 | + |
| 194 | + const originalFetch = globalThis.fetch; |
| 195 | + globalThis.fetch = mockFetch; |
| 196 | + |
| 197 | + const action = createProjectAction(mockDiscoveryApi); |
| 198 | + const mockContext = createMockActionContext({ |
| 199 | + input: { |
| 200 | + name: 'Log Test', |
| 201 | + abbreviation: 'LOG', |
| 202 | + sourceRepoUrl: 'https://github.com/org/repo', |
| 203 | + areTargeAndSourceRepoShared: false, |
| 204 | + targetRepoBranch: 'main', |
| 205 | + }, |
| 206 | + logger: { |
| 207 | + ...createMockActionContext().logger, |
| 208 | + info: loggerInfo, |
| 209 | + }, |
| 210 | + }); |
| 211 | + |
| 212 | + await action.handler(mockContext); |
| 213 | + |
| 214 | + globalThis.fetch = originalFetch; |
| 215 | + |
| 216 | + expect(loggerInfo).toHaveBeenCalledWith( |
| 217 | + 'Running x2a:project:create template action for undefined', |
| 218 | + ); |
| 219 | + }); |
| 220 | +}); |
0 commit comments